Skip to content

Commit 5b5a816

Browse files
committed
general cleanup, tests passing again, added attribute & code to conceal a thrown Exceptions output as a suggestion in the console
1 parent 71c53d1 commit 5b5a816

27 files changed

Lines changed: 231 additions & 158 deletions

‎koans-lib/src/com/sandwich/koan/KoanConstants.java‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,13 @@
66
public abstract class KoanConstants {
77

88
private KoanConstants(){}
9+
10+
public static final boolean ENABLE_ENCOURAGEMENT= false;
911

1012
public static final String __ = "REPLACE ME";
1113
public static final String PATH_XML_NAME = "PathToEnlightment.xml";
12-
public static final String PATH_XML = "src/"; // TODO: Adapt so osx and windows dont need to
13-
// public static final String PATH_XML = "";
14+
// public static final String PATH_XML = "src/"; // TODO: Adapt so osx and windows dont need to
15+
public static final String PATH_XML = "";
1416
public static final String PATH_XML_LOCATION = PATH_XML+PATH_XML_NAME;
1517

1618
public static final String EOL = System.getProperty("line.separator");
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.sandwich.koan;
2+
3+
public class KoanIncompleteException extends AssertionError {
4+
5+
private static final long serialVersionUID = -4207677767865451508L;
6+
7+
public KoanIncompleteException(String message){
8+
super(message);
9+
}
10+
11+
@Override public String toString(){
12+
return super.getMessage();
13+
}
14+
15+
}

‎koans-lib/src/com/sandwich/koan/KoanMethod.java‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,22 @@
55
import com.sandwich.koan.path.XmlVariableInjector;
66

77
public class KoanMethod {
8+
89
private final transient Method method;
910
private final String lesson;
11+
private final boolean displayIncompleteException;
12+
1013
public KoanMethod(String lesson, Method method){
14+
this(lesson,method,true);
15+
}
16+
17+
public KoanMethod(String lesson, Method method, boolean displayIncompleteException){
1118
if(method == null){
1219
throw new IllegalArgumentException("method may not be null");
1320
}
1421
this.method = method;
1522
this.lesson = new XmlVariableInjector(lesson, method).injectLessonVariables();
23+
this.displayIncompleteException = displayIncompleteException;
1624
}
1725

1826
public String getLesson() {
@@ -23,6 +31,10 @@ public Method getMethod() {
2331
return method;
2432
}
2533

34+
public boolean displayIncompleteException() {
35+
return displayIncompleteException;
36+
}
37+
2638
@Override public String toString(){
2739
return "{"+getMethod().getName()
2840
+" : "+ (lesson.length() > 20 ? lesson.substring(0, 20) + "..." : lesson)+"}";

‎koans-lib/src/com/sandwich/koan/KoanResult.java‎

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,13 @@
33
import java.util.Collections;
44
import java.util.List;
55

6+
import com.sandwich.util.Builder;
7+
68
public class KoanResult {
79

810
private final KoanResultModel state;
911

10-
private static class KoanResultModel {
12+
static class KoanResultModel {
1113
String level;
1214
int numberPassing = -1;
1315
int totalNumberOfKoanMethods = -1;
@@ -33,7 +35,7 @@ public String toString() {
3335
this.state = state;
3436
}
3537

36-
public static class KoanResultBuilder implements com.sandwich.util.Builder<KoanResult>{
38+
public static class KoanResultBuilder implements Builder<KoanResult>{
3739

3840
private final KoanResultModel state = new KoanResultModel();
3941

@@ -121,6 +123,10 @@ public String getLevel() {
121123
public String toString() {
122124
return state.toString();
123125
}
126+
127+
public boolean displayIncompleteException() {
128+
return getFailingMethod() == null || getFailingMethod().displayIncompleteException();
129+
}
124130

125131

126132
}

‎koans-lib/src/com/sandwich/koan/path/PathToEnlightenment.java‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
import com.sandwich.koan.KoanConstants;
1414
import com.sandwich.koan.KoanMethod;
15+
import com.sandwich.koan.path.XmlToPathTransformer.KoanElementAttributes;
1516

1617
public abstract class PathToEnlightenment {
1718

@@ -100,12 +101,12 @@ static void stagePathToEnlightenment(Object koanSuite) {
100101
PathToEnlightenment.theWay = new Path(koans);
101102
}
102103

103-
private static Map<String, String> createBogusLessonMap(
104+
private static Map<String, KoanElementAttributes> createBogusLessonMap(
104105
Class<? extends Object> clazz) {
105-
Map<String, String> bogusLessonByKoanMap = new HashMap<String, String>();
106+
Map<String, KoanElementAttributes> bogusLessonByKoanMap = new HashMap<String, KoanElementAttributes>();
106107
String fakeLesson = "for testing - no real lesson";
107108
for(Method m : clazz.getMethods()){
108-
bogusLessonByKoanMap.put(m.getName(), fakeLesson);
109+
bogusLessonByKoanMap.put(m.getName(), new KoanElementAttributes(fakeLesson, m.getName(), "true"));
109110
}
110111
return bogusLessonByKoanMap;
111112
}

‎koans-lib/src/com/sandwich/koan/path/XmlToPathTransformer.java‎

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -72,30 +72,34 @@ static Map<Object, List<KoanMethod>> createSuitesAndKoans(String pkg, NodeList c
7272
}
7373

7474
static List<KoanMethod> createKoanMethods(Class<?> koanSuiteClass, NodeList nodes) {
75-
Map<String, String> rawKoanLessonByMethodName = extractKoansAndRawLessons(koanSuiteClass.getName(), nodes);
75+
Map<String, KoanElementAttributes> rawKoanLessonByMethodName = extractKoansAndRawLessons(koanSuiteClass.getName(), nodes);
7676
List<KoanMethod> koanMethods = getKoanMethods(koanSuiteClass, rawKoanLessonByMethodName);
7777
KoanComparator koanComparator = new KoanComparator(rawKoanLessonByMethodName.keySet());
7878
Collections.sort(koanMethods, koanComparator);
7979
assertXmlAndCodeAreInSynch(nodes, koanMethods, koanComparator);
8080
return koanMethods;
8181
}
8282

83-
static Map<String, String> extractKoansAndRawLessons(
83+
static Map<String, KoanElementAttributes> extractKoansAndRawLessons(
8484
String className, NodeList nodes) {
85-
Map<String, String> rawKoanLessonByMethodName = new LinkedHashMap<String, String>();
85+
Map<String, KoanElementAttributes> rawKoanAttributesByMethodName = new LinkedHashMap<String, KoanElementAttributes>();
8686
for(int i = 0; i < nodes.getLength(); i++){
8787
Node node = nodes.item(i);
8888
if("Koan".equalsIgnoreCase(node.getNodeName())){
8989
NamedNodeMap attributes = node.getAttributes();
9090
String name = attributes.getNamedItem("name").getNodeValue();
9191
String rawLesson = attributes.getNamedItem("lesson").getNodeValue();
92-
if(rawKoanLessonByMethodName.containsKey(name)){
92+
Node displayKoanIncompleteExceptionNode = attributes.getNamedItem("displayIncompleteKoanException");
93+
String displayIncompleteKoanException = displayKoanIncompleteExceptionNode == null ?
94+
null : displayKoanIncompleteExceptionNode.getNodeValue();
95+
if(rawKoanAttributesByMethodName.containsKey(name)){
9396
throw new DuplicateKoanException(className, name);
9497
}
95-
rawKoanLessonByMethodName.put(name, rawLesson);
98+
rawKoanAttributesByMethodName.put(name, new KoanElementAttributes(
99+
rawLesson, name, displayIncompleteKoanException));
96100
}
97101
}
98-
return rawKoanLessonByMethodName;
102+
return rawKoanAttributesByMethodName;
99103
}
100104

101105
static class DuplicateKoanException extends RuntimeException {
@@ -117,15 +121,28 @@ static void assertXmlAndCodeAreInSynch(NodeList csvOrder,
117121
}
118122
}
119123

120-
static List<KoanMethod> getKoanMethods(Class<?> koanSuiteClass, Map<String, String> lessonByKoanName) {
124+
static List<KoanMethod> getKoanMethods(Class<?> koanSuiteClass, Map<String, KoanElementAttributes> attributesByKoanName) {
121125
List<KoanMethod> koanMethods = new ArrayList<KoanMethod>();
122126
for(final Method koan : koanSuiteClass.getMethods()){
123127
final Koan annotation = koan.getAnnotation(Koan.class);
124128
if(annotation != null){
125-
koanMethods.add(new KoanMethod(lessonByKoanName.get(koan.getName()), koan));
129+
KoanElementAttributes koanMethodAttributes = attributesByKoanName.get(koan.getName());
130+
String incompleteKoanException = koanMethodAttributes.displayIncompleteKoanException;
131+
boolean displayIncompleteKoanException = incompleteKoanException == null ||
132+
"true".equalsIgnoreCase(incompleteKoanException.trim());
133+
koanMethods.add(new KoanMethod(koanMethodAttributes.lesson, koan, displayIncompleteKoanException));
126134
}
127135
}
128136
return koanMethods;
129137
}
138+
139+
static class KoanElementAttributes{
140+
String lesson, name, displayIncompleteKoanException;
141+
public KoanElementAttributes(String lesson, String name, String displayIncompleteKoanException){
142+
this.lesson = lesson;
143+
this.name = name;
144+
this.displayIncompleteKoanException = displayIncompleteKoanException;
145+
}
146+
}
130147
}
131148

‎koans-lib/src/com/sandwich/koan/runner/KoanSuiteRunner.java‎

Lines changed: 45 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
package com.sandwich.koan.runner;
22

3+
import static com.sandwich.koan.KoanConstants.EOLS;
34
import static com.sandwich.koan.KoanConstants.EXPECTATION_LEFT_ARG;
45
import static com.sandwich.koan.KoanConstants.EXPECTED_LEFT;
56
import static com.sandwich.koan.KoanConstants.EXPECTED_RIGHT;
67
import static com.sandwich.koan.KoanConstants.__;
78

89
import java.io.IOException;
10+
import java.io.PrintWriter;
11+
import java.io.StringWriter;
912
import java.lang.reflect.InvocationTargetException;
1013
import java.lang.reflect.Method;
1114
import java.util.ArrayList;
@@ -14,14 +17,15 @@
1417
import java.util.Map.Entry;
1518
import java.util.logging.Logger;
1619

20+
import com.sandwich.koan.KoanConstants;
21+
import com.sandwich.koan.KoanIncompleteException;
1722
import com.sandwich.koan.KoanMethod;
1823
import com.sandwich.koan.KoanResult;
1924
import com.sandwich.koan.KoanResult.KoanResultBuilder;
2025
import com.sandwich.koan.path.PathToEnlightenment;
2126
import com.sandwich.koan.path.PathToEnlightenment.Path;
2227
import com.sandwich.koan.ui.ConsolePresenter;
2328
import com.sandwich.koan.ui.SuitePresenter;
24-
import com.sandwich.util.OsSpecificHelper;
2529

2630
public class KoanSuiteRunner {
2731

@@ -93,18 +97,51 @@ KoanResult runKoans()
9397
}
9498
}
9599
}
96-
String message = failure instanceof AssertionError ? failure.getMessage() : null;
100+
// only display message if it is from a KoanIncompleteException
101+
String message = failure != null && KoanIncompleteException.class.isAssignableFrom(failure.getClass()) ?
102+
failure.getMessage() : null;
97103
if(message != null && message.contains(EXPECTED_LEFT+__+EXPECTED_RIGHT)){
98104
logExpectationOnWrongSideWarning(firstFailingSuite, firstFailingMethod.getMethod());
99105
}
100-
return new KoanResultBuilder().level(level)
101-
.numberPassing(successfull).totalNumberOfKoanMethods(path.getTotalNumberOfKoans())
102-
.failingCase(firstFailingSuite).failingMethod(firstFailingMethod)
103-
.message(message).lineNumber(
104-
OsSpecificHelper.getOriginalLineNumber(failure, firstFailingSuite))
105-
.passingCases(passingSuites).build();
106+
return new KoanResultBuilder()
107+
.level(level)
108+
.numberPassing(successfull)
109+
.totalNumberOfKoanMethods(path.getTotalNumberOfKoans())
110+
.failingCase(firstFailingSuite)
111+
.failingMethod(firstFailingMethod)
112+
.message(message)
113+
.lineNumber(getOriginalLineNumber(failure, firstFailingSuite))
114+
.passingCases(passingSuites)
115+
.remainingCases(failingSuites)
116+
.build();
106117
}
107118

119+
/**
120+
* Return the line number found closest to the point of failure, with a reference to the
121+
* failing suite's classname.
122+
*
123+
* @param t
124+
* @param failingSuite
125+
* @return
126+
*/
127+
static String getOriginalLineNumber(Throwable t, Class<?> failingSuite){
128+
StringWriter stringWriter = new StringWriter();
129+
if(t == null){
130+
return null;
131+
}
132+
t.printStackTrace(new PrintWriter(stringWriter));
133+
String[] lines = stringWriter.toString().split(EOLS);
134+
for(int i = lines.length - 1; i >= 0; --i){
135+
String line = lines[i];
136+
if(line.contains(failingSuite.getName())){
137+
return line.substring(
138+
line.indexOf(KoanConstants.LINE_NO_START)+KoanConstants.LINE_NO_START.length(),
139+
line.lastIndexOf(KoanConstants.LINE_NO_END));
140+
}
141+
}
142+
return null;
143+
}
144+
108145
/**
109146
* permit forwarding by overriding classes
110147
* @return

‎koans-lib/src/com/sandwich/koan/ui/ConsolePresenter.java‎

Lines changed: 49 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
import com.sandwich.koan.KoanResult;
2323

2424
public class ConsolePresenter extends AbstractSuitePresenter {
25-
25+
2626
protected void displayHeader(KoanResult result){
2727
}
2828

@@ -76,43 +76,65 @@ protected void displayAllSuccess(KoanResult result) {
7676
protected void displayOneOrMoreFailure(KoanResult result) {
7777
printSuggestion(result);
7878
String message = result.getMessage();
79-
System.out.print(message == null || message.length() == 0 ? ""
79+
StringBuilder sb = new StringBuilder(
80+
message == null || message.length() == 0 || !result.displayIncompleteException() ? ""
8081
: new StringBuilder(WHATS_WRONG).append(
8182
EOL).append(
82-
message).toString());
83-
int totalKoans = result.getTotalNumberOfKoans();
84-
int numberPassing = result.getNumberPassing();
85-
System.out.println(new StringBuilder(CONQUERED).append(
86-
" ").append(
87-
numberPassing).append(
88-
" ").append(
89-
OUT_OF).append(
90-
" ").append(
91-
totalKoans).append(
92-
" ").append(
93-
KOAN).append(
94-
totalKoans != 1 ? 's' : "").append(
95-
"! ").append(ENCOURAGEMENT).append(
96-
EOL).toString());
83+
message).append(
84+
EOL).append(
85+
EOL));
86+
if(KoanConstants.ENABLE_ENCOURAGEMENT){ // added noise to console output, and no real value
87+
int totalKoans = result.getTotalNumberOfKoans();
88+
int numberPassing = result.getNumberPassing();
89+
sb.append( EOL).append(
90+
CONQUERED).append(
91+
" ").append(
92+
numberPassing).append(
93+
" ").append(
94+
OUT_OF).append(
95+
" ").append(
96+
totalKoans).append(
97+
" ").append(
98+
KOAN).append(
99+
totalKoans != 1 ? 's' : "").append(
100+
"! ").append(ENCOURAGEMENT).append(
101+
EOL);
102+
}
103+
System.out.print(sb.toString());
97104
}
98105

99106
protected void printSuggestion(KoanResult result) {
100107
KoanMethod failedKoan = result.getFailingMethod();
101-
System.out.println(failedKoan.getLesson()+EOL);
102-
StringBuilder sb = new StringBuilder(
108+
StringBuilder sb = buildLessonLine(failedKoan);
109+
sb.append(EOL).append(EOL);
110+
buildInvestigateLine(sb, result.getFailingCase().getSimpleName(),failedKoan.getMethod().getName());
111+
sb.append(EOL).append(EOL);
112+
buildLineClue(sb, result);
113+
System.out.println(sb.toString());
114+
}
115+
116+
private StringBuilder buildInvestigateLine(StringBuilder sb, String simpleName,
117+
String methodName) {
118+
return sb.append(
103119
INVESTIGATE_IN_THE).append(
104120
" ").append(
105-
result.getFailingCase().getSimpleName()).append(
121+
simpleName).append(
106122
" class's ").append(
107-
result.getFailingMethod().getMethod().getName()).append(
123+
methodName).append(
108124
" method.");
125+
}
126+
127+
private StringBuilder buildLineClue(StringBuilder sb, KoanResult result) {
109128
if(result.getLineNumber() != null && !result.getLineNumber().trim().isEmpty()){
110-
sb.append(
111-
EOL).append(EOL).append(
112-
"Line ").append(
113-
result.getLineNumber()).append(
114-
" may offer a clue as to how you may progress, now make haste!");
129+
sb.append( "Line ").append(
130+
result.getLineNumber()).append(
131+
" may offer a clue as to how you may progress, now make haste!").append(
132+
EOL);
115133
}
116-
System.out.println(sb.append(EOL).toString());
134+
return sb;
135+
}
136+
137+
private StringBuilder buildLessonLine(KoanMethod failedKoan) {
138+
return new StringBuilder(failedKoan.getLesson());
117139
}
118140
}

0 commit comments

Comments
 (0)