forked from sourcegraph/srcscan
/
unit_test.go
73 lines (70 loc) · 1.51 KB
/
unit_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
package srcscan
import (
"encoding/json"
"github.com/kr/pretty"
"reflect"
"strings"
"testing"
)
func TestUnmarshalJSON(t *testing.T) {
type unmarshalJSONTest struct {
dir string
}
tests := []unmarshalJSONTest{
{"testdata"},
}
for _, test := range tests {
units, err := Default.Scan(test.dir)
if err != nil {
t.Errorf("scan error: %s", err)
continue
}
for _, unit := range units {
data, err := json.Marshal(unit)
if err != nil {
t.Errorf("marshal error: %s", err)
continue
}
unit2, err := UnmarshalJSON(data, UnitType(unit))
if err != nil {
t.Errorf("UnmarshalJSON error: %s", err)
continue
}
if !reflect.DeepEqual(unit, unit2) {
t.Errorf("unit != unit2:\n%+v\n%+v\n%v", unit, unit2, strings.Join(pretty.Diff(unit, unit2), "\n"))
}
}
}
}
func TestMarshalableUnit(t *testing.T) {
type unmarshalJSONTest struct {
dir string
}
tests := []unmarshalJSONTest{
{"testdata"},
}
for _, test := range tests {
units, err := Default.Scan(test.dir)
if err != nil {
t.Errorf("scan error: %s", err)
continue
}
for _, unit := range units {
mu := &MarshalableUnit{unit}
data, err := json.Marshal(mu)
if err != nil {
t.Errorf("marshal error: %s", err)
continue
}
var mu2 *MarshalableUnit
err = json.Unmarshal(data, &mu2)
if err != nil {
t.Errorf("Unmarshal error: %s", err)
continue
}
if !reflect.DeepEqual(mu, mu2) {
t.Errorf("mu != mu2:\n%+v\n%+v\n%v", mu, mu2, strings.Join(pretty.Diff(mu, mu2), "\n"))
}
}
}
}