Skip to content

Commit

Permalink
decoder: add tests for ConditionalExpr hover
Browse files Browse the repository at this point in the history
  • Loading branch information
radeksimko committed Nov 22, 2023
1 parent 41081e0 commit 5f9fc4f
Show file tree
Hide file tree
Showing 3 changed files with 220 additions and 13 deletions.
214 changes: 208 additions & 6 deletions decoder/expr_any_hover_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -464,13 +464,13 @@ TEXT
map[string]*schema.AttributeSchema{
"attr": {
Constraint: schema.AnyExpression{
OfType: cty.List(cty.String),
OfType: cty.List(cty.Number),
},
},
},
`attr = [
4223,
"foobar",
true,
12345678,
]`,
hcl.Pos{Line: 2, Column: 6, Byte: 14},
nil,
Expand Down Expand Up @@ -615,13 +615,13 @@ TEXT
map[string]*schema.AttributeSchema{
"attr": {
Constraint: schema.AnyExpression{
OfType: cty.Set(cty.String),
OfType: cty.Set(cty.Number),
},
},
},
`attr = [
42223,
"foobar",
false,
12345678,
]`,
hcl.Pos{Line: 2, Column: 6, Byte: 14},
nil,
Expand Down Expand Up @@ -1786,3 +1786,205 @@ EOT
})
}
}

func TestHoverAtPos_exprAny_conditional(t *testing.T) {
testCases := []struct {
testName string
attrSchema map[string]*schema.AttributeSchema
refOrigins reference.Origins
refTargets reference.Targets
cfg string
pos hcl.Pos
expectedHoverData *lang.HoverData
}{
{
"condition",
map[string]*schema.AttributeSchema{
"attr": {
Constraint: schema.AnyExpression{
OfType: cty.String,
},
},
},
reference.Origins{},
reference.Targets{},
`attr = true ? "bar" : "baz"
`,
hcl.Pos{Line: 1, Column: 10, Byte: 9},
&lang.HoverData{
Content: lang.Markdown("_bool_"),
Range: hcl.Range{
Filename: "test.tf",
Start: hcl.Pos{Line: 1, Column: 8, Byte: 7},
End: hcl.Pos{Line: 1, Column: 12, Byte: 11},
},
},
},
{
"true",
map[string]*schema.AttributeSchema{
"attr": {
Constraint: schema.AnyExpression{
OfType: cty.String,
},
},
},
reference.Origins{},
reference.Targets{},
`attr = true ? 4223 : "baz"
`,
hcl.Pos{Line: 1, Column: 17, Byte: 16},
&lang.HoverData{
Content: lang.Markdown("_number_"),
Range: hcl.Range{
Filename: "test.tf",
Start: hcl.Pos{Line: 1, Column: 15, Byte: 14},
End: hcl.Pos{Line: 1, Column: 19, Byte: 18},
},
},
},
{
"false",
map[string]*schema.AttributeSchema{
"attr": {
Constraint: schema.AnyExpression{
OfType: cty.String,
},
},
},
reference.Origins{},
reference.Targets{},
`attr = true ? 4223 : 5713
`,
hcl.Pos{Line: 1, Column: 24, Byte: 23},
&lang.HoverData{
Content: lang.Markdown("_number_"),
Range: hcl.Range{
Filename: "test.tf",
Start: hcl.Pos{Line: 1, Column: 22, Byte: 21},
End: hcl.Pos{Line: 1, Column: 26, Byte: 25},
},
},
},
{
"condition in template",
map[string]*schema.AttributeSchema{
"attr": {
Constraint: schema.AnyExpression{
OfType: cty.String,
},
},
},
reference.Origins{},
reference.Targets{},
`attr = "x${true ? 4223 : 5713}"
`,
hcl.Pos{Line: 1, Column: 14, Byte: 13},
&lang.HoverData{
Content: lang.Markdown("_bool_"),
Range: hcl.Range{
Filename: "test.tf",
Start: hcl.Pos{Line: 1, Column: 12, Byte: 11},
End: hcl.Pos{Line: 1, Column: 16, Byte: 15},
},
},
},
{
"condition as directive in template",
map[string]*schema.AttributeSchema{
"attr": {
Constraint: schema.AnyExpression{
OfType: cty.String,
},
},
},
reference.Origins{},
reference.Targets{},
`attr = "x%{if true}4223%{else}5713%{endif}"
`,
hcl.Pos{Line: 1, Column: 17, Byte: 16},
&lang.HoverData{
Content: lang.Markdown("_bool_"),
Range: hcl.Range{
Filename: "test.tf",
Start: hcl.Pos{Line: 1, Column: 15, Byte: 14},
End: hcl.Pos{Line: 1, Column: 19, Byte: 18},
},
},
},
{
"condition as directive in template inside true",
map[string]*schema.AttributeSchema{
"attr": {
Constraint: schema.AnyExpression{
OfType: cty.String,
},
},
},
reference.Origins{},
reference.Targets{},
`attr = "x%{if true}4223%{else}5713%{endif}"
`,
hcl.Pos{Line: 1, Column: 22, Byte: 21},
&lang.HoverData{
Content: lang.Markdown("_string_"),
Range: hcl.Range{
Filename: "test.tf",
Start: hcl.Pos{Line: 1, Column: 20, Byte: 19},
End: hcl.Pos{Line: 1, Column: 24, Byte: 23},
},
},
},
{
"condition as directive in template inside false",
map[string]*schema.AttributeSchema{
"attr": {
Constraint: schema.AnyExpression{
OfType: cty.String,
},
},
},
reference.Origins{},
reference.Targets{},
`attr = "x%{if true}4223%{else}5713%{endif}"
`,
hcl.Pos{Line: 1, Column: 33, Byte: 32},
&lang.HoverData{
Content: lang.Markdown("_string_"),
Range: hcl.Range{
Filename: "test.tf",
Start: hcl.Pos{Line: 1, Column: 31, Byte: 30},
End: hcl.Pos{Line: 1, Column: 35, Byte: 34},
},
},
},
}

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,
},
ReferenceOrigins: tc.refOrigins,
ReferenceTargets: tc.refTargets,
})

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

