|
5 | 5 | "testing" |
6 | 6 |
|
7 | 7 | "github.com/stretchr/testify/assert" |
| 8 | + "github.com/stretchr/testify/require" |
8 | 9 | ) |
9 | 10 |
|
10 | 11 | func TestExpand(t *testing.T) { |
@@ -58,6 +59,18 @@ func TestExpand(t *testing.T) { |
58 | 59 | envVars: map[string]string{}, |
59 | 60 | expected: "UNKNOWN", |
60 | 61 | }, |
| 62 | + { |
| 63 | + name: "backticks in template (markdown code fence)", |
| 64 | + commands: "Here is code:\n```\n${env.CODE}\n```\nEnd.", |
| 65 | + envVars: map[string]string{"CODE": "fmt.Println()"}, |
| 66 | + expected: "Here is code:\n```\nfmt.Println()\n```\nEnd.", |
| 67 | + }, |
| 68 | + { |
| 69 | + name: "multiple backticks", |
| 70 | + commands: "Use `inline` and ```block``` code", |
| 71 | + envVars: map[string]string{}, |
| 72 | + expected: "Use `inline` and ```block``` code", |
| 73 | + }, |
61 | 74 | } |
62 | 75 |
|
63 | 76 | for _, tt := range tests { |
@@ -128,6 +141,63 @@ func TestExpandMap_Empty(t *testing.T) { |
128 | 141 | assert.Empty(t, result) |
129 | 142 | } |
130 | 143 |
|
| 144 | +func TestExpandString(t *testing.T) { |
| 145 | + t.Parallel() |
| 146 | + |
| 147 | + tests := []struct { |
| 148 | + name string |
| 149 | + template string |
| 150 | + values map[string]string |
| 151 | + expected string |
| 152 | + wantErr bool |
| 153 | + }{ |
| 154 | + { |
| 155 | + name: "simple substitution", |
| 156 | + template: "Hello ${name}!", |
| 157 | + values: map[string]string{"name": "World"}, |
| 158 | + expected: "Hello World!", |
| 159 | + }, |
| 160 | + { |
| 161 | + name: "multiple values", |
| 162 | + template: "File: ${path} (chunk ${index})", |
| 163 | + values: map[string]string{"path": "/foo/bar.go", "index": "0"}, |
| 164 | + expected: "File: /foo/bar.go (chunk 0)", |
| 165 | + }, |
| 166 | + { |
| 167 | + name: "backticks in template are preserved", |
| 168 | + template: "Code:\n```\n${content}\n```", |
| 169 | + values: map[string]string{"content": "func main() {}"}, |
| 170 | + expected: "Code:\n```\nfunc main() {}\n```", |
| 171 | + }, |
| 172 | + { |
| 173 | + name: "backticks in value are preserved", |
| 174 | + template: "The code is: ${code}", |
| 175 | + values: map[string]string{"code": "use `fmt.Println()`"}, |
| 176 | + expected: "The code is: use `fmt.Println()`", |
| 177 | + }, |
| 178 | + { |
| 179 | + name: "semantic prompt with code fence", |
| 180 | + template: "Summarize:\n```\n${content}\n```\nBe concise.", |
| 181 | + values: map[string]string{"content": "package main\n\nfunc main() {\n\tfmt.Println(`hello`)\n}"}, |
| 182 | + expected: "Summarize:\n```\npackage main\n\nfunc main() {\n\tfmt.Println(`hello`)\n}\n```\nBe concise.", |
| 183 | + }, |
| 184 | + } |
| 185 | + |
| 186 | + for _, tt := range tests { |
| 187 | + t.Run(tt.name, func(t *testing.T) { |
| 188 | + t.Parallel() |
| 189 | + |
| 190 | + result, err := ExpandString(t.Context(), tt.template, tt.values) |
| 191 | + if tt.wantErr { |
| 192 | + require.Error(t, err) |
| 193 | + return |
| 194 | + } |
| 195 | + require.NoError(t, err) |
| 196 | + assert.Equal(t, tt.expected, result) |
| 197 | + }) |
| 198 | + } |
| 199 | +} |
| 200 | + |
131 | 201 | type testEnvProvider map[string]string |
132 | 202 |
|
133 | 203 | func (p *testEnvProvider) Get(_ context.Context, name string) string { |
|
0 commit comments