/
Aggregating.scala
576 lines (535 loc) · 30.5 KB
/
Aggregating.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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
/*
* 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.enablers
import scala.collection.JavaConverters._
import org.scalactic.{Equality, Every}
import scala.collection.GenTraversable
import org.scalatest.FailureMessages
import org.scalatest.verbs.ArrayWrapper
import org.scalactic.ColCompatHelper.aggregate
import scala.annotation.tailrec
/**
* Typeclass that enables for aggregations certain <code>contain</code> syntax in the ScalaTest matchers DSL.
*
* <p>
* An <code>Aggregating[A]</code> provides access to the "aggregating nature" of type <code>A</code> in such
* a way that relevant <code>contain</code> matcher syntax can be used with type <code>A</code>. An <code>A</code>
* can be any type of <em>aggregation</em>—an object that in some way aggregates or brings together other objects. ScalaTest provides
* implicit implementations for several types out of the box in the
* <a href="Aggregating$.html"><code>Aggregating</code> companion object</a>:
* </p>
*
* <ul>
* <li><code>scala.collection.GenTraversable</code></li>
* <li><code>String</code></li>
* <li><code>Array</code></li>
* <li><code>java.util.Collection</code></li>
* <li><code>java.util.Map</code></li>
* </ul>
*
* <p>
* The <code>contain</code> syntax enabled by this trait is:
* <p>
*
* <ul>
* <li><code>result</code> <code>should</code> <code>contain</code> <code>atLeastOneOf</code> <code>(1, 2, 3)</code></li>
* <li><code>result</code> <code>should</code> <code>contain</code> <code>atMostOneOf</code> <code>(1, 2, 3)</code></li>
* <li><code>result</code> <code>should</code> <code>contain</code> <code>only</code> <code>(1, 2, 3)</code></li>
* <li><code>result</code> <code>should</code> <code>contain</code> <code>allOf</code> <code>(1, 2, 3)</code></li>
* <li><code>result</code> <code>should</code> <code>contain</code> <code>theSameElementsAs</code> <code>(List(1, 2, 3))</code></li>
* </ul>
*
* <p>
* You can enable the <code>contain</code> matcher syntax enabled by <code>Aggregating</code> on your own
* type <code>U</code> by defining an <code>Aggregating[U]</code> for the type and making it available implicitly.
* </p>
*
* <p>
* Note, for an explanation of the difference between <code>Containing</code> and <code>Aggregating</code>, both of which
* enable <code>contain</code> matcher syntax, see the <a href="Containing.html#containingVersusAggregating">Containing
* versus Aggregating</a> section of the main documentation for trait <code>Containing</code>.
* </p>
*/
trait Aggregating[-A] {
// TODO: Write tests that a NotAllowedException is thrown when no elements are passed, maybe if only one element is passed, and
// likely if an object is repeated in the list.
/**
* Implements <code>contain</code> <code>atLeastOneOf</code> syntax for aggregations of type <code>A</code>.
*
* @param aggregation an aggregation about which an assertion is being made
* @param eles elements at least one of which should be contained in the passed aggregation
* @return true if the passed aggregation contains at least one of the passed elements
*/
def containsAtLeastOneOf(aggregation: A, eles: scala.collection.Seq[Any]): Boolean
/**
* Implements <code>contain</code> <code>theSameElementsAs</code> syntax for aggregations of type <code>A</code>.
*
* @param leftAggregation an aggregation about which an assertion is being made
* @param rightAggregation an aggregation that should contain the same elements as the passed <code>leftAggregation</code>
* @return true if the passed <code>leftAggregation</code> contains the same elements as the passed <code>rightAggregation</code>
*/
def containsTheSameElementsAs(leftAggregation: A, rightAggregation: GenTraversable[Any]): Boolean
/**
* Implements <code>contain</code> <code>only</code> syntax for aggregations of type <code>A</code>.
*
* @param aggregation an aggregation about which an assertion is being made
* @param eles the only elements that should be contained in the passed aggregation
* @return true if the passed aggregation contains only the passed elements
*/
def containsOnly(aggregation: A, eles: scala.collection.Seq[Any]): Boolean
/**
* Implements <code>contain</code> <code>allOf</code> syntax for aggregations of type <code>A</code>.
*
* @param aggregation an aggregation about which an assertion is being made
* @param eles elements all of which should be contained in the passed aggregation
* @return true if the passed aggregation contains all of the passed elements
*/
def containsAllOf(aggregation: A, eles: scala.collection.Seq[Any]): Boolean
/**
* Implements <code>contain</code> <code>atMostOneOf</code> syntax for aggregations of type <code>A</code>.
*
* @param aggregation an aggregation about which an assertion is being made
* @param eles elements at most one of which should be contained in the passed aggregation
* @return true if the passed aggregation contains at most one of the passed elements
*/
def containsAtMostOneOf(aggregation: A, eles: scala.collection.Seq[Any]): Boolean
}
trait AggregatingImpls {
// TODO: Throwing exceptions is slow. Just do a pattern match and test the type before trying to cast it.
private[scalatest] def tryEquality[T](left: Any, right: Any, equality: Equality[T]): Boolean =
try equality.areEqual(left.asInstanceOf[T], right)
catch {
case cce: ClassCastException => false
}
private[scalatest] def checkTheSameElementsAs[T](left: GenTraversable[T], right: GenTraversable[Any], equality: Equality[T]): Boolean = {
case class ElementCount(element: Any, leftCount: Int, rightCount: Int)
object ZipNoMatch
def leftNewCount(next: Any, count: IndexedSeq[ElementCount]): IndexedSeq[ElementCount] = {
val idx = count.indexWhere(ec => tryEquality(next, ec.element, equality))
if (idx >= 0) {
val currentElementCount = count(idx)
count.updated(idx, ElementCount(currentElementCount.element, currentElementCount.leftCount + 1, currentElementCount.rightCount))
}
else
count :+ ElementCount(next, 1, 0)
}
def rightNewCount(next: Any, count: IndexedSeq[ElementCount]): IndexedSeq[ElementCount] = {
val idx = count.indexWhere(ec => tryEquality(next, ec.element, equality))
if (idx >= 0) {
val currentElementCount = count(idx)
count.updated(idx, ElementCount(currentElementCount.element, currentElementCount.leftCount, currentElementCount.rightCount + 1))
}
else
count :+ ElementCount(next, 0, 1)
}
val counts = aggregate(right.toIterable.zipAll(left.toIterable, ZipNoMatch, ZipNoMatch), IndexedSeq.empty[ElementCount])(
{ case (count, (nextLeft, nextRight)) =>
if (nextLeft == ZipNoMatch || nextRight == ZipNoMatch)
return false // size not match, can fail early
rightNewCount(nextRight, leftNewCount(nextLeft, count))
},
{ case (count1, count2) =>
count2.foldLeft(count1) { case (count, next) =>
val idx = count.indexWhere(ec => tryEquality(next.element, ec.element, equality))
if (idx >= 0) {
val currentElementCount = count(idx)
count.updated(idx, ElementCount(currentElementCount.element, currentElementCount.leftCount + next.leftCount, currentElementCount.rightCount + next.rightCount))
}
else
count :+ next
}
}
)
!counts.exists(e => e.leftCount != e.rightCount)
}
private[scalatest] def checkOnly[T](left: GenTraversable[T], right: GenTraversable[Any], equality: Equality[T]): Boolean =
left.forall(l => right.find(r => tryEquality(l, r, equality)).isDefined) &&
right.forall(r => left.find(l => tryEquality(l, r, equality)).isDefined)
private[scalatest] def checkAllOf[T](left: GenTraversable[T], right: GenTraversable[Any], equality: Equality[T]): Boolean = {
@tailrec
def checkEqual(left: GenTraversable[T], rightItr: Iterator[Any]): Boolean = {
if (rightItr.hasNext) {
val nextRight = rightItr.next
if (left.exists(t => equality.areEqual(t, nextRight)))
checkEqual(left, rightItr)
else
false // Element not found, let's fail early
}
else // No more element in right, left contains all of right.
true
}
checkEqual(left, right.toIterator)
}
private[scalatest] def checkAtMostOneOf[T](left: GenTraversable[T], right: GenTraversable[Any], equality: Equality[T]): Boolean = {
def countElements: Int =
aggregate(right, 0)(
{ case (count, nextRight) =>
if (left.exists(l => equality.areEqual(l, nextRight))) {
val newCount = count + 1
if (newCount > 1)
return newCount
else
newCount
}
else
count
},
{ case (count1, count2) => count1 + count2 }
)
val count = countElements
count <= 1
}
}
trait AggregatingJavaImplicits extends AggregatingImpls {
/**
* Implicit to support <code>Aggregating</code> nature of <code>java.util.Collection</code>.
*
* @param equality <a href="../../scalactic/Equality.html"><code>Equality</code></a> type class that is used to check equality of element in the <code>java.util.Collection</code>
* @tparam E the type of the element in the <code>java.util.Collection</code>
* @tparam JCOL any subtype of <code>java.util.Collection</code>
* @return <code>Aggregating[JCOL[E]]</code> that supports <code>java.util.Collection</code> in relevant <code>contain</code> syntax
*/
implicit def aggregatingNatureOfJavaCollection[E, JCOL[e] <: java.util.Collection[e]](implicit equality: Equality[E]): Aggregating[JCOL[E]] =
new Aggregating[JCOL[E]] {
def containsAtLeastOneOf(col: JCOL[E], elements: scala.collection.Seq[Any]): Boolean = {
col.asScala.exists((e: E) => elements.exists((ele: Any) => equality.areEqual(e, ele)))
}
def containsTheSameElementsAs(col: JCOL[E], elements: GenTraversable[Any]): Boolean = {
checkTheSameElementsAs(col.asScala, elements, equality)
}
def containsOnly(col: JCOL[E], elements: scala.collection.Seq[Any]): Boolean = {
checkOnly(col.asScala, elements, equality)
}
def containsAllOf(col: JCOL[E], elements: scala.collection.Seq[Any]): Boolean = {
checkAllOf(col.asScala, elements, equality)
}
def containsAtMostOneOf(col: JCOL[E], elements: scala.collection.Seq[Any]): Boolean = {
checkAtMostOneOf(col.asScala, elements, equality)
}
}
/**
* Implicit conversion that converts an <a href="../../scalactic/Equality.html"><code>Equality</code></a> of type <code>E</code>
* into <code>Aggregating</code> of type <code>JCOL[E]</code>, where <code>JCOL</code> is a subtype of <code>java.util.Collection</code>.
* This is required to support the explicit <a href="../../scalactic/Equality.html"><code>Equality</code></a> syntax, for example:
*
* <pre class="stHighlighted">
* <span class="stReserved">val</span> javaList = <span class="stReserved">new</span> <span class="stType">java.util.ArrayList[String]</span>()
* javaList.add(<span class="stQuotedString">"hi"</span>)
* (javaList should contain (<span class="stQuotedString">"HI"</span>)) (after being lowerCased)
* </pre>
*
* <code>(after being lowerCased)</code> will returns an <a href="../../scalactic/Equality.html"><code>Equality[String]</code></a>
* and this implicit conversion will convert it into <code>Aggregating[java.util.ArrayList[String]]</code>.
*
* @param equality <a href="../../scalactic/Equality.html"><code>Equality</code></a> of type <code>E</code>
* @tparam E type of elements in the <code>java.util.Collection</code>
* @tparam JCOL subtype of <code>java.util.Collection</code>
* @return <code>Aggregating</code> of type <code>JCOL[E]</code>
*/
implicit def convertEqualityToJavaCollectionAggregating[E, JCOL[e] <: java.util.Collection[e]](equality: Equality[E]): Aggregating[JCOL[E]] =
aggregatingNatureOfJavaCollection(equality)
/**
* Implicit to support <code>Aggregating</code> nature of <code>java.util.Map</code>.
*
* @param equality <a href="../../scalactic/Equality.html"><code>Equality</code></a> type class that is used to check equality of entry in the <code>java.util.Map</code>
* @tparam K the type of the key in the <code>java.util.Map</code>
* @tparam V the type of the value in the <code>java.util.Map</code>
* @tparam JMAP any subtype of <code>java.util.Map</code>
* @return <code>Aggregating[JMAP[K, V]]</code> that supports <code>java.util.Map</code> in relevant <code>contain</code> syntax
*/
implicit def aggregatingNatureOfJavaMap[K, V, JMAP[k, v] <: java.util.Map[k, v]](implicit equality: Equality[java.util.Map.Entry[K, V]]): Aggregating[JMAP[K, V]] =
new Aggregating[JMAP[K, V]] {
import scala.collection.JavaConverters._
def containsAtLeastOneOf(map: JMAP[K, V], elements: scala.collection.Seq[Any]): Boolean = {
map.entrySet.asScala.exists((e: java.util.Map.Entry[K, V]) => elements.exists((ele: Any) => equality.areEqual(e, ele)))
}
def containsTheSameElementsAs(map: JMAP[K, V], elements: GenTraversable[Any]): Boolean = {
checkTheSameElementsAs(map.entrySet.asScala, elements, equality)
}
def containsOnly(map: JMAP[K, V], elements: scala.collection.Seq[Any]): Boolean = {
checkOnly(map.entrySet.asScala, elements, equality)
}
def containsAllOf(map: JMAP[K, V], elements: scala.collection.Seq[Any]): Boolean = {
checkAllOf(map.entrySet.asScala, elements, equality)
}
def containsAtMostOneOf(map: JMAP[K, V], elements: scala.collection.Seq[Any]): Boolean = {
checkAtMostOneOf(map.entrySet.asScala, elements, equality)
}
}
/**
* Implicit conversion that converts an <a href="../../scalactic/Equality.html"><code>Equality</code></a> of type <code>java.util.Map.Entry[K, V]</code>
* into <code>Aggregating</code> of type <code>JMAP[K, V]</code>, where <code>JMAP</code> is a subtype of <code>java.util.Map</code>.
* This is required to support the explicit <a href="../../scalactic/Equality.html"><code>Equality</code></a> syntax, for example:
*
* <pre class="stHighlighted">
* <span class="stReserved">val</span> javaMap = <span class="stReserved">new</span> <span class="stType">java.util.HashMap[Int, String]</span>()
* javaMap.put(<span class="stLiteral">1</span>, <span class="stQuotedString">"one"</span>)
* <span class="stLineComment">// lowerCased needs to be implemented as Normalization[java.util.Map.Entry[K, V]]</span>
* (javaMap should contain (<span class="stType">Entry</span>(<span class="stLiteral">1</span>, <span class="stQuotedString">"ONE"</span>))) (after being lowerCased)
* </pre>
*
* <code>(after being lowerCased)</code> will returns an <a href="../../scalactic/Equality.html"><code>java.util.Map.Entry[Int, String]</code></a>
* and this implicit conversion will convert it into <code>Aggregating[java.util.HashMap[Int, String]]</code>.
*
* @param equality <a href="../../scalactic/Equality.html"><code>Equality</code></a> of type <code>java.util.Map.Entry[K, V]</code>
* @tparam K the type of the key in the <code>java.util.Map</code>
* @tparam V the type of the value in the <code>java.util.Map</code>
* @tparam JMAP any subtype of <code>java.util.Map</code>
* @return <code>Aggregating</code> of type <code>JMAP[K, V]</code>
*/
implicit def convertEqualityToJavaMapAggregating[K, V, JMAP[k, v] <: java.util.Map[k, v]](equality: Equality[java.util.Map.Entry[K, V]]): Aggregating[JMAP[K, V]] =
aggregatingNatureOfJavaMap(equality)
}
trait AggregatingStandardImplicits extends AggregatingJavaImplicits {
import scala.language.higherKinds
import scala.language.implicitConversions
/**
* Implicit to support <code>Aggregating</code> nature of <code>Array</code>.
*
* @param equality <a href="../../scalactic/Equality.html"><code>Equality</code></a> type class that is used to check equality of element in the <code>Array</code>
* @tparam E the type of the element in the <code>Array</code>
* @return <code>Aggregating[Array[E]]</code> that supports <code>Array</code> in relevant <code>contain</code> syntax
*/
implicit def aggregatingNatureOfArray[E](implicit equality: Equality[E]): Aggregating[Array[E]] =
new Aggregating[Array[E]] {
def containsAtLeastOneOf(array: Array[E], elements: scala.collection.Seq[Any]): Boolean = {
new ArrayWrapper(array).exists((e: E) => elements.exists((ele: Any) => equality.areEqual(e, ele)))
}
def containsTheSameElementsAs(array: Array[E], elements: GenTraversable[Any]): Boolean = {
checkTheSameElementsAs[E](new ArrayWrapper(array), elements, equality)
}
def containsOnly(array: Array[E], elements: scala.collection.Seq[Any]): Boolean = {
checkOnly(new ArrayWrapper(array), elements, equality)
}
def containsAllOf(array: Array[E], elements: scala.collection.Seq[Any]): Boolean = {
checkAllOf(new ArrayWrapper(array), elements, equality)
}
def containsAtMostOneOf(array: Array[E], elements: scala.collection.Seq[Any]): Boolean = {
checkAtMostOneOf(new ArrayWrapper(array), elements, equality)
}
}
/**
* Implicit conversion that converts an <a href="../../scalactic/Equality.html"><code>Equality</code></a> of type <code>E</code>
* into <code>Aggregating</code> of type <code>Array[E]</code>.
* This is required to support the explicit <a href="../../scalactic/Equality.html"><code>Equality</code></a> syntax, for example:
*
* <pre class="stHighlighted">
* (<span class="stType">Array</span>(<span class="stQuotedString">"hi"</span>) should contain (<span class="stQuotedString">"HI"</span>)) (after being lowerCased)
* </pre>
*
* <code>(after being lowerCased)</code> will returns an <a href="../../scalactic/Equality.html"><code>Equality[String]</code></a>
* and this implicit conversion will convert it into <code>Aggregating[Array[String]]</code>.
*
* @param equality <a href="../../scalactic/Equality.html"><code>Equality</code></a> of type <code>E</code>
* @tparam E type of elements in the <code>Array</code>
* @return <code>Aggregating</code> of type <code>Array[E]</code>
*/
implicit def convertEqualityToArrayAggregating[E](equality: Equality[E]): Aggregating[Array[E]] =
aggregatingNatureOfArray(equality)
/**
* Implicit to support <code>Aggregating</code> nature of <code>String</code>.
*
* @param equality <a href="../../scalactic/Equality.html"><code>Equality</code></a> type class that is used to check equality of <code>Char</code> in the <code>String</code>
* @return <code>Aggregating[String]</code> that supports <code>String</code> in relevant <code>contain</code> syntax
*/
implicit def aggregatingNatureOfString(implicit equality: Equality[Char]): Aggregating[String] =
new Aggregating[String] {
def containsAtLeastOneOf(s: String, elements: scala.collection.Seq[Any]): Boolean = {
s.exists((e: Char) => elements.exists((ele: Any) => equality.areEqual(e, ele)))
}
def containsTheSameElementsAs(s: String, elements: GenTraversable[Any]): Boolean = {
checkTheSameElementsAs(s, elements, equality)
}
def containsOnly(s: String, elements: scala.collection.Seq[Any]): Boolean = {
checkOnly(s, elements, equality)
}
def containsAllOf(s: String, elements: scala.collection.Seq[Any]): Boolean = {
checkAllOf(s, elements, equality)
}
def containsAtMostOneOf(s: String, elements: scala.collection.Seq[Any]): Boolean = {
checkAtMostOneOf(s, elements, equality)
}
}
/**
* Implicit conversion that converts an <a href="../../scalactic/Equality.html"><code>Equality</code></a> of type <code>Char</code>
* into <code>Aggregating</code> of type <code>String</code>.
* This is required to support the explicit <a href="../../scalactic/Equality.html"><code>Equality</code></a> syntax, for example:
*
* <pre class="stHighlighted">
* <span class="stLineComment">// lowerCased needs to be implemented as Normalization[Char]</span>
* (<span class="stQuotedString">"hi hello"</span> should contain (<span class="stQuotedString">'E'</span>)) (after being lowerCased)
* </pre>
*
* <code>(after being lowerCased)</code> will returns an <a href="../../scalactic/Equality.html"><code>Equality[Char]</code></a>
* and this implicit conversion will convert it into <code>Aggregating[String]</code>.
*
* @param equality <a href="../../scalactic/Equality.html"><code>Equality</code></a> of type <code>Char</code>
* @return <code>Aggregating</code> of type <code>String</code>
*/
implicit def convertEqualityToStringAggregating(equality: Equality[Char]): Aggregating[String] =
aggregatingNatureOfString(equality)
/**
* Implicit to support <code>Aggregating</code> nature of <code>Every</code>.
*
* @param equality <a href="../../scalactic/Equality.html"><code>Equality</code></a> type class that is used to check equality of element in the <code>Every</code>
* @tparam E the type of the element in the <code>Every</code>
* @return <code>Aggregating[Every[E]]</code> that supports <code>Every</code> in relevant <code>contain</code> syntax
*/
implicit def aggregatingNatureOfEvery[E](implicit equality: Equality[E]): Aggregating[Every[E]] =
new Aggregating[Every[E]] {
def containsAtLeastOneOf(every: Every[E], elements: scala.collection.Seq[Any]): Boolean = {
every.exists((e: E) => elements.exists((ele: Any) => equality.areEqual(e, ele)))
}
def containsTheSameElementsAs(every: Every[E], elements: GenTraversable[Any]): Boolean = {
checkTheSameElementsAs[E](every, elements, equality)
}
def containsOnly(every: Every[E], elements: scala.collection.Seq[Any]): Boolean = {
checkOnly(every, elements, equality)
}
def containsAllOf(every: Every[E], elements: scala.collection.Seq[Any]): Boolean = {
checkAllOf(every, elements, equality)
}
def containsAtMostOneOf(every: Every[E], elements: scala.collection.Seq[Any]): Boolean = {
checkAtMostOneOf(every, elements, equality)
}
}
/**
* Implicit conversion that converts an <a href="../../scalactic/Equality.html"><code>Equality</code></a> of type <code>E</code>
* into <code>Aggregating</code> of type <code>Every[E]</code>.
* This is required to support the explicit <a href="../../scalactic/Equality.html"><code>Equality</code></a> syntax, for example:
*
* <pre class="stHighlighted">
* (<span class="stType">Every</span>(<span class="stQuotedString">"hi"</span>) should contain (<span class="stQuotedString">"HI"</span>)) (after being lowerCased)
* </pre>
*
* <code>(after being lowerCased)</code> will returns an <a href="../../scalactic/Equality.html"><code>Equality[String]</code></a>
* and this implicit conversion will convert it into <code>Aggregating[Every[String]]</code>.
*
* @param equality <a href="../../scalactic/Equality.html"><code>Equality</code></a> of type <code>E</code>
* @tparam E type of elements in the <code>Every</code>
* @return <code>Aggregating</code> of type <code>Every[E]</code>
*/
implicit def convertEqualityToEveryAggregating[E](equality: Equality[E]): Aggregating[Every[E]] =
aggregatingNatureOfEvery(equality)
/**
* Implicit to support <code>Aggregating</code> nature of <code>GenTraversable</code>.
*
* @param equality <a href="../../scalactic/Equality.html"><code>Equality</code></a> type class that is used to check equality of element in the <code>GenTraversable</code>
* @tparam E the type of the element in the <code>GenTraversable</code>
* @tparam TRAV any subtype of <code>GenTraversable</code>
* @return <code>Aggregating[TRAV[E]]</code> that supports <code>GenTraversable</code> in relevant <code>contain</code> syntax
*/
implicit def aggregatingNatureOfGenTraversable[E, TRAV[e] <: scala.collection.GenTraversable[e]](implicit equality: Equality[E]): Aggregating[TRAV[E]] =
new Aggregating[TRAV[E]] {
def containsAtLeastOneOf(trav: TRAV[E], elements: scala.collection.Seq[Any]): Boolean = {
trav.exists((e: E) => elements.exists((ele: Any) => equality.areEqual(e, ele)))
}
def containsTheSameElementsAs(trav: TRAV[E], elements: GenTraversable[Any]): Boolean = {
checkTheSameElementsAs[E](trav, elements, equality)
}
def containsOnly(trav: TRAV[E], elements: scala.collection.Seq[Any]): Boolean = {
checkOnly[E](trav, elements, equality)
}
def containsAllOf(trav: TRAV[E], elements: scala.collection.Seq[Any]): Boolean = {
checkAllOf(trav, elements, equality)
}
def containsAtMostOneOf(trav: TRAV[E], elements: scala.collection.Seq[Any]): Boolean = {
checkAtMostOneOf(trav, elements, equality)
}
}
/**
* Implicit conversion that converts an <a href="../../scalactic/Equality.html"><code>Equality</code></a> of type <code>E</code>
* into <code>Aggregating</code> of type <code>TRAV[E]</code>, where <code>TRAV</code> is a subtype of <code>GenTraversable</code>.
* This is required to support the explicit <a href="../../scalactic/Equality.html"><code>Equality</code></a> syntax, for example:
*
* <pre class="stHighlighted">
* (<span class="stType">List</span>(<span class="stQuotedString">"hi"</span>) should contain (<span class="stQuotedString">"HI"</span>)) (after being lowerCased)
* </pre>
*
* <code>(after being lowerCased)</code> will returns an <a href="../../scalactic/Equality.html"><code>Equality[String]</code></a>
* and this implicit conversion will convert it into <code>Aggregating[List[String]]</code>.
*
* @param equality <a href="../../scalactic/Equality.html"><code>Equality</code></a> of type <code>E</code>
* @tparam E type of elements in the <code>GenTraversable</code>
* @tparam TRAV subtype of <code>GenTraversable</code>
* @return <code>Aggregating</code> of type <code>TRAV[E]</code>
*/
implicit def convertEqualityToGenTraversableAggregating[E, TRAV[e] <: scala.collection.GenTraversable[e]](equality: Equality[E]): Aggregating[TRAV[E]] =
aggregatingNatureOfGenTraversable(equality)
}
trait AggregatingHighPriorityImplicits extends AggregatingStandardImplicits {
/**
* Implicit to support <code>Aggregating</code> nature of <code>scala.collection.GenMap</code>.
*
* @param equality <a href="../../scalactic/Equality.html"><code>Equality</code></a> type class that is used to check equality of entry in the <code>scala.collection.GenMap</code>
* @tparam K the type of the key in the <code>scala.collection.GenMap</code>
* @tparam V the type of the value in the <code>scala.collection.GenMap</code>
* @tparam MAP any subtype of <code>scala.collection.GenMap</code>
* @return <code>Aggregating[MAP[K, V]]</code> that supports <code>scala.collection.GenMap</code> in relevant <code>contain</code> syntax
*/
implicit def aggregatingNatureOfMap[K, V, MAP[k, v] <: scala.collection.GenMap[k, v]](implicit equality: Equality[(K, V)]): Aggregating[MAP[K, V]] =
new Aggregating[MAP[K, V]] {
def containsAtLeastOneOf(map: MAP[K, V], elements: scala.collection.Seq[Any]): Boolean = {
map.exists((e: (K, V)) => elements.exists((ele: Any) => equality.areEqual(e, ele)))
}
def containsTheSameElementsAs(map: MAP[K, V], elements: GenTraversable[Any]): Boolean = {
checkTheSameElementsAs(map, elements, equality)
}
def containsOnly(map: MAP[K, V], elements: scala.collection.Seq[Any]): Boolean = {
checkOnly(map, elements, equality)
}
def containsAllOf(map: MAP[K, V], elements: scala.collection.Seq[Any]): Boolean = {
checkAllOf(map, elements, equality)
}
def containsAtMostOneOf(map: MAP[K, V], elements: scala.collection.Seq[Any]): Boolean = {
checkAtMostOneOf(map, elements, equality)
}
}
/**
* Implicit conversion that converts an <a href="../../scalactic/Equality.html"><code>Equality</code></a> of type <code>Tuple2[K, V]</code>
* into <code>Aggregating</code> of type <code>MAP[K, V]</code>, where <code>MAP</code> is a subtype of <code>scala.collection.GenMap</code>.
* This is required to support the explicit <a href="../../scalactic/Equality.html"><code>Equality</code></a> syntax, for example:
*
* <pre class="stHighlighted">
* <span class="stReserved">val</span> map = <span class="stType">Map</span>(<span class="stLiteral">1</span> -> <span class="stQuotedString">"one"</span>)
* <span class="stLineComment">// lowerCased needs to be implemented as Normalization[Tuple2[K, V]]</span>
* (map should contain ((<span class="stLiteral">1</span>, <span class="stQuotedString">"ONE"</span>))) (after being lowerCased)
* </pre>
*
* <code>(after being lowerCased)</code> will returns an <a href="../../scalactic/Equality.html"><code>Tuple2[Int, String]</code></a>
* and this implicit conversion will convert it into <code>Aggregating[scala.collection.GenMap[Int, String]]</code>.
*
* @param equality <a href="../../scalactic/Equality.html"><code>Equality</code></a> of type <code>Tuple2[K, V]</code>
* @tparam K the type of the key in the <code>scala.collection.GenMap</code>
* @tparam V the type of the value in the <code>scala.collection.GenMap</code>
* @tparam MAP any subtype of <code>scala.collection.GenMap</code>
* @return <code>Aggregating</code> of type <code>MAP[K, V]</code>
*/
implicit def convertEqualityToMapAggregating[K, V, MAP[k, v] <: scala.collection.GenMap[k, v]](equality: Equality[(K, V)]): Aggregating[scala.collection.GenMap[K, V]] =
aggregatingNatureOfMap(equality)
}
/**
* Companion object for <code>Aggregating</code> that provides implicit implementations for the following types:
*
* <ul>
* <li><code>scala.collection.GenTraversable</code></li>
* <li><code>String</code></li>
* <li><code>Array</code></li>
* <li><code>java.util.Collection</code></li>
* <li><code>java.util.Map</code></li>
* </ul>
*/
object Aggregating extends AggregatingHighPriorityImplicits