aboutsummaryrefslogtreecommitdiffstats
path: root/test
diff options
context:
space:
mode:
authorKevin Newton <kddnewton@gmail.com>2023-10-13 13:34:56 -0400
committerJemma Issroff <jemmaissroff@gmail.com>2023-10-16 15:40:19 -0700
commit9f16f07cf1e340acd9c41acaf8d46394353a0cea (patch)
treed4b6a8e83ba4ca75f7203da8ed05f93a3284861e /test
parent5523a23469987f92e38d52d4332bde09bdd8896c (diff)
downloadruby-9f16f07cf1e340acd9c41acaf8d46394353a0cea.tar.gz
[ruby/prism] Additionally handle encoding comments in vim mode
https://github.com/ruby/prism/commit/bf9bdb9d82
Diffstat (limited to 'test')
-rw-r--r--test/prism/magic_comment_test.rb3
-rw-r--r--test/prism/parse_test.rb26
2 files changed, 28 insertions, 1 deletions
diff --git a/test/prism/magic_comment_test.rb b/test/prism/magic_comment_test.rb
index c40364ccfa..76c4fcb71f 100644
--- a/test/prism/magic_comment_test.rb
+++ b/test/prism/magic_comment_test.rb
@@ -16,7 +16,8 @@ module Prism
"# -*- CoDiNg: ascii -*-",
"# -*- \s\t\v encoding \s\t\v : \s\t\v ascii \s\t\v -*-",
"# -*- foo: bar; encoding: ascii -*-",
- "# coding \t \r \v : \t \v \r ascii-8bit\n"
+ "# coding \t \r \v : \t \v \r ascii-8bit\n",
+ "# vim: filetype=ruby, fileencoding=big5, tabsize=3, shiftwidth=3\n"
]
examples.each do |example|
diff --git a/test/prism/parse_test.rb b/test/prism/parse_test.rb
index aed41b5f84..eada2952df 100644
--- a/test/prism/parse_test.rb
+++ b/test/prism/parse_test.rb
@@ -4,6 +4,21 @@ require_relative "test_helper"
module Prism
class ParseTest < TestCase
+ # A subclass of Ripper that extracts out magic comments.
+ class MagicCommentRipper < Ripper
+ attr_reader :magic_comments
+
+ def initialize(*)
+ super
+ @magic_comments = []
+ end
+
+ def on_magic_comment(key, value)
+ @magic_comments << [key, value]
+ super
+ end
+ end
+
# When we pretty-print the trees to compare against the snapshots, we want to
# be certain that we print with the same external encoding. This is because
# methods like Symbol#inspect take into account external encoding and it could
@@ -159,6 +174,17 @@ module Prism
rescue SyntaxError
raise ArgumentError, "Test file has invalid syntax #{filepath}"
end
+
+ # Next, check that we get the correct number of magic comments when
+ # lexing with ripper.
+ expected = MagicCommentRipper.new(source).tap(&:parse).magic_comments
+ actual = result.magic_comments
+
+ assert_equal expected.length, actual.length
+ expected.zip(actual).each do |(expected_key, expected_value), magic_comment|
+ assert_equal expected_key, magic_comment.key
+ assert_equal expected_value, magic_comment.value
+ end
end
end
end