This repository has been archived by the owner on Dec 22, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 84
/
convey.go
163 lines (132 loc) · 4.06 KB
/
convey.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
157
158
159
160
161
162
163
// Copyright 2015-present Oursky Ltd.
//
// 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 skytest
import (
"encoding/json"
"errors"
"fmt"
"reflect"
"sort"
"github.com/skygeario/skygear-server/pkg/server/skyerr"
)
// ShouldEqualSkyError asserts the equality of skyerr.Error
func ShouldEqualSkyError(actual interface{}, expected ...interface{}) string {
if len(expected) < 1 || len(expected) > 3 {
return fmt.Sprintf("ShouldEqualSkyError receives 1 to 3 arguments")
}
lhs, ok := actual.(skyerr.Error)
if !ok {
return fmt.Sprintf("%v is not skyerr.Error", actual)
}
// expected[0] is code
// expected[1] is message
// expected[2] is info
code, ok := expected[0].(skyerr.ErrorCode)
if !ok {
return fmt.Sprintf("%v is not skyerr.ErrorCode", expected[0])
}
message := ""
if len(expected) >= 2 {
message, ok = expected[1].(string)
if !ok {
return fmt.Sprintf("%v is not message", expected[1])
}
}
var info map[string]interface{}
if len(expected) == 3 {
info, ok = expected[2].(map[string]interface{})
if !ok {
return fmt.Sprintf("%v is not info", expected[2])
}
}
rhs := skyerr.NewErrorWithInfo(code, message, info)
if !reflect.DeepEqual(lhs, rhs) {
return fmt.Sprintf(`Expected: '%v' Actual: '%v'`, lhs, rhs)
}
return ""
}
// ShouldEqualStringSliceWithoutOrder compares two string slice
// by considering them as string set
func ShouldEqualStringSliceWithoutOrder(actual interface{}, expected ...interface{}) string {
if len(expected) != 1 {
return fmt.Sprintf("ShouldEqualStringSliceWithoutOrder receives only expected argument")
}
l, ok := actual.([]string)
if !ok {
return fmt.Sprintf("%v is not []string", actual)
}
r, ok := expected[0].([]string)
if !ok {
return fmt.Sprintf("%v is not []string", expected[0])
}
errMessage := func() string {
return fmt.Sprintf(`Expected: '%v' Actual: '%v'`, l, r)
}
if len(l) != len(r) {
return errMessage()
}
ll := make([]string, len(l))
copy(ll, l)
rr := make([]string, len(r))
copy(rr, r)
lll := sort.StringSlice(ll)
lll.Sort()
rrr := sort.StringSlice(rr)
rrr.Sort()
if !reflect.DeepEqual(lll, rrr) {
return errMessage()
}
return ""
}
// ShouldEqualJSON asserts equality of two JSON bytes or strings by
// their key / value, regardless of the actual position of those
// key-value pairs
func ShouldEqualJSON(actual interface{}, expected ...interface{}) string {
if len(expected) != 1 {
return fmt.Sprintf("ShouldEqualJSON receives only one expected argument")
}
actualBytes, err := interfaceToByteSlice(actual)
if err != nil {
return fmt.Sprintf("%[1]v is %[1]T, not []byte or string", actual)
}
expectedBytes, err := interfaceToByteSlice(expected[0])
if err != nil {
return fmt.Sprintf("%[1]v is %[1]T, not []byte or string", expected[0])
}
var actualJSON, expectedJSON interface{}
if err := json.Unmarshal(actualBytes, &actualJSON); err != nil {
return fmt.Sprintf("invalid JSON of L.H.S.: %v; actual = \n%v", err, actual)
}
if err := json.Unmarshal(expectedBytes, &expectedJSON); err != nil {
return fmt.Sprintf("invalid JSON of R.H.S.: %v; expected = \n%v", err, expected[0])
}
if !reflect.DeepEqual(actualJSON, expectedJSON) {
return fmt.Sprintf(`Expected: '%s'
Actual: '%s'`, prettyPrintJSONMap(expectedJSON), prettyPrintJSONMap(actualJSON))
}
return ""
}
func interfaceToByteSlice(i interface{}) ([]byte, error) {
if b, ok := i.([]byte); ok {
return b, nil
}
if s, ok := i.(string); ok {
return []byte(s), nil
}
return nil, errors.New("cannot convert")
}
func prettyPrintJSONMap(i interface{}) []byte {
b, _ := json.Marshal(&i)
return b
}