Skip to content

Commit 0674e68

Browse files
author
mat bentley
committed
properly replacing hack in koan method sorting
1 parent 78980e9 commit 0674e68

2 files changed

Lines changed: 39 additions & 20 deletions

File tree

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,15 @@
11
package com.sandwich.util;
22

3-
import java.util.ArrayList;
4-
import java.util.Arrays;
5-
import java.util.Collection;
63
import java.util.Comparator;
7-
import java.util.HashSet;
8-
import java.util.List;
9-
import java.util.Set;
104
import java.util.logging.Logger;
5+
import java.util.regex.Matcher;
6+
import java.util.regex.Pattern;
117

128
import com.sandwich.koan.KoanMethod;
139
import com.sandwich.util.io.directories.DirectoryManager;
1410
import com.sandwich.util.io.filecompiler.FileCompiler;
1511

1612
public class KoanComparator implements Comparator<KoanMethod> {
17-
List<String> orderedKeywords;
18-
Set<String> methodNamesCompared = new HashSet<String>();
19-
20-
public KoanComparator(String...koans){
21-
this(Arrays.asList(koans));
22-
}
23-
24-
public KoanComparator(Collection<String> koans){
25-
this.orderedKeywords = new ArrayList<String>(koans);
26-
}
2713

2814
public int compare(KoanMethod arg0, KoanMethod arg1) {
2915
Class<?> declaringClass0 = arg0.getMethod().getDeclaringClass();
@@ -33,9 +19,22 @@ public int compare(KoanMethod arg0, KoanMethod arg1) {
3319
return 0;
3420
}
3521
String contentsOfOriginalJavaFile = FileCompiler.getContentsOfJavaFile(DirectoryManager.getSourceDir(), declaringClass0.getName());
36-
Integer index0 = Integer.valueOf( contentsOfOriginalJavaFile.indexOf(arg0.getMethod().getName()));
37-
Integer index1 = Integer.valueOf( contentsOfOriginalJavaFile.indexOf(arg1.getMethod().getName()));
22+
String pattern = ".*\\s%s(\\(|\\s*\\))";
23+
Integer index0 = indexOfMatch(contentsOfOriginalJavaFile, String.format(pattern, arg0.getMethod().getName()));
24+
Integer index1 = indexOfMatch(contentsOfOriginalJavaFile, String.format(pattern, arg1.getMethod().getName()));
3825
return index0.compareTo(index1);
3926
}
27+
28+
/*
29+
* TODO: This is utility code...
30+
*/
31+
private int indexOfMatch(String inputString, String pattern) {
32+
Pattern p = Pattern.compile(pattern);
33+
Matcher m = p.matcher(inputString);
34+
if (m.find()) {
35+
return m.start();
36+
}
37+
return -1;
38+
}
4039

4140
}

‎lib/src/test/java/com/sandwich/util/KoanComparatorTest.java‎

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,16 @@
1515

1616
public class KoanComparatorTest extends CommandLineTestCase {
1717

18+
interface Caps {
19+
public String capitalize(String name);
20+
}
21+
1822
@Test
1923
public void testThatKomparatorBombsWhenNotFound() throws Exception {
2024
Method m = new Object(){
2125
@Koan public void someMethod(){}
2226
}.getClass().getDeclaredMethod("someMethod");
23-
KoanComparator comparator = new KoanComparator("meh");
27+
KoanComparator comparator = new KoanComparator();
2428
try{
2529
comparator.compare(KoanMethod.getInstance("2",m), KoanMethod.getInstance("1",m));
2630
}catch(RuntimeException fileNotFound){}
@@ -35,9 +39,25 @@ public void testComparatorRanksByOrder() throws Exception {
3539
KoanMethod m1 = KoanMethod.getInstance("",clazz.getDeclaredMethod("someMethodOne"));
3640
KoanMethod m2 = KoanMethod.getInstance("",clazz.getDeclaredMethod("someMethodTwo"));
3741
List<KoanMethod> methods = Arrays.asList(m2,m1);
38-
Collections.sort(methods, new KoanComparator("someMethodOne","someMethodTwo"));
42+
Collections.sort(methods, new KoanComparator());
3943
assertSame(m1,methods.get(0));
4044
assertSame(m2,methods.get(1));
4145
}
46+
47+
@Test
48+
public void testVariableNamesArentConfusedAsKoanMethodsWhenSorting() throws Exception {
49+
Class<? extends Object> clazz = new Object(){
50+
String foo;
51+
@Koan public void bar(){}
52+
@Koan public void foo(){}
53+
}.getClass();
54+
KoanMethod m1 = KoanMethod.getInstance("", clazz.getDeclaredMethod("bar"));
55+
KoanMethod m2 = KoanMethod.getInstance("", clazz.getDeclaredMethod("foo"));
56+
List<KoanMethod> methods = Arrays.asList(m2,m1);
57+
Collections.sort(methods, new KoanComparator());
58+
assertSame(m1,methods.get(0));
59+
assertSame(m2,methods.get(1));
60+
}
61+
4262
}
4363

0 commit comments

Comments
 (0)