From 2e59cf00cc35183fe9b616672cb8d2b461b1cf9b Mon Sep 17 00:00:00 2001 From: Nobuyoshi Nakada Date: Fri, 14 Jun 2024 14:10:03 +0900 Subject: [Bug #20578] ripper: Fix dispatching part at invalid escapes --- test/ripper/test_lexer.rb | 109 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 109 insertions(+) (limited to 'test') diff --git a/test/ripper/test_lexer.rb b/test/ripper/test_lexer.rb index 64b4336375..e460a7fd13 100644 --- a/test/ripper/test_lexer.rb +++ b/test/ripper/test_lexer.rb @@ -390,6 +390,115 @@ world" assert_lexer(expected, code) end + def test_invalid_escape_string + code = "\"hello\\x world" + expected = [ + [[1, 0], :on_tstring_beg, "\"", state(:EXPR_BEG)], + [[1, 1], :on_tstring_content, "hello", state(:EXPR_BEG)], + [[1, 5], :on_tstring_content, "\\x", state(:EXPR_BEG)], + [[1, 7], :on_tstring_content, " world", state(:EXPR_BEG)], + ] + + code = "\"\nhello\\x world" + expected = [ + [[1, 0], :on_tstring_beg, "\"", state(:EXPR_BEG)], + [[1, 1], :on_tstring_content, "\n" "hello", state(:EXPR_BEG)], + [[2, 5], :on_tstring_content, "\\x", state(:EXPR_BEG)], + [[2, 7], :on_tstring_content, " world", state(:EXPR_BEG)], + ] + assert_lexer(expected, code) + + code = "\"\n\\Cxx\"" + expected = [ + [[1, 0], :on_tstring_beg, "\"", state(:EXPR_BEG)], + [[1, 1], :on_tstring_content, "\n", state(:EXPR_BEG)], + [[2, 0], :on_tstring_content, "\\Cx", state(:EXPR_BEG)], + [[2, 3], :on_tstring_content, "x", state(:EXPR_BEG)], + [[2, 4], :on_tstring_end, "\"", state(:EXPR_END)], + ] + assert_lexer(expected, code) + + code = "\"\n\\Mxx\"" + expected = [ + [[1, 0], :on_tstring_beg, "\"", state(:EXPR_BEG)], + [[1, 1], :on_tstring_content, "\n", state(:EXPR_BEG)], + [[2, 0], :on_tstring_content, "\\Mx", state(:EXPR_BEG)], + [[2, 3], :on_tstring_content, "x", state(:EXPR_BEG)], + [[2, 4], :on_tstring_end, "\"", state(:EXPR_END)], + ] + assert_lexer(expected, code) + + code = "\"\n\\c\\cx\"" + expected = [ + [[1, 0], :on_tstring_beg, "\"", state(:EXPR_BEG)], + [[1, 1], :on_tstring_content, "\n", state(:EXPR_BEG)], + [[2, 0], :on_tstring_content, "\\c\\c", state(:EXPR_BEG)], + [[2, 4], :on_tstring_content, "x", state(:EXPR_BEG)], + [[2, 5], :on_tstring_end, "\"", state(:EXPR_END)], + ] + assert_lexer(expected, code) + + code = "\"\n\\ux\"" + expected = [ + [[1, 0], :on_tstring_beg, "\"", state(:EXPR_BEG)], + [[1, 1], :on_tstring_content, "\n", state(:EXPR_BEG)], + [[2, 0], :on_tstring_content, "\\u", state(:EXPR_BEG)], + [[2, 2], :on_tstring_content, "x", state(:EXPR_BEG)], + [[2, 3], :on_tstring_end, "\"", state(:EXPR_END)], + ] + assert_lexer(expected, code) + + code = "\"\n\\xx\"" + expected = [ + [[1, 0], :on_tstring_beg, "\"", state(:EXPR_BEG)], + [[1, 1], :on_tstring_content, "\n", state(:EXPR_BEG)], + [[2, 0], :on_tstring_content, "\\x", state(:EXPR_BEG)], + [[2, 2], :on_tstring_content, "x", state(:EXPR_BEG)], + [[2, 3], :on_tstring_end, "\"", state(:EXPR_END)], + ] + assert_lexer(expected, code) + + code = "<