diff options
Diffstat (limited to 'test')
-rw-r--r-- | test/prism/magic_comment_test.rb | 3 | ||||
-rw-r--r-- | test/prism/parse_test.rb | 26 |
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 |