-
Notifications
You must be signed in to change notification settings - Fork 299
/
elements.go
151 lines (136 loc) · 6.73 KB
/
elements.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
// Copyright © 2019 The Things Network Foundation, The Things Industries B.V.
//
// 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 assertions
import (
"fmt"
"reflect"
"go.thethings.network/lorawan-stack/v3/pkg/util/test"
)
const (
shouldHaveHadSameElements = "Expected: '%v'\nActual: '%v'\n(Should have same elements)!"
shouldHaveHadSameElementsErr = "Expected: '%v'\nActual: '%v'\n(Should have same elements, but equality check errored with '%v')!"
shouldNotHaveHadSameElements = "Expected: '%v'\nActual: '%v'\n(Should not have same elements)!"
shouldNotHaveHadSameElementsErr = "Expected: '%v'\nActual: '%v'\n(Should not have same elements, but equality check errored with '%v')!"
shouldHaveBeenProperSubsetOfElements = "Expected: '%v'\nActual: '%v'\n(Should represent proper subset of elements)!"
shouldHaveBeenProperSubsetOfElementsErr = "Expected: '%v'\nActual: '%v'\n(Should represent proper subset of elements, but equality check errored with '%v')!"
)
// ShouldHaveSameElementsFunc takes as arguments the actual value, a comparison function and the expected value.
// If the actual value equals the expected value using the comparison function, this
// function returns an empty string. Otherwise, it returns a string describing the error.
func ShouldHaveSameElementsFunc(actual interface{}, expected ...interface{}) (message string) {
defer func() {
if r := recover(); r != nil {
message = fmt.Sprintf(shouldHaveHadSameElementsErr, expected[0], actual, r)
}
}()
if message = need(2, expected); message != success {
return message
}
if !test.SameElements(expected[0], expected[1], actual) {
return fmt.Sprintf(shouldHaveHadSameElements, expected[1], actual)
}
return success
}
// ShouldNotHaveSameElementsFunc takes as arguments the actual value, a comparison function and the expected value.
// If the actual value does not equal the expected value using the comparison function,
// this function returns an empty string. Otherwise, it returns a string describing the
// error.
func ShouldNotHaveSameElementsFunc(actual interface{}, expected ...interface{}) (message string) {
defer func() {
if r := recover(); r != nil {
message = fmt.Sprintf(shouldNotHaveHadSameElementsErr, expected[0], actual, r)
}
}()
if message = need(2, expected); message != success {
return message
}
if test.SameElements(expected[0], expected[1], actual) {
return fmt.Sprintf(shouldNotHaveHadSameElements, expected[1], actual)
}
return success
}
// ShouldHaveSameElementsDeep takes as arguments the actual value and the expected value.
// If the actual value equals the expected value using reflect.DeepEqual, this
// function returns an empty string. Otherwise, it returns a string describing the error.
func ShouldHaveSameElementsDeep(actual interface{}, expected ...interface{}) (message string) {
if message = need(1, expected); message != success {
return
}
return ShouldHaveSameElementsFunc(actual, reflect.DeepEqual, expected[0])
}
// ShouldNotHaveSameElementsDeep takes as arguments the actual value and the expected
// value.
// If the actual value does not equal the expected value using reflect.DeepEqual, this
// function returns an empty string. Otherwise, it returns a string describing the error.
func ShouldNotHaveSameElementsDeep(actual interface{}, expected ...interface{}) (message string) {
if message = need(1, expected); message != success {
return
}
return ShouldNotHaveSameElementsFunc(actual, reflect.DeepEqual, expected[0])
}
// ShouldHaveSameElementsDiff takes as arguments the actual value and the expected value.
// If the actual value equals the expected value using pretty.Diff, this
// function returns an empty string. Otherwise, it returns a string describing the error.
func ShouldHaveSameElementsDiff(actual interface{}, expected ...interface{}) (message string) {
if message = need(1, expected); message != success {
return
}
return ShouldHaveSameElementsFunc(actual, test.DiffEqual, expected[0])
}
// ShouldNotHaveSameElementsDiff takes as arguments the actual value and the expected
// value.
// If the actual value does not equal the expected value using pretty.Diff, this
// function returns an empty string. Otherwise, it returns a string describing the error.
func ShouldNotHaveSameElementsDiff(actual interface{}, expected ...interface{}) (message string) {
if message = need(1, expected); message != success {
return
}
return ShouldNotHaveSameElementsFunc(actual, test.DiffEqual, expected[0])
}
// ShouldHaveSameElementsEvent takes as arguments the actual value and the expected value.
// If the actual value equals the expected value using test.EventEqual, this
// function returns an empty string. Otherwise, it returns a string describing the error.
func ShouldHaveSameElementsEvent(actual interface{}, expected ...interface{}) (message string) {
if message = need(1, expected); message != success {
return
}
return ShouldHaveSameElementsFunc(actual, test.EventEqual, expected[0])
}
// ShouldNotHaveSameElementsEvent takes as arguments the actual value and the expected
// value.
// If the actual value does not equal the expected value using test.EventEqual, this
// function returns an empty string. Otherwise, it returns a string describing the error.
func ShouldNotHaveSameElementsEvent(actual interface{}, expected ...interface{}) (message string) {
if message = need(1, expected); message != success {
return
}
return ShouldNotHaveSameElementsFunc(actual, test.EventEqual, expected[0])
}
// ShouldBeProperSupersetOfElementsFunc takes as arguments the actual value, a comparison function and the expected value.
// If the actual value represents a proper superset of expected value under equality given by the comparison function, this
// function returns an empty string. Otherwise, it returns a string describing the error.
func ShouldBeProperSupersetOfElementsFunc(actual interface{}, expected ...interface{}) (message string) {
defer func() {
if r := recover(); r != nil {
message = fmt.Sprintf(shouldHaveBeenProperSubsetOfElementsErr, expected[0], actual, r)
}
}()
if message = need(2, expected); message != success {
return message
}
if !test.IsProperSubsetOfElements(expected[0], expected[1], actual) {
return fmt.Sprintf(shouldHaveBeenProperSubsetOfElements, expected[1], actual)
}
return success
}