(t *testing.T)
| 134 | } |
| 135 | |
| 136 | func TestExprVisitor(t *testing.T) { |
| 137 | tests := []struct { |
| 138 | expr string |
| 139 | preOrderIDs []int64 |
| 140 | postOrderIDs []int64 |
| 141 | }{ |
| 142 | { |
| 143 | // [2] ==, [1] 'a', [3] 'b' |
| 144 | expr: `'a' == 'b'`, |
| 145 | preOrderIDs: []int64{2, 1, 3}, |
| 146 | postOrderIDs: []int64{1, 3, 2}, |
| 147 | }, |
| 148 | { |
| 149 | // [2] size(), [1] 'a' |
| 150 | expr: `'a'.size()`, |
| 151 | preOrderIDs: []int64{2, 1}, |
| 152 | postOrderIDs: []int64{1, 2}, |
| 153 | }, |
| 154 | { |
| 155 | // [3] ==, [1] type(), [2] 1, [4] int |
| 156 | expr: `type(1) == int`, |
| 157 | preOrderIDs: []int64{3, 1, 2, 4}, |
| 158 | postOrderIDs: []int64{2, 1, 4, 3}, |
| 159 | }, |
| 160 | { |
| 161 | // [5] .hello, [1] {}, [3] 'hello', [4] 'world' |
| 162 | expr: `{'hello': 'world'}.hello`, |
| 163 | preOrderIDs: []int64{5, 1, 3, 4}, |
| 164 | postOrderIDs: []int64{3, 4, 1, 5}, |
| 165 | }, |
| 166 | { |
| 167 | // [1] TestAllTypes, [3] 1 |
| 168 | expr: `google.expr.proto3.test.TestAllTypes{single_int32: 1}`, |
| 169 | preOrderIDs: []int64{1, 3}, |
| 170 | postOrderIDs: []int64{3, 1}, |
| 171 | }, |
| 172 | { |
| 173 | // [13] comprehension |
| 174 | // range: [1] [], [2] true |
| 175 | // accuInit: [6] result=false |
| 176 | // loopCond: [9] @not_strictly_false, [8] !, [7] result |
| 177 | // loopStep: [11] ||, [10] result [5] i |
| 178 | // result: [12] result |
| 179 | expr: `[true].exists(i, i)`, |
| 180 | preOrderIDs: []int64{13, 1, 2, 6, 9, 8, 7, 11, 10, 5, 12}, |
| 181 | postOrderIDs: []int64{2, 1, 6, 7, 8, 9, 10, 5, 11, 12, 13}, |
| 182 | }, |
| 183 | } |
| 184 | |
| 185 | for _, tst := range tests { |
| 186 | tc := tst |
| 187 | t.Run(tc.expr, func(t *testing.T) { |
| 188 | checked := mustTypeCheck(t, tc.expr) |
| 189 | root := ast.NavigateAST(checked) |
| 190 | // Verify pre order visit behavior |
| 191 | preOrderExprIDs := []int64{} |
| 192 | navVisitor := ast.NewExprVisitor(func(e ast.Expr) { |
| 193 | nav := e.(ast.NavigableExpr) |
nothing calls this directly
no test coverage detected