summaryrefslogtreecommitdiff
path: root/test/prism/command_line_test.rb
blob: 4b04c36f3aa5e29ef11d6e299eca2740065af28e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# frozen_string_literal: true

require_relative "test_helper"

module Prism
  class CommandLineTest < TestCase
    def test_command_line_p
      program = 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 = 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 = 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 = 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.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 = Prism.parse("1 if 2..3")
      assert_equal 2, result.warnings.length

      result = Prism.parse("1 if 2..3", command_line: "e")
      assert_equal 0, result.warnings.length
    end

    def test_command_line_x_implicit
      result = Prism.parse(<<~RUBY)
        #!/bin/bash
        exit 1

        #!/usr/bin/env ruby
        1
      RUBY

      assert_kind_of IntegerNode, result.value.statements.body.first
    end

    def test_command_line_x_explicit
      result = Prism.parse(<<~RUBY, command_line: "x")
        exit 1

        #!/usr/bin/env ruby
        1
      RUBY

      assert_kind_of IntegerNode, result.value.statements.body.first
    end

    def test_command_line_x_implicit_fail
      result = Prism.parse(<<~RUBY)
        #!/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 = 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