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
112
|
# frozen_string_literal: true
require_relative "test_helper"
module Prism
class RubyAPITest < TestCase
def test_ruby_api
filepath = __FILE__
source = File.read(filepath, binmode: true, external_encoding: Encoding::UTF_8)
assert_equal Prism.lex(source, filepath: filepath).value, Prism.lex_file(filepath).value
assert_equal Prism.dump(source, filepath: filepath), Prism.dump_file(filepath)
serialized = Prism.dump(source, filepath: filepath)
ast1 = Prism.load(source, serialized).value
ast2 = Prism.parse(source, filepath: filepath).value
ast3 = Prism.parse_file(filepath).value
assert_equal_nodes ast1, ast2
assert_equal_nodes ast2, ast3
end
def test_options
assert_equal "", Prism.parse("__FILE__").value.statements.body[0].filepath
assert_equal "foo.rb", Prism.parse("__FILE__", filepath: "foo.rb").value.statements.body[0].filepath
assert_equal 1, Prism.parse("foo").value.statements.body[0].location.start_line
assert_equal 10, Prism.parse("foo", line: 10).value.statements.body[0].location.start_line
refute Prism.parse("\"foo\"").value.statements.body[0].frozen?
assert Prism.parse("\"foo\"", frozen_string_literal: true).value.statements.body[0].frozen?
refute Prism.parse("\"foo\"", frozen_string_literal: false).value.statements.body[0].frozen?
assert_kind_of Prism::CallNode, Prism.parse("foo").value.statements.body[0]
assert_kind_of Prism::LocalVariableReadNode, Prism.parse("foo", scopes: [[:foo]]).value.statements.body[0]
assert_equal 2, Prism.parse("foo", scopes: [[:foo], []]).value.statements.body[0].depth
end
def test_literal_value_method
assert_equal 123, parse_expression("123").value
assert_equal 3.14, parse_expression("3.14").value
assert_equal 42i, parse_expression("42i").value
assert_equal 42.1ri, parse_expression("42.1ri").value
assert_equal 3.14i, parse_expression("3.14i").value
assert_equal 42r, parse_expression("42r").value
assert_equal 0.5r, parse_expression("0.5r").value
assert_equal 42ri, parse_expression("42ri").value
assert_equal 0.5ri, parse_expression("0.5ri").value
end
def test_location_join
recv, args_node, _ = parse_expression("1234 + 567").child_nodes
arg = args_node.arguments[0]
joined = recv.location.join(arg.location)
assert_equal 0, joined.start_offset
assert_equal 10, joined.length
assert_raise RuntimeError, "Incompatible locations" do
arg.location.join(recv.location)
end
other_arg = parse_expression("1234 + 567").arguments.arguments[0]
assert_raise RuntimeError, "Incompatible sources" do
other_arg.location.join(recv.location)
end
assert_raise RuntimeError, "Incompatible sources" do
recv.location.join(other_arg.location)
end
end
def test_location_character_offsets
program = Prism.parse("😀 + 😀\n😍 ||= 😍").value
# first 😀
location = program.statements.body.first.receiver.location
assert_equal 0, location.start_character_offset
assert_equal 1, location.end_character_offset
assert_equal 0, location.start_character_column
assert_equal 1, location.end_character_column
# second 😀
location = program.statements.body.first.arguments.arguments.first.location
assert_equal 4, location.start_character_offset
assert_equal 5, location.end_character_offset
assert_equal 4, location.start_character_column
assert_equal 5, location.end_character_column
# first 😍
location = program.statements.body.last.name_loc
assert_equal 6, location.start_character_offset
assert_equal 7, location.end_character_offset
assert_equal 0, location.start_character_column
assert_equal 1, location.end_character_column
# second 😍
location = program.statements.body.last.value.location
assert_equal 12, location.start_character_offset
assert_equal 13, location.end_character_offset
assert_equal 6, location.start_character_column
assert_equal 7, location.end_character_column
end
private
def parse_expression(source)
Prism.parse(source).value.statements.body.first
end
end
end
|