Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update legacy tests to use Constraint #251

Merged
merged 2 commits into from
Apr 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 36 additions & 29 deletions decoder/attribute_candidates_legacy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func TestLegacyDecoder_CompletionAtPos_EmptyCompletionData(t *testing.T) {
"primitive type",
"primitive",
&schema.AttributeSchema{
Expr: schema.LiteralTypeOnly(cty.String),
Constraint: schema.LiteralType{Type: cty.String},
},
lang.CompleteCandidates([]lang.Candidate{
{
Expand All @@ -50,7 +50,7 @@ func TestLegacyDecoder_CompletionAtPos_EmptyCompletionData(t *testing.T) {
"map of strings",
"mymap",
&schema.AttributeSchema{
Expr: schema.LiteralTypeOnly(cty.Map(cty.String)),
Constraint: schema.LiteralType{Type: cty.Map(cty.String)},
},
lang.CompleteCandidates([]lang.Candidate{
{
Expand All @@ -64,7 +64,7 @@ func TestLegacyDecoder_CompletionAtPos_EmptyCompletionData(t *testing.T) {
},
NewText: "mymap",
Snippet: `mymap = {
"${1:key}" = "${2:value}"
"${1:name}" = "${2:value}"
}`,
},
Kind: lang.AttributeCandidateKind,
Expand All @@ -75,7 +75,7 @@ func TestLegacyDecoder_CompletionAtPos_EmptyCompletionData(t *testing.T) {
"map of numbers",
"mymap",
&schema.AttributeSchema{
Expr: schema.LiteralTypeOnly(cty.Map(cty.Number)),
Constraint: schema.LiteralType{Type: cty.Map(cty.Number)},
},
lang.CompleteCandidates([]lang.Candidate{
{
Expand All @@ -89,7 +89,7 @@ func TestLegacyDecoder_CompletionAtPos_EmptyCompletionData(t *testing.T) {
},
NewText: "mymap",
Snippet: `mymap = {
"${1:key}" = ${2:1}
"${1:name}" = ${2:0}
}`,
},
Kind: lang.AttributeCandidateKind,
Expand All @@ -100,7 +100,7 @@ func TestLegacyDecoder_CompletionAtPos_EmptyCompletionData(t *testing.T) {
"list of numbers",
"mylist",
&schema.AttributeSchema{
Expr: schema.LiteralTypeOnly(cty.List(cty.Number)),
Constraint: schema.LiteralType{Type: cty.List(cty.Number)},
},
lang.CompleteCandidates([]lang.Candidate{
{
Expand All @@ -113,7 +113,7 @@ func TestLegacyDecoder_CompletionAtPos_EmptyCompletionData(t *testing.T) {
End: hcl.InitialPos,
},
NewText: "mylist",
Snippet: `mylist = [ ${1:1} ]`,
Snippet: `mylist = [ ${1:0} ]`,
},
Kind: lang.AttributeCandidateKind,
},
Expand All @@ -123,10 +123,12 @@ func TestLegacyDecoder_CompletionAtPos_EmptyCompletionData(t *testing.T) {
"list of objects",
"mylistobj",
&schema.AttributeSchema{
Expr: schema.LiteralTypeOnly(cty.List(cty.Object(map[string]cty.Type{
"first": cty.String,
"second": cty.Number,
}))),
Constraint: schema.LiteralType{
Type: cty.List(cty.Object(map[string]cty.Type{
"first": cty.String,
"second": cty.Number,
})),
},
},
lang.CompleteCandidates([]lang.Candidate{
{
Expand All @@ -141,7 +143,7 @@ func TestLegacyDecoder_CompletionAtPos_EmptyCompletionData(t *testing.T) {
NewText: "mylistobj",
Snippet: `mylistobj = [ {
first = "${1:value}"
second = ${2:1}
second = ${2:0}
} ]`,
},
Kind: lang.AttributeCandidateKind,
Expand All @@ -152,7 +154,7 @@ func TestLegacyDecoder_CompletionAtPos_EmptyCompletionData(t *testing.T) {
"set of numbers",
"myset",
&schema.AttributeSchema{
Expr: schema.LiteralTypeOnly(cty.Set(cty.Number)),
Constraint: schema.LiteralType{Type: cty.Set(cty.Number)},
},
lang.CompleteCandidates([]lang.Candidate{
{
Expand All @@ -165,7 +167,7 @@ func TestLegacyDecoder_CompletionAtPos_EmptyCompletionData(t *testing.T) {
End: hcl.InitialPos,
},
NewText: "myset",
Snippet: `myset = [ ${1:1} ]`,
Snippet: `myset = [ ${1:0} ]`,
},
Kind: lang.AttributeCandidateKind,
},
Expand All @@ -175,11 +177,13 @@ func TestLegacyDecoder_CompletionAtPos_EmptyCompletionData(t *testing.T) {
"object",
"myobj",
&schema.AttributeSchema{
Expr: schema.LiteralTypeOnly(cty.Object(map[string]cty.Type{
"keystr": cty.String,
"keynum": cty.Number,
"keybool": cty.Bool,
})),
Constraint: schema.LiteralType{
Type: cty.Object(map[string]cty.Type{
"keystr": cty.String,
"keynum": cty.Number,
"keybool": cty.Bool,
}),
},
},
lang.CompleteCandidates([]lang.Candidate{
{
Expand All @@ -194,7 +198,7 @@ func TestLegacyDecoder_CompletionAtPos_EmptyCompletionData(t *testing.T) {
NewText: "myobj",
Snippet: `myobj = {
keybool = ${1:false}
keynum = ${2:1}
keynum = ${2:0}
keystr = "${3:value}"
}`,
},
Expand All @@ -206,7 +210,7 @@ func TestLegacyDecoder_CompletionAtPos_EmptyCompletionData(t *testing.T) {
"unknown type",
"mynil",
&schema.AttributeSchema{
Expr: schema.LiteralTypeOnly(cty.DynamicPseudoType),
Constraint: schema.LiteralType{Type: cty.DynamicPseudoType},
},
lang.CompleteCandidates([]lang.Candidate{
{
Expand All @@ -219,7 +223,7 @@ func TestLegacyDecoder_CompletionAtPos_EmptyCompletionData(t *testing.T) {
End: hcl.InitialPos,
},
NewText: "mynil",
Snippet: `mynil = ${1}`,
Snippet: `mynil = `,
},
Kind: lang.AttributeCandidateKind,
},
Expand All @@ -229,13 +233,15 @@ func TestLegacyDecoder_CompletionAtPos_EmptyCompletionData(t *testing.T) {
"nested object",
"myobj",
&schema.AttributeSchema{
Expr: schema.LiteralTypeOnly(cty.Object(map[string]cty.Type{
"keystr": cty.String,
"another": cty.Object(map[string]cty.Type{
"nestedstr": cty.String,
"nested_number": cty.Number,
Constraint: schema.LiteralType{
Type: cty.Object(map[string]cty.Type{
"keystr": cty.String,
"another": cty.Object(map[string]cty.Type{
"nestedstr": cty.String,
"nested_number": cty.Number,
}),
}),
})),
},
},
lang.CompleteCandidates([]lang.Candidate{
{
Expand All @@ -250,7 +256,7 @@ func TestLegacyDecoder_CompletionAtPos_EmptyCompletionData(t *testing.T) {
NewText: "myobj",
Snippet: `myobj = {
another = {
nested_number = ${1:1}
nested_number = ${1:0}
nestedstr = "${2:value}"
}
keystr = "${3:value}"
Expand All @@ -275,6 +281,7 @@ func TestLegacyDecoder_CompletionAtPos_EmptyCompletionData(t *testing.T) {
"test.tf": f,
},
})
d.PrefillRequiredFields = true

ctx := context.Background()
candidates, err := d.CandidatesAtPos(ctx, "test.tf", hcl.InitialPos)
Expand Down
37 changes: 20 additions & 17 deletions decoder/body_candidates_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@ func TestDecoder_CandidateAtPos_incompleteAttributes(t *testing.T) {
},
Body: &schema.BodySchema{
Attributes: map[string]*schema.AttributeSchema{
"attr1": {Expr: schema.LiteralTypeOnly(cty.Number)},
"attr2": {Expr: schema.LiteralTypeOnly(cty.Number)},
"some_other_attr": {Expr: schema.LiteralTypeOnly(cty.Number)},
"another_attr": {Expr: schema.LiteralTypeOnly(cty.Number)},
"attr1": {Constraint: schema.LiteralType{Type: cty.Number}},
"attr2": {Constraint: schema.LiteralType{Type: cty.Number}},
"some_other_attr": {Constraint: schema.LiteralType{Type: cty.Number}},
"another_attr": {Constraint: schema.LiteralType{Type: cty.Number}},
},
},
},
Expand Down Expand Up @@ -76,7 +76,7 @@ func TestDecoder_CandidateAtPos_incompleteAttributes(t *testing.T) {
},
},
NewText: "attr1",
Snippet: "attr1 = ${1:1}",
Snippet: "attr1 = ${1:0}",
},
Kind: lang.AttributeCandidateKind,
},
Expand All @@ -98,10 +98,10 @@ func TestDecoder_CandidateAtPos_computedAttributes(t *testing.T) {
},
Body: &schema.BodySchema{
Attributes: map[string]*schema.AttributeSchema{
"attr1": {Expr: schema.LiteralTypeOnly(cty.Number), IsComputed: true},
"attr2": {Expr: schema.LiteralTypeOnly(cty.Number), IsComputed: true, IsOptional: true},
"some_other_attr": {Expr: schema.LiteralTypeOnly(cty.Number)},
"another_attr": {Expr: schema.LiteralTypeOnly(cty.Number)},
"attr1": {Constraint: schema.LiteralType{Type: cty.Number}, IsComputed: true},
"attr2": {Constraint: schema.LiteralType{Type: cty.Number}, IsComputed: true, IsOptional: true},
"some_other_attr": {Constraint: schema.LiteralType{Type: cty.Number}},
"another_attr": {Constraint: schema.LiteralType{Type: cty.Number}},
},
},
},
Expand Down Expand Up @@ -147,7 +147,7 @@ func TestDecoder_CandidateAtPos_computedAttributes(t *testing.T) {
},
},
NewText: "attr2",
Snippet: "attr2 = ${1:1}",
Snippet: "attr2 = ${1:0}",
},
Kind: lang.AttributeCandidateKind,
},
Expand Down Expand Up @@ -239,18 +239,20 @@ func TestDecoder_CandidateAtPos_duplicateNames(t *testing.T) {
Attributes: map[string]*schema.AttributeSchema{
"ingress": {
IsOptional: true,
Expr: schema.LiteralTypeOnly(cty.Object(map[string]cty.Type{
"attr1": cty.String,
"attr2": cty.Number,
})),
Constraint: schema.LiteralType{
Type: cty.Object(map[string]cty.Type{
"attr1": cty.String,
"attr2": cty.Number,
}),
},
},
},
Blocks: map[string]*schema.BlockSchema{
"ingress": {
Body: &schema.BodySchema{
Attributes: map[string]*schema.AttributeSchema{
"attr1": {Expr: schema.LiteralTypeOnly(cty.String)},
"attr2": {Expr: schema.LiteralTypeOnly(cty.Number)},
"attr1": {Constraint: schema.LiteralType{Type: cty.String}, IsRequired: true},
"attr2": {Constraint: schema.LiteralType{Type: cty.Number}, IsRequired: true},
},
},
},
Expand All @@ -265,6 +267,7 @@ func TestDecoder_CandidateAtPos_duplicateNames(t *testing.T) {
"test.tf": f,
},
})
d.PrefillRequiredFields = true

candidates, err := d.CandidatesAtPos(ctx, "test.tf", hcl.InitialPos)
if err != nil {
Expand All @@ -284,7 +287,7 @@ func TestDecoder_CandidateAtPos_duplicateNames(t *testing.T) {
NewText: "ingress",
Snippet: `ingress = {
attr1 = "${1:value}"
attr2 = ${2:1}
attr2 = ${2:0}
}`,
},
Kind: lang.AttributeCandidateKind,
Expand Down
Loading