config.TestDictFindGroup   A
last analyzed

Complexity

Conditions 4

Size

Total Lines 18
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 13
nop 1
dl 0
loc 18
rs 9.75
c 0
b 0
f 0
1
package config
2
3
import (
4
	"github.com/vvval/go-metadata-scanner/configuration"
5
	"github.com/vvval/go-metadata-scanner/util"
6
	"github.com/vvval/go-metadata-scanner/vars"
7
	"testing"
8
)
9
10
func TestAppConfig(t *testing.T) {
11
	type check struct {
12
		a, b, exp AppConfig
13
	}
14
15
	set := []check{
16
		{
17
			AppConfig{"path", []string{"ext1", "ext2"}, []string{"f1", "f2"}},
18
			AppConfig{"", []string{}, []string{}},
19
			AppConfig{"path", []string{"ext1", "ext2"}, []string{"f1", "f2"}},
20
		},
21
		{
22
			AppConfig{"", []string{}, []string{}},
23
			AppConfig{"path", []string{"ext1", "ext2"}, []string{"f1", "f2"}},
24
			AppConfig{"path", []string{"ext1", "ext2"}, []string{"f1", "f2"}},
25
		},
26
		{
27
			AppConfig{"path", []string{"ext1", "ext2"}, []string{"f1", "f2"}},
28
			AppConfig{"path2", []string{"ext3", "ext2"}, []string{"f3", "f2"}},
29
			AppConfig{"path", []string{"ext1", "ext2", "ext3"}, []string{"f1", "f2", "f3"}},
30
		},
31
	}
32
33
	for i, s := range set {
34
		m := s.a.MergeDefault(s.b).(AppConfig)
35
		if m.toolPath != s.exp.toolPath || !util.Equal(m.fields, s.exp.fields) || !util.Equal(m.extensions, s.exp.extensions) {
36
			t.Errorf("merge failed (line `%d`):\ngot `%v`\nexpected `%v`", i, m, s.exp)
37
		}
38
	}
39
}
40
41
func TestDictConfig(t *testing.T) {
42
	type check struct {
43
		a, b, exp DictConfig
44
	}
45
46
	set := []check{
47
		{
48
			DictConfig{map[string][]string{"a": {"a1", "a2"}}, map[string]string{}, []string{"bool1", "bool2"}, []string{"list1", "list2"}},
49
			DictConfig{map[string][]string{}, map[string]string{}, []string{}, []string{}},
50
			DictConfig{map[string][]string{"a": {"a1", "a2"}}, map[string]string{}, []string{"bool1", "bool2"}, []string{"list1", "list2"}},
51
		},
52
		{
53
			DictConfig{map[string][]string{}, map[string]string{}, []string{}, []string{}},
54
			DictConfig{map[string][]string{"a": {"a1", "a2"}}, map[string]string{}, []string{"bool1", "bool2"}, []string{"list1", "list2"}},
55
			DictConfig{map[string][]string{"a": {"a1", "a2"}}, map[string]string{}, []string{"bool1", "bool2"}, []string{"list1", "list2"}},
56
		},
57
		{
58
			DictConfig{map[string][]string{"a": {"a1", "a2"}}, map[string]string{}, []string{"bool1", "bool2"}, []string{"list1", "list2"}},
59
			DictConfig{map[string][]string{"b": {"b1", "b2"}}, map[string]string{}, []string{"bool3", "bool2"}, []string{"list3", "list2"}},
60
			DictConfig{map[string][]string{"a": {"a2", "a1"}, "b": {"b1", "b2"}}, map[string]string{}, []string{"bool1", "bool2", "bool3"}, []string{"list1", "list2", "list3"}},
61
		},
62
	}
63
64
	for i, s := range set {
65
		m := s.a.MergeDefault(s.b).(DictConfig)
66
		if !mapEqual(m.known, s.exp.known) || !util.Equal(m.booleans, s.exp.booleans) || !util.Equal(m.lists, s.exp.lists) {
67
			t.Errorf("merge failed (line `%d`):\ngot `%v`\nexpected `%v`", i, m, s.exp)
68
		}
69
	}
70
}
71
72
func TestDictFind(t *testing.T) {
73
	type check struct {
74
		name  string
75
		found bool
76
	}
77
78
	set := []check{
79
		{"", false},
80
		{"test", false},
81
		{"title", true},
82
		{"IPTC:Headline", true},
83
		{"iptc:Headline", true},
84
		{"keywords", true},
85
		{"IPTC:keywords", true},
86
		{"iptc:KEYWORDS", true},
87
	}
88
89
	dict := configuration.Load(Dict, "./../dict.yaml").(DictConfig)
90
91
	for i, s := range set {
92
		_, f := dict.Find(s.name)
93
		if f != s.found {
94
			t.Errorf("find failed (line `%d`):\ngot `%t`\nexpected `%t`", i, f, s.found)
95
		}
96
	}
97
}
98
99
func TestDictFindGroup(t *testing.T) {
100
	type check struct {
101
		name  string
102
		found bool
103
	}
104
105
	set := []check{
106
		{"keywords", true},
107
		{"kkeywords", false},
108
		{"keywordss", true},
109
	}
110
111
	dict := configuration.Load(Dict, "./../dict.yaml").(DictConfig)
112
113
	for i, s := range set {
114
		_, f := dict.Find(s.name)
115
		if f != s.found {
116
			t.Errorf("find groups failed (line `%d`):\ngot `%t`\nexpected `%t`", i, f, s.found)
117
		}
118
	}
119
}
120
121
func TestDictIsBoolean(t *testing.T) {
122
	type check struct {
123
		name string
124
		is   bool
125
	}
126
127
	set := []check{
128
		{"", false},
129
		{"test", false},
130
		{"copyrighted", true},
131
		{"XMP:marked", true},
132
	}
133
134
	dict := configuration.Load(Dict, "./../dict.yaml").(DictConfig)
135
136
	for i, s := range set {
137
		tag, ok := dict.Find(s.name)
138
		if !ok && s.is {
139
			t.Errorf("find in booleans failed (line `%d`) for `%+v` (unknown tag)", i, s)
140
		}
141
142
		found := isFound(tag, dict)
143
144
		if found != s.is {
145
			t.Errorf("find in booleans failed (line `%d`) for `%+v`:\ngot `%t`\nexpected `%t`", i, s, found, s.is)
146
		}
147
	}
148
}
149
150
func isFound(tag vars.Tag, dict DictConfig) bool {
151
	for _, mapTag := range tag.Map() {
152
		if dict.IsBoolean(tag.Key(), mapTag) {
153
			return true
154
		}
155
	}
156
157
	return false
158
}
159
160
func TestDictIsList(t *testing.T) {
161
	type check struct {
162
		name, tag string
163
		is        bool
164
	}
165
166
	set := []check{
167
		{"", "", false},
168
		{"test", "", false},
169
		{"keywords", "test", true},
170
		{"", "IPTC:Writer-Editor", true},
171
		{"", "captionWriter", false},
172
	}
173
174
	dict := configuration.Load(Dict, "./../dict.yaml").(DictConfig)
175
176
	for i, s := range set {
177
		f := dict.IsList(s.name, s.tag)
178
		if f != s.is {
179
			t.Errorf("find in lists failed (line `%d`) for `%+v`:\ngot `%t`\nexpected `%t`", i, s, f, s.is)
180
		}
181
	}
182
}
183
184
func TestDictTagEquals(t *testing.T) {
185
	type check struct {
186
		t1, t2 string
187
		exp    bool
188
	}
189
190
	set := []check{
191
		{"", "", true},
192
		{"test", "", false},
193
		{"", "test", false},
194
		{"test", "test", true},
195
		{"IPTC:Contact", "IPTC:Contact", true},
196
		{"contact", "IPTC:Contact", true},
197
		{"IPTC:Contact", "contact", false},
198
	}
199
200
	for i, s := range set {
201
		f := tagEquals(s.t1, s.t2)
202
		if f != s.exp {
203
			t.Errorf("tags equality failed (line `%d`):\ngot `%t`\nexpected `%t`", i, f, s.exp)
204
		}
205
	}
206
}
207
208
func TestMSCSV(t *testing.T) {
209
	type check struct {
210
		a, b, exp MSCSVConfig
211
	}
212
213
	set := []check{
214
		{MSCSVConfig{"provider"}, MSCSVConfig{""}, MSCSVConfig{"provider"}},
215
		{MSCSVConfig{""}, MSCSVConfig{"provider"}, MSCSVConfig{"provider"}},
216
		{MSCSVConfig{"provider"}, MSCSVConfig{"provider2"}, MSCSVConfig{"provider"}},
217
	}
218
219
	for i, s := range set {
220
		m := s.a.MergeDefault(s.b).(MSCSVConfig)
221
		if m.provider != s.exp.provider {
222
			t.Errorf("merge failed (line `%d`):\ngot `%v`\nexpected `%v`", i, m, s.exp)
223
		}
224
	}
225
}
226
227
func mapEqual(a, b map[string][]string) bool {
228
	if a == nil && b != nil || a != nil && b == nil {
229
		return false
230
	}
231
232
	if len(a) != len(b) {
233
		return false
234
	}
235
236
	for k, v := range a {
237
		vv, ok := b[k]
238
		if !ok || !util.Equal(v, vv) {
239
			return false
240
		}
241
	}
242
243
	return true
244
}
245