Skip to content

Commit 7bb120f

Browse files
davethomas11matthewmorgan
authored andcommitted
Issue exercism#253: grade school improvements (exercism#271)
* Issue exercism#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 exercism#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.
1 parent 077f563 commit 7bb120f

2 files changed

Lines changed: 40 additions & 28 deletions

File tree

exercises/grade-school/build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ repositories {
77
}
88

99
dependencies {
10+
testCompile group: 'org.hamcrest', name: 'hamcrest-library', version: '1.3'
1011
testCompile "junit:junit:4.12"
1112
}
1213
test {

exercises/grade-school/src/test/java/SchoolTest.java

Lines changed: 39 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
import org.junit.Ignore;
22
import org.junit.Test;
33

4-
import java.lang.Integer;
5-
import java.util.*;
64
import java.util.ArrayList;
7-
import java.util.Arrays;
85
import java.util.HashMap;
9-
import java.util.List;
106
import java.util.Map;
7+
import java.util.List;
8+
import java.util.Collection;
119

10+
import org.hamcrest.Matcher;
11+
import org.hamcrest.collection.IsIterableContainingInOrder;
1212
import static org.hamcrest.CoreMatchers.*;
1313
import static org.junit.Assert.assertThat;
1414
import static org.junit.Assert.assertTrue;
@@ -60,31 +60,29 @@ public void getsStudentsInEmptyGrade() {
6060
assertTrue(school.grade(1).isEmpty());
6161
}
6262

63-
@Ignore
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-
7663
@Ignore
7764
@Test
7865
public void sortsSchool() {
66+
school.add("Kyle", 4);
67+
school.add("Zed", 4);
68+
school.add("Adam", 4);
7969
school.add("Jennifer", 4);
8070
school.add("Kareem", 6);
8171
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+
}
8886
}
8987

9088
@Ignore
@@ -93,8 +91,14 @@ public void modifyingFetchedGradeShouldNotModifyInternalDatabase() {
9391
String shouldNotBeAdded = "Should not be added to school";
9492
int grade = 1;
9593

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+
}
98102

99103
assertThat(school.grade(grade), not(hasItem(shouldNotBeAdded)));
100104
}
@@ -107,9 +111,16 @@ public void modifyingSortedStudentsShouldNotModifyInternalDatabase() {
107111
List<String> listWhichShouldNotBeAdded = new ArrayList<>();
108112
listWhichShouldNotBeAdded.add(studentWhichShouldNotBeAdded);
109113

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+
}
112122

113-
assertThat(school.studentsByGradeAlphabetical().get(grade), not(hasItem(studentWhichShouldNotBeAdded)));
123+
assertThat(school.studentsByGradeAlphabetical().get(grade),
124+
not(hasItem(studentWhichShouldNotBeAdded)));
114125
}
115126
}

0 commit comments

Comments
 (0)