Files
ruby/test/prism/api/command_line_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

115 lines
3.1 KiB
Ruby

# frozen_string_literal: true
require_relative "../test_helper"
module Ruby::Prism
class CommandLineTest < TestCase
def test_command_line_p
program = Ruby::Prism.parse("1", command_line: "p").value
statements = program.statements.body
assert_equal 2, statements.length
assert_kind_of CallNode, statements.last
assert_equal :print, statements.last.name
end
def test_command_line_n
program = Ruby::Prism.parse("1", command_line: "n").value
statements = program.statements.body
assert_equal 1, statements.length
assert_kind_of WhileNode, statements.first
predicate = statements.first.predicate
assert_kind_of CallNode, predicate
assert_equal :gets, predicate.name
arguments = predicate.arguments.arguments
assert_equal 1, arguments.length
assert_equal :$/, arguments.first.name
end
def test_command_line_a
program = Ruby::Prism.parse("1", command_line: "na").value
statements = program.statements.body
assert_equal 1, statements.length
assert_kind_of WhileNode, statements.first
statement = statements.first.statements.body.first
assert_kind_of GlobalVariableWriteNode, statement
assert_equal :$F, statement.name
end
def test_command_line_l
program = Ruby::Prism.parse("1", command_line: "nl").value
statements = program.statements.body
assert_equal 1, statements.length
assert_kind_of WhileNode, statements.first
predicate = statements.first.predicate
assert_kind_of CallNode, predicate
assert_equal :gets, predicate.name
arguments = predicate.arguments
assert arguments.contains_keywords?
arguments = predicate.arguments.arguments
assert_equal 2, arguments.length
assert_equal :$/, arguments.first.name
assert_equal "chomp", arguments.last.elements.first.key.unescaped
end
def test_command_line_e
result = Ruby::Prism.parse("1 if 2..3")
assert_equal 2, result.warnings.length
result = Ruby::Prism.parse("1 if 2..3", command_line: "e")
assert_equal 0, result.warnings.length
end
def test_command_line_x_implicit
result = Ruby::Prism.parse_statement(<<~RUBY, main_script: true)
#!/bin/bash
exit 1
#!/usr/bin/env ruby
1
RUBY
assert_kind_of IntegerNode, result
end
def test_command_line_x_explicit
result = Ruby::Prism.parse_statement(<<~RUBY, command_line: "x")
exit 1
#!/usr/bin/env ruby
1
RUBY
assert_kind_of IntegerNode, result
end
def test_command_line_x_implicit_fail
result = Ruby::Prism.parse(<<~RUBY, main_script: true)
#!/bin/bash
exit 1
RUBY
assert_equal 1, result.errors.length
assert_equal :load, result.errors.first.level
end
def test_command_line_x_explicit_fail
result = Ruby::Prism.parse(<<~RUBY, command_line: "x")
exit 1
RUBY
assert_equal 1, result.errors.length
assert_equal :load, result.errors.first.level
end
end
end