-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathxmlpatch_test.go
More file actions
121 lines (119 loc) · 4.73 KB
/
xmlpatch_test.go
File metadata and controls
121 lines (119 loc) · 4.73 KB
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
package xmlpatch
import (
"github.com/stretchr/testify/require"
"os"
"testing"
)
func TestPatch(t *testing.T) {
tests := []struct {
name string
docDataFilepath string
xmlDiffDataFilepath string
options []Ops
expectedFilepath string
wantErr bool
}{
{
name: "replace existing attribute 'url' value of element 'project->component' identified by attribute 'name'",
docDataFilepath: "testdata/replace/attribute/1/workspace.before.xml",
xmlDiffDataFilepath: "testdata/replace/attribute/1/diff.xml",
expectedFilepath: "testdata/replace/attribute/1/workspace.after.xml",
wantErr: false,
},
{
name: "set missing attribute 'url' value of element 'project->component' identified by attribute 'name'",
docDataFilepath: "testdata/replace/attribute/2/workspace.before.xml",
xmlDiffDataFilepath: "testdata/replace/attribute/2/diff.xml",
expectedFilepath: "testdata/replace/attribute/2/workspace.after.xml",
wantErr: false,
},
{
name: "set attribute 'url' value of missing element 'project->component' identified by attribute 'name' with auto create on",
docDataFilepath: "testdata/replace/attribute/3/workspace.before.xml",
xmlDiffDataFilepath: "testdata/replace/attribute/3/diff.xml",
expectedFilepath: "testdata/replace/attribute/3/workspace.after.xml",
options: []Ops{ReplaceAutoCreateMissing},
wantErr: false,
},
{
name: "set attribute 'url' value of missing element 'project->component' identified by attribute 'name' with auto create off",
docDataFilepath: "testdata/replace/attribute/3/workspace.before.xml",
xmlDiffDataFilepath: "testdata/replace/attribute/3/diff.xml",
expectedFilepath: "testdata/replace/attribute/3/workspace.after.xml",
wantErr: true,
},
{
name: "set attribute 'url' value of missing element 'project->component->inner' identified by attribute 'name' with auto create on",
docDataFilepath: "testdata/replace/attribute/4/workspace.before.xml",
xmlDiffDataFilepath: "testdata/replace/attribute/4/diff.xml",
expectedFilepath: "testdata/replace/attribute/4/workspace.after.xml",
options: []Ops{ReplaceAutoCreateMissing},
wantErr: false,
},
{
name: "set attribute 'url' value of 'project->component' identified by attribute 'name' with auto create on and doc data is empty",
docDataFilepath: "testdata/replace/attribute/5/workspace.before.xml",
xmlDiffDataFilepath: "testdata/replace/attribute/5/diff.xml",
expectedFilepath: "testdata/replace/attribute/5/workspace.after.xml",
options: []Ops{ReplaceAutoCreateMissing},
wantErr: false,
},
{
name: "malformed target file",
docDataFilepath: "xmlpatch_test.go",
xmlDiffDataFilepath: "testdata/replace/attribute/3/diff.xml",
expectedFilepath: "testdata/replace/attribute/3/workspace.after.xml",
wantErr: true,
},
{
name: "malformed diff file",
docDataFilepath: "testdata/replace/attribute/3/workspace.before.xml",
xmlDiffDataFilepath: "xmlpatch_test.go",
expectedFilepath: "testdata/replace/attribute/3/workspace.after.xml",
wantErr: true,
},
{
name: "replace element",
docDataFilepath: "testdata/replace/element/1/domain.before.xml",
xmlDiffDataFilepath: "testdata/replace/element/1/diff.xml",
expectedFilepath: "testdata/replace/element/1/domain.after.xml",
wantErr: false,
},
{
name: "add element",
docDataFilepath: "testdata/add/element/1/domain.before.xml",
xmlDiffDataFilepath: "testdata/add/element/1/diff.xml",
expectedFilepath: "testdata/add/element/1/domain.after.xml",
wantErr: false,
},
{
name: "add duplicated element",
docDataFilepath: "testdata/add/element/2/domain.before.xml",
xmlDiffDataFilepath: "testdata/add/element/2/diff.xml",
expectedFilepath: "testdata/add/element/2/domain.after.xml",
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
// given
req := require.New(t)
xmlDiffData, err := os.ReadFile(tt.xmlDiffDataFilepath)
req.NoError(err)
docData, err := os.ReadFile(tt.docDataFilepath)
req.NoError(err)
expected, err := os.ReadFile(tt.expectedFilepath)
req.NoError(err)
// when
actual, err := Patch(docData, xmlDiffData, tt.options...)
// then
if tt.wantErr {
req.Error(err)
req.Nil(actual)
} else {
req.NoError(err)
req.Equal(string(expected), string(actual))
}
})
}
}