/
TypeMatcherHelper.scala
312 lines (295 loc) · 15.5 KB
/
TypeMatcherHelper.scala
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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
/*
* Copyright 2001-2014 Artima, Inc.
*
* 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 org.scalatest.matchers
import org.scalactic.source
import org.scalatest.{FailureMessages, Resources, Suite, UnquotedString}
import org.scalactic.Prettifier
import org.scalatest.matchers.MatchersHelper._
import org.scalatest.matchers.dsl.{ResultOfATypeInvocation, ResultOfAnTypeInvocation}
import scala.reflect.ClassTag
//import org.scalatest.words.{FactResultOfAnTypeInvocation, FactResultOfATypeInvocation}
/**
* <code>TypeMatcherHelper</code> is called by <code>TypeMatcherMacro</code> to support <code>a [Type]</code> and <code>an [Type]</code> syntax.
*
* <p>
* This object needs to be public so that the macro-generated code can be compiled. It is expected that ScalaTest
* users would ever need to use <code>TypeMatcherHelper</code> directly.
* </p>
*/
object TypeMatcherHelper {
/**
* Create a type matcher for the given <code>ResultOfATypeInvocation</code>.
*
* @param aType an instance of <code>ResultOfATypeInvocation</code>
* @return a type <code>Matcher</code>
*/
def aTypeMatcher(aType: ResultOfATypeInvocation[_]): Matcher[Any] =
new Matcher[Any] {
def apply(left: Any): MatchResult = {
val clazzTag = aType.clazzTag
MatchResult(
conform(clazzTag, left),
Resources.rawWasNotAnInstanceOf,
Resources.rawWasAnInstanceOf,
Vector(left, UnquotedString(clazzTag.toString), UnquotedString(left.getClass.getName)),
Vector(left, UnquotedString(clazzTag.toString))
)
}
override def toString: String = "be (" + Prettifier.default(aType) + ")"
}
/**
* Create a type matcher for the given <code>ResultOfAnTypeInvocation</code>.
*
* @param anType an instance of <code>ResultOfAnTypeInvocation</code>
* @return a type <code>Matcher</code>
*/
def anTypeMatcher(anType: ResultOfAnTypeInvocation[_]): Matcher[Any] =
new Matcher[Any] {
def apply(left: Any): MatchResult = {
val clazzTag = anType.clazzTag
MatchResult(
conform(clazzTag, left),
Resources.rawWasNotAnInstanceOf,
Resources.rawWasAnInstanceOf,
Vector(left, UnquotedString(clazzTag.toString), UnquotedString(left.getClass.getName)),
Vector(left, UnquotedString(clazzTag.toString))
)
}
override def toString: String = "be (" + Prettifier.default(anType) + ")"
}
/**
* Create a negated type matcher for the given <code>ResultOfATypeInvocation</code>.
*
* @param aType an instance of <code>ResultOfATypeInvocation</code>
* @return a negated type <code>Matcher</code>
*/
def notATypeMatcher(aType: ResultOfATypeInvocation[_]): Matcher[Any] =
new Matcher[Any] {
def apply(left: Any): MatchResult = {
val clazzTag = aType.clazzTag
MatchResult(
!conform(clazzTag, left),
Resources.rawWasAnInstanceOf,
Resources.rawWasNotAnInstanceOf,
Vector(left, UnquotedString(clazzTag.toString)),
Vector(left, UnquotedString(clazzTag.toString), UnquotedString(left.getClass.getName))
)
}
override def toString: String = "not be " + Prettifier.default(aType)
}
/**
* Create a negated type matcher for the given <code>ResultOfAnTypeInvocation</code>.
*
* @param anType an instance of <code>ResultOfAnTypeInvocation</code>
* @return a negated type <code>Matcher</code>
*/
def notAnTypeMatcher(anType: ResultOfAnTypeInvocation[_]): Matcher[Any] =
new Matcher[Any] {
def apply(left: Any): MatchResult = {
val clazzTag = anType.clazzTag
MatchResult(
!conform(clazzTag, left),
Resources.rawWasAnInstanceOf,
Resources.rawWasNotAnInstanceOf,
Vector(left, UnquotedString(clazzTag.toString)),
Vector(left, UnquotedString(clazzTag.toString), UnquotedString(left.getClass.getName))
)
}
override def toString: String = "not be " + Prettifier.default(anType)
}
private def conform[T](classTag: ClassTag[T], x: Any): Boolean =
if (classTag == ClassTag.AnyVal)
x.isInstanceOf[Byte] ||
x.isInstanceOf[Short] ||
x.isInstanceOf[Char] ||
x.isInstanceOf[Int] ||
x.isInstanceOf[Long] ||
x.isInstanceOf[Float] ||
x.isInstanceOf[Double] ||
x.isInstanceOf[Boolean]
else {
val runtimeClass = classTag.runtimeClass
// This part of code is inspired from ClassTag's unapply method starting Scala 2.11,
// we can't use ClassTag's unapply directly because in Scala 2.10 it wasn't
// written this way and it can't meet our purpose.
if (null != x && (
(runtimeClass.isInstance(x))
|| (x.isInstanceOf[Byte] && runtimeClass.isAssignableFrom(classOf[Byte]))
|| (x.isInstanceOf[Short] && runtimeClass.isAssignableFrom(classOf[Short]))
|| (x.isInstanceOf[Char] && runtimeClass.isAssignableFrom(classOf[Char]))
|| (x.isInstanceOf[Int] && runtimeClass.isAssignableFrom(classOf[Int]))
|| (x.isInstanceOf[Long] && runtimeClass.isAssignableFrom(classOf[Long]))
|| (x.isInstanceOf[Float] && runtimeClass.isAssignableFrom(classOf[Float]))
|| (x.isInstanceOf[Double] && runtimeClass.isAssignableFrom(classOf[Double]))
|| (x.isInstanceOf[Boolean] && runtimeClass.isAssignableFrom(classOf[Boolean]))
|| (x.isInstanceOf[Unit] && runtimeClass.isAssignableFrom(classOf[Unit])))
) true
else false
}
/**
* Check if the given <code>left</code> is an instance of the type as described in the given <code>ResultOfATypeInvocation</code>.
* A <code>TestFailedException</code> will be thrown if <code>left</code> is not an instance of the type given by <code>ResultOfATypeInvocation</code>.
*
* @param left the left-hand-side (LHS) to be checked for the type
* @param aType an instance of <code>ResultOfATypeInvocation</code>
*/
def assertAType(left: Any, aType: ResultOfATypeInvocation[_], prettifier: Prettifier, pos: source.Position): org.scalatest.Assertion = {
val clazz = aType.clazz
if (!conform(aType.clazzTag, left)) {
val (leftee, rightee) = Suite.getObjectsForFailureMessage(left, aType.clazzTag.toString)
throw newTestFailedException(FailureMessages.wasNotAnInstanceOf(prettifier, left, UnquotedString(aType.clazzTag.toString), UnquotedString(left.getClass.getName)), None, pos)
}
org.scalatest.Succeeded
}
/**
* Check if the given <code>left</code> is an instance of the type as described in the given <code>ResultOfATypeInvocation</code>.
* A <code>Fact.No</code> will be returned if <code>left</code> is not an instance of the type given by <code>ResultOfATypeInvocation</code>.
*
* @param left the left-hand-side (LHS) to be checked for the type
* @param aType an instance of <code>ResultOfATypeInvocation</code>
*/
/*private[scalatest] def expectAType(left: Any, aType: ResultOfATypeInvocation[_], prettifier: Prettifier): org.scalatest.Fact = {
val clazz = aType.clazz
if (!conform(clazz, left.getClass)) {
val (leftee, rightee) = Suite.getObjectsForFailureMessage(left, clazz.getName)
org.scalatest.Fact.No(FailureMessages.wasNotAnInstanceOf(aType.prettifier, left, UnquotedString(clazz.getName), UnquotedString(left.getClass.getName)))(aType.prettifier)
}
else org.scalatest.Fact.Yes(FailureMessages.wasAnInstanceOf(aType.prettifier, left, UnquotedString(clazz.getName)))(aType.prettifier)
}*/
/**
* Check if the given <code>left</code> is an instance of the type as described in the given <code>ResultOfAnTypeInvocation</code>.
* A <code>TestFailedException</code> will be thrown if <code>left</code> is not an instance of the type given by <code>ResultOfAnTypeInvocation</code>.
*
* @param left the left-hand-side (LHS) to be checked for the type
* @param anType an instance of <code>ResultOfAnTypeInvocation</code>
*/
def assertAnType(left: Any, anType: ResultOfAnTypeInvocation[_], prettifier: Prettifier, pos: source.Position): org.scalatest.Assertion = {
val clazz = anType.clazz
if (!conform(anType.clazzTag, left)) {
val (leftee, rightee) = Suite.getObjectsForFailureMessage(left, anType.clazzTag.toString)
throw newTestFailedException(FailureMessages.wasNotAnInstanceOf(prettifier, left, UnquotedString(anType.clazzTag.toString), UnquotedString(left.getClass.getName)), None, pos)
}
org.scalatest.Succeeded
}
/**
* Check if the given <code>left</code> is an instance of the type as described in the given <code>ResultOfAnTypeInvocation</code>.
* A <code>Fact.No</code> will be returned if <code>left</code> is not an instance of the type given by <code>ResultOfAnTypeInvocation</code>.
*
* @param left the left-hand-side (LHS) to be checked for the type
* @param anType an instance of <code>ResultOfAnTypeInvocation</code>
*/
/*private[scalatest] def expectAnType(left: Any, anType: ResultOfAnTypeInvocation[_]): org.scalatest.Fact = {
val clazz = anType.clazz
if (!conform(clazz, left)) {
val (leftee, rightee) = Suite.getObjectsForFailureMessage(left, clazz.getName)
org.scalatest.Fact.No(FailureMessages.wasNotAnInstanceOf(anType.prettifier, left, UnquotedString(clazz.getName), UnquotedString(left.getClass.getName)))(anType.prettifier)
} else org.scalatest.Fact.Yes(FailureMessages.wasAnInstanceOf(anType.prettifier, left, UnquotedString(clazz.getName)))(anType.prettifier)
}*/
/**
* Based on <code>shouldBeTrue</code> value, check if the given <code>left</code> is an instance of the type as described in the given <code>ResultOfATypeInvocation</code>.
* If <code>shouldBeTrue</code> is true, a <code>TestFailedException</code> will be thrown if <code>left</code> is not an instance of the type given by <code>ResultOfATypeInvocation</code>.
* If <code>shouldBeTrue</code> is false, a <code>TestFailedException</code> will be thrown if <code>left</code> is an instance of the type given by <code>ResultOfATypeInvocation</code>.
*
* @param left the left-hand-side (LHS) to be checked for the type
* @param aType an instance of <code>ResultOfATypeInvocation</code>
*/
def assertATypeShouldBeTrue(left: Any, aType: ResultOfATypeInvocation[_], shouldBeTrue: Boolean, prettifier: Prettifier, pos: source.Position): org.scalatest.Assertion = {
val clazz = aType.clazz
if (conform(aType.clazzTag, left) != shouldBeTrue) {
throw newTestFailedException(
if (shouldBeTrue)
FailureMessages.wasNotAnInstanceOf(prettifier, left, UnquotedString(aType.clazzTag.toString), UnquotedString(left.getClass.getName))
else
FailureMessages.wasAnInstanceOf(prettifier, left, UnquotedString(aType.clazzTag.toString)),
None,
pos
)
} else org.scalatest.Succeeded
}
/**
* Based on <code>shouldBeTrue</code> value, check if the given <code>left</code> is an instance of the type as described in the given <code>ResultOfATypeInvocation</code>.
* If <code>shouldBeTrue</code> is true, a <code>Fact.No</code> will be returned if <code>left</code> is not an instance of the type given by <code>ResultOfATypeInvocation</code>.
* If <code>shouldBeTrue</code> is false, a <code>Fact.No</code> will be returned if <code>left</code> is an instance of the type given by <code>ResultOfATypeInvocation</code>.
*
* @param left the left-hand-side (LHS) to be checked for the type
* @param aType an instance of <code>ResultOfATypeInvocation</code>
*/
/*def expectATypeWillBeTrue(left: Any, aType: FactResultOfATypeInvocation[_], shouldBeTrue: Boolean): org.scalatest.Fact = {
val clazz = aType.clazz
if (conform(clazz, left) != shouldBeTrue) {
org.scalatest.Fact.No(
if (shouldBeTrue)
FailureMessages.wasNotAnInstanceOf(prettifier, left, UnquotedString(clazz.getName), UnquotedString(left.getClass.getName))
else
FailureMessages.wasAnInstanceOf(prettifier, left, UnquotedString(clazz.getName))
)
}
else
org.scalatest.Fact.Yes(
if (shouldBeTrue)
FailureMessages.wasAnInstanceOf(prettifier, left, UnquotedString(clazz.getName))
else
FailureMessages.wasNotAnInstanceOf(prettifier, left, UnquotedString(clazz.getName), UnquotedString(left.getClass.getName))
)
}*/
/**
* Based on <code>shouldBeTrue</code> value, check if the given <code>left</code> is an instance of the type as described in the given <code>ResultOfAnTypeInvocation</code>.
* If <code>shouldBeTrue</code> is true, a <code>TestFailedException</code> will be thrown if <code>left</code> is not an instance of the type given by <code>ResultOfAnTypeInvocation</code>.
* If <code>shouldBeTrue</code> is false, a <code>TestFailedException</code> will be thrown if <code>left</code> is an instance of the type given by <code>ResultOfAnTypeInvocation</code>.
*
* @param left the left-hand-side (LHS) to be checked for the type
* @param anType an instance of <code>ResultOfAnTypeInvocation</code>
*/
def assertAnTypeShouldBeTrue(left: Any, anType: ResultOfAnTypeInvocation[_], shouldBeTrue: Boolean, prettifier: Prettifier, pos: source.Position): org.scalatest.Assertion = {
val clazz = anType.clazz
if (conform(anType.clazzTag, left) != shouldBeTrue) {
throw newTestFailedException(
if (shouldBeTrue)
FailureMessages.wasNotAnInstanceOf(prettifier, left, UnquotedString(anType.clazzTag.toString), UnquotedString(left.getClass.getName))
else
FailureMessages.wasAnInstanceOf(prettifier, left, UnquotedString(anType.clazzTag.toString)),
None,
pos
)
} else org.scalatest.Succeeded
}
/*
* Based on <code>shouldBeTrue</code> value, check if the given <code>left</code> is an instance of the type as described in the given <code>ResultOfAnTypeInvocation</code>.
* If <code>shouldBeTrue</code> is true, a <code>Fact.No</code> will be returned if <code>left</code> is not an instance of the type given by <code>ResultOfAnTypeInvocation</code>.
* If <code>shouldBeTrue</code> is false, a <code>Fact.No</code> will be returned if <code>left</code> is an instance of the type given by <code>ResultOfAnTypeInvocation</code>.
*
* @param left the left-hand-side (LHS) to be checked for the type
* @param anType an instance of <code>ResultOfAnTypeInvocation</code>
*/
/*def expectAnTypeWillBeTrue(left: Any, anType: FactResultOfAnTypeInvocation[_], shouldBeTrue: Boolean): org.scalatest.Fact = {
val clazz = anType.clazz
if (conform(clazz, left) != shouldBeTrue) {
org.scalatest.Fact.No(
if (shouldBeTrue)
FailureMessages.wasNotAnInstanceOf(prettifier, left, UnquotedString(clazz.getName), UnquotedString(left.getClass.getName))
else
FailureMessages.wasAnInstanceOf(prettifier, left, UnquotedString(clazz.getName))
)
}
else
org.scalatest.Fact.Yes(
if (shouldBeTrue)
FailureMessages.wasAnInstanceOf(prettifier, left, UnquotedString(clazz.getName))
else
FailureMessages.wasNotAnInstanceOf(prettifier, left, UnquotedString(clazz.getName), UnquotedString(left.getClass.getName))
)
}*/
}