Completed
Push — master ( e66762...2df545 )
by Valentin
02:05
created

config.TestDictIsList   A

Complexity

Conditions 4

Size

Total Lines 19
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

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