From cd5cafa4a380e2459862b6e99ff0c381362ef1be Mon Sep 17 00:00:00 2001 From: Nobuyoshi Nakada Date: Tue, 14 Jun 2022 12:27:25 +0900 Subject: Respect the encoding of the source [Bug #18827] Do not override the input string encoding at the time of preparation, the source encoding is not determined from the input yet. --- test/ruby/test_ast.rb | 13 +++++++++++++ test/ruby/test_syntax.rb | 9 +++++++++ 2 files changed, 22 insertions(+) (limited to 'test/ruby') diff --git a/test/ruby/test_ast.rb b/test/ruby/test_ast.rb index cd96027654..5cf7a7f1b5 100644 --- a/test/ruby/test_ast.rb +++ b/test/ruby/test_ast.rb @@ -542,6 +542,19 @@ dummy assert_equal("def test_keep_script_lines_for_of\n", node_method.source.lines.first) end + def test_encoding_with_keep_script_lines + enc = Encoding::EUC_JP + code = "__ENCODING__".encode(enc) + + assert_equal(enc, eval(code)) + + node = RubyVM::AbstractSyntaxTree.parse(code, keep_script_lines: false) + assert_equal(enc, node.children[2].children[0]) + + node = RubyVM::AbstractSyntaxTree.parse(code, keep_script_lines: true) + assert_equal(enc, node.children[2].children[0]) + end + def test_e_option assert_in_out_err(["-e", "def foo; end; pp RubyVM::AbstractSyntaxTree.of(method(:foo)).type"], "", [":SCOPE"], []) diff --git a/test/ruby/test_syntax.rb b/test/ruby/test_syntax.rb index b1fa3e5227..1d7b89de57 100644 --- a/test/ruby/test_syntax.rb +++ b/test/ruby/test_syntax.rb @@ -66,6 +66,15 @@ class TestSyntax < Test::Unit::TestCase f&.close! end + def test_script_lines_encoding + require 'tmpdir' + Dir.mktmpdir do |dir| + File.write(File.join(dir, "script_lines.rb"), "SCRIPT_LINES__ = {}\n") + assert_in_out_err(%w"-r./script_lines -w -Ke", "puts __ENCODING__.name", + %w"EUC-JP", /-K is specified/, chdir: dir) + end + end + def test_anonymous_block_forwarding assert_syntax_error("def b; c(&); end", /no anonymous block parameter/) assert_separately([], "#{<<-"begin;"}\n#{<<-'end;'}") -- cgit v1.2.3