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
|
# frozen_string_literal: true
require_relative "../test_helper"
module Prism
class ParseTest < TestCase
def test_parse_result
result = Prism.parse("")
assert_kind_of ParseResult, result
result = Prism.parse_file(__FILE__)
assert_kind_of ParseResult, result
end
def test_parse_empty_string
result = Prism.parse("")
assert_equal [], result.value.statements.body
end
def test_parse_takes_file_path
filepath = "filepath.rb"
result = Prism.parse("def foo; __FILE__; end", filepath: filepath)
assert_equal filepath, find_source_file_node(result.value).filepath
end
def test_parse_takes_line
line = 4
result = Prism.parse("def foo\n __FILE__\nend", line: line)
assert_equal line, result.value.location.start_line
assert_equal line + 1, find_source_file_node(result.value).location.start_line
result = Prism.parse_lex("def foo\n __FILE__\nend", line: line)
assert_equal line, result.value.first.location.start_line
end
def test_parse_takes_negative_lines
line = -2
result = Prism.parse("def foo\n __FILE__\nend", line: line)
assert_equal line, result.value.location.start_line
assert_equal line + 1, find_source_file_node(result.value).location.start_line
result = Prism.parse_lex("def foo\n __FILE__\nend", line: line)
assert_equal line, result.value.first.location.start_line
end
def test_parse_file
node = Prism.parse_file(__FILE__).value
assert_kind_of ProgramNode, node
error = assert_raise Errno::ENOENT do
Prism.parse_file("idontexist.rb")
end
assert_equal "No such file or directory - idontexist.rb", error.message
assert_raise TypeError do
Prism.parse_file(nil)
end
end
private
def find_source_file_node(program)
queue = [program]
while (node = queue.shift)
return node if node.is_a?(SourceFileNode)
queue.concat(node.compact_child_nodes)
end
end
end
end
|