/
NonImplicitAssertions.scala
247 lines (236 loc) · 14.5 KB
/
NonImplicitAssertions.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
/*
* 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
import org.scalactic._
import TripleEqualsSupport._
/**
* Trait that can be mixed into a <code>Suite</code> to disable the implicit conversions provided by default in trait
* <a href="Assertions.html"><code>Assertions</code></a>, which trait <code>Suite</code> extends.
*
* <p>
* This trait can be used to quickly solve a problem in which ScalaTest's default implicit conversion is clashing with those of some other library
* you need to use in your tests. After mixing in this trait, like this:
* </p>
*
* <pre class="stHighlighted">
* <span class="stReserved">class</span> <span class="stType">MySuite</span> <span class="stReserved">extends</span> <span class="stType">FunSuite</span> <span class="stReserved">with</span> <span class="stType">NonImplicitAssertions</span> {
* <span class="stLineComment">// ... </span>
* }
* </pre>
*
* <p>
* You can write tests using <code>assert</code> (without triple equals), <code>assertResult</code>, <code>assertThrows</code>,
* <code>intercept</code>, <code>assertCompiles</code>, <code>assertDoesNotCompile</code>, and <code>assertTypeError</code>:
* </p>
*
* <pre class="stHighlighted">
* assert(a < <span class="stLiteral">7</span>)
* <br/>assertResult(<span class="stLiteral">2</span>) { <span class="stLiteral">1</span> + <span class="stLiteral">1</span> }
* <br/>assertThrows[<span class="stType">IndexOutOfBoundsException</span>] {
* <span class="stQuotedString">"hi"</span>.charAt(-<span class="stLiteral">1</span>)
* }
* <br/><span class="stReserved">val</span> caught =
* intercept[<span class="stType">IndexOutOfBoundsException</span>] {
* <span class="stQuotedString">"hi"</span>.charAt(-<span class="stLiteral">1</span>)
* }
* <br/>assertDoesNotCompile(<span class="stQuotedString">"val a: String = 1"</span>)
* <br/>assertTypeError(<span class="stQuotedString">"val a: String = 1"</span>)
* <br/>assertCompiles(<span class="stQuotedString">"val a: Int = 1"</span>)
* </pre>
*
* @author Chua Chee Seng
* @author Bill Venners
*/
trait NonImplicitAssertions extends Assertions {
/**
* Overrides the <code>super</code> implementation of <code>convertToEqualizer</code>, turning off the implicit
* modifier (if present) to remove the method from the space of implicit conversions.
*
* @param left the object whose type to convert to <code>Equalizer</code>.
* @throws NullArgumentException if <code>left</code> is <code>null</code>.
*/
override def convertToEqualizer[T](left: T): Equalizer[T] = new Equalizer(left)
override def convertToCheckingEqualizer[T](left: T): CheckingEqualizer[T] = new CheckingEqualizer(left)
override def lowPriorityTypeCheckedConstraint[A, B](implicit equivalenceOfB: Equivalence[B], ev: A <:< B): A CanEqual B = new AToBEquivalenceConstraint[A, B](equivalenceOfB, ev)
override def convertEquivalenceToAToBConstraint[A, B](equivalenceOfB: Equivalence[B])(implicit ev: A <:< B): A CanEqual B = new AToBEquivalenceConstraint[A, B](equivalenceOfB, ev)
override def typeCheckedConstraint[A, B](implicit equivalenceOfA: Equivalence[A], ev: B <:< A): A CanEqual B = new BToAEquivalenceConstraint[A, B](equivalenceOfA, ev)
override def convertEquivalenceToBToAConstraint[A, B](equivalenceOfA: Equivalence[A])(implicit ev: B <:< A): A CanEqual B = new BToAEquivalenceConstraint[A, B](equivalenceOfA, ev)
/**
* <strong>The <code>lowPriorityConversionCheckedConstraint</code> method has been deprecated and will be removed in a future version of Scalactic. It
* is no longer needed now that the deprecation period of <code>ConversionCheckedTripleEquals</code> has expired. It will not be replaced.</strong>
*
* <p>
* Provides an <code>A CanEqual B</code> instance for any two types <code>A</code> and <code>B</code>, enforcing the type constraint that <code>A</code> is
* implicitly convertible to <code>B</code>, given an implicit <code>Equivalence[B]</code>.
* </p>
*
* <p>
* The returned <code>Constraint</code>'s <code>areEqual</code> method uses the implicitly passed <code>Equivalence[B]</code>'s
* <code>areEquivalent</code> method to determine equality.
* </p>
*
* <p>
* This method is overridden and made implicit by subtraits
* <a href="LowPriorityConversionCheckedConstraint.html"><code>LowPriorityConversionCheckedConstraint</code></a> (extended by
* <a href="ConversionCheckedTripleEquals.html"><code>ConversionCheckedTripleEquals</code></a>), and
* overriden as non-implicit by the other subtraits in this package.
* </p>
*
* @param equalityOfB an <code>Equivalence[B]</code> type class to which the <code>Constraint.areEqual</code> method will delegate to determine equality.
* @param cnv an implicit conversion from <code>A</code> to </code>B</code>
* @return an <code>A CanEqual B</code> instance whose <code>areEqual</code> method delegates to the <code>areEquivalent</code> method of
* the passed <code>Equivalence[B]</code>.
*/
@deprecated("The lowPriorityConversionCheckedConstraint method has been deprecated and will be removed in a future version of ScalaTest. It is no longer needed now that the deprecation period of ConversionCheckedTripleEquals has expired. It will not be replaced.", "3.1.0")
override def lowPriorityConversionCheckedConstraint[A, B](implicit equivalenceOfB: Equivalence[B], cnv: A => B): A CanEqual B = new AToBEquivalenceConstraint[A, B](equivalenceOfB, cnv)
/**
* <strong>The <code>convertEquivalenceToAToBConversionConstraint</code> method has been deprecated and will be removed in a future version of Scalactic.
* It is no longer needed now that the deprecation period of <code>ConversionCheckedTripleEquals</code> has expired. It will not be replaced.</strong>
*
* <p>
* Provides an <code>A CanEqual B</code> instance for any two types <code>A</code> and <code>B</code>, enforcing the type constraint that <code>A</code> is
* implicitly convertible to <code>B</code>, given an <em>explicit</em> <code>Equivalence[B]</code>.
* </p>
*
* <p>
* This method is used to enable the <a href="Explicitly.html"><code>Explicitly</code></a> DSL for
* <a href="ConversionCheckedTripleEquals.html"><code>ConversionCheckedTripleEquals</code></a> by requiring an explicit <code>Equivalance[B]</code>, but
* taking an implicit function that converts from <code>A</code> to </code>B</code>.
* </p>
*
* <p>
* The returned <code>Constraint</code>'s <code>areEqual</code> method uses the implicitly passed <code>Equivalence[B]</code>'s
* <code>areEquivalent</code> method to determine equality.
* </p>
*
* <p>
* This method is overridden and made implicit by subtraits
* <a href="LowPriorityConversionCheckedConstraint.html"><code>LowPriorityConversionCheckedConstraint</code></a> (extended by
* <a href="ConversionCheckedTripleEquals.html"><code>ConversionCheckedTripleEquals</code></a>), and
* overriden as non-implicit by the other subtraits in this package.
* </p>
*
* @param equalityOfB an <code>Equivalence[B]</code> type class to which the <code>Constraint.areEqual</code> method will delegate to determine equality.
* @param cnv an implicit conversion from <code>A</code> to </code>B</code>
* @return an <code>A CanEqual B</code> instance whose <code>areEqual</code> method delegates to the <code>areEquivalent</code> method of
* the passed <code>Equivalence[B]</code>.
*/
@deprecated("The convertEquivalenceToAToBConversionConstraint method has been deprecated and will be removed in a future version of ScalaTest. It is no longer needed now that the deprecation period of ConversionCheckedTripleEquals has expired. It will not be replaced.", "3.1.0")
override def convertEquivalenceToAToBConversionConstraint[A, B](equivalenceOfB: Equivalence[B])(implicit ev: A => B): A CanEqual B = new AToBEquivalenceConstraint[A, B](equivalenceOfB, ev)
/**
* <strong>The <code>conversionCheckedConstraint</code> method has been deprecated and will be removed in a future version of Scalactic. It
* is no longer needed now that the deprecation period of <code>ConversionCheckedTripleEquals</code> has expired. It will not be replaced.</strong>
*
* <p>
* Provides an <code>A CanEqual B</code> instance for any two types <code>A</code> and <code>B</code>, enforcing the type constraint that <code>B</code> is
* implicitly convertible to <code>A</code>, given an implicit <code>Equivalence[A]</code>.
* </p>
*
* <p>
* The returned <code>Constraint</code>'s <code>areEqual</code> method uses the implicitly passed <code>Equivalence[A]</code>'s
* <code>areEquivalent</code> method to determine equality.
* </p>
*
* <p>
* This method is overridden and made implicit by subtraits
* <a href="ConversionCheckedTripleEquals.html"><code>ConversionCheckedTripleEquals</code></a>) and
* overriden as non-implicit by the other subtraits in this package.
* </p>
*
* @param equivalenceOfA an <code>Equivalence[A]</code> type class to which the <code>Constraint.areEqual</code> method will delegate to determine equality.
* @param cnv an implicit conversion from <code>B</code> to </code>A</code>
* @return an <code>A CanEqual B</code> instance whose <code>areEqual</code> method delegates to the <code>areEquivalent</code> method of
* the passed <code>Equivalence[A]</code>.
*/
@deprecated("The conversionCheckedConstraint method has been deprecated and will be removed in a future version of ScalaTest. It is no longer needed now that the deprecation period of ConversionCheckedTripleEquals has expired. It will not be replaced.", "3.1.0")
override def conversionCheckedConstraint[A, B](implicit equivalenceOfA: Equivalence[A], cnv: B => A): A CanEqual B = new BToAEquivalenceConstraint[A, B](equivalenceOfA, cnv)
/**
* <strong>The <code>convertEquivalenceToBToAConversionConstraint</code> method has been deprecated and will be removed in a future version of Scalactic.
* It is no longer needed now that the deprecation period of <code>ConversionCheckedTripleEquals</code> has expired. It will not be replaced.</strong>
*
* <p>
* Provides an <code>A CanEqual B</code> instance for any two types <code>A</code> and <code>B</code>, enforcing the type constraint that <code>B</code> is
* implicitly convertible to <code>A</code>, given an <em>explicit</em> <code>Equivalence[A]</code>.
* </p>
*
* <p>
* This method is used to enable the <a href="Explicitly.html"><code>Explicitly</code></a> DSL for
* <a href="ConversionCheckedTripleEquals.html"><code>ConversionCheckedTripleEquals</code></a> by requiring an explicit <code>Equivalance[A]</code>, but
* taking an implicit function that converts from <code>B</code> to </code>A</code>. For example, under <code>ConversionCheckedTripleEquals</code>,
* this method (as an implicit method), would be used to compile this statement:
* </p>
*
* <pre class="stHighlighted">
* <span class="stReserved">def</span> closeEnoughTo1(num: <span class="stType">Double</span>): <span class="stType">Boolean</span> =
* (num === <span class="stLiteral">1.0</span>)(decided by forgivingEquality)
* </pre>
*
* <p>
* The returned <code>Constraint</code>'s <code>areEqual</code> method uses the implicitly passed <code>Equivalence[A]</code>'s
* <code>areEquivalent</code> method to determine equality.
* </p>
*
* <p>
* This method is overridden and made implicit by subtraits
* <a href="ConversionCheckedTripleEquals.html"><code>ConversionCheckedTripleEquals</code></a>) and
* overriden as non-implicit by the other subtraits in this package.
* </p>
*
* @param equivalenceOfA an <code>Equivalence[A]</code> type class to which the <code>Constraint.areEqual</code> method will delegate to determine equality.
* @param cnv an implicit conversion from <code>B</code> to </code>A</code>
* @return an <code>A CanEqual B</code> instance whose <code>areEqual</code> method delegates to the <code>areEquivalent</code> method of
* the passed <code>Equivalence[A]</code>.
*/
@deprecated("The convertEquivalenceToBToAConversionConstraint method has been deprecated and will be removed in a future version of ScalaTest. It is no longer needed now that the deprecation period of ConversionCheckedTripleEquals has expired. It will not be replaced.", "3.1.0")
override def convertEquivalenceToBToAConversionConstraint[A, B](equivalenceOfA: Equivalence[A])(implicit ev: B => A): A CanEqual B = new BToAEquivalenceConstraint[A, B](equivalenceOfA, ev)
}
/**
* Companion object that facilitates the importing of the members of trait <code>Assertions</code> without importing the implicit conversions
* it provides by default. One use case for this object is to import the non-implicit <code>Assertions</code> members so you can use
* them in the Scala interpreter along with another library whose implicits conflict with those provided by <code>Assertions</code>:
*
* <pre class="stREPL">
* $ scala -cp scalatest-1.7.jar
* Welcome to Scala version 2.9.1.final (Java HotSpot(TM) 64-Bit Server VM, Java 1.6.0_29).
* Type in expressions to have them evaluated.
* Type :help for more information.
*
* scala> import org.scalatest._
* import org.scalatest._
*
* scala> import NonImplicitAssertions._
* import NonImplicitAssertions._
*
* scala> assert(1 + 1 === 2)
* <console>:14: error: value === is not a member of Int
* assert(1 + 1 === 2)
* ^
*
* scala> assert(1 + 1 == 2)
*
* scala> expect(2) { 1 + 1 }
*
* scala> expect(2) { 1 + 1 + 1 }
* org.scalatest.TestFailedException: Expected 2, but got 3
* at org.scalatest.Assertions$class.newAssertionFailedException(Assertions.scala:318)
* at org.scalatest.NonImplicitAssertions$.newAssertionFailedException(NonImplicitAssertions.scala:73)
* ...
*
* scala> intercept[IndexOutOfBoundsException] { "hi".charAt(-1) }
* res3: IndexOutOfBoundsException = java.lang.StringIndexOutOfBoundsException: String index out of range: -1
* </pre>
*/
object NonImplicitAssertions extends NonImplicitAssertions