(t *testing.T)
| 934 | } |
| 935 | |
| 936 | func 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 | |
| 984 | func TestBodySetAttributeValueInNestedBlock(t *testing.T) { |
| 985 | src := `parent { |
nothing calls this directly
no test coverage detected