|
63 | 63 | function testgraph(edge_ranges, edges, more_attrs...) |
64 | 64 | kinds = Dict(map(i->(i=>K"block"), eachindex(edge_ranges))) |
65 | 65 | sources = Dict(map(i->(i=>LineNumberNode(i)), eachindex(edge_ranges))) |
| 66 | + orig = Dict(map(i->(i=>i), eachindex(edge_ranges))) |
66 | 67 | SyntaxGraph( |
67 | 68 | edge_ranges, |
68 | 69 | edges, |
69 | | - Dict(:kind => kinds, :source => sources, more_attrs...)) |
| 70 | + Dict(:kind => kinds, :source => sources, |
| 71 | + :orig => orig, more_attrs...)) |
70 | 72 | end |
71 | 73 |
|
72 | 74 | @testset "copy_ast" begin |
@@ -107,4 +109,175 @@ end |
107 | 109 | # Disallow for now, since we can't prevent dangling sourcerefs |
108 | 110 | @test_throws ErrorException JuliaLowering.copy_ast(new_g, st; copy_source=false) |
109 | 111 | end |
| 112 | + |
| 113 | + @testset "unalias_nodes" begin |
| 114 | + # 1 -+-> 2 ->-> 4 |
| 115 | + # | | |
| 116 | + # +-> 3 -+ |
| 117 | + g = testgraph([1:2, 3:3, 4:4, 0:-1], [2, 3, 4, 4]) |
| 118 | + st = SyntaxTree(g, 1) |
| 119 | + stu = JuliaLowering.unalias_nodes(st) |
| 120 | + @test st ≈ stu |
| 121 | + @test length(stu._graph.edge_ranges) == 5 |
| 122 | + @test length(stu._graph.edges) == 4 |
| 123 | + # Properties of node 4 should be preserved |
| 124 | + @test 4 == stu[1][1].orig == stu[2][1].orig |
| 125 | + @test st[1][1].source == stu[1][1].source == stu[2][1].source |
| 126 | + @test stu[1][1]._id != stu[2][1]._id |
| 127 | + |
| 128 | + # Try again with overlapping edge_ranges |
| 129 | + g = testgraph([1:2, 3:3, 3:3, 0:-1], [2, 3, 4]) |
| 130 | + st = SyntaxTree(g, 1) |
| 131 | + stu = JuliaLowering.unalias_nodes(st) |
| 132 | + @test st ≈ stu |
| 133 | + @test length(stu._graph.edge_ranges) == 5 |
| 134 | + @test length(stu._graph.edges) == 4 |
| 135 | + @test 4 == stu[1][1].orig == stu[2][1].orig |
| 136 | + @test st[1][1].source == stu[1][1].source == stu[2][1].source |
| 137 | + @test stu[1][1]._id != stu[2][1]._id |
| 138 | + |
| 139 | + # +-> 5 |
| 140 | + # | |
| 141 | + # 1 -+-> 2 -+---->>>-> 6 |
| 142 | + # | ||| |
| 143 | + # +-> 3 -> 7 -+|| |
| 144 | + # | || |
| 145 | + # +-> 4 -+-----+| |
| 146 | + # | | |
| 147 | + # +------+ |
| 148 | + g = testgraph([1:3, 4:5, 6:6, 7:8, 0:-1, 0:-1, 9:9], |
| 149 | + [2, 3, 4, 5, 6, 7, 6, 6, 6]) |
| 150 | + st = SyntaxTree(g, 1) |
| 151 | + stu = JuliaLowering.unalias_nodes(st) |
| 152 | + @test st ≈ stu |
| 153 | + # node 6 should be copied three times |
| 154 | + @test length(stu._graph.edge_ranges) == 10 |
| 155 | + @test length(stu._graph.edges) == 9 |
| 156 | + # the four copies of node 6 should have attrs identical to the original and distinct ids |
| 157 | + @test 6 == stu[1][2].orig == stu[2][1][1].orig == stu[3][1].orig == stu[3][2].orig |
| 158 | + @test stu[1][2]._id != stu[2][1][1]._id != stu[3][1]._id != stu[3][2]._id |
| 159 | + |
| 160 | + # 1 -+-> 2 ->-> 4 -+----> 5 ->-> 7 |
| 161 | + # | | | | |
| 162 | + # +-> 3 -+ +-->-> 6 -+ |
| 163 | + # | | |
| 164 | + # +------------+ |
| 165 | + g = testgraph([1:2, 3:3, 4:5, 6:7, 8:8, 9:9, 0:-1], |
| 166 | + [2,3,4,4,6,5,6,7,7]) |
| 167 | + st = SyntaxTree(g, 1) |
| 168 | + stu = JuliaLowering.unalias_nodes(st) |
| 169 | + @test st ≈ stu |
| 170 | + @test length(stu._graph.edge_ranges) == 15 |
| 171 | + @test length(stu._graph.edges) == 14 |
| 172 | + # attrs of nodes 4-7 |
| 173 | + @test 4 == stu[1][1].orig == stu[2][1].orig |
| 174 | + @test 5 == stu[1][1][1].orig == stu[2][1][1].orig |
| 175 | + @test 6 == stu[1][1][2].orig == stu[2][1][2].orig == stu[2][2].orig |
| 176 | + @test 7 == stu[1][1][1][1].orig == stu[1][1][2][1].orig == |
| 177 | + stu[2][1][1][1].orig == stu[2][1][2][1].orig == stu[2][2][1].orig |
| 178 | + # ensure no duplication |
| 179 | + @test stu[1][1][1][1]._id != stu[1][1][2][1]._id != |
| 180 | + stu[2][1][1][1]._id != stu[2][1][2][1]._id != stu[2][2][1]._id |
| 181 | + end |
| 182 | + |
| 183 | + @testset "prune" begin |
| 184 | + # [1]-+-> 2 5 --> 6 |
| 185 | + # | |
| 186 | + # +-> 3 --> 4 7 |
| 187 | + g = testgraph([1:2, 0:-1, 3:3, 0:-1, 4:4, 0:-1, 0:-1], [2, 3, 4, 6]) |
| 188 | + st = SyntaxTree(g, 1) |
| 189 | + stp = JuliaLowering.prune(st) |
| 190 | + @test st ≈ stp |
| 191 | + @test length(syntax_graph(stp).edge_ranges) === 4 |
| 192 | + @test stp.source == LineNumberNode(1) |
| 193 | + @test stp[1].source == LineNumberNode(2) |
| 194 | + @test stp[2].source == LineNumberNode(3) |
| 195 | + @test stp[2][1].source == LineNumberNode(4) |
| 196 | + |
| 197 | + # (also checks that the last prune didn't destroy the graph) |
| 198 | + # 1 -+-> 2 5 --> 6 |
| 199 | + # | |
| 200 | + # +-> 3 --> 4 [7] |
| 201 | + st = SyntaxTree(g, 7) |
| 202 | + stp = JuliaLowering.prune(st) |
| 203 | + @test st ≈ stp |
| 204 | + @test length(syntax_graph(stp).edge_ranges) === 1 |
| 205 | + @test stp.orig == 7 |
| 206 | + |
| 207 | + # 1 -+->[2]->-> 4 |
| 208 | + # | | |
| 209 | + # +-> 3 -+ |
| 210 | + g = testgraph([1:2, 3:3, 4:4, 0:-1], [2, 3, 4, 4]) |
| 211 | + st = SyntaxTree(g, 2) |
| 212 | + stp = JuliaLowering.prune(st) |
| 213 | + @test st ≈ stp |
| 214 | + @test length(syntax_graph(stp).edge_ranges) === 2 |
| 215 | + @test stp.orig == 2 |
| 216 | + @test stp[1].orig == 4 |
| 217 | + |
| 218 | + # 9 -->[1]--> 5 src(1) = 2 |
| 219 | + # 10 --> 2 --> 6 src(2) = 3 |
| 220 | + # 11 --> 3 --> 7 src(3) = 4 |
| 221 | + # 12 --> 4 --> 8 else src(i) = line(i) |
| 222 | + g = testgraph([1:1, 2:2, 3:3, 4:4, 0:-1, 0:-1, 0:-1, 0:-1, 5:5, 6:6, 7:7, 8:8], |
| 223 | + [5, 6, 7, 8, 1, 2, 3, 4], |
| 224 | + :source => Dict( |
| 225 | + 1=>2, 2=>3, 3=>4, |
| 226 | + map(i->(i=>LineNumberNode(i)), 4:12)...)) |
| 227 | + st = SyntaxTree(g, 1) |
| 228 | + stp = JuliaLowering.prune(st) |
| 229 | + @test st ≈ stp |
| 230 | + # 1, 5, 4, 8 should remain |
| 231 | + @test length(syntax_graph(stp).edge_ranges) === 4 |
| 232 | + @test stp.source isa NodeId |
| 233 | + orig_4 = SyntaxTree(syntax_graph(stp), stp.source) |
| 234 | + @test orig_4.source === LineNumberNode(4) |
| 235 | + @test numchildren(orig_4) === 1 |
| 236 | + @test orig_4[1].source === LineNumberNode(8) |
| 237 | + @test stp[1].source === LineNumberNode(5) |
| 238 | + |
| 239 | + # Try again with node 3 explicitly marked reachable |
| 240 | + stp = JuliaLowering.prune(st, keep=JuliaLowering.SyntaxList(g, NodeId[3, 4])) |
| 241 | + @test st ≈ stp |
| 242 | + # 1, 5, 4, 8, and now 3, 7 as well |
| 243 | + @test length(syntax_graph(stp).edge_ranges) === 6 |
| 244 | + @test stp.source isa NodeId |
| 245 | + @test stp[1].source === LineNumberNode(5) |
| 246 | + |
| 247 | + orig_3 = SyntaxTree(syntax_graph(stp), stp.source) |
| 248 | + @test orig_3.source isa NodeId |
| 249 | + orig_4 = SyntaxTree(syntax_graph(stp), orig_3.source) |
| 250 | + @test orig_4.source === LineNumberNode(4) |
| 251 | + |
| 252 | + @test numchildren(orig_3) === 1 |
| 253 | + @test numchildren(orig_4) === 1 |
| 254 | + @test orig_3[1].source === LineNumberNode(7) |
| 255 | + @test orig_4[1].source === LineNumberNode(8) |
| 256 | + |
| 257 | + # Try again with no node provenance |
| 258 | + stp = JuliaLowering.prune(st, keep=nothing) |
| 259 | + @test st ≈ stp |
| 260 | + @test length(syntax_graph(stp).edge_ranges) === 2 |
| 261 | + @test stp.source === LineNumberNode(4) |
| 262 | + @test stp[1].source === LineNumberNode(5) |
| 263 | + |
| 264 | + # "real world" test with lowered output---not many properties we can |
| 265 | + # check without fragile tests, but there are some. |
| 266 | + test_mod = Module() |
| 267 | + code = "begin; x1=1; x2=2; x3=3; x4=begin; 4; end; begin; end; end" |
| 268 | + st0 = parsestmt(SyntaxTree, code) |
| 269 | + st5 = JuliaLowering.lower(test_mod, st0) |
| 270 | + stp = JuliaLowering.prune(st5) |
| 271 | + @test st5 ≈ stp |
| 272 | + @test length(syntax_graph(stp).edge_ranges) < length(syntax_graph(st5).edge_ranges) |
| 273 | + @test stp.source isa NodeId |
| 274 | + @test SyntaxTree(syntax_graph(stp), stp.source) ≈ st0 |
| 275 | + @test sourcetext(stp) == code |
| 276 | + # try without preserving st0 |
| 277 | + stp = JuliaLowering.prune(st5, keep=nothing) |
| 278 | + @test st5 ≈ stp |
| 279 | + @test length(syntax_graph(stp).edge_ranges) < length(syntax_graph(st5).edge_ranges) |
| 280 | + @test stp.source isa SourceRef |
| 281 | + @test sourcetext(stp) == code |
| 282 | + end |
110 | 283 | end |
0 commit comments