(t *testing.T)
| 198 | } |
| 199 | |
| 200 | func TestOptimizeWithSource(t *testing.T) { |
| 201 | initial := `has(a.b)` |
| 202 | replacement := `x["a"]` |
| 203 | e := optimizerEnv(t) |
| 204 | initialAST, iss := e.Compile(initial) |
| 205 | if iss.Err() != nil { |
| 206 | t.Fatalf("Compile(%q) failed: %v", initial, iss.Err()) |
| 207 | } |
| 208 | replacementAST, iss := e.Compile(replacement) |
| 209 | if iss.Err() != nil { |
| 210 | t.Fatalf("Compile(%q) failed: %v", replacement, iss.Err()) |
| 211 | } |
| 212 | |
| 213 | opt, err := cel.NewStaticOptimizer( |
| 214 | &replaceOptimizer{t: t, targetAST: replacementAST.NativeRep()}, |
| 215 | cel.OptimizeWithSource(replacementAST.Source()), |
| 216 | ) |
| 217 | if err != nil { |
| 218 | t.Fatalf("NewStaticOptimizer() failed: %v", err) |
| 219 | } |
| 220 | optAST, iss := opt.Optimize(e, initialAST) |
| 221 | if iss.Err() != nil { |
| 222 | t.Fatalf("Optimize() returned an error: %v", iss.Err()) |
| 223 | } |
| 224 | |
| 225 | if optAST.Source().Content() != replacement { |
| 226 | t.Errorf("got source content %q, wanted %q", optAST.Source().Content(), replacement) |
| 227 | } |
| 228 | sourceInfoPB, err := ast.SourceInfoToProto(optAST.NativeRep().SourceInfo()) |
| 229 | if err != nil { |
| 230 | t.Fatalf("cel.AstToCheckedExpr() failed: %v", err) |
| 231 | } |
| 232 | wantTextPB := ` |
| 233 | location: "<input>" |
| 234 | line_offsets: 7 |
| 235 | positions: { |
| 236 | key: 1 |
| 237 | value: 1 |
| 238 | } |
| 239 | positions: { |
| 240 | key: 2 |
| 241 | value: 0 |
| 242 | } |
| 243 | positions: { |
| 244 | key: 3 |
| 245 | value: 2 |
| 246 | } |
| 247 | ` |
| 248 | var wantSourceInfoPB exprpb.SourceInfo |
| 249 | if err := prototext.Unmarshal([]byte(wantTextPB), &wantSourceInfoPB); err != nil { |
| 250 | t.Fatalf("prototext.Unmarshal() failed: %v", err) |
| 251 | } |
| 252 | if !proto.Equal(&wantSourceInfoPB, sourceInfoPB) { |
| 253 | t.Errorf("got source info: %s, wanted %s", prototext.Format(sourceInfoPB), wantTextPB) |
| 254 | } |
| 255 | } |
| 256 | |
| 257 | func TestStaticOptimizerNilAST(t *testing.T) { |
nothing calls this directly
no test coverage detected