Skip to content

Commit f444218

Browse files
committed
I really want the student to be able to delete or comment out the fail() methods in AboutKoans. So, add the ability to mark those Koans as "don't require an assert" in PathToEnlightenment.xml.
1 parent 443b9ab commit f444218

8 files changed

Lines changed: 48 additions & 15 deletions

File tree

koans/app/config/PathToEnlightenment.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
<packages>
33
<package pkg="beginner" name="Novice">
44
<suite class="AboutKoans">
5-
<koan name="findAboutKoansFile" displayIncompleteKoanException="false" />
6-
<koan name="definitionOfKoanCompletion" displayIncompleteKoanException="false" />
5+
<koan name="findAboutKoansFile" displayIncompleteKoanException="false" requireAssertion="false" />
6+
<koan name="definitionOfKoanCompletion" displayIncompleteKoanException="false" requireAssertion="false" />
77
</suite>
88
<suite class="AboutAssertions"/>
99
<suite class="AboutEquality"/>

koans/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@
6464
<dependency>
6565
<groupId>io.github.davidwhitlock.joy.com.sandwich</groupId>
6666
<artifactId>koans-lib</artifactId>
67-
<version>1.2.3</version>
67+
<version>1.3.0-SNAPSHOT</version>
6868
</dependency>
6969
</dependencies>
7070
<properties>

