Skip to content

Commit 2a0efad

Browse files
authored
== operator for green trees (#275)
Also add some more direct GreeNode tests
1 parent 53dd9d3 commit 2a0efad

File tree

4 files changed

+62
-0
lines changed

4 files changed

+62
-0
lines changed

src/green_tree.jl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,10 @@ head(node::GreenNode) = node.head
5555

5656
Base.summary(node::GreenNode) = summary(node.head)
5757

58+
function Base.:(==)(n1::GreenNode, n2::GreenNode)
59+
n1.head == n2.head && n1.span == n2.span && n1.args == n2.args
60+
end
61+
5862
# Pretty printing
5963
function _show_green_node(io, node, indent, pos, str, show_trivia)
6064
if !show_trivia && is_trivia(node)

test/green_node.jl

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
@testset "GreenNode" begin
2+
t = parsestmt(GreenNode, "aa + b")
3+
4+
@test span(t) == 6
5+
@test haschildren(t)
6+
@test head(t) == SyntaxHead(K"call", 0x0008)
7+
@test span.(children(t)) == [2,1,1,1,1]
8+
@test head.(children(t)) == [
9+
SyntaxHead(K"Identifier", 0x0000)
10+
SyntaxHead(K"Whitespace", 0x0001)
11+
SyntaxHead(K"+", 0x0000)
12+
SyntaxHead(K"Whitespace", 0x0001)
13+
SyntaxHead(K"Identifier", 0x0000)
14+
]
15+
16+
t2 = parsestmt(GreenNode, "aa + b")
17+
@test t == t2
18+
@test t !== t2
19+
20+
text = "f(@x(y), z)"
21+
@test sprint(show, MIME("text/plain"), parsestmt(GreenNode, text)) ==
22+
"""
23+
1:11 │[call]
24+
1:1 │ Identifier ✔
25+
2:2 │ (
26+
3:7 │ [macrocall]
27+
3:3 │ @
28+
4:4 │ MacroName ✔
29+
5:5 │ (
30+
6:6 │ Identifier ✔
31+
7:7 │ )
32+
8:8 │ ,
33+
9:9 │ Whitespace
34+
10:10 │ Identifier ✔
35+
11:11 │ )
36+
"""
37+
38+
@test sprint(show, MIME("text/plain"), parsestmt(GreenNode, text), text) ==
39+
"""
40+
1:11 │[call]
41+
1:1 │ Identifier ✔ "f"
42+
2:2 │ ( "("
43+
3:7 │ [macrocall]
44+
3:3 │ @ "@"
45+
4:4 │ MacroName ✔ "x"
46+
5:5 │ ( "("
47+
6:6 │ Identifier ✔ "y"
48+
7:7 │ ) ")"
49+
8:8 │ , ","
50+
9:9 │ Whitespace " "
51+
10:10 │ Identifier ✔ "z"
52+
11:11 │ ) ")"
53+
"""
54+
end

test/runtests.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ end
5151

5252
include("parse_stream.jl")
5353
include("parser.jl")
54+
include("green_node.jl")
5455
include("syntax_tree.jl")
5556
include("diagnostics.jl")
5657
include("parser_api.jl")

test/test_utils.jl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ using .JuliaSyntax:
2020
# Node inspection
2121
kind,
2222
flags,
23+
head,
24+
span,
25+
SyntaxHead,
2326
is_trivia,
2427
sourcetext,
2528
haschildren,

0 commit comments

Comments
 (0)