MCPcopy
hub / github.com/hashicorp/hcl / TestBodySetAttributeValueInBlock

Function TestBodySetAttributeValueInBlock

hclwrite/ast_body_test.go:936–982  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

934}
935
936func TestBodySetAttributeValueInBlock(t *testing.T) {
937 src := `service "label1" {
938 attr1 = "val1"
939}
940`
941 tests := []struct {
942 src string
943 typeName string
944 labels []string
945 attr string
946 val cty.Value
947 want string
948 }{
949 {
950 src,
951 "service",
952 []string{"label1"},
953 "attr1",
954 cty.StringVal("updated1"),
955 `service "label1" {
956 attr1 = "updated1"
957}
958`,
959 },
960 }
961
962 for _, test := range tests {
963 t.Run(fmt.Sprintf("%s = %#v in %s %s", test.attr, test.val, test.typeName, strings.Join(test.labels, " ")), func(t *testing.T) {
964 f, diags := ParseConfig([]byte(test.src), "", hcl.Pos{Line: 1, Column: 1})
965 if len(diags) != 0 {
966 for _, diag := range diags {
967 t.Logf("- %s", diag.Error())
968 }
969 t.Fatalf("unexpected diagnostics")
970 }
971
972 b := f.Body().FirstMatchingBlock(test.typeName, test.labels)
973 b.Body().SetAttributeValue(test.attr, test.val)
974 tokens := f.BuildTokens(nil)
975 format(tokens)
976 got := string(tokens.Bytes())
977 if got != test.want {
978 t.Errorf("wrong result\ngot: %s\nwant: %s\n", got, test.want)
979 }
980 })
981 }
982}
983
984func TestBodySetAttributeValueInNestedBlock(t *testing.T) {
985 src := `parent {

Callers

nothing calls this directly

Calls 10

formatFunction · 0.85
RunMethod · 0.80
JoinMethod · 0.80
FirstMatchingBlockMethod · 0.80
SetAttributeValueMethod · 0.80
ParseConfigFunction · 0.70
BuildTokensMethod · 0.65
ErrorMethod · 0.45
BodyMethod · 0.45
BytesMethod · 0.45

Tested by

no test coverage detected