11package beginner ;
22
3- import static com .sandwich .koan .constant .KoanConstants .__ ;
4- import static com .sandwich .util .Assert .assertEquals ;
5-
63import com .sandwich .koan .Koan ;
74
5+ import java .text .MessageFormat ;
6+
7+ import static com .sandwich .koan .constant .KoanConstants .*;
8+ import static com .sandwich .util .Assert .*;
9+ import static com .sandwich .util .TypeUtils .*;
10+
811public class AboutObjects {
912
1013 @ Koan ()
11- public void objectEqualsNull (){
12- // does a new object instance equal the null keyword?
13- assertEquals (new Object ().equals (null ), __ );
14- }
15-
16- @ Koan ()
17- public void objectEqualsSelf (){
18- Object obj = new Object ();
19- // does a new object equal itself?
20- assertEquals (obj .equals (obj ), __ );
21- }
22-
23- @ Koan ()
24- public void objectIdentityEqualityIsTrueWhenReferringToSameObject (){
25- Object objectReference = new Object ();
26- Object referenceToSameObject = objectReference ;
27- // does a new object == itself?
28- assertEquals (objectReference == referenceToSameObject , __ );
29- }
30-
31- @ Koan ()
32- public void subclassesEqualsMethodIsLooserThanDoubleEquals (){
33- Integer integer0 = new Integer (0 );
34- Integer integer1 = new Integer (0 );
35- assertEquals (integer0 .equals (integer1 ), __ );
36- }
37-
38- @ Koan ()
39- public void doubleEqualsOperatorEvaluatesToTrueOnlyWithSameInstance (){
40- Integer integer0 = new Integer (0 );
41- Integer integer1 = integer0 ; // <- assigning same instance to different reference
42- assertEquals (integer0 == integer1 , __ );
14+ public void newObjectInstancesCanBeCreatedDirectly () {
15+ assertEquals (new Object () instanceof Object , __ );
4316 }
44-
17+
4518 @ Koan ()
46- public void doubleEqualsOperatorEvaluatesToFalseWithDifferentInstances (){
47- Integer integer0 = new Integer (0 );
48- Integer integer1 = new Integer (0 ); // <- new keyword is generating new object instance
49- assertEquals (integer0 == integer1 , __ );
19+ public void allClassesInheritFromObject () {
20+ class Foo {}
21+
22+ Class [] ancestors = ancestors (new Foo ());
23+ assertEquals (ancestors [0 ], __ );
24+ assertEquals (ancestors [1 ], __ );
5025 }
51-
26+
5227 @ Koan ()
53- public void objectToString (){
28+ public void objectToString () {
5429 Object object = new Object ();
5530 // TODO: Why is it best practice to ALWAYS override toString?
56- assertEquals ((new StringBuilder ()).append (Object .class .getName ())
57- .append ('@' )
58- .append (Integer .toHexString (object .hashCode ())).toString (), __ ); //object.toString()
31+ String expectedToString = MessageFormat .format ("{0}@{1}" , Object .class .getName (), Integer .toHexString (object .hashCode ()));
32+ assertEquals (expectedToString , __ ); // hint: object.toString()
5933 }
60-
34+
6135 @ Koan ()
6236 public void toStringConcatenates (){
6337 final String string = "ha" ;
@@ -74,4 +48,5 @@ public void toStringIsTestedForNullWhenInvokedImplicitly(){
7448 String string = "string" ;
7549 assertEquals (string +null , __ );
7650 }
51+
7752}
0 commit comments