Completed
Push — master ( 015fff...2fb069 )
by Valentin
02:25
created

etool.TestParse   B

Complexity

Conditions 6

Size

Total Lines 24
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
eloc 17
nop 1
dl 0
loc 24
rs 8.6166
c 0
b 0
f 0
1
package etool
2
3
import (
4
	"github.com/vvval/go-metadata-scanner/util"
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 TestReadArgs(t *testing.T) {
12
	type check struct {
13
		names  []string
14
		fields []string
15
		exp    []string
16
	}
17
18
	set := []check{
19
		{[]string{"a", "b"}, []string{"f1", "f2"}, []string{"-j", "-G", "-b", "-f1:all", "-f2:all", "a", "b"}},
20
		{[]string{}, []string{"f1", "f2"}, []string{"-j", "-G", "-b", "-f1:all", "-f2:all"}},
21
		{[]string{"a", "b"}, []string{}, []string{"-j", "-G", "-b", "a", "b"}},
22
	}
23
24
	for i, v := range set {
25
		p := packReadArgs(v.names, v.fields)
26
		if !reflect.DeepEqual(p, v.exp) {
27
			t.Errorf("args mismatch (line `%d`):\ninput `%s` and `%s`:\ngot `%s`\nexpected `%s`", i, v.names, v.fields, p, v.exp)
28
		}
29
	}
30
}
31
32
func TestWriteArgs(t *testing.T) {
33
	type check struct {
34
		name      string
35
		tags      metadata.Tags
36
		useSep    bool
37
		originals bool
38
		exp       []string
39
	}
40
41
	set := []check{
42
		{"name", metadata.Tags{"n1": "v1", "n2": "v2,v3"}, true, true,
43
			[]string{"-n1=v1", "-n2=v2,v3", "-sep", metadata.Separator(), "name"}},
44
		{"", metadata.Tags{"n1": "v1", "n2": "v2,v3"}, true, true,
45
			[]string{"-n1=v1", "-n2=v2,v3", "-sep", metadata.Separator(), ""}},
46
		{"", metadata.Tags{}, true, true,
47
			[]string{"-sep", metadata.Separator(), ""}},
48
		{"name", metadata.Tags{"n1": "v1", "n2": "v2,v3"}, false, true,
49
			[]string{"-n1=v1", "-n2=v2,v3", "name"}},
50
		{"name", metadata.Tags{"n1": "v1", "n2": "v2,v3"}, false, false,
51
			[]string{"-n1=v1", "-n2=v2,v3", overwriteFlag, "name"}},
52
	}
53
	for i, v := range set {
54
		p := packWriteArgs(v.name, v.tags, v.useSep, v.originals)
55
		if !util.Equal(p, v.exp) {
56
			t.Errorf("args mismatch (line `%d`) check\ngot `%+v`\nexp `%+v`", i, p, v.exp)
57
		}
58
	}
59
}
60
61
func TestParse(t *testing.T) {
62
	type check struct {
63
		data string
64
		exp  []vars.File
65
	}
66
67
	set := []check{
68
		{`[{"SourceFile": "folder/test1.jpg","File:FileName": "test1.jpg"},{"SourceFile": "folder/test2.jpg","File:FileName": "test2.jpg"}]`, []vars.File{
69
			vars.NewFile("folder/test1.jpg", metadata.Tags{"File:FileName": "test1.jpg"}),
70
			vars.NewFile("folder/test2.jpg", metadata.Tags{"File:FileName": "test2.jpg"}),
71
		}},
72
		{`[]`, []vars.File{}},
73
		{`[{"SourceFile": "folder/test1.jpg","File:FileName": "test1.jpg"},{"SourceFile2": "folder/test2.jpg","File:FileName": "test2.jpg"}]`, []vars.File{
74
			vars.NewFile("folder/test1.jpg", metadata.Tags{"File:FileName": "test1.jpg"}),
75
		}},
76
		{`[{"SourceFile": "folder/test1.jpg","File:FileName": "test1.jpg"},{"File:FileName": "test2.jpg"}]`, []vars.File{
77
			vars.NewFile("folder/test1.jpg", metadata.Tags{"File:FileName": "test1.jpg"}),
78
		}},
79
	}
80
81
	for i, s := range set {
82
		p := Parse([]byte(s.data))
83
		if !reflect.DeepEqual(p, s.exp) && len(p) > 0 && len(s.exp) > 0 {
84
			t.Errorf("parse failed (line `%d`) check\ngot `%+v`\nexp `%+v`", i, p, s.exp)
85
		}
86
	}
87
}
88