lib/src/main/java/com/sandwich/koan/KoanMethod.java

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ public class KoanMethod {
1111
private final transient Method method;
1212
private final String lesson;
1313
private final boolean displayIncompleteException;
14+
private final boolean requiresAssertion;
1415
private static final KoanSuiteCompilationListener listener = new KoanSuiteCompilationListener();
1516

1617
private KoanMethod(KoanElementAttributes koanAttributes) throws SecurityException, NoSuchMethodException{
@@ -26,27 +27,33 @@ public static KoanMethod getInstance(KoanElementAttributes koanAttributes){
2627
}
2728

2829
public static KoanMethod getInstance(Method method){
29-
return new KoanMethod(null, method, true);
30+
return new KoanMethod(null, method, true, true);
31+
}
32+
33+
public static KoanMethod getInstance(Method method, boolean requiresAssertion){
34+
return new KoanMethod(null, method, true, requiresAssertion);
3035
}
3136

3237
public static KoanMethod getInstance(String lesson, Method method){
33-
return new KoanMethod(lesson, method, true);
38+
return new KoanMethod(lesson, method, true, true);
3439
}
3540

36-
private KoanMethod(String lesson, Method method, boolean displayIncompleteException){
41+
private KoanMethod(String lesson, Method method, boolean displayIncompleteException, boolean requiresAssertion){
3742
if(method == null){
3843
throw new IllegalArgumentException("method may not be null");
3944
}
4045
this.method = method;
4146
this.lesson = new RbVariableInjector(lesson, method).injectLessonVariables();
4247
this.displayIncompleteException = displayIncompleteException;
48+
this.requiresAssertion = requiresAssertion;
4349
}
4450

4551
public KoanMethod(String lesson, KoanElementAttributes koanAttributes) throws SecurityException, NoSuchMethodException {
4652
this( lesson,
4753
KoanClassLoader.getInstance().loadClass(koanAttributes.className, listener)
4854
.getMethod(koanAttributes.name),
49-
!"false".equalsIgnoreCase(koanAttributes.displayIncompleteKoanException));
55+
!"false".equalsIgnoreCase(koanAttributes.displayIncompleteKoanException),
56+
!"false".equalsIgnoreCase(koanAttributes.requireAssertion));
5057
}
5158

5259
public String getLesson() {
@@ -60,9 +67,13 @@ public Method getMethod() {
6067
public boolean displayIncompleteException() {
6168
return displayIncompleteException;
6269
}
70+
71+
public boolean requiresAssertion() {
72+
return requiresAssertion;
73+
}
6374

6475
public KoanMethod clone(Method method){
65-
return new KoanMethod(lesson, method, displayIncompleteException);
76+
return new KoanMethod(lesson, method, displayIncompleteException, requiresAssertion);
6677
}
6778

6879
@Override public String toString(){
@@ -75,6 +86,7 @@ public int hashCode() {
7586
final int prime = 31;
7687
int result = 1;
7788
result = prime * result + (displayIncompleteException ? 1231 : 1237);
89+
result = prime * result + (requiresAssertion ? 1231 : 1237);
7890
result = prime * result + ((lesson == null) ? 0 : lesson.hashCode());
7991
return result;
8092
}
@@ -90,6 +102,8 @@ public boolean equals(Object obj) {
90102
KoanMethod other = (KoanMethod) obj;
91103
if (displayIncompleteException != other.displayIncompleteException)
92104
return false;
105+
if (requiresAssertion != other.requiresAssertion)
106+
return false;
93107
if (lesson == null) {
94108
if (other.lesson != null)
95109
return false;

lib/src/main/java/com/sandwich/koan/path/xmltransformation/KoanElementAttributes.java

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,12 @@
33

44
public class KoanElementAttributes{
55

6-
public String name, displayIncompleteKoanException, className;
6+
public String name, displayIncompleteKoanException, requireAssertion, className;
77

8-
public KoanElementAttributes(String name, String displayIncompleteKoanException, String className){
8+
public KoanElementAttributes(String name, String displayIncompleteKoanException, String requireAssertion, String className){
99
this.name = name;
1010
this.displayIncompleteKoanException = displayIncompleteKoanException;
11+
this.requireAssertion = requireAssertion;
1112
this.className = className;
1213
}
1314

@@ -21,6 +22,8 @@ public int hashCode() {
2122
* result
2223
+ ((displayIncompleteKoanException == null) ? 0
2324
: displayIncompleteKoanException.hashCode());
25+
result = prime * result
26+
+ ((requireAssertion == null) ? 0 : requireAssertion.hashCode());
2427
result = prime * result + ((name == null) ? 0 : name.hashCode());
2528
return result;
2629
}
@@ -45,6 +48,11 @@ public boolean equals(Object obj) {
4548
} else if (!displayIncompleteKoanException
4649
.equals(other.displayIncompleteKoanException))
4750
return false;
51+
if (requireAssertion == null) {
52+
if (other.requireAssertion != null)
53+
return false;
54+
} else if (!requireAssertion.equals(other.requireAssertion))
55+
return false;
4856
if (name == null) {
4957
if (other.name != null)
5058
return false;
@@ -57,7 +65,8 @@ public boolean equals(Object obj) {
5765
public String toString() {
5866
return "KoanElementAttributes [name=" + name
5967
+ ", displayIncompleteKoanException="
60-
+ displayIncompleteKoanException + ", className=" + className
68+
+ displayIncompleteKoanException + ", requireAssertion="
69+
+ requireAssertion + ", className=" + className
6170
+ "]";
6271
}
6372

lib/src/main/java/com/sandwich/koan/path/xmltransformation/XmlToPathTransformerImpl.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,11 +95,14 @@ Map<String, KoanElementAttributes> extractKoansAndRawLessons(
9595
.getNamedItem("displayIncompleteKoanException");
9696
String displayIncompleteKoanException = displayKoanIncompleteExceptionNode == null ? null
9797
: displayKoanIncompleteExceptionNode.getNodeValue();
98+
Node requireAssertionNode = attributes.getNamedItem("requireAssertion");
99+
String requireAssertion = requireAssertionNode == null ? null
100+
: requireAssertionNode.getNodeValue();
98101
if (rawKoanAttributesByMethodName.containsKey(name)) {
99102
throw new DuplicateKoanException(className, name);
100103
}
101104
rawKoanAttributesByMethodName.put(name, new KoanElementAttributes(
102-
name, displayIncompleteKoanException, className));
105+
name, displayIncompleteKoanException, requireAssertion, className));
103106
}
104107
}
105108
return rawKoanAttributesByMethodName;
@@ -113,4 +116,3 @@ public DuplicateKoanException(String className, String name){
113116
}
114117

115118
}
116-

lib/src/main/java/com/sandwich/koan/runner/KoanMethodRunner.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public static KoanMethodResult run(Object suite, KoanMethod koan){
3232
Method method = koan.getMethod();
3333
method.setAccessible(true);
3434
method.invoke(suite);
35-
if(!Assert.wasAssertionInvoked()){
35+
if(koan.requiresAssertion() && !Assert.wasAssertionInvoked()){
3636
return new KoanMethodResult(koan, NO_ASSERTION_MESSAGE, null);
3737
}
3838
} catch (Throwable t) {

lib/src/test/java/com/sandwich/koan/path/CommandLineTestCase.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ protected Path stubAllKoans(String packageName, List<String> path){
105105
KoanSuiteCompilationListener listener = new KoanSuiteCompilationListener();
106106
for(Method m : loader.loadClass(suite, listener).getMethods()){
107107
if(m.getAnnotation(Koan.class) != null){
108-
methodsByName.put(m.getName(), new KoanElementAttributes(m.getName(), "", m.getDeclaringClass().getName()));
108+
methodsByName.put(m.getName(), new KoanElementAttributes(m.getName(), "", "", m.getDeclaringClass().getName()));
109109
}
110110
}
111111
tempSuitesAndMethods.put(suite, methodsByName);

lib/src/test/java/com/sandwich/koan/runner/KoanMethodRunnerTest.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,4 +42,12 @@ public void assertionTrackingResetsBetweenKoans() throws Exception {
4242
assertTrue(first.isPassed());
4343
assertFalse(second.isPassed());
4444
}
45+
46+
@Test
47+
public void koanWithoutAssertionCanBeAllowedToPass() throws Exception {
48+
KoanMethodResult result = KoanMethodRunner.run(
49+
new NoAssertionKoan(),
50+
KoanMethod.getInstance(NoAssertionKoan.class.getDeclaredMethod("koan"), false));
51+
assertTrue(result.isPassed());
52+
}
4553
}

0 commit comments

Comments
 (0)