Skip to content

Commit 913b3d3

Browse files
authored
Remove uses of Hamcrest assertions or migrate to AssertJ (exercism#1823)
* Migrate robot-name tests to AssertJ. * Migrate nucleotide-count to AssertJ. * Remove unused hamcrest dependency in grade-school. * Migrate nth-prime to AssertJ. * Migrate diamond to AssertJ. * Migrate simple-linked-list to AssertJ. * Migrate linked-list to AssertJ. * Migrate proverb to AssertJ. * Migrate anagram to AssertJ. * Migrate circular-buffer to AssertJ. * Remove unnecessary hamcrest usage from alphametics. * Update .gitignore to ignore generated files from the IDE.
1 parent 82745d4 commit 913b3d3

File tree

21 files changed

+274
-246
lines changed

21 files changed

+274
-246
lines changed

.gitignore

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,13 @@ bin/configlet
1010
bin/configlet.exe
1111
/exercises/.nb-gradle/
1212
**/out
13+
exercises/.project
14+
exercises/.settings/
15+
exercises/*/bin
16+
exercises/*/.classpath
17+
exercises/*/.project
18+
exercises/*/.settings/
19+
_template/bin
20+
_template/.classpath
21+
_template/.project
22+
_template/.settings/

exercises/alphametics/src/test/java/AlphameticsTest.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1+
import static org.junit.Assert.assertEquals;
2+
13
import org.junit.Ignore;
24
import org.junit.Rule;
35
import org.junit.Test;
46
import org.junit.rules.ExpectedException;
57

68
import java.util.LinkedHashMap;
79

8-
import static org.hamcrest.CoreMatchers.*;
9-
import static org.junit.Assert.*;
1010

1111
public class AlphameticsTest {
1212
@Rule
@@ -33,7 +33,7 @@ public void testUniqueValue() throws UnsolvablePuzzleException {
3333
@Test
3434
public void testLeadingZero() throws UnsolvablePuzzleException {
3535
expectedException.expect(UnsolvablePuzzleException.class);
36-
assertNull(new Alphametics("ACA + DD == BD").solve());
36+
new Alphametics("ACA + DD == BD").solve();
3737
}
3838

3939
@Ignore("Remove to run test")

exercises/anagram/build.gradle

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

99
dependencies {
1010
testCompile "junit:junit:4.12"
11+
testImplementation "org.assertj:assertj-core:3.15.0"
1112
}
1213

1314
test {
Lines changed: 60 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,121 +1,154 @@
1+
import static org.assertj.core.api.Assertions.assertThat;
2+
13
import org.junit.Ignore;
24
import org.junit.Test;
35

46
import java.util.Arrays;
57
import java.util.Collections;
68
import java.util.List;
79

8-
import static org.hamcrest.CoreMatchers.*;
9-
import static org.junit.Assert.*;
10-
1110
public class AnagramTest {
1211

1312

1413
@Test
1514
public void testNoMatches() {
1615
Anagram detector = new Anagram("diaper");
17-
assertTrue(detector.match(Arrays.asList("hello", "world", "zombies", "pants")).isEmpty());
16+
17+
assertThat(
18+
detector.match(
19+
Arrays.asList("hello", "world", "zombies", "pants")))
20+
.isEmpty();
1821
}
1922

2023
@Ignore("Remove to run test")
2124
@Test
2225
public void testDetectMultipleAnagrams() {
2326
Anagram detector = new Anagram("master");
24-
List<String> anagrams = detector.match(Arrays.asList("stream", "pigeon", "maters"));
25-
assertThat(anagrams, allOf(hasItem("maters"), hasItem("stream")));
27+
28+
assertThat(detector.match(Arrays.asList("stream", "pigeon", "maters")))
29+
.containsExactlyInAnyOrder​("maters", "stream");
2630
}
2731

2832
@Ignore("Remove to run test")
2933
@Test
3034
public void testEliminateAnagramSubsets() {
3135
Anagram detector = new Anagram("good");
32-
assertTrue(detector.match(Arrays.asList("dog", "goody")).isEmpty());
36+
37+
assertThat(detector.match(Arrays.asList("dog", "goody"))).isEmpty();
3338
}
3439

3540
@Ignore("Remove to run test")
3641
@Test
3742
public void testDetectLongerAnagram() {
3843
Anagram detector = new Anagram("listen");
39-
List<String> anagrams = detector.match(Arrays.asList("enlists", "google", "inlets", "banana"));
40-
assertThat(anagrams, hasItem("inlets"));
44+
45+
assertThat(
46+
detector.match(
47+
Arrays.asList("enlists", "google", "inlets", "banana")))
48+
.containsExactlyInAnyOrder​("inlets");
4149
}
4250

4351
@Ignore("Remove to run test")
4452
@Test
4553
public void testDetectMultipleAnagramsForLongerWord() {
4654
Anagram detector = new Anagram("allergy");
47-
List<String> anagrams = detector.match(Arrays.asList("gallery", "ballerina",
48-
"regally", "clergy",
49-
"largely", "leading"));
50-
assertThat(anagrams, allOf(hasItem("gallery"), hasItem("regally"), hasItem("largely")));
55+
assertThat(
56+
detector.match(
57+
Arrays.asList(
58+
"gallery",
59+
"ballerina",
60+
"regally",
61+
"clergy",
62+
"largely",
63+
"leading")))
64+
.containsExactlyInAnyOrder​("gallery", "regally", "largely");
5165
}
5266

5367
@Ignore("Remove to run test")
5468
@Test
5569
public void testDetectsMultipleAnagramsWithDifferentCase() {
5670
Anagram detector = new Anagram("nose");
57-
List<String> anagrams = detector.match(Arrays.asList("Eons", "ONES"));
58-
assertThat(anagrams, allOf(hasItem("Eons"), hasItem("ONES")));
71+
72+
assertThat(detector.match(Arrays.asList("Eons", "ONES")))
73+
.containsExactlyInAnyOrder​("Eons", "ONES");
5974
}
6075

6176
@Ignore("Remove to run test")
6277
@Test
6378
public void testEliminateAnagramsWithSameChecksum() {
6479
Anagram detector = new Anagram("mass");
65-
assertTrue(detector.match(Collections.singletonList("last")).isEmpty());
80+
81+
assertThat(detector.match(Collections.singletonList("last")))
82+
.isEmpty();
6683
}
6784

6885
@Ignore("Remove to run test")
6986
@Test
7087
public void testCaseInsensitiveWhenBothAnagramAndSubjectStartWithUpperCaseLetter() {
7188
Anagram detector = new Anagram("Orchestra");
72-
List<String> anagrams = detector.match(Arrays.asList("cashregister", "Carthorse", "radishes"));
73-
assertThat(anagrams, hasItem("Carthorse"));
89+
90+
assertThat(
91+
detector.match(
92+
Arrays.asList("cashregister", "Carthorse", "radishes")))
93+
.containsExactlyInAnyOrder​("Carthorse");
7494
}
7595

7696
@Ignore("Remove to run test")
7797
@Test
7898
public void testCaseInsensitiveWhenSubjectStartsWithUpperCaseLetter() {
7999
Anagram detector = new Anagram("Orchestra");
80-
List<String> anagrams = detector.match(Arrays.asList("cashregister", "carthorse", "radishes"));
81-
assertThat(anagrams, hasItem("carthorse"));
100+
101+
assertThat(
102+
detector.match(
103+
Arrays.asList("cashregister", "carthorse", "radishes")))
104+
.containsExactlyInAnyOrder​("carthorse");
82105
}
83106

84107
@Ignore("Remove to run test")
85108
@Test
86109
public void testCaseInsensitiveWhenAnagramStartsWithUpperCaseLetter() {
87110
Anagram detector = new Anagram("orchestra");
88-
List<String> anagrams = detector.match(Arrays.asList("cashregister", "Carthorse", "radishes"));
89-
assertThat(anagrams, hasItem("Carthorse"));
111+
112+
assertThat(
113+
detector.match(
114+
Arrays.asList("cashregister", "Carthorse", "radishes")))
115+
.containsExactlyInAnyOrder​("Carthorse");
90116
}
91117

92118
@Ignore("Remove to run test")
93119
@Test
94120
public void testIdenticalWordRepeatedIsNotAnagram() {
95121
Anagram detector = new Anagram("go");
96-
assertTrue(detector.match(Collections.singletonList("go Go GO")).isEmpty());
122+
123+
assertThat(detector.match(Collections.singletonList("go Go GO")))
124+
.isEmpty();
97125
}
98126

99127
@Ignore("Remove to run test")
100128
@Test
101129
public void testAnagramMustUseAllLettersExactlyOnce() {
102130
Anagram detector = new Anagram("tapper");
103-
assertTrue(detector.match(Collections.singletonList("patter")).isEmpty());
131+
132+
assertThat(detector.match(Collections.singletonList("patter")))
133+
.isEmpty();
104134
}
105135

106136
@Ignore("Remove to run test")
107137
@Test
108138
public void testWordsAreNotAnagramsOfThemselvesCaseInsensitive() {
109139
Anagram detector = new Anagram("BANANA");
110-
assertTrue(detector.match(Arrays.asList("BANANA", "Banana", "banana")).isEmpty());
140+
141+
assertThat(detector.match(Arrays.asList("BANANA", "Banana", "banana")))
142+
.isEmpty();
111143
}
112144

113145
@Ignore("Remove to run test")
114146
@Test
115147
public void testWordsOtherThanThemselvesCanBeAnagrams() {
116148
Anagram detector = new Anagram("LISTEN");
117-
List<String> anagrams = detector.match(Arrays.asList("Listen", "Silent", "LISTEN"));
118-
assertThat(anagrams, hasItem("Silent"));
149+
150+
assertThat(detector.match(Arrays.asList("Listen", "Silent", "LISTEN")))
151+
.containsExactlyInAnyOrder​("Silent");
119152
}
120153

121154
}

exercises/circular-buffer/build.gradle

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

99
dependencies {
1010
testCompile "junit:junit:4.12"
11+
testImplementation "org.assertj:assertj-core:3.15.0"
1112
}
1213

1314
test {

exercises/circular-buffer/src/test/java/CircularBufferTest.java

Lines changed: 22 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
import static org.junit.Assert.assertThat;
2-
import static org.hamcrest.CoreMatchers.is;
1+
import static org.assertj.core.api.Assertions.assertThat;
32

43
import org.junit.Ignore;
54
import org.junit.Rule;
@@ -26,7 +25,7 @@ public void canReadItemJustWritten() throws BufferIOException {
2625
CircularBuffer<Integer> buffer = new CircularBuffer<>(1);
2726

2827
buffer.write(1);
29-
assertThat(buffer.read(), is(1));
28+
assertThat(buffer.read()).isEqualTo(1);
3029
}
3130

3231
@Ignore("Remove to run test")
@@ -35,7 +34,7 @@ public void canReadItemOnlyOnce() throws BufferIOException {
3534
CircularBuffer<Integer> buffer = new CircularBuffer<>(1);
3635

3736
buffer.write(1);
38-
assertThat(buffer.read(), is(1));
37+
assertThat(buffer.read()).isEqualTo(1);
3938

4039
expectedException.expect(BufferIOException.class);
4140
expectedException.expectMessage("Tried to read from empty buffer");
@@ -49,8 +48,8 @@ public void readsItemsInOrderWritten() throws BufferIOException {
4948

5049
buffer.write(1);
5150
buffer.write(2);
52-
assertThat(buffer.read(), is(1));
53-
assertThat(buffer.read(), is(2));
51+
assertThat(buffer.read()).isEqualTo(1);
52+
assertThat(buffer.read()).isEqualTo(2);
5453
}
5554

5655
@Ignore("Remove to run test")
@@ -70,9 +69,9 @@ public void readFreesUpSpaceForWrite() throws BufferIOException {
7069
CircularBuffer<Integer> buffer = new CircularBuffer<>(1);
7170

7271
buffer.write(1);
73-
assertThat(buffer.read(), is(1));
72+
assertThat(buffer.read()).isEqualTo(1);
7473
buffer.write(2);
75-
assertThat(buffer.read(), is(2));
74+
assertThat(buffer.read()).isEqualTo(2);
7675
}
7776

7877
@Ignore("Remove to run test")
@@ -82,10 +81,10 @@ public void maintainsReadPositionAcrossWrites() throws BufferIOException {
8281

8382
buffer.write(1);
8483
buffer.write(2);
85-
assertThat(buffer.read(), is(1));
84+
assertThat(buffer.read()).isEqualTo(1);
8685
buffer.write(3);
87-
assertThat(buffer.read(), is(2));
88-
assertThat(buffer.read(), is(3));
86+
assertThat(buffer.read()).isEqualTo(2);
87+
assertThat(buffer.read()).isEqualTo(3);
8988
}
9089

9190
@Ignore("Remove to run test")
@@ -108,7 +107,7 @@ public void clearFreesUpCapacity() throws BufferIOException {
108107
buffer.write(1);
109108
buffer.clear();
110109
buffer.write(2);
111-
assertThat(buffer.read(), is(2));
110+
assertThat(buffer.read()).isEqualTo(2);
112111
}
113112

114113
@Ignore("Remove to run test")
@@ -118,7 +117,7 @@ public void clearDoesNothingOnEmptyBuffer() throws BufferIOException {
118117

119118
buffer.clear();
120119
buffer.write(1);
121-
assertThat(buffer.read(), is(1));
120+
assertThat(buffer.read()).isEqualTo(1);
122121
}
123122

124123
@Ignore("Remove to run test")
@@ -128,8 +127,8 @@ public void overwriteActsLikeWriteOnNonFullBuffer() throws BufferIOException {
128127

129128
buffer.write(1);
130129
buffer.overwrite(2);
131-
assertThat(buffer.read(), is(1));
132-
assertThat(buffer.read(), is(2));
130+
assertThat(buffer.read()).isEqualTo(1);
131+
assertThat(buffer.read()).isEqualTo(2);
133132
}
134133

135134
@Ignore("Remove to run test")
@@ -140,8 +139,8 @@ public void overwriteRemovesOldestElementOnFullBuffer() throws BufferIOException
140139
buffer.write(1);
141140
buffer.write(2);
142141
buffer.overwrite(3);
143-
assertThat(buffer.read(), is(2));
144-
assertThat(buffer.read(), is(3));
142+
assertThat(buffer.read()).isEqualTo(2);
143+
assertThat(buffer.read()).isEqualTo(3);
145144
}
146145

147146
@Ignore("Remove to run test")
@@ -152,12 +151,12 @@ public void overwriteDoesntRemoveAnAlreadyReadElement() throws BufferIOException
152151
buffer.write(1);
153152
buffer.write(2);
154153
buffer.write(3);
155-
assertThat(buffer.read(), is(1));
154+
assertThat(buffer.read()).isEqualTo(1);
156155
buffer.write(4);
157156
buffer.overwrite(5);
158-
assertThat(buffer.read(), is(3));
159-
assertThat(buffer.read(), is(4));
160-
assertThat(buffer.read(), is(5));
157+
assertThat(buffer.read()).isEqualTo(3);
158+
assertThat(buffer.read()).isEqualTo(4);
159+
assertThat(buffer.read()).isEqualTo(5);
161160
}
162161

163162
@Ignore("Remove to run test")
@@ -170,8 +169,8 @@ public void initialClearDoesNotAffectWrappingAround() throws BufferIOException {
170169
buffer.write(2);
171170
buffer.overwrite(3);
172171
buffer.overwrite(4);
173-
assertThat(buffer.read(), is(3));
174-
assertThat(buffer.read(), is(4));
172+
assertThat(buffer.read()).isEqualTo(3);
173+
assertThat(buffer.read()).isEqualTo(4);
175174
expectedException.expect(BufferIOException.class);
176175
expectedException.expectMessage("Tried to read from empty buffer");
177176
buffer.read();

exercises/diamond/build.gradle

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

99
dependencies {
1010
testCompile "junit:junit:4.12"
11+
testImplementation "org.assertj:assertj-core:3.15.0"
1112
}
1213

1314
test {

0 commit comments

Comments
 (0)