if diff := cmp.Diff(tc.expectedHoverData, hoverData); diff != "" {
t.Fatalf("unexpected hover data: %s", diff)
}
})
}
}
7 changes: 6 additions & 1 deletion decoder/expr_literal_type_hover.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/hashicorp/hcl/v2"
"github.com/hashicorp/hcl/v2/hclsyntax"
"github.com/zclconf/go-cty/cty"
"github.com/zclconf/go-cty/cty/convert"
)

func (lt LiteralType) HoverAtPos(ctx context.Context, pos hcl.Pos) *lang.HoverData {
Expand Down Expand Up @@ -44,7 +45,11 @@ func (lt LiteralType) HoverAtPos(ctx context.Context, pos hcl.Pos) *lang.HoverDa
return nil
}

if !lt.cons.Type.Equals(expr.Val.Type()) {
// While interpolation is not allowed/expected in LiteralType
// we still assume that expressions are convertible.
// This makes it easier to deal with a case where we land here
// from inside of AnyExpression which may be e.g. a TemplateExpr.
if _, err := convert.Convert(expr.Val, typ); err != nil {
return nil
}

Expand Down
12 changes: 6 additions & 6 deletions decoder/expr_literal_type_hover_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -303,13 +303,13 @@ TEXT
map[string]*schema.AttributeSchema{
"attr": {
Constraint: schema.LiteralType{
Type: cty.List(cty.String),
Type: cty.List(cty.Number),
},
},
},
`attr = [
4223,
"foobar",
true,
12345678,
]`,
hcl.Pos{Line: 2, Column: 6, Byte: 14},
nil,
Expand Down Expand Up @@ -454,13 +454,13 @@ TEXT
map[string]*schema.AttributeSchema{
"attr": {
Constraint: schema.LiteralType{
Type: cty.Set(cty.String),
Type: cty.Set(cty.Number),
},
},
},
`attr = [
42223,
"foobar",
false,
12345678,
]`,
hcl.Pos{Line: 2, Column: 6, Byte: 14},
nil,
Expand Down

0 comments on commit 5f9fc4f

Please sign in to comment.