This repository was archived by the owner on May 13, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathmap_test.go
156 lines (129 loc) · 4.95 KB
/
map_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
package tableprinter
import (
"reflect"
"testing"
)
type person struct {
FirstName string
LastName string
}
func (p person) String() string {
return p.FirstName + " " + p.LastName
}
func TestMapParse(t *testing.T) {
t.Parallel()
tt := map[string][]person{
"Sellers": {{"Georgios", "Callas"},
{"Ioannis", "Christou"}},
"Consumers": {
{"Dimitrios", "Dellis"},
{"Nikolaos", "Doukas"}},
}
var (
expectedHeaders = []string{"Sellers", "Consumers"}
// the order may differs, remember map doesn't keep its order, so make a check if row contains these values.
expectedRowPart1 = []string{"Georgios Callas", "Dimitrios Dellis"}
expectedRowPart2 = []string{"Ioannis Christou", "Nikolaos Doukas"}
expectedRows = [][]string{expectedRowPart1, expectedRowPart2}
)
v := reflect.ValueOf(tt)
headers, rows, _ := MapParser.Parse(v, nil)
// check the length.
if expected, got := len(expectedHeaders), len(headers); expected != got {
t.Fatalf("expected length of headers: %d but got: %d", expected, got)
}
// we should not care about the order of headers on maps, we just check if all expected headers are there.
if !((headers[0] == expectedHeaders[0] || headers[0] == expectedHeaders[1]) && (headers[1] == expectedHeaders[0] || headers[1] == expectedHeaders[1])) {
t.Fatalf("expected headers: %v but got: %v", expectedHeaders, headers)
}
// check the length.
if expected, got := len(expectedRows), len(rows); expected != got {
t.Fatalf("expected length of rows: %d but got: %d", expected, got)
}
// we care if the correct header contains the correct columns.
if !((rows[0][0] == expectedRows[0][0] || rows[0][1] == expectedRows[0][0]) && (rows[1][0] == expectedRows[1][0] || rows[1][1] == expectedRows[1][0])) {
t.Fatalf("expected rows: %v but got: %v", expectedRows, rows)
}
}
func TestMapParseSingle(t *testing.T) {
t.Parallel()
tt := map[string]person{
"Seller": {"Georgios", "Callas"},
"Consumer": {"Dimitrios", "Dellis"},
}
var (
expectedHeaders = []string{"Seller", "Consumer"}
expectedRows = [][]string{{"Georgios Callas", "Dimitrios Dellis"}}
)
v := reflect.ValueOf(tt)
headers, rows, _ := MapParser.Parse(v, nil)
// check the length.
if expected, got := len(expectedHeaders), len(headers); expected != got {
t.Fatalf("expected length of headers: %d but got: %d", expected, got)
}
// we should not care about the order of headers on maps, we just check if all expected headers are there.
if !((headers[0] == expectedHeaders[0] || headers[0] == expectedHeaders[1]) && (headers[1] == expectedHeaders[0] || headers[1] == expectedHeaders[1])) {
t.Fatalf("expected headers: %v but got: %v", expectedHeaders, headers)
}
// check the length.
if expected, got := len(expectedRows), len(rows); expected != got {
t.Fatalf("expected length of rows: %d but got: %d", expected, got)
}
// if expected, got := len(expectedRows[0][0]), len(rows[0][0]); expected != got {
// t.Logf("got[0][0]: %s", rows[0][0])
// t.Logf("expected[0][0]: %s", expectedRows[0][0])
// t.Logf("expected rows: %v but got: %v", expectedRows, rows)
// t.Fatalf("expected length of rows got[0][0]: %d but got: %d", expected, got)
// }
// we care if the correct header contains the correct columns.
if !(rows[0][0] == expectedRows[0][0] || rows[0][1] == expectedRows[0][0]) {
t.Fatalf("expected rows: %v but got: %v", expectedRows, rows)
}
}
func TestMapEmpties(t *testing.T) {
persons := map[string][]person{
"Access 1": {{"Georgios", "Callas"},
{"Ioannis", "Christou"}},
"Access 2": {
{"Dimitrios", "Dellis"}},
"Access 3": {{"Giannhs", "Papadopoulos"},
{"Giwrgos", "Papadopoulos"},
{"Oresths", "Papadopoulos"}},
}
_, rows, _ := MapParser.Parse(reflect.ValueOf(persons), nil)
if expected, got := len(persons), len(rows); expected != got {
t.Fatalf("expected %d rows but got %d", expected, got)
}
/* Remember: This can be different from runtime to runtime, maps are not always have the same key order(= our headers),
so this test can fail sometimes because it checks the exact pos of those empties (prev tests are written to adjust that behavior, they should always SUCC)
ACCESS 1 ACCESS 2 ACCESS 3
-------------------- ------------------ ----------------------
[0] Georgios Callas[0] Dimitrios Dellis[0:1] Giannhs Papadopoulos[0:2]
[1] Ioannis Christou[0:1] EMPTY [1:1] Giwrgos Papadopoulos[1:2]
[2] EMPTY [2:0] EMPTY [2:1] Oresths Papadopoulos[2]
*/
var (
space = " "
empties = map[int][]int{
2: {0, 1},
1: {1},
}
someNotEmpties = map[int][]int{
2: {2},
}
)
for idx, list := range empties {
for _, e := range list {
if got := rows[idx][e]; got != space {
t.Logf("expected %d:%d to have space but got: %s", idx, e, got)
}
}
}
for idx, list := range someNotEmpties {
for _, e := range list {
if got := rows[idx][e]; got == space {
t.Logf("expected %d:%d to have filled value but got space", idx, e)
}
}
}
}