Skip to content

Commit 8fbd90e

Browse files
authored
Convert practice exercises starting with 'a' to use AssertJ (exercism#2046)
* Convert 'A' practice exercises to use AssertJ - accumulate - acronym - affine-cipher - all-your-base - allergies - alphametics - anagraph - armstrong-numbers - atbash-cipher * Clean up 'A' exercise tests further to improve readability - Prefer list and map-specific assertions over generic equality - Prefer inlined variables to separate simple setup - Prefer .containsExactly & friends to .containsExactlyElementsOf & co
1 parent 61c9272 commit 8fbd90e

File tree

9 files changed

+262
-403
lines changed

9 files changed

+262
-403
lines changed
Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,40 @@
1-
import org.junit.Test;
21
import org.junit.Ignore;
2+
import org.junit.Test;
33

44
import java.util.Arrays;
55
import java.util.LinkedList;
66
import java.util.List;
77

8-
import static org.junit.Assert.assertEquals;
8+
import static org.assertj.core.api.Assertions.assertThat;
99

1010
public class AccumulateTest {
1111

1212

1313
@Test
1414
public void emptyAccumulateProducesEmptyAccumulation() {
15-
List<Integer> input = new LinkedList<>();
16-
List<Integer> expectedOutput = new LinkedList<>();
17-
assertEquals(expectedOutput, Accumulate.accumulate(input, x -> x * x));
15+
assertThat(Accumulate.accumulate(new LinkedList<Integer>(), x -> x * x))
16+
.isEmpty();
1817
}
1918

2019
@Ignore("Remove to run test")
2120
@Test
2221
public void accumulateSquares() {
23-
List<Integer> input = Arrays.asList(1, 2, 3);
24-
List<Integer> expectedOutput = Arrays.asList(1, 4, 9);
25-
assertEquals(expectedOutput, Accumulate.accumulate(input, x -> x * x));
22+
assertThat(Accumulate.accumulate(Arrays.asList(1, 2, 3), x -> x * x))
23+
.containsExactly(1, 4, 9);
2624
}
2725

2826
@Ignore("Remove to run test")
2927
@Test
3028
public void accumulateUpperCases() {
31-
List<String> input = Arrays.asList("hello", "world");
32-
List<String> expectedOutput = Arrays.asList("HELLO", "WORLD");
33-
assertEquals(expectedOutput, Accumulate.accumulate(input, x -> x.toUpperCase()));
29+
assertThat(Accumulate.accumulate(Arrays.asList("hello", "world"), String::toUpperCase))
30+
.containsExactly("HELLO", "WORLD");
3431
}
3532

3633
@Ignore("Remove to run test")
3734
@Test
3835
public void accumulateReversedStrings() {
39-
List<String> input = Arrays.asList("the quick brown fox etc".split(" "));
40-
List<String> expectedOutput = Arrays.asList("eht kciuq nworb xof cte".split(" "));
41-
assertEquals(expectedOutput, Accumulate.accumulate(input, this::reverse));
36+
assertThat(Accumulate.accumulate(Arrays.asList("the", "quick", "brown", "fox", "etc"), this::reverse))
37+
.containsExactly("eht", "kciuq", "nworb", "xof", "cte");
4238
}
4339

4440
private String reverse(String input) {
@@ -50,10 +46,9 @@ private String reverse(String input) {
5046
public void accumulateWithinAccumulate() {
5147
List<String> input1 = Arrays.asList("a", "b", "c");
5248
List<String> input2 = Arrays.asList("1", "2", "3");
53-
List<String> expectedOutput = Arrays.asList("a1 a2 a3", "b1 b2 b3", "c1 c2 c3");
54-
assertEquals(expectedOutput, Accumulate.accumulate(
49+
assertThat(Accumulate.accumulate(
5550
input1, c ->
5651
String.join(" ", Accumulate.accumulate(input2, d -> c + d))
57-
));
52+
)).containsExactly("a1 a2 a3", "b1 b2 b3", "c1 c2 c3");
5853
}
5954
}

exercises/practice/acronym/src/test/java/AcronymTest.java

Lines changed: 20 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,79 +1,70 @@
1-
import org.junit.Test;
21
import org.junit.Ignore;
2+
import org.junit.Test;
33

4-
import static org.junit.Assert.assertEquals;
4+
import static org.assertj.core.api.Assertions.assertThat;
55

66
public class AcronymTest {
77

88
@Test
99
public void basic() {
10-
String phrase = "Portable Network Graphics";
11-
String expected = "PNG";
12-
assertEquals(expected, new Acronym(phrase).get());
10+
assertThat(new Acronym("Portable Network Graphics").get())
11+
.isEqualTo("PNG");
1312
}
1413

1514
@Ignore("Remove to run test")
1615
@Test
1716
public void lowercaseWords() {
18-
String phrase = "Ruby on Rails";
19-
String expected = "ROR";
20-
assertEquals(expected, new Acronym(phrase).get());
17+
assertThat(new Acronym("Ruby on Rails").get())
18+
.isEqualTo("ROR");
2119
}
2220

2321
@Ignore("Remove to run test")
2422
@Test
2523
public void punctuation() {
26-
String phrase = "First In, First Out";
27-
String expected = "FIFO";
28-
assertEquals(expected, new Acronym(phrase).get());
24+
assertThat(new Acronym("First In, First Out").get())
25+
.isEqualTo("FIFO");
2926
}
3027

3128
@Ignore("Remove to run test")
3229
@Test
3330
public void nonAcronymAllCapsWord() {
34-
String phrase = "GNU Image Manipulation Program";
35-
String expected = "GIMP";
36-
assertEquals(expected, new Acronym(phrase).get());
31+
assertThat(new Acronym("GNU Image Manipulation Program").get())
32+
.isEqualTo("GIMP");
3733
}
3834

3935
@Ignore("Remove to run test")
4036
@Test
4137
public void punctuationWithoutWhitespace() {
42-
String phrase = "Complementary metal-oxide semiconductor";
43-
String expected = "CMOS";
44-
assertEquals(expected, new Acronym(phrase).get());
38+
assertThat(new Acronym("Complementary metal-oxide semiconductor").get())
39+
.isEqualTo("CMOS");
4540
}
4641

4742
@Ignore("Remove to run test")
4843
@Test
4944
public void veryLongAbbreviation() {
50-
String phrase = "Rolling On The Floor Laughing So Hard That My Dogs Came Over And Licked Me";
51-
String expected = "ROTFLSHTMDCOALM";
52-
assertEquals(expected, new Acronym(phrase).get());
45+
assertThat(new Acronym("Rolling On The Floor Laughing So Hard That My Dogs Came Over And Licked Me").get())
46+
.isEqualTo("ROTFLSHTMDCOALM");
5347
}
5448

5549
@Ignore("Remove to run test")
5650
@Test
5751
public void consecutiveDelimiters() {
58-
String phrase = "Something - I made up from thin air";
59-
String expected = "SIMUFTA";
60-
assertEquals(expected, new Acronym(phrase).get());
52+
assertThat(new Acronym("Something - I made up from thin air").get())
53+
.isEqualTo("SIMUFTA");
6154
}
6255

6356
@Ignore("Remove to run test")
6457
@Test
6558
public void apostrophes() {
66-
String phrase = "Halley's Comet";
67-
String expected = "HC";
68-
assertEquals(expected, new Acronym(phrase).get());
59+
assertThat(new Acronym("Halley's Comet").get())
60+
.isEqualTo("HC");
6961
}
7062

7163
@Ignore("Remove to run test")
7264
@Test
7365
public void underscoreEmphasis() {
74-
String phrase = "The Road _Not_ Taken";
75-
String expected = "TRNT";
76-
assertEquals(expected, new Acronym(phrase).get());
66+
assertThat(new Acronym("The Road _Not_ Taken").get())
67+
.isEqualTo("TRNT");
7768
}
7869

7970

exercises/practice/affine-cipher/src/test/java/AffineCipherTest.java

Lines changed: 26 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,63 +1,62 @@
1-
import static org.assertj.core.api.Assertions.assertThat;
2-
import static org.junit.Assert.assertEquals;
3-
import static org.junit.Assert.assertThrows;
4-
51
import org.junit.Ignore;
62
import org.junit.Test;
73

4+
import static org.assertj.core.api.Assertions.assertThat;
5+
import static org.junit.Assert.assertThrows;
6+
87
public class AffineCipherTest {
98

109
private AffineCipher affineCipher = new AffineCipher();
1110

1211
@Test
1312
public void testEncodeYes() {
14-
assertEquals("xbt", affineCipher.encode("yes", 5, 7));
13+
assertThat(affineCipher.encode("yes", 5, 7)).isEqualTo("xbt");
1514
}
1615

1716
@Ignore("Remove to run test")
1817
@Test
1918
public void testEncodeNo() {
20-
assertEquals("fu", affineCipher.encode("no", 15, 18));
19+
assertThat(affineCipher.encode("no", 15, 18)).isEqualTo("fu");
2120
}
2221

2322

2423
@Ignore("Remove to run test")
2524
@Test
2625
public void testEncodeOMG() {
27-
assertEquals("lvz", affineCipher.encode("OMG", 21, 3));
26+
assertThat(affineCipher.encode("OMG", 21, 3)).isEqualTo("lvz");
2827
}
2928

3029
@Ignore("Remove to run test")
3130
@Test
3231
public void testEncodeO_M_G() {
33-
assertEquals("hjp", affineCipher.encode("O M G", 25, 47));
32+
assertThat(affineCipher.encode("O M G", 25, 47)).isEqualTo("hjp");
3433
}
3534

3635
@Ignore("Remove to run test")
3736
@Test
3837
public void testEncodeMindBlowingly() {
39-
assertEquals("rzcwa gnxzc dgt", affineCipher.encode("mindblowingly", 11, 15));
38+
assertThat(affineCipher.encode("mindblowingly", 11, 15)).isEqualTo("rzcwa gnxzc dgt");
4039
}
4140

4241
@Ignore("Remove to run test")
4342
@Test
4443
public void testEncodeNumbers() {
45-
assertEquals("jqgjc rw123 jqgjc rw",
46-
affineCipher.encode("Testing,1 2 3, testing.", 3, 4));
44+
assertThat(affineCipher.encode("Testing,1 2 3, testing.", 3, 4))
45+
.isEqualTo("jqgjc rw123 jqgjc rw");
4746
}
4847

4948
@Ignore("Remove to run test")
5049
@Test
5150
public void testEncodeDeepThought() {
52-
assertEquals("iynia fdqfb ifje",
53-
affineCipher.encode("Truth is fiction.", 5, 17));
51+
assertThat(affineCipher.encode("Truth is fiction.", 5, 17))
52+
.isEqualTo("iynia fdqfb ifje");
5453
}
5554

5655
@Ignore("Remove to run test")
5756
@Test
5857
public void testEncodeAllTheLetters() {
59-
assertEquals("swxtj npvyk lruol iejdc blaxk swxmh qzglf",
60-
affineCipher.encode("The quick brown fox jumps over the lazy dog.", 17, 33));
58+
assertThat(affineCipher.encode("The quick brown fox jumps over the lazy dog.", 17, 33))
59+
.isEqualTo("swxtj npvyk lruol iejdc blaxk swxmh qzglf");
6160
}
6261

6362
@Ignore("Remove to run test")
@@ -75,40 +74,43 @@ public void testEncodeThrowsMeaningfulException() {
7574
@Ignore("Remove to run test")
7675
@Test
7776
public void testDecodeExercism() {
78-
assertEquals("exercism", affineCipher.decode("tytgn fjr", 3, 7));
77+
assertThat(affineCipher.decode("tytgn fjr", 3, 7))
78+
.isEqualTo("exercism");
7979
}
8080

8181
@Ignore("Remove to run test")
8282
@Test
8383
public void testDecodeSentence() {
84-
assertEquals("anobstacleisoftenasteppingstone",
85-
affineCipher.decode("qdwju nqcro muwhn odqun oppmd aunwd o", 19, 16));
84+
assertThat(affineCipher.decode("qdwju nqcro muwhn odqun oppmd aunwd o", 19, 16))
85+
.isEqualTo("anobstacleisoftenasteppingstone");
8686
}
8787

8888
@Ignore("Remove to run test")
8989
@Test
9090
public void testDecodeNumbers() {
91-
assertEquals("testing123testing", affineCipher.decode("odpoz ub123 odpoz ub", 25, 7));
91+
assertThat(affineCipher.decode("odpoz ub123 odpoz ub", 25, 7))
92+
.isEqualTo("testing123testing");
9293
}
9394

9495
@Ignore("Remove to run test")
9596
@Test
9697
public void testDecodeAllTheLetters() {
97-
assertEquals("thequickbrownfoxjumpsoverthelazydog",
98-
affineCipher.decode("swxtj npvyk lruol iejdc blaxk swxmh qzglf", 17, 33));
98+
assertThat(affineCipher.decode("swxtj npvyk lruol iejdc blaxk swxmh qzglf", 17, 33))
99+
.isEqualTo("thequickbrownfoxjumpsoverthelazydog");
99100
}
100101

101102
@Ignore("Remove to run test")
102103
@Test
103104
public void testDecodeWithNoSpaces() {
104-
assertEquals("thequickbrownfoxjumpsoverthelazydog",
105-
affineCipher.decode("swxtjnpvyklruoliejdcblaxkswxmhqzglf", 17, 33));
105+
assertThat(affineCipher.decode("swxtjnpvyklruoliejdcblaxkswxmhqzglf", 17, 33))
106+
.isEqualTo("thequickbrownfoxjumpsoverthelazydog");
106107
}
107108

108109
@Ignore("Remove to run test")
109110
@Test
110111
public void testDecodeWithTooManySpaces() {
111-
assertEquals("jollygreengiant", affineCipher.decode("vszzm cly yd cg qdp", 15, 16));
112+
assertThat(affineCipher.decode("vszzm cly yd cg qdp", 15, 16))
113+
.isEqualTo("jollygreengiant");
112114
}
113115

114116
@Ignore("Remove to run test")

0 commit comments

Comments
 (0)