| 1 | // Copyright 2025 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 | // https://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 cel |
| 16 | |
| 17 | import ( |
| 18 | _ "embed" |
| 19 | "testing" |
| 20 | |
| 21 | "github.com/google/cel-go/common/env" |
| 22 | "github.com/google/cel-go/test" |
| 23 | ) |
| 24 | |
| 25 | //go:embed testdata/basic.prompt.txt |
| 26 | var wantBasicPrompt string |
| 27 | |
| 28 | //go:embed testdata/macros.prompt.txt |
| 29 | var wantMacrosPrompt string |
| 30 | |
| 31 | //go:embed testdata/standard_env.prompt.txt |
| 32 | var wantStandardEnvPrompt string |
| 33 | |
| 34 | func TestPromptTemplate(t *testing.T) { |
| 35 | tests := []struct { |
| 36 | name string |
| 37 | envOpts []EnvOption |
| 38 | out string |
| 39 | }{ |
| 40 | { |
| 41 | name: "basic", |
| 42 | out: wantBasicPrompt, |
| 43 | }, |
| 44 | { |
| 45 | name: "macros", |
| 46 | envOpts: []EnvOption{Macros(StandardMacros...)}, |
| 47 | out: wantMacrosPrompt, |
| 48 | }, |
| 49 | { |
| 50 | name: "standard_env", |
| 51 | envOpts: []EnvOption{StdLib(StdLibSubset(env.NewLibrarySubset().SetDisableMacros(true)))}, |
| 52 | out: wantStandardEnvPrompt, |
| 53 | }, |
| 54 | } |
| 55 | |
| 56 | for _, tst := range tests { |
| 57 | tc := tst |
| 58 | t.Run(tc.name, func(t *testing.T) { |
| 59 | env, err := NewCustomEnv(tc.envOpts...) |
| 60 | if err != nil { |
| 61 | t.Fatalf("cel.NewCustomEnv() failed: %v", err) |
| 62 | } |
| 63 | prompt, err := AuthoringPrompt(env) |
| 64 | if err != nil { |
| 65 | t.Fatalf("cel.AuthoringPrompt() failed: %v", err) |
| 66 | } |
| 67 | out := prompt.Render("<USER_PROMPT>") |
| 68 | if !test.Compare(out, tc.out) { |
| 69 | t.Errorf("got %s, wanted %s", out, tc.out) |
| 70 | } |
| 71 | }) |
| 72 | } |
| 73 | } |