Bladeren bron

Issue #253: grade school improvements (#271)

* Issue #253: Tried to improve grade school structure. Removed .db() function, added numberOfStudents functions and renamed the sort function to studentsByGradeAlphabetical. Added test to check that grade() gets the students in the order they were inserted. Removed getsStudentsInAGrade() test as what it was testing was already being covered by the tests before it.

* Added tests for protection agains mutation of the list object

* Issue #253: Make the sort test more generic. This to allow a broader range of implementations. Currently users are hindered into return the type HashMap<Integer, List<String>. This test update will check the order of any Map that maps to any Collection value type. This opens the doors for more possible types of solutions.
Dave Thomas 9 jaren geleden
bovenliggende
commit
7bb120f5bb
2 gewijzigde bestanden met toevoegingen van 40 en 28 verwijderingen
  1. 1
    0
      exercises/grade-school/build.gradle
  2. 39
    28
      exercises/grade-school/src/test/java/SchoolTest.java

+ 1
- 0
exercises/grade-school/build.gradle Bestand weergeven

7
 }
7
 }
8
 
8
 
9
 dependencies {
9
 dependencies {
10
+  testCompile group: 'org.hamcrest', name: 'hamcrest-library', version: '1.3'
10
   testCompile "junit:junit:4.12"
11
   testCompile "junit:junit:4.12"
11
 }
12
 }
12
 test {
13
 test {

+ 39
- 28
exercises/grade-school/src/test/java/SchoolTest.java Bestand weergeven

1
 import org.junit.Ignore;
1
 import org.junit.Ignore;
2
 import org.junit.Test;
2
 import org.junit.Test;
3
 
3
 
4
-import java.lang.Integer;
5
-import java.util.*;
6
 import java.util.ArrayList;
4
 import java.util.ArrayList;
7
-import java.util.Arrays;
8
 import java.util.HashMap;
5
 import java.util.HashMap;
9
-import java.util.List;
10
 import java.util.Map;
6
 import java.util.Map;
7
+import java.util.List;
8
+import java.util.Collection;
11
 
9
 
10
+import org.hamcrest.Matcher;
11
+import org.hamcrest.collection.IsIterableContainingInOrder;
12
 import static org.hamcrest.CoreMatchers.*;
12
 import static org.hamcrest.CoreMatchers.*;
13
 import static org.junit.Assert.assertThat;
13
 import static org.junit.Assert.assertThat;
14
 import static org.junit.Assert.assertTrue;
14
 import static org.junit.Assert.assertTrue;
62
 
62
 
63
   @Ignore
63
   @Ignore
64
   @Test
64
   @Test
65
-  public void gradeReturnsStudentsInTheOrderTheyWereInserted() {
66
-    int grade = 4;
67
-    school.add("Bartimaeus", grade);
68
-    school.add("Nathaniel", grade);
69
-    school.add("Faquarl", grade);
70
-    List<String> studentsInGrade = school.grade(grade);
71
-    assertThat(studentsInGrade.get(0), is("Bartimaeus"));
72
-    assertThat(studentsInGrade.get(1), is("Nathaniel"));
73
-    assertThat(studentsInGrade.get(2), is("Faquarl"));
74
-  }
75
-
76
-  @Ignore
77
-  @Test
78
   public void sortsSchool() {
65
   public void sortsSchool() {
66
+    school.add("Kyle", 4);
67
+    school.add("Zed", 4);
68
+    school.add("Adam", 4);
79
     school.add("Jennifer", 4);
69
     school.add("Jennifer", 4);
80
     school.add("Kareem", 6);
70
     school.add("Kareem", 6);
81
     school.add("Christopher", 4);
71
     school.add("Christopher", 4);
82
-    school.add("Kyle", 3);
83
-    Map<Integer, List<String>> sortedStudents = new HashMap<Integer, List<String>>();
84
-    sortedStudents.put(6, Arrays.asList("Kareem"));
85
-    sortedStudents.put(4, Arrays.asList("Christopher", "Jennifer"));
86
-    sortedStudents.put(3, Arrays.asList("Kyle"));
87
-    assertEquals(school.studentsByGradeAlphabetical(), sortedStudents);
72
+    school.add("Kylie", 3);
73
+    Map<Integer, Matcher> sortedStudents = new HashMap<Integer, Matcher>();
74
+    sortedStudents.put(6, IsIterableContainingInOrder
75
+      .contains("Kareem"));
76
+    sortedStudents.put(4, IsIterableContainingInOrder
77
+      .contains("Adam", "Christopher", "Jennifer", "Kyle", "Zed"));
78
+    sortedStudents.put(3, IsIterableContainingInOrder
79
+      .contains("Kylie"));
80
+
81
+    Map schoolStudents = school.studentsByGradeAlphabetical();
82
+    for (Map.Entry<?, Matcher> entry : sortedStudents.entrySet()) {
83
+
84
+      assertThat((Collection) schoolStudents.get(entry.getKey()), entry.getValue());
85
+    }
88
   }
86
   }
89
 
87
 
90
   @Ignore
88
   @Ignore
93
     String shouldNotBeAdded = "Should not be added to school";
91
     String shouldNotBeAdded = "Should not be added to school";
94
     int grade = 1;
92
     int grade = 1;
95
 
93
 
96
-    List<String> students = school.grade(grade);
97
-    students.add(shouldNotBeAdded);
94
+    Collection students = school.grade(grade);
95
+
96
+    try {
97
+      students.add(shouldNotBeAdded);
98
+    } catch (Exception exception) {
99
+      // Also valid that the add operation throws an exception
100
+      // Such as UnsupportedOperationException when an umodifiable collection type is used
101
+    }
98
 
102
 
99
     assertThat(school.grade(grade), not(hasItem(shouldNotBeAdded)));
103
     assertThat(school.grade(grade), not(hasItem(shouldNotBeAdded)));
100
   }
104
   }
107
     List<String> listWhichShouldNotBeAdded = new ArrayList<>();
111
     List<String> listWhichShouldNotBeAdded = new ArrayList<>();
108
     listWhichShouldNotBeAdded.add(studentWhichShouldNotBeAdded);
112
     listWhichShouldNotBeAdded.add(studentWhichShouldNotBeAdded);
109
 
113
 
110
-    Map<Integer, List<String>> sortedStudents = school.studentsByGradeAlphabetical();
111
-    sortedStudents.put(grade,listWhichShouldNotBeAdded);
114
+    Map sortedStudents = school.studentsByGradeAlphabetical();
115
+
116
+    try {
117
+      sortedStudents.put(grade, listWhichShouldNotBeAdded);
118
+    } catch (Exception exception) {
119
+      // Also valid that the put operation throws an exception
120
+      // Such as UnsupportedOperationException when an unmodifiableMap is used
121
+    }
112
 
122
 
113
-    assertThat(school.studentsByGradeAlphabetical().get(grade), not(hasItem(studentWhichShouldNotBeAdded)));
123
+    assertThat(school.studentsByGradeAlphabetical().get(grade), 
124
+      not(hasItem(studentWhichShouldNotBeAdded)));
114
   }
125
   }
115
 }
126
 }