Skip to content

Commit 0f15df6

Browse files
author
David Reed
committed
Adding a simple AboutEquality suite to explain '==' and '.equals()' because they are used in later koans
1 parent da15227 commit 0f15df6

5 files changed

Lines changed: 60 additions & 4 deletions

File tree

‎koans/config/PathToEnlightenment.xml‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,9 @@
77
</suite>
88
<suite class="AboutAssertions"/>
99
<!-- TODO: AboutDataTypes... -->
10-
<suite class="AboutObjects"/>
10+
<suite class="AboutEquality"/>
1111
<suite class="AboutStrings"/>
12+
<suite class="AboutObjects"/>
1213
<suite class="AboutLoops" />
1314
<suite class="AboutCasting"/>
1415
<suite class="AboutConditionals"/>

‎koans/i18n/messages_en.properties‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@ beginner.AboutAssertions.assertBooleanTrue=The __ are an attempt to communicate
55
beginner.AboutAssertions.assertBooleanFalse=Like the prior koan. Ponder if you will, the power of simple assertions when verifying an object's behavior.
66
beginner.AboutAssertions.assertNullObject=A keyword in java to represent an uninitialized reference is 'null'. There are times when something should be null, and this assertion can prove that.
77
beginner.AboutAssertions.assertNotNullObject=Sometimes you merely wish to assert an object is not null. This assertion should be used sparingly, often a more specific assertion is appropriate.
8-
beginner.AboutAssertions.assertEqualsWithDescriptiveMessage=Like the prior assertions, only this one invokes equals method on the 2nd to last argument, in this case, 1. This will blow up if the last two arguments are not .equal(...)
8+
beginner.AboutAssertions.assertEqualsUsingExpression=You can use expressions inside the assertion to calculate the value you are asserting on.
9+
beginner.AboutAssertions.assertEqualsWithBetterFailureMessage=However if there is a specific assertion for the .equals() method that gives a much nicer failure message. (Compare its failure to the previous koan's failure)
10+
beginner.AboutAssertions.assertEqualsWithDescriptiveMessage=You can add your own message to an assertXXX() method to provide more information if it is relevant.
911
beginner.AboutAssertions.assertSameInstance=An object may equal another object, but it will never be the same as another object.
1012
beginner.AboutAssertions.assertNotSameInstance=Now we can see that two references to the same object is not the same as two references to two equal objects.
1113

‎koans/src/beginner/AboutAssertions.java‎

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,23 @@ public void assertNullObject(){
3333
public void assertNotNullObject(){
3434
assertNotNull(null); // anything other than null should pass here...
3535
}
36-
36+
37+
@Koan()
38+
public void assertEqualsUsingExpression(){
39+
assertTrue("Hello World!".equals(__));
40+
}
41+
42+
@Koan()
43+
public void assertEqualsWithBetterFailureMessage(){
44+
assertEquals(1, __);
45+
}
46+
3747
@Koan()
3848
public void assertEqualsWithDescriptiveMessage() {
3949
// Generally, when using an assertXXX methods, expectation is on the
4050
// left and it is best practice to use a String for the first arg
4151
// indication what has failed
42-
assertEquals("1 should equal 1", 1, __);
52+
assertEquals("The answer to 'life the universe and everything' should be 42", 42, __);
4353
}
4454

4555
@Koan()
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package beginner;
2+
3+
import com.sandwich.koan.Koan;
4+
5+
import static com.sandwich.koan.constant.KoanConstants.__;
6+
import static com.sandwich.util.Assert.*;
7+
8+
public class AboutEquality {
9+
10+
@Koan()
11+
public void doubleEqualsTestsIfTwoObjectsAreTheSame(){
12+
Object object = new Object();
13+
assertEquals(object == object, __);
14+
assertEquals(object == new Object(), __);
15+
}
16+
17+
@Koan()
18+
public void equalsMethodByDefaultTestsIfTwoObjectsAreTheSame(){
19+
Object object = new Object();
20+
assertEquals(object.equals(object), __);
21+
assertEquals(object.equals(new Object()), __);
22+
}
23+
24+
@Koan()
25+
public void equalsMethodCanBeChangedBySubclassesToTestsIfTwoObjectsAreEqual(){
26+
Object object = new Integer(1);
27+
assertEquals(object.equals(object), __);
28+
assertEquals(object.equals(new Integer(1)), __);
29+
// Note: This means that for the class 'Object' there is no difference between 'equal' and 'same'
30+
}
31+
32+
@Koan()
33+
public void objectsNeverEqualNull(){
34+
assertEquals(new Object().equals(null), __);
35+
}
36+
37+
@Koan()
38+
public void objectsEqualThemselves(){
39+
Object obj = new Object();
40+
assertEquals(obj.equals(obj), __);
41+
}
42+
}

‎koans/src/intermediate/AboutEquality.java‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import static com.sandwich.util.Assert.assertEquals;
66

77
public class AboutEquality {
8+
// This suite of Koans expands on the concepts introduced in beginner.AboutEquality
89

910
@Koan
1011
public void sameObject() {

0 commit comments

Comments
 (0)