|
1 | 1 | package json |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "fmt" |
4 | 5 | "strings" |
5 | 6 | "testing" |
6 | 7 |
|
@@ -182,3 +183,137 @@ func TestParseWithStartPos(t *testing.T) { |
182 | 183 | t.Errorf("The two ranges did not match: src=%s, part=%s", srcRange, partRange) |
183 | 184 | } |
184 | 185 | } |
| 186 | + |
| 187 | +func TestParseExpression(t *testing.T) { |
| 188 | + tests := []struct { |
| 189 | + Input string |
| 190 | + Want string |
| 191 | + }{ |
| 192 | + { |
| 193 | + `"hello"`, |
| 194 | + `cty.StringVal("hello")`, |
| 195 | + }, |
| 196 | + { |
| 197 | + `"hello ${noun}"`, |
| 198 | + `cty.StringVal("hello world")`, |
| 199 | + }, |
| 200 | + { |
| 201 | + "true", |
| 202 | + "cty.True", |
| 203 | + }, |
| 204 | + { |
| 205 | + "false", |
| 206 | + "cty.False", |
| 207 | + }, |
| 208 | + { |
| 209 | + "1", |
| 210 | + "cty.NumberIntVal(1)", |
| 211 | + }, |
| 212 | + { |
| 213 | + "{}", |
| 214 | + "cty.EmptyObjectVal", |
| 215 | + }, |
| 216 | + { |
| 217 | + `{"foo":"bar","baz":1}`, |
| 218 | + `cty.ObjectVal(map[string]cty.Value{"baz":cty.NumberIntVal(1), "foo":cty.StringVal("bar")})`, |
| 219 | + }, |
| 220 | + { |
| 221 | + "[]", |
| 222 | + "cty.EmptyTupleVal", |
| 223 | + }, |
| 224 | + { |
| 225 | + `["1",2,3]`, |
| 226 | + `cty.TupleVal([]cty.Value{cty.StringVal("1"), cty.NumberIntVal(2), cty.NumberIntVal(3)})`, |
| 227 | + }, |
| 228 | + } |
| 229 | + |
| 230 | + for _, test := range tests { |
| 231 | + t.Run(test.Input, func(t *testing.T) { |
| 232 | + expr, diags := ParseExpression([]byte(test.Input), "") |
| 233 | + if diags.HasErrors() { |
| 234 | + t.Errorf("got %d diagnostics; want 0", len(diags)) |
| 235 | + for _, d := range diags { |
| 236 | + t.Logf(" - %s", d.Error()) |
| 237 | + } |
| 238 | + } |
| 239 | + |
| 240 | + value, diags := expr.Value(&hcl.EvalContext{ |
| 241 | + Variables: map[string]cty.Value{ |
| 242 | + "noun": cty.StringVal("world"), |
| 243 | + }, |
| 244 | + }) |
| 245 | + if diags.HasErrors() { |
| 246 | + t.Errorf("got %d diagnostics on decode value; want 0", len(diags)) |
| 247 | + for _, d := range diags { |
| 248 | + t.Logf(" - %s", d.Error()) |
| 249 | + } |
| 250 | + } |
| 251 | + got := fmt.Sprintf("%#v", value) |
| 252 | + |
| 253 | + if got != test.Want { |
| 254 | + t.Errorf("got %s, but want %s", got, test.Want) |
| 255 | + } |
| 256 | + }) |
| 257 | + } |
| 258 | +} |
| 259 | + |
| 260 | +func TestParseExpression_malformed(t *testing.T) { |
| 261 | + src := `invalid` |
| 262 | + expr, diags := ParseExpression([]byte(src), "") |
| 263 | + if got, want := len(diags), 1; got != want { |
| 264 | + t.Errorf("got %d diagnostics; want %d", got, want) |
| 265 | + } |
| 266 | + if err, want := diags.Error(), `Invalid JSON keyword`; !strings.Contains(err, want) { |
| 267 | + t.Errorf("diags are %q, but should contain %q", err, want) |
| 268 | + } |
| 269 | + if expr == nil { |
| 270 | + t.Errorf("got nil Expression; want actual expression") |
| 271 | + } |
| 272 | +} |
| 273 | + |
| 274 | +func TestParseExpressionWithStartPos(t *testing.T) { |
| 275 | + src := `{ |
| 276 | + "foo": "bar" |
| 277 | +}` |
| 278 | + part := `"bar"` |
| 279 | + |
| 280 | + file, diags := Parse([]byte(src), "") |
| 281 | + partExpr, partDiags := ParseExpressionWithStartPos([]byte(part), "", hcl.Pos{Byte: 0, Line: 2, Column: 10}) |
| 282 | + if len(diags) != 0 { |
| 283 | + t.Errorf("got %d diagnostics on parse src; want 0", len(diags)) |
| 284 | + for _, diag := range diags { |
| 285 | + t.Logf("- %s", diag.Error()) |
| 286 | + } |
| 287 | + } |
| 288 | + if len(partDiags) != 0 { |
| 289 | + t.Errorf("got %d diagnostics on parse part src; want 0", len(partDiags)) |
| 290 | + for _, diag := range partDiags { |
| 291 | + t.Logf("- %s", diag.Error()) |
| 292 | + } |
| 293 | + } |
| 294 | + |
| 295 | + if file == nil { |
| 296 | + t.Errorf("got nil File; want actual file") |
| 297 | + } |
| 298 | + if file.Body == nil { |
| 299 | + t.Errorf("got nil Body: want actual body") |
| 300 | + } |
| 301 | + if partExpr == nil { |
| 302 | + t.Errorf("got nil Expression; want actual expression") |
| 303 | + } |
| 304 | + |
| 305 | + content, diags := file.Body.Content(&hcl.BodySchema{ |
| 306 | + Attributes: []hcl.AttributeSchema{{Name: "foo"}}, |
| 307 | + }) |
| 308 | + if len(diags) != 0 { |
| 309 | + t.Errorf("got %d diagnostics on decode; want 0", len(diags)) |
| 310 | + for _, diag := range diags { |
| 311 | + t.Logf("- %s", diag.Error()) |
| 312 | + } |
| 313 | + } |
| 314 | + expr := content.Attributes["foo"].Expr |
| 315 | + |
| 316 | + if expr.Range().String() != partExpr.Range().String() { |
| 317 | + t.Errorf("The two ranges did not match: src=%s, part=%s", expr.Range(), partExpr.Range()) |
| 318 | + } |
| 319 | +} |
0 commit comments