Skip to content

Commit 9fa5661

Browse files
authored
Improve test coverge of source_files, syntax_tree (#204)
These two files are particularly affected by recent and upcoming changes (e.g., #193). This adds a bit more coverage as a guard against breakage.
1 parent f1440dc commit 9fa5661

File tree

3 files changed

+34
-5
lines changed

3 files changed

+34
-5
lines changed

src/syntax_tree.jl

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -286,10 +286,10 @@ end
286286
"""
287287
Print the code, highlighting the part covered by `node` at tree `path`.
288288
"""
289-
function highlight(code::String, node, path::Int...; color=(40,40,70))
289+
function highlight(io::IO, code::String, node, path::Int...; color=(40,40,70))
290290
node, p, span = child_position_span(node, path...)
291291
q = p + span
292-
print(stdout, code[1:p-1])
293-
_printstyled(stdout, code[p:q-1]; bgcolor=color)
294-
print(stdout, code[q:end])
292+
print(io, code[1:p-1])
293+
_printstyled(io, code[p:q-1]; bgcolor=color)
294+
print(io, code[q:end])
295295
end

test/source_files.jl

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,9 @@
2626
@test source_location(SourceFile(; filename=path), 1) == (1,1)
2727
@test source_location(SourceFile(; filename=path, first_line=7), 1) == (7,1)
2828
end
29+
end
2930

31+
@testset "SourceFile position indexing" begin
3032
@test SourceFile("a\nb\n")[1:2] == "a\n"
3133
@test SourceFile("a\nb\n")[3:end] == "b\n"
3234
if Base.VERSION >= v"1.4"
@@ -36,4 +38,14 @@
3638

3739
# unicode
3840
@test SourceFile("αβ")[1:2] == "α"
41+
@test SourceFile("αβ")[3] == 'β'
42+
end
43+
44+
@testset "SourceFile printing and text extraction" begin
45+
srcf = SourceFile("module Foo\nend")
46+
@test sprint(show, MIME("text/plain"), srcf) == """
47+
## SourceFile ##
48+
module Foo
49+
end"""
50+
@test sourcetext(srcf) == "module Foo\nend"
3951
end

test/syntax_tree.jl

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
@testset "SyntaxNode" begin
22
# Child access
3-
t = parse(SyntaxNode, "a*b + c")
3+
tt = "a*b + c"
4+
t = parse(SyntaxNode, tt)
45

56
@test sourcetext(child(t, 1)) == "a*b"
67
@test sourcetext(child(t, 1, 1)) == "a"
@@ -9,10 +10,26 @@
910
@test sourcetext(child(t, 2)) == "+"
1011
@test sourcetext(child(t, 3)) == "c"
1112

13+
@test JuliaSyntax.first_byte(child(t, 2)) == findfirst(==('+'), tt)
14+
1215
# Child indexing
1316
@test t[1] === child(t, 1)
1417
@test t[1, 1] === child(t, 1, 1)
1518
@test t[end] === child(t, 3)
1619
# Unfortunately, can't make t[1, end] work
1720
# as `lastindex(t, 2)` isn't well defined
21+
22+
@test sprint(show, t) == "(call-i (call-i a * b) + c)"
23+
str = sprint(show, MIME("text/plain"), t)
24+
# These tests are deliberately quite relaxed to avoid being too specific about display style
25+
@test occursin("line:col", str)
26+
@test occursin("call-i", str)
27+
@test sprint(JuliaSyntax.highlight, tt, t, 1, 3) == "a*\e[48;2;40;40;70mb\e[0;0m + c"
28+
@test sprint(JuliaSyntax.highlight, tt, t.raw, 5) == "a*b + \e[48;2;40;40;70mc\e[0;0m"
29+
30+
node = parse(SyntaxNode, "f()")
31+
push!(node, parse(SyntaxNode, "x"))
32+
@test length(children(node)) == 2
33+
node[2] = parse(SyntaxNode, "y")
34+
@test sourcetext(child(node, 2)) == "y"
1835
end

0 commit comments

Comments
 (0)