Skip to content

Commit 90f3efe

Browse files
committed
Stub source_location for older Rubies in NodeForTest
1 parent e56e49a commit 90f3efe

1 file changed

Lines changed: 25 additions & 11 deletions

File tree

test/prism/ruby/node_for_test.rb

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,80 +10,94 @@ class NodeForTest < TestCase
1010
def m(foo)
1111
42
1212
end
13+
M_LOCATION = [__LINE__-3, 4, __LINE__-1, 7]
1314

1415
def inline_method = 42
16+
INLINE_LOCATION = [__LINE__-1, 4, __LINE__-1, 26]
1517

1618
define_method(:define_method_method) { 42 }
19+
DEFINE_METHOD_LOCATION = [__LINE__-1, 41, __LINE__-1, 47]
1720

1821
def return_block(&block) = block
1922

23+
def with_location(callable, locs, file = __FILE__)
24+
source_location = [file, *locs]
25+
if RUBY_VERSION >= "4.0"
26+
assert_equal callable.source_location, source_location
27+
else
28+
callable.define_singleton_method(:source_location) { source_location }
29+
end
30+
callable
31+
end
32+
2033
def test_def_method
21-
node = Prism.node_for(NodeForTest.instance_method(:m))
34+
node = Prism.node_for(with_location(NodeForTest.instance_method(:m), M_LOCATION))
2235
assert_instance_of(Prism::DefNode, node)
2336
assert_equal "def m(foo)\n#{INDENT} 42\n#{INDENT}end", node.slice
2437

25-
node = Prism.node_for(method(:m))
38+
node = Prism.node_for(with_location(method(:m), M_LOCATION))
2639
assert_instance_of(Prism::DefNode, node)
2740
assert_equal "def m(foo)\n 42\n end", node.slice
2841
end
2942

3043
def test_inline_method
31-
node = Prism.node_for(method(:inline_method))
44+
node = Prism.node_for(with_location(method(:inline_method), INLINE_LOCATION))
3245
assert_instance_of(Prism::DefNode, node)
3346
assert_equal "def inline_method = 42", node.slice
3447
end
3548

3649
def test_define_method
37-
node = Prism.node_for(method(:define_method_method))
50+
node = Prism.node_for(with_location(method(:define_method_method), DEFINE_METHOD_LOCATION))
3851
assert_instance_of(Prism::CallNode, node)
3952
assert_equal "define_method(:define_method_method) { 42 }", node.slice
4053
assert_equal "{ 42 }", node.block.slice
4154
end
4255

4356
def test_lambda
44-
node = Prism.node_for(-> { 42 })
57+
node = Prism.node_for(with_location(-> { 42 }, [__LINE__, 44, __LINE__, 51]))
4558
assert_instance_of(Prism::LambdaNode, node)
4659
assert_equal "-> { 42 }", node.slice
4760
assert_equal "{ 42 }", node.opening_loc.join(node.closing_loc).slice
4861

49-
node = Prism.node_for(lambda { 42 })
62+
node = Prism.node_for(with_location(lambda { 42 }, [__LINE__, 49, __LINE__, 55]))
5063
assert_instance_of(Prism::CallNode, node)
5164
assert_equal "lambda { 42 }", node.slice
5265
assert_equal "{ 42 }", node.block.slice
5366
end
5467

5568
def test_proc
56-
node = Prism.node_for(proc { 42 })
69+
node = Prism.node_for(with_location(proc { 42 }, [__LINE__, 47, __LINE__, 53]))
5770
assert_instance_of(Prism::CallNode, node)
5871
assert_equal "proc { 42 }", node.slice
5972
assert_equal "{ 42 }", node.block.slice
6073

61-
node = Prism.node_for(return_block { 42 })
74+
node = Prism.node_for(with_location(return_block { 42 }, [__LINE__, 55, __LINE__, 61]))
6275
assert_instance_of(Prism::CallNode, node)
6376
assert_equal "return_block { 42 }", node.slice
6477
assert_equal "{ 42 }", node.block.slice
6578

6679
heredoc_proc = proc { <<~END }
6780
heredoc
6881
END
69-
node = Prism.node_for(heredoc_proc)
82+
node = Prism.node_for(with_location(heredoc_proc, [__LINE__-3, 26, __LINE__-3, 36]))
7083
assert_instance_of(Prism::CallNode, node)
7184
assert_equal "proc { <<~END }", node.slice
7285
assert_equal "heredoc\n", node.block.body.body.first.unescaped
7386
end
7487

7588
def test_method_to_proc
76-
node = Prism.node_for(method(:inline_method).to_proc)
89+
node = Prism.node_for(with_location(method(:inline_method).to_proc, INLINE_LOCATION))
7790
assert_instance_of(Prism::DefNode, node)
7891
assert_equal "def inline_method = 42", node.slice
7992
end
8093

8194
def test_eval
82-
l = eval "-> { 42 }"
95+
l = with_location(eval("-> { 42 }"), [1, 2, 1, 9], "(eval at #{__FILE__}:#{__LINE__})")
8396
e = assert_raise(ArgumentError) { Prism.node_for(l) }
8497
assert_include e.message, 'eval'
8598

8699
l = eval "-> { 42 }", nil, __FILE__, __LINE__
100+
l = with_location(l, [__LINE__-1, 2, __LINE__-1, 9])
87101
e = assert_raise(ArgumentError) { Prism.node_for(l) }
88102
assert_include e.message, 'Could not find node'
89103
end

0 commit comments

Comments
 (0)