1
1
/*
2
- Copyright 2009-2014 Igor Polevoy
2
+ Copyright 2009-2015 Igor Polevoy
3
3
4
- Licensed under the Apache License, Version 2.0 (the "License");
5
- you may not use this file except in compliance with the License.
6
- You may obtain a copy of the License at
4
+ Licensed under the Apache License, Version 2.0 (the "License");
5
+ you may not use this file except in compliance with the License.
6
+ You may obtain a copy of the License at
7
7
8
- http://www.apache.org/licenses/LICENSE-2.0
8
+ http://www.apache.org/licenses/LICENSE-2.0
9
9
10
- Unless required by applicable law or agreed to in writing, software
11
- distributed under the License is distributed on an "AS IS" BASIS,
12
- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
- See the License for the specific language governing permissions and
14
- limitations under the License.
10
+ Unless required by applicable law or agreed to in writing, software
11
+ distributed under the License is distributed on an "AS IS" BASIS,
12
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ See the License for the specific language governing permissions and
14
+ limitations under the License.
15
15
*/
16
-
17
-
18
16
package org .javalite .test .jspec ;
19
17
import java .lang .reflect .Method ;
18
+ import java .util .Collection ;
20
19
import java .util .List ;
21
20
import java .util .Map ;
22
21
23
22
import static org .javalite .common .Inflector .capitalize ;
24
23
24
+ /**
25
+ * @author Igor Polevoy
26
+ * @author Eric Nielsen
27
+ */
25
28
public class Expectation <T > {
26
29
27
30
private final T actual ;
@@ -40,30 +43,42 @@ public void shouldEqual(T expected){
40
43
}
41
44
42
45
/**
43
- * Tested value is equal expected.
46
+ * Tested value is equal expected.
44
47
*
45
48
* @param expected expected value.
46
49
*/
47
50
public void shouldBeEqual (T expected ) {
48
51
checkNull ();
49
- String expectedName = expected == null ? "null" :expected .getClass ().getName ();
50
- String actualName = actual == null ? "null" :actual .getClass ().getName ();
51
-
52
- TestException te = new TestException ("\n Test object: \n " +
53
- actualName + " == <" + actual + "> \n " +
54
- "and expected\n " +
55
- expectedName + " == <" + expected + "> \n are not equal, but they should be." );
56
-
57
-
58
- if (actual == null && expected != null || actual != null && expected == null )
59
- throw te ;
52
+ if (actual == null ) {
53
+ if (expected != null ) { throw newShouldBeEqualException (expected ); }
54
+ } else {
55
+ if (expected == null ) { throw newShouldBeEqualException (expected ); }
56
+ //TODO: improve Number comparison, see http://stackoverflow.com/questions/2683202/comparing-the-values-of-two-generic-numbers
57
+ if (actual instanceof Number && expected instanceof Number ) {
58
+ if (((Number ) actual ).doubleValue () != ((Number ) expected ).doubleValue ()) {
59
+ throw newShouldBeEqualException (expected );
60
+ }
61
+ } else if (!actual .equals (expected )) {
62
+ throw newShouldBeEqualException (expected );
63
+ }
64
+ }
65
+ }
60
66
61
- if (actual instanceof Number && expected instanceof Number ) {
62
- Double t1 = ((Number ) actual ).doubleValue ();
63
- Double t2 = ((Number ) expected ).doubleValue ();
64
- if (!t1 .equals (t2 ))
65
- throw te ;
66
- } else if (!actual .equals (expected )) throw te ;
67
+ private TestException newShouldBeEqualException (T expected ) {
68
+ StringBuilder sb = new StringBuilder ().append ("Test object:\n " );
69
+ if (actual == null ) {
70
+ sb .append ("null" );
71
+ } else {
72
+ sb .append (actual .getClass ().getName ()).append (" == <" ).append (actual ).append (">\n " );
73
+ }
74
+ sb .append ("and expected\n " );
75
+ if (expected == null ) {
76
+ sb .append ("null" );
77
+ } else {
78
+ sb .append (expected .getClass ().getName ()).append (" == <" ).append (expected ).append (">\n " );
79
+ }
80
+ sb .append ("are not equal, but they should be." );
81
+ return new TestException (sb .toString ());
67
82
}
68
83
69
84
/**
0 commit comments