Skip to content

Commit

Permalink
decoder: Test hover for Any
Browse files Browse the repository at this point in the history
  • Loading branch information
dbanck committed Mar 17, 2023
1 parent cba2da3 commit f482476
Showing 1 changed file with 91 additions and 0 deletions.
91 changes: 91 additions & 0 deletions decoder/expr_any_hover_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
package decoder

import (
"context"
"fmt"
"testing"

"github.com/google/go-cmp/cmp"
"github.com/hashicorp/hcl-lang/lang"
"github.com/hashicorp/hcl-lang/schema"
"github.com/hashicorp/hcl/v2"
"github.com/hashicorp/hcl/v2/hclsyntax"
"github.com/zclconf/go-cty/cty"
)

func TestHoverAtPos_exprAny_functions(t *testing.T) {
testCases := []struct {
testName string
attrSchema map[string]*schema.AttributeSchema
cfg string
pos hcl.Pos
expectedData *lang.HoverData
}{
{
"over unknown function",
map[string]*schema.AttributeSchema{
"attr": {
Constraint: schema.AnyExpression{
OfType: cty.String,
},
},
},
`attr = unknown()
`,
hcl.Pos{Line: 1, Column: 10, Byte: 9},
nil,
},
{
"over name",
map[string]*schema.AttributeSchema{
"attr": {
Constraint: schema.AnyExpression{
OfType: cty.String,
},
},
},
`attr = lower("FOO")
`,
hcl.Pos{Line: 1, Column: 10, Byte: 9},
&lang.HoverData{
Content: lang.MarkupContent{
Value: "`lower(str string) string`\n\n`lower` converts all cased letters in the given string to lowercase.",
Kind: lang.MarkdownKind,
},
Range: hcl.Range{
Filename: "test.tf",
Start: hcl.Pos{Line: 1, Column: 8, Byte: 7},
End: hcl.Pos{Line: 1, Column: 20, Byte: 19},
},
},
},
}

for i, tc := range testCases {
t.Run(fmt.Sprintf("%d-%s", i, tc.testName), func(t *testing.T) {
bodySchema := &schema.BodySchema{
Attributes: tc.attrSchema,
}

f, _ := hclsyntax.ParseConfig([]byte(tc.cfg), "test.tf", hcl.InitialPos)
d := testPathDecoder(t, &PathContext{
Schema: bodySchema,
Files: map[string]*hcl.File{
"test.tf": f,
},
Functions: testFunctionSignatures(),
})

ctx := context.Background()
data, err := d.HoverAtPos(ctx, "test.tf", tc.pos)
if err != nil {
t.Fatal(err)
}

if diff := cmp.Diff(tc.expectedData, data); diff != "" {
t.Fatalf("unexpected data: %s", diff)
}
})
}

}

0 comments on commit f482476

Please sign in to comment.