-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathtestutils_test.go
142 lines (114 loc) · 3.71 KB
/
testutils_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
// Copyright 2016 Russ Olsen. All Rights Reserved.
//
// This code is a Go port of the Java version created and maintained by Cognitect, therefore:
//
// Copyright 2014 Cognitect. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS-IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package transit
import (
"encoding/json"
"github.com/russolsen/same"
"io/ioutil"
"net/url"
"os"
"reflect"
"testing"
)
func assertEquals(t *testing.T, v1, v2 interface{}) {
if v1 != v2 {
t.Errorf("Expected %v[%v] == %v[%v]", v1, reflect.TypeOf(v1), v2, reflect.TypeOf(v2))
}
}
func assertTrue(t *testing.T, v interface{}) {
assertEquals(t, v, true)
}
func assertFalse(t *testing.T, v interface{}) {
assertEquals(t, v, false)
}
func ExemplarPath(fileName string) string {
return "transit-format/examples/0.8/simple/" + fileName
}
func toUrl(s string) *url.URL {
url, _ := url.Parse(s)
return url
}
func VerifyRoundTrip(t *testing.T, value interface{}) interface{} {
var newValue interface{}
for _, verbose := range []bool{true, false} {
json, err := EncodeToString(value, verbose)
if err != nil {
t.Errorf("Error encoding to Transit %v: %v", value, err)
}
newValue, err = DecodeFromString(json)
if err != nil {
t.Errorf("Error decoding %v: %v.\nJson:\n%v", value, err, json)
}
if !same.IsSame(value, newValue) {
t.Errorf("Round trip values do not match.\nValue:[%v]\n%v\nNew value:[%v]\n %v\nJson:\n%v",
value, reflect.TypeOf(value), newValue, reflect.TypeOf(newValue), json)
}
}
return newValue
}
func VerifyExemplar(t *testing.T, transitValue interface{}, exemplarPath string) {
f, err := os.Open(exemplarPath)
if err != nil {
t.Errorf("%v", err)
return
}
exemplarValue, err := NewDecoder(f).Decode()
if err != nil {
t.Errorf("Error decoding exemplar JSON [%v]: %v", exemplarPath, err)
return
}
// Use the more stringent DeepEqual here because both values were read with
// Transit and should therefore have the same type.
if !reflect.DeepEqual(exemplarValue, transitValue) {
t.Errorf("Value read from exemplar file [%v]:\n%v\nDoes not match round trip value:\n%v",
exemplarPath, exemplarValue, transitValue)
}
}
func VerifyJson(t *testing.T, transit string, path string) error {
var actual interface{}
var err error
if err = json.Unmarshal([]byte(transit), &actual); err != nil {
t.Errorf("Error reading generated transit %v: %v", transit, err)
return err
}
expectedJson, err := ioutil.ReadFile(path)
if err != nil {
t.Errorf("Error reading exemplare [%v]: %v", path, err)
return err
}
var expected interface{}
if err = json.Unmarshal([]byte(expectedJson), &expected); err != nil {
t.Errorf("Error decoding expected [%v]: %v", path, err)
return err
}
if !same.IsSame(actual, expected) {
t.Errorf("Actual does not match exemplar.\nExpected: %v\nActual: %v", expected, actual)
return NewTransitError("No match", actual)
}
return nil
}
func Verify(t *testing.T, value interface{}, exemplarPath string) {
transit := VerifyRoundTrip(t, value)
VerifyExemplar(t, transit, exemplarPath)
}
func VerifyReadError(t *testing.T, badTransitJson string) {
_, err := DecodeFromString(badTransitJson)
if err == nil {
t.Errorf("Expected that decoding [%v] would generate an error, but it did not.", badTransitJson)
}
}