forked from hashicorp/hcl
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
hclwrite: Allow selecting blocks for updating
- Loading branch information
1 parent
ed70541
commit 9d1235a
Showing
6 changed files
with
402 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
package hclwrite | ||
|
||
import ( | ||
"fmt" | ||
"reflect" | ||
"strings" | ||
"testing" | ||
|
||
"github.com/hashicorp/hcl/v2" | ||
) | ||
|
||
func TestBlockType(t *testing.T) { | ||
tests := []struct { | ||
src string | ||
want string | ||
}{ | ||
{ | ||
` | ||
service { | ||
attr0 = "val0" | ||
} | ||
`, | ||
"service", | ||
}, | ||
} | ||
|
||
for _, test := range tests { | ||
t.Run(fmt.Sprintf("%s", test.want), func(t *testing.T) { | ||
f, diags := ParseConfig([]byte(test.src), "", hcl.Pos{Line: 1, Column: 1}) | ||
if len(diags) != 0 { | ||
for _, diag := range diags { | ||
t.Logf("- %s", diag.Error()) | ||
} | ||
t.Fatalf("unexpected diagnostics") | ||
} | ||
|
||
block := f.Body().Blocks()[0] | ||
got := string(block.Type()) | ||
if got != test.want { | ||
t.Errorf("wrong result\ngot: %s\nwant: %s", got, test.want) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func TestBlockLabels(t *testing.T) { | ||
tests := []struct { | ||
src string | ||
want []string | ||
}{ | ||
{ | ||
` | ||
nolabel { | ||
} | ||
`, | ||
[]string{}, | ||
}, | ||
{ | ||
` | ||
quoted "label1" { | ||
} | ||
`, | ||
[]string{"label1"}, | ||
}, | ||
{ | ||
` | ||
quoted "label1" "label2" { | ||
} | ||
`, | ||
[]string{"label1", "label2"}, | ||
}, | ||
{ | ||
` | ||
unquoted label1 { | ||
} | ||
`, | ||
[]string{"label1"}, | ||
}, | ||
{ | ||
` | ||
escape "\u0041" { | ||
} | ||
`, | ||
[]string{"\u0041"}, | ||
}, | ||
} | ||
|
||
for _, test := range tests { | ||
t.Run(fmt.Sprintf("%s", strings.Join(test.want, " ")), func(t *testing.T) { | ||
f, diags := ParseConfig([]byte(test.src), "", hcl.Pos{Line: 1, Column: 1}) | ||
if len(diags) != 0 { | ||
for _, diag := range diags { | ||
t.Logf("- %s", diag.Error()) | ||
} | ||
t.Fatalf("unexpected diagnostics") | ||
} | ||
|
||
block := f.Body().Blocks()[0] | ||
got := block.Labels() | ||
if !reflect.DeepEqual(got, test.want) { | ||
t.Errorf("wrong result\ngot: %#v\nwant: %#v", got, test.want) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.