-
Notifications
You must be signed in to change notification settings - Fork 2
/
DefaultEquality.scala
77 lines (67 loc) · 3.15 KB
/
DefaultEquality.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
/*
* 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.scalactic
import org.scalactic.anyvals.NonEmptyArray
/**
* A default <code>Equality</code> type class implementation (which can be used for any type) whose
* <code>areEqual</code> method compares the passed objects with <code>==</code>, calling <code>.deep</code>
* first on any passed object that is an array.
* </p>
*/
private[scalactic] final class DefaultEquality[A] extends Equality[A] {
/**
* Indicates whether the objects passed as <code>a</code> and <code>b</code> are equal by invoking <code>==</code> on <code>a</code>
* passing in <code>b</code>, treating arrays specially by invoking <code>.deep</code> on <code>a</code> and/or </code>b</code> if they
* are arrays, and using the result or results of invoking <code>.deep</code> in the equality check.
*
* @param a a left-hand-side object being compared with another (right-hand-side one) for equality (<em>e.g.</em>, <code>a == b</code>)
* @param b a right-hand-side object being compared with another (left-hand-side one) for equality (<em>e.g.</em>, <code>a == b</code>)
*/
def areEqual(a: A, b: Any): Boolean = {
DefaultEquality.areEqualComparingArraysStructurally(a, b)
}
override def toString: String = "Equality.default"
}
object DefaultEquality {
import ArrayHelper.deep
private[org] def areEqualComparingArraysStructurally(left: Any, right: Any): Boolean = {
// Prior to 2.0 this only called .deep if both sides were arrays. Loosened it
// when nearing 2.0.M6 to call .deep if either left or right side is an array.
// TODO: this is the same algo as in scalactic.DefaultEquality. Put that one in
// a singleton and use it in both places.
left match {
case leftArray: Array[_] =>
right match {
case rightArray: Array[_] => deep(leftArray) == deep(rightArray)
case rightNonEmptyArray: NonEmptyArray[_] => deep(leftArray) == deep(rightNonEmptyArray.toArray)
case _ => deep(leftArray) == right
}
case leftNonEmptyArray: NonEmptyArray[_] =>
right match {
case rightArray: Array[_] => deep(leftNonEmptyArray.toArray) == deep(rightArray)
case rightNonEmptyArray: NonEmptyArray[_] => deep(leftNonEmptyArray.toArray) == deep(rightNonEmptyArray.toArray)
case _ => deep(leftNonEmptyArray.toArray) == right
}
case other => {
right match {
case rightArray: Array[_] => left == deep(rightArray)
case rightNonEmptyArray: NonEmptyArray[_] => left == deep(rightNonEmptyArray.toArray)
case _ => left == right
}
}
}
}
}