Passed
Push — master ( 08f95b...a410ee )
by Valentin
01:53
created

config/configs_test.go   B

Size/Duplication

Total Lines 218
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
cc 48
eloc 131
dl 0
loc 218
rs 8.5599
c 0
b 0
f 0

9 Methods

Rating   Name   Duplication   Size   Complexity  
A config.TestDictFind 0 20 4
C config.mapEqual 0 17 10
B config.TestDictConfig 0 27 6
A config.isFound 0 8 4
A config.TestDictTagEquals 0 20 4
B config.TestDictIsBoolean 0 25 6
A config.TestDictIsList 0 20 4
A config.TestMSCSV 0 15 4
B config.TestAppConfig 0 27 6
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"}}, []string{"bool1", "bool2"}, []string{"list1", "list2"}},
49
			DictConfig{map[string][]string{}, []string{}, []string{}},
50
			DictConfig{map[string][]string{"a": {"a1", "a2"}}, []string{"bool1", "bool2"}, []string{"list1", "list2"}},
51
		},
52
		{
53
			DictConfig{map[string][]string{}, []string{}, []string{}},
54
			DictConfig{map[string][]string{"a": {"a1", "a2"}}, []string{"bool1", "bool2"}, []string{"list1", "list2"}},
55
			DictConfig{map[string][]string{"a": {"a1", "a2"}}, []string{"bool1", "bool2"}, []string{"list1", "list2"}},
56
		},
57
		{
58
			DictConfig{map[string][]string{"a": {"a1", "a2"}}, []string{"bool1", "bool2"}, []string{"list1", "list2"}},
59
			DictConfig{map[string][]string{"b": {"b1", "b2"}}, []string{"bool3", "bool2"}, []string{"list3", "list2"}},
60
			DictConfig{map[string][]string{"a": {"a2", "a1"}, "b": {"b1", "b2"}}, []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
		{"keywords", true},
82
		{"IPTC:keywords", true},
83
		{"iptc:KEYWORDS", true},
84
	}
85
86
	dict := configuration.Load(Dict, "./../dict.yaml").(DictConfig)
87
88
	for i, s := range set {
89
		_, f := dict.Find(s.name)
90
		if f != s.found {
91
			t.Errorf("find failed (line `%d`):\ngot `%t`\nexpected `%t`", i, f, s.found)
92
		}
93
	}
94
}
95
96
func TestDictIsBoolean(t *testing.T) {
97
	type check struct {
98
		name string
99
		is   bool
100
	}
101
102
	set := []check{
103
		{"", false},
104
		{"test", false},
105
		{"copyrighted", true},
106
		{"XMP:marked", true},
107
	}
108
109
	dict := configuration.Load(Dict, "./../dict.yaml").(DictConfig)
110
111
	for i, s := range set {
112
		tag, ok := dict.Find(s.name)
113
		if !ok && s.is {
114
			t.Errorf("find in booleans failed (line `%d`) for `%+v` (unknown tag)", i, s)
115
		}
116
117
		found := isFound(tag, dict)
118
119
		if found != s.is {
120
			t.Errorf("find in booleans failed (line `%d`) for `%+v`:\ngot `%t`\nexpected `%t`", i, s, found, s.is)
121
		}
122
	}
123
}
124
125
func isFound(tag vars.Tag, dict DictConfig) bool {
126
	for _, mapTag := range tag.Map() {
127
		if dict.IsBoolean(tag.Key(), mapTag) {
128
			return true
129
		}
130
	}
131
132
	return false
133
}
134
135
func TestDictIsList(t *testing.T) {
136
	type check struct {
137
		name, tag string
138
		is        bool
139
	}
140
141
	set := []check{
142
		{"", "", false},
143
		{"test", "", false},
144
		{"keywords", "test", true},
145
		{"", "IPTC:Writer-Editor", true},
146
		{"", "captionWriter", false},
147
	}
148
149
	dict := configuration.Load(Dict, "./../dict.yaml").(DictConfig)
150
151
	for i, s := range set {
152
		f := dict.IsList(s.name, s.tag)
153
		if f != s.is {
154
			t.Errorf("find in lists failed (line `%d`) for `%+v`:\ngot `%t`\nexpected `%t`", i, s, f, s.is)
155
		}
156
	}
157
}
158
159
func TestDictTagEquals(t *testing.T) {
160
	type check struct {
161
		t1, t2 string
162
		exp    bool
163
	}
164
165
	set := []check{
166
		{"", "", true},
167
		{"test", "", false},
168
		{"", "test", false},
169
		{"test", "test", true},
170
		{"IPTC:Contact", "IPTC:Contact", true},
171
		{"contact", "IPTC:Contact", true},
172
		{"IPTC:Contact", "contact", false},
173
	}
174
175
	for i, s := range set {
176
		f := tagEquals(s.t1, s.t2)
177
		if f != s.exp {
178
			t.Errorf("tags equality failed (line `%d`):\ngot `%t`\nexpected `%t`", i, f, s.exp)
179
		}
180
	}
181
}
182
183
func TestMSCSV(t *testing.T) {
184
	type check struct {
185
		a, b, exp MSCSVConfig
186
	}
187
188
	set := []check{
189
		{MSCSVConfig{"provider"}, MSCSVConfig{""}, MSCSVConfig{"provider"}},
190
		{MSCSVConfig{""}, MSCSVConfig{"provider"}, MSCSVConfig{"provider"}},
191
		{MSCSVConfig{"provider"}, MSCSVConfig{"provider2"}, MSCSVConfig{"provider"}},
192
	}
193
194
	for i, s := range set {
195
		m := s.a.MergeDefault(s.b).(MSCSVConfig)
196
		if m.provider != s.exp.provider {
197
			t.Errorf("merge failed (line `%d`):\ngot `%v`\nexpected `%v`", i, m, s.exp)
198
		}
199
	}
200
}
201
202
func mapEqual(a, b map[string][]string) bool {
203
	if a == nil && b != nil || a != nil && b == nil {
204
		return false
205
	}
206
207
	if len(a) != len(b) {
208
		return false
209
	}
210
211
	for k, v := range a {
212
		vv, ok := b[k]
213
		if !ok || !util.Equal(v, vv) {
214
			return false
215
		}
216
	}
217
218
	return true
219
}
220