-
Notifications
You must be signed in to change notification settings - Fork 31
/
attribute_remove_test.go
125 lines (120 loc) · 1.71 KB
/
attribute_remove_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
package editor
import (
"bytes"
"testing"
)
func TestAttributeRemove(t *testing.T) {
cases := []struct {
name string
src string
address string
ok bool
want string
}{
{
name: "simple top level attribute",
src: `
a0 = v0
a1 = v1
`,
address: "a0",
ok: true,
want: `
a1 = v1
`,
},
{
name: "simple top level attribute (with comments)",
src: `
// before attr
a0 = "v0" // inline
a1 = "v1"
`,
address: "a0",
ok: true,
want: `
a1 = "v1"
`, // Unfortunately we can't keep the before attr comment.
},
{
name: "attribute in block",
src: `
a0 = v0
b1 "l1" {
a1 = v1
a2 = v2
}
`,
address: "b1.l1.a1",
ok: true,
want: `
a0 = v0
b1 "l1" {
a2 = v2
}
`,
},
{
name: "top level attribute not found",
src: `
a0 = v0
`,
address: "a1",
ok: true,
want: `
a0 = v0
`,
},
{
name: "attribute not found in block",
src: `
a0 = v0
b1 "l1" {
a1 = v1
}
`,
address: "b1.l1.a2",
ok: true,
want: `
a0 = v0
b1 "l1" {
a1 = v1
}
`,
},
{
name: "block not found",
src: `
a0 = v0
b1 "l1" {
a1 = v1
}
`,
address: "b2.l1.a1",
ok: true,
want: `
a0 = v0
b1 "l1" {
a1 = v1
}
`,
},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
inStream := bytes.NewBufferString(tc.src)
outStream := new(bytes.Buffer)
err := RemoveAttribute(inStream, outStream, "test", tc.address)
if tc.ok && err != nil {
t.Fatalf("unexpected err = %s", err)
}
got := outStream.String()
if !tc.ok && err == nil {
t.Fatalf("expected to return an error, but no error, outStream: \n%s", got)
}
if got != tc.want {
t.Fatalf("got:\n%s\nwant:\n%s", got, tc.want)
}
})
}
}