cmd/scancmd/writers/csv_test.go   A
last analyzed

Size/Duplication

Total Lines 94
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
cc 26
eloc 58
dl 0
loc 94
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A writers.inMap 0 8 4
A writers.convert 0 9 3
B writers.TestPackLine 0 37 7
D writers.mapEqual 0 22 12
1
package writers
2
3
import (
4
	"encoding/json"
5
	"github.com/vvval/go-metadata-scanner/vars"
6
	"github.com/vvval/go-metadata-scanner/vars/metadata"
7
	"reflect"
8
	"testing"
9
)
10
11
func TestPackLine(t *testing.T) {
12
	type check struct {
13
		f      vars.File
14
		groups []string
15
		exp    []map[string]string
16
		ok     bool
17
	}
18
19
	set := []check{
20
		{
21
			vars.NewFile("file1.jpg", metadata.Tags{"test:tag": "some test tag", "XMP:tag1": "xmp tag1", "XMP:tag2": "xmp tag2", "iptc:tag3": "iptc tag3"}),
22
			[]string{"filename", "XMP", "iptc"},
23
			[]map[string]string{
24
				{"tag1": "xmp tag1", "tag2": "xmp tag2"},
25
				{"tag3": "iptc tag3"},
26
			},
27
			true,
28
		},
29
		{
30
			vars.NewFile("file2.png", metadata.Tags{"test:tag": "some test tag", "XMP:tag1": "xmp tag1", "XMP:tag2": "xmp tag2", "iptc:tag2": "iptc tag2"}),
31
			[]string{"filename", "XMP", "iptc"},
32
			[]map[string]string{
33
				{"tag1": "xmp tag1"},
34
				{"tag2": "iptc tag2"},
35
			},
36
			false,
37
		},
38
	}
39
40
	//Case sensitive comparison
41
	for i, s := range set {
42
		p := packCSVLine(&s.f, s.groups)
43
		pp := convert(p[1:])
44
		if !mapEqual(pp, s.exp) && s.ok {
45
			t.Errorf("grouping tags failed (line `%d`) (wrong inequality):\ngot `%v`\nexp `%v` (`%t` `%t`)", i, pp, s.exp, s.ok, mapEqual(pp[1:], s.exp))
46
		} else if reflect.DeepEqual(pp[1:], s.exp) && !s.ok {
47
			t.Errorf("grouping tags failed (line `%d`) (wrong equality):\ngot `%v`\nexp `%v` (`%t`)", i, pp, s.exp, s.ok)
48
		}
49
	}
50
}
51
52
func convert(i []string) []map[string]string {
53
	var o []map[string]string
54
	for _, v := range i {
55
		m := make(map[string]string)
56
		json.Unmarshal([]byte(v), &m)
57
		o = append(o, m)
58
	}
59
60
	return o
61
}
62
63
func mapEqual(a, b []map[string]string) bool {
64
	if a == nil && b != nil || a != nil && b == nil {
65
		return false
66
	}
67
68
	if len(a) != len(b) {
69
		return false
70
	}
71
72
	for _, va := range a {
73
		if !inMap(va, b) {
74
			return false
75
		}
76
	}
77
78
	for _, vb := range b {
79
		if !inMap(vb, a) {
80
			return false
81
		}
82
	}
83
84
	return true
85
}
86
87
func inMap(el map[string]string, m []map[string]string) bool {
88
	for _, v := range m {
89
		if reflect.DeepEqual(v, el) {
90
			return true
91
		}
92
	}
93
94
	return false
95
}
96