Skip to content

Commit 557e507

Browse files
author
David Reed
committed
Removing tests that are covered in beginner.AboutEquality + adding test to show all classes implicitly inheirit from Object
1 parent 4d358a1 commit 557e507

3 files changed

Lines changed: 35 additions & 46 deletions

File tree

‎koans-lib/src/com/sandwich/util/TypeUtils.java‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
package com.sandwich.util;
22

3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
import static com.sandwich.util.Assert.fail;
7+
38
public class TypeUtils {
49

510
public static Class<?> getType(int value) {
@@ -34,4 +39,13 @@ public static Class<?> getType(Object value) {
3439
return value.getClass();
3540
}
3641

42+
public static Class[] ancestors(Object object) {
43+
List<Class> ancestors = new ArrayList<Class>();
44+
Class clazz = object.getClass();
45+
while(clazz != null) {
46+
ancestors.add(clazz);
47+
clazz = clazz.getSuperclass();
48+
}
49+
return ancestors.toArray(new Class[]{});
50+
}
3751
}

‎koans/lib/koans.jar‎

-171 Bytes
Binary file not shown.
Lines changed: 21 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,63 +1,37 @@
11
package beginner;
22

3-
import static com.sandwich.koan.constant.KoanConstants.__;
4-
import static com.sandwich.util.Assert.assertEquals;
5-
63
import 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+
811
public 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

Comments
 (0)