| 1 | // Copyright 2022 Google LLC |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
| 15 | package parserscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/parser`/ |
| 16 | |
| 17 | import ( |
| 18 | "testingscip-go gomod github.com/golang/go/src go1.23.0 testing/" |
| 19 | |
| 20 | "github.com/google/cel-go/commonscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common`/" |
| 21 | "github.com/google/cel-go/common/astscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common/ast`/" |
| 22 | |
| 23 | "google.golang.org/protobuf/protoscip-go gomod google.golang.org/protobuf v1.36.10 `google.golang.org/protobuf/proto`/" |
| 24 | ) |
| 25 | |
| 26 | func TestExprHelperCopyscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/parser`/TestExprHelperCopy().(t *testingscip-go gomod github.com/golang/go/src go1.23.0 testing/.Tscip-go gomod github.com/golang/go/src go1.23.0 testing/T#) { |
| 27 | src := commonscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common`/.NewStringSourcescip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common`/NewStringSource().( |
| 28 | `noop([1, 2, 3].map(i, Msg{first: 1 + 2, second: a.b, third: {true: true}}))`, |
| 29 | "") |
| 30 | p, err := NewParserscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/parser`/NewParser().( |
| 31 | PopulateMacroCallsscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/parser`/PopulateMacroCalls().(true), |
| 32 | Macrosscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/parser`/Macros().( |
| 33 | MapMacroscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/parser`/MapMacro., |
| 34 | NewGlobalMacroscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/parser`/NewGlobalMacro().("noop", 1, |
| 35 | func(eh ExprHelperscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/parser`/ExprHelper#, target astscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common/ast`/.Exprscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common/ast`/Expr#, args []astscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common/ast`/.Exprscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common/ast`/Expr#) (astscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common/ast`/.Exprscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common/ast`/Expr#, *commonscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common`/.Errorscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common`/Error#) { |
| 36 | return eh.Copyscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/parser`/ExprHelper#Copy.(args[0]), nil |
| 37 | }, |
| 38 | ), |
| 39 | ), |
| 40 | ) |
| 41 | if err != nil { |
| 42 | t.Fatalfscip-go gomod github.com/golang/go/src go1.23.0 testing/common#Fatalf().("NewParser() failed: %v", err) |
| 43 | } |
| 44 | parsed, errs := p.Parsescip-go gomod github.com/google/cel-go . `github.com/google/cel-go/parser`/Parser#Parse().(src) |
| 45 | if errs != nil && len(errs.GetErrorsscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common`/Errors#GetErrors().()) != 0 { |
| 46 | t.Fatalfscip-go gomod github.com/golang/go/src go1.23.0 testing/common#Fatalf().("p.Parse(%v) failed: %v", src, errs.ToDisplayStringscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common`/Errors#ToDisplayString().()) |
| 47 | } |
| 48 | // id generation is consistent between runs, and in this case '27' refers to the |
| 49 | // macro expression that's the sole argument to the noop() macro. |
| 50 | macroTarget, found := parsed.SourceInfoscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common/ast`/AST#SourceInfo().().GetMacroCallscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common/ast`/SourceInfo#GetMacroCall().(27) |
| 51 | if !found { |
| 52 | t.Fatalscip-go gomod github.com/golang/go/src go1.23.0 testing/common#Fatal().("GetMacroCall(27) failed to find call") |
| 53 | } |
| 54 | macroExpr, err := astscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common/ast`/.ExprToProtoscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common/ast`/ExprToProto().(macroTarget) |
| 55 | if err != nil { |
| 56 | t.Fatalfscip-go gomod github.com/golang/go/src go1.23.0 testing/common#Fatalf().("ast.ExprToProto() failed: %v", err) |
| 57 | } |
| 58 | protoExpr, err := astscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common/ast`/.ExprToProtoscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common/ast`/ExprToProto().(parsed.Exprscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common/ast`/AST#Expr().()) |
| 59 | if err != nil { |
| 60 | t.Fatalfscip-go gomod github.com/golang/go/src go1.23.0 testing/common#Fatalf().("ast.ExprToProto() failed: %v", err) |
| 61 | } |
| 62 | if protoscip-go gomod google.golang.org/protobuf v1.36.10 `google.golang.org/protobuf/proto`/.Equalscip-go gomod google.golang.org/protobuf v1.36.10 `google.golang.org/protobuf/proto`/Equal().(protoExpr, macroExpr) { |
| 63 | t.Errorfscip-go gomod github.com/golang/go/src go1.23.0 testing/common#Errorf().("Copy() failed to provide a unique ids: %v", macroExpr) |
| 64 | } |
| 65 | } |