Files
ruby/test/prism/magic_comment_test.rb
Yusuke EndohandClaude Opus 5.5 9b23d39c44 [ruby/prism] Resync prism as Ruby::Prism
This is the output of

    ruby tool/sync_default_gems.rb prism
    git rm -r lib/prism.rb lib/prism

at https://github.com/ruby/prism/commit/0a5f9c9, with the new mapping:
lib/prism is now lib/ruby/prism, the namespace is rewritten to
Ruby::Prism, and lib/prism/prism.gemspec is no longer imported, so
prism is no longer installed as a default gem.

prism/srcs.mk.in (excluded from the sync) follows the template engine,
now Ruby::Prism::Template: it defines the Ruby module, which does not
exist in BASERUBY older than Ruby 4.0, before loading
templates/template.rb.

Ruby::Prism is the very parser that parsed the running code, no
matter which version of the prism gem is installed; the two can be
loaded side by side (the ruby binary is built with -fvisibility=hidden,
so the gem's pm_* and rb_cPrism* symbols do not collide with the
built-in ones).

[Bug #22379]

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-11 04:27:15 +09:00

123 lines
3.3 KiB
Ruby

# frozen_string_literal: true
require_relative "test_helper"
require "ripper"
module Ruby::Prism
class MagicCommentTest < TestCase
if RUBY_ENGINE == "ruby"
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
Fixture.each do |fixture|
define_method(fixture.test_name) { assert_magic_comments(fixture) }
end
end
def test_encoding
assert_magic_encoding(Encoding::US_ASCII, "# encoding: ascii")
end
def test_coding
assert_magic_encoding(Encoding::US_ASCII, "# coding: ascii")
end
def test_eNcOdInG
assert_magic_encoding(Encoding::US_ASCII, "# eNcOdInG: ascii")
end
def test_CoDiNg
assert_magic_encoding(Encoding::US_ASCII, "# CoDiNg: ascii")
end
def test_encoding_whitespace
assert_magic_encoding(Encoding::US_ASCII, "# \s\t\v encoding \s\t\v : \s\t\v ascii \s\t\v")
end
def test_emacs_encoding
assert_magic_encoding(Encoding::US_ASCII, "# -*- encoding: ascii -*-")
end
def test_emacs_coding
assert_magic_encoding(Encoding::US_ASCII, "# -*- coding: ascii -*-")
end
def test_emacs_eNcOdInG
assert_magic_encoding(Encoding::US_ASCII, "# -*- eNcOdInG: ascii -*-")
end
def test_emacs_CoDiNg
assert_magic_encoding(Encoding::US_ASCII, "# -*- CoDiNg: ascii -*-")
end
def test_emacs_whitespace
assert_magic_encoding(Encoding::US_ASCII, "# -*- \s\t\v encoding \s\t\v : \s\t\v ascii \s\t\v -*-")
end
def test_emacs_multiple
assert_magic_encoding(Encoding::US_ASCII, "# -*- foo: bar; encoding: ascii -*-")
end
def test_emacs_missing_delimiter
assert_magic_encoding(Encoding::US_ASCII, '# -*- \1; encoding: ascii -*-')
end
def test_coding_whitespace
assert_magic_encoding(Encoding::ASCII_8BIT, "# coding \t \r \v : \t \v \r ascii-8bit")
end
def test_vim
assert_magic_encoding(Encoding::Windows_31J, "# vim: filetype=ruby, fileencoding=windows-31j, tabsize=3, shiftwidth=3")
end
private
def assert_magic_encoding(expected, line)
source = %Q{#{line}\n""}
actual = Ruby::Prism.parse(source).encoding
# Compare against our expectation.
assert_equal expected, actual
# Compare against Ruby's expectation.
if defined?(RubyVM::InstructionSequence)
previous = $VERBOSE
expected =
begin
$VERBOSE = nil
RubyVM::InstructionSequence.compile(source).eval.encoding
ensure
$VERBOSE = previous
end
assert_equal expected, actual
end
end
def assert_magic_comments(fixture)
source = fixture.read
# Check that we get the correct number of magic comments when lexing with
# ripper.
expected = MagicCommentRipper.new(source).tap(&:parse).magic_comments
actual = Ruby::Prism.parse(source).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