File: elements_are_test.go

package info (click to toggle)
golang-github-jacobsa-oglematchers 0.0~git20150320-1~bpo8%2B1
  • links: PTS, VCS
  • area: main
  • in suites: jessie-backports
  • size: 564 kB
  • sloc: makefile: 4
file content (208 lines) | stat: -rw-r--r-- 4,975 bytes parent folder | download | duplicates (6)
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
// Copyright 2012 Aaron Jacobs. All Rights Reserved.
// Author: aaronjjacobs@gmail.com (Aaron Jacobs)
//
// 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 oglematchers_test

import (
	. "github.com/jacobsa/oglematchers"
	. "github.com/jacobsa/ogletest"
)

////////////////////////////////////////////////////////////////////////
// Helpers
////////////////////////////////////////////////////////////////////////

type ElementsAreTest struct {
}

func init()                     { RegisterTestSuite(&ElementsAreTest{}) }

////////////////////////////////////////////////////////////////////////
// Tests
////////////////////////////////////////////////////////////////////////

func (t *ElementsAreTest) EmptySet() {
	m := ElementsAre()
	ExpectEq("elements are: []", m.Description())

	var c []interface{}
	var err error

	// No candidates.
	c = []interface{}{}
	err = m.Matches(c)
	ExpectEq(nil, err)

	// One candidate.
	c = []interface{}{17}
	err = m.Matches(c)
	ExpectThat(err, Error(HasSubstr("length 1")))
}

func (t *ElementsAreTest) OneMatcher() {
	m := ElementsAre(LessThan(17))
	ExpectEq("elements are: [less than 17]", m.Description())

	var c []interface{}
	var err error

	// No candidates.
	c = []interface{}{}
	err = m.Matches(c)
	ExpectThat(err, Error(HasSubstr("length 0")))

	// Matching candidate.
	c = []interface{}{16}
	err = m.Matches(c)
	ExpectEq(nil, err)

	// Non-matching candidate.
	c = []interface{}{19}
	err = m.Matches(c)
	ExpectNe(nil, err)

	// Two candidates.
	c = []interface{}{17, 19}
	err = m.Matches(c)
	ExpectThat(err, Error(HasSubstr("length 2")))
}

func (t *ElementsAreTest) OneValue() {
	m := ElementsAre(17)
	ExpectEq("elements are: [17]", m.Description())

	var c []interface{}
	var err error

	// No candidates.
	c = []interface{}{}
	err = m.Matches(c)
	ExpectThat(err, Error(HasSubstr("length 0")))

	// Matching int.
	c = []interface{}{int(17)}
	err = m.Matches(c)
	ExpectEq(nil, err)

	// Matching float.
	c = []interface{}{float32(17)}
	err = m.Matches(c)
	ExpectEq(nil, err)

	// Non-matching candidate.
	c = []interface{}{19}
	err = m.Matches(c)
	ExpectNe(nil, err)

	// Two candidates.
	c = []interface{}{17, 19}
	err = m.Matches(c)
	ExpectThat(err, Error(HasSubstr("length 2")))
}

func (t *ElementsAreTest) MultipleElements() {
	m := ElementsAre("taco", LessThan(17))
	ExpectEq("elements are: [taco, less than 17]", m.Description())

	var c []interface{}
	var err error

	// One candidate.
	c = []interface{}{17}
	err = m.Matches(c)
	ExpectThat(err, Error(HasSubstr("length 1")))

	// Both matching.
	c = []interface{}{"taco", 16}
	err = m.Matches(c)
	ExpectEq(nil, err)

	// First non-matching.
	c = []interface{}{"burrito", 16}
	err = m.Matches(c)
	ExpectThat(err, Error(Equals("whose element 0 doesn't match")))

	// Second non-matching.
	c = []interface{}{"taco", 17}
	err = m.Matches(c)
	ExpectThat(err, Error(Equals("whose element 1 doesn't match")))

	// Three candidates.
	c = []interface{}{"taco", 17, 19}
	err = m.Matches(c)
	ExpectThat(err, Error(HasSubstr("length 3")))
}

func (t *ElementsAreTest) ArrayCandidates() {
	m := ElementsAre("taco", LessThan(17))

	var err error

	// One candidate.
	err = m.Matches([1]interface{}{"taco"})
	ExpectThat(err, Error(HasSubstr("length 1")))

	// Both matching.
	err = m.Matches([2]interface{}{"taco", 16})
	ExpectEq(nil, err)

	// First non-matching.
	err = m.Matches([2]interface{}{"burrito", 16})
	ExpectThat(err, Error(Equals("whose element 0 doesn't match")))
}

func (t *ElementsAreTest) WrongTypeCandidate() {
	m := ElementsAre("taco")

	var err error

	// String candidate.
	err = m.Matches("taco")
	ExpectTrue(isFatal(err))
	ExpectThat(err, Error(HasSubstr("array")))
	ExpectThat(err, Error(HasSubstr("slice")))

	// Map candidate.
	err = m.Matches(map[string]string{})
	ExpectTrue(isFatal(err))
	ExpectThat(err, Error(HasSubstr("array")))
	ExpectThat(err, Error(HasSubstr("slice")))

	// Nil candidate.
	err = m.Matches(nil)
	ExpectTrue(isFatal(err))
	ExpectThat(err, Error(HasSubstr("array")))
	ExpectThat(err, Error(HasSubstr("slice")))
}

func (t *ElementsAreTest) PropagatesFatality() {
	m := ElementsAre(LessThan(17))
	ExpectEq("elements are: [less than 17]", m.Description())

	var c []interface{}
	var err error

	// Non-fatal error.
	c = []interface{}{19}
	err = m.Matches(c)
	AssertNe(nil, err)
	ExpectFalse(isFatal(err))

	// Fatal error.
	c = []interface{}{"taco"}
	err = m.Matches(c)
	AssertNe(nil, err)
	ExpectTrue(isFatal(err))
}