/
HaveWord.scala
239 lines (216 loc) · 8.92 KB
/
HaveWord.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
/*
* Copyright 2001-2013 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.dsl
import org.scalatest.matchers._
import org.scalatest.enablers._
import org.scalactic._
import org.scalatest.FailureMessages
import org.scalatest.UnquotedString
import org.scalatest.Resources
import scala.collection.GenTraversable
import scala.collection.GenSeq
/**
* This class is part of the ScalaTest matchers DSL. Please see the documentation for <a href="../Matchers.html"><code>Matchers</code></a> for an overview of
* the matchers DSL.
*
* @author Bill Venners
*/
final class HaveWord {
/**
* This method enables the following syntax:
*
* <pre class="stHighlighted">
* book should have length (<span class="stLiteral">9</span>)
* ^
* </pre>
*/
//DOTTY-ONLY infix def length(expectedLength: Long): MatcherFactory1[Any, Length] =
// SKIP-DOTTY-START
def length(expectedLength: Long): MatcherFactory1[Any, Length] =
// SKIP-DOTTY-END
new MatcherFactory1[Any, Length] {
def matcher[T <: Any : Length]: Matcher[T] = {
val length = implicitly[Length[T]]
new Matcher[T] {
def apply(left: T): MatchResult = {
val lengthOfLeft = length.lengthOf(left)
MatchResult(
lengthOfLeft == expectedLength,
Resources.rawHadLengthInsteadOfExpectedLength,
Resources.rawHadLength,
Vector(left, lengthOfLeft, expectedLength),
Vector(left, expectedLength)
)
}
override def toString: String = "have length " + expectedLength
}
}
override def toString: String = "have length " + expectedLength
}
/**
* This method enables the following syntax:
*
* <pre class="stHighlighted">
* book should have size (<span class="stLiteral">9</span>)
* ^
* </pre>
*
* <p>
* Currently, this method will produce a <code>Matcher[AnyRef]</code>, and if the
* <code>AnyRef</code> passed to that matcher's <code>apply</code> method does not have the appropriate <code>size</code> property
* structure, all will compile but a <code>TestFailedException</code> will result at runtime explaining the problem.
* In a future ScalaTest release, this may be tightened so that all is statically checked at compile time.
* </p>
*/
//DOTTY-ONLY infix def size(expectedSize: Long): MatcherFactory1[Any, Size] =
// SKIP-DOTTY-START
def size(expectedSize: Long): MatcherFactory1[Any, Size] =
// SKIP-DOTTY-END
new MatcherFactory1[Any, Size] {
def matcher[T <: Any : Size]: Matcher[T] = {
val size = implicitly[Size[T]]
new Matcher[T] {
def apply(left: T): MatchResult = {
val sizeOfLeft = size.sizeOf(left)
MatchResult(
sizeOfLeft == expectedSize,
Resources.rawHadSizeInsteadOfExpectedSize,
Resources.rawHadSize,
Vector(left, sizeOfLeft, expectedSize),
Vector(left, expectedSize)
)
}
override def toString: String = "have size " + expectedSize
}
}
override def toString: String = "have size " + expectedSize
}
/**
* This method enables the following syntax:
*
* <pre class="stHighlighted">
* result should have message (<span class="stQuotedString">"A message from Mars!"</span>)
* ^
* </pre>
*/
//DOTTY-ONLY infix def message(expectedMessage: String): MatcherFactory1[Any, Messaging] =
// SKIP-DOTTY-START
def message(expectedMessage: String): MatcherFactory1[Any, Messaging] =
// SKIP-DOTTY-END
new MatcherFactory1[Any, Messaging] {
def matcher[T <: Any : Messaging]: Matcher[T] = {
val messaging = implicitly[Messaging[T]]
new Matcher[T] {
def apply(left: T): MatchResult = {
val messageOfLeft = messaging.messageOf(left)
MatchResult(
messageOfLeft == expectedMessage,
Resources.rawHadMessageInsteadOfExpectedMessage,
Resources.rawHadExpectedMessage,
Vector(left, messageOfLeft, expectedMessage),
Vector(left, expectedMessage)
)
}
override def toString: String = "have message " + Prettifier.default(expectedMessage)
}
}
override def toString: String = "have message " + Prettifier.default(expectedMessage)
}
/**
* Enables parentheses to be placed around <code>length (N)</code> in expressions of the form: <code>should have (length (N))</code>.
*/
def apply[T](resultOfLengthWordApplication: ResultOfLengthWordApplication): MatcherFactory1[Any, Length] = length(resultOfLengthWordApplication.expectedLength)
/**
* Enables parentheses to be placed around <code>size (N)</code> in expressions of the form: <code>should have (size (N))</code>.
*/
def apply[T](resultOfSizeWordApplication: ResultOfSizeWordApplication): MatcherFactory1[Any, Size] = size(resultOfSizeWordApplication.expectedSize)
/**
* This method enables the following syntax:
*
* <pre class="stHighlighted">
* book should have (title (<span class="stQuotedString">"A Tale of Two Cities"</span>))
* ^
* </pre>
*/
def apply[T](firstPropertyMatcher: HavePropertyMatcher[T, _], propertyMatchers: HavePropertyMatcher[T, _]*): Matcher[T] =
new Matcher[T] {
def apply(left: T): MatchResult = {
val results =
for (propertyVerifier <- firstPropertyMatcher :: propertyMatchers.toList) yield
propertyVerifier(left)
val firstFailureOption = results.find(pv => !pv.matches)
val justOneProperty = propertyMatchers.isEmpty
firstFailureOption match {
case Some(firstFailure) =>
val failedVerification = firstFailure
val (rawFailureMessage, failureMessageArgs) =
(
Resources.rawPropertyDidNotHaveExpectedValue,
Vector(
UnquotedString(failedVerification.propertyName),
failedVerification.expectedValue,
failedVerification.actualValue,
left
)
)
val (rawMidSentenceFailureMessage, midSentenceFailureMessageArgs) =
(
Resources.rawMidSentencePropertyDidNotHaveExpectedValue,
Vector(
UnquotedString(failedVerification.propertyName),
failedVerification.expectedValue,
failedVerification.actualValue,
left
)
)
MatchResult(false, rawFailureMessage, rawFailureMessage, rawMidSentenceFailureMessage, rawMidSentenceFailureMessage, failureMessageArgs, midSentenceFailureMessageArgs)
case None =>
val (rawFailureMessage, failureMessageArgs) =
if (justOneProperty) {
val firstPropertyResult = results.head // know this will succeed, because firstPropertyMatcher was required
(
Resources.rawPropertyHadExpectedValue,
Vector(
UnquotedString(firstPropertyResult.propertyName),
firstPropertyResult.expectedValue,
left
)
)
}
else (Resources.rawAllPropertiesHadExpectedValues, Vector(left))
val (rawMidSentenceFailureMessage, rawMidSentenceFailureMessageArgs) =
if (justOneProperty) {
val firstPropertyResult = results.head // know this will succeed, because firstPropertyMatcher was required
(
Resources.rawMidSentencePropertyHadExpectedValue,
Vector(
UnquotedString(firstPropertyResult.propertyName),
firstPropertyResult.expectedValue,
left
)
)
}
else (Resources.rawMidSentenceAllPropertiesHadExpectedValues, Vector(left))
MatchResult(true, rawFailureMessage, rawFailureMessage, rawMidSentenceFailureMessage, rawMidSentenceFailureMessage, failureMessageArgs, rawMidSentenceFailureMessageArgs)
}
}
override def toString: String = "have (" + Prettifier.default(firstPropertyMatcher) + ")"
}
/**
* Overrides toString to return "length"
*/
override def toString: String = "have"
}