diff options
| author | wata_mac <watassbass@gmail.com> | 2020-05-24 21:56:16 +0900 |
|---|---|---|
| committer | Martin Atkins <mart@degeneration.co.uk> | 2020-09-04 14:12:01 -0700 |
| commit | 636e660fac2a9ee31de5346b96e314a9ef6500b0 (patch) | |
| tree | 039e423e6d5ac093af82e884213b91832c33a96d /json/public_test.go | |
| parent | 90676d47a0ce038f02596e2aa5894bf0c57fad56 (diff) | |
json: Add ParseExpression function
Diffstat (limited to 'json/public_test.go')
| -rw-r--r-- | json/public_test.go | 135 |
1 files changed, 135 insertions, 0 deletions
diff --git a/json/public_test.go b/json/public_test.go index 70944d9..554b165 100644 --- a/json/public_test.go +++ b/json/public_test.go @@ -1,6 +1,7 @@ package json import ( + "fmt" "strings" "testing" @@ -182,3 +183,137 @@ func TestParseWithStartPos(t *testing.T) { t.Errorf("The two ranges did not match: src=%s, part=%s", srcRange, partRange) } } + +func TestParseExpression(t *testing.T) { + tests := []struct { + Input string + Want string + }{ + { + `"hello"`, + `cty.StringVal("hello")`, + }, + { + `"hello ${noun}"`, + `cty.StringVal("hello world")`, + }, + { + "true", + "cty.True", + }, + { + "false", + "cty.False", + }, + { + "1", + "cty.NumberIntVal(1)", + }, + { + "{}", + "cty.EmptyObjectVal", + }, + { + `{"foo":"bar","baz":1}`, + `cty.ObjectVal(map[string]cty.Value{"baz":cty.NumberIntVal(1), "foo":cty.StringVal("bar")})`, + }, + { + "[]", + "cty.EmptyTupleVal", + }, + { + `["1",2,3]`, + `cty.TupleVal([]cty.Value{cty.StringVal("1"), cty.NumberIntVal(2), cty.NumberIntVal(3)})`, + }, + } + + for _, test := range tests { + t.Run(test.Input, func(t *testing.T) { + expr, diags := ParseExpression([]byte(test.Input), "") + if diags.HasErrors() { + t.Errorf("got %d diagnostics; want 0", len(diags)) + for _, d := range diags { + t.Logf(" - %s", d.Error()) + } + } + + value, diags := expr.Value(&hcl.EvalContext{ + Variables: map[string]cty.Value{ + "noun": cty.StringVal("world"), + }, + }) + if diags.HasErrors() { + t.Errorf("got %d diagnostics on decode value; want 0", len(diags)) + for _, d := range diags { + t.Logf(" - %s", d.Error()) + } + } + got := fmt.Sprintf("%#v", value) + + if got != test.Want { + t.Errorf("got %s, but want %s", got, test.Want) + } + }) + } +} + +func TestParseExpression_malformed(t *testing.T) { + src := `invalid` + expr, diags := ParseExpression([]byte(src), "") + if got, want := len(diags), 1; got != want { + t.Errorf("got %d diagnostics; want %d", got, want) + } + if err, want := diags.Error(), `Invalid JSON keyword`; !strings.Contains(err, want) { + t.Errorf("diags are %q, but should contain %q", err, want) + } + if expr == nil { + t.Errorf("got nil Expression; want actual expression") + } +} + +func TestParseExpressionWithStartPos(t *testing.T) { + src := `{ + "foo": "bar" +}` + part := `"bar"` + + file, diags := Parse([]byte(src), "") + partExpr, partDiags := ParseExpressionWithStartPos([]byte(part), "", hcl.Pos{Byte: 0, Line: 2, Column: 10}) + if len(diags) != 0 { + t.Errorf("got %d diagnostics on parse src; want 0", len(diags)) + for _, diag := range diags { + t.Logf("- %s", diag.Error()) + } + } + if len(partDiags) != 0 { + t.Errorf("got %d diagnostics on parse part src; want 0", len(partDiags)) + for _, diag := range partDiags { + t.Logf("- %s", diag.Error()) + } + } + + if file == nil { + t.Errorf("got nil File; want actual file") + } + if file.Body == nil { + t.Errorf("got nil Body: want actual body") + } + if partExpr == nil { + t.Errorf("got nil Expression; want actual expression") + } + + content, diags := file.Body.Content(&hcl.BodySchema{ + Attributes: []hcl.AttributeSchema{{Name: "foo"}}, + }) + if len(diags) != 0 { + t.Errorf("got %d diagnostics on decode; want 0", len(diags)) + for _, diag := range diags { + t.Logf("- %s", diag.Error()) + } + } + expr := content.Attributes["foo"].Expr + + if expr.Range().String() != partExpr.Range().String() { + t.Errorf("The two ranges did not match: src=%s, part=%s", expr.Range(), partExpr.Range()) + } +} |
