Преглед изворни кода

Issue #152: Made simple-cipher more approachable (#224)

* Issue #152: Made simple-cipher more approachable by splitting test classes in a more sensible way and making clear which steps they relate to.

* Changed SimpleCipherTest to use the junit Rule annotation for expected exceptions. Changed variable names in SimpleCipherTest to be more descriptive and moved the test cipherThrowsWithEmptyKey from step three to step two.

* Added HINT.md which explains how to run only one test class.

* Changed HINT.md in simple-cipher to refer to the actual test file names and not to contain the same information as in SETUP.md
FridaTveit пре 9 година
родитељ
комит
13b0e59711

+ 6
- 0
exercises/simple-cipher/HINT.md Прегледај датотеку

@@ -0,0 +1,6 @@
1
+This exercise has three test classes: SimpleCipherStepOneTest.java, SimpleCipherStepTwoTest.java and SimpleCipherStepThreeTest.java.
2
+
3
+To run only one test class (e.g. in this example SimpleCipherStepOneTest.java):
4
+```
5
+$ gradle test --tests *StepOne*
6
+```

+ 0
- 35
exercises/simple-cipher/src/test/java/IncorrectKeyCipherTest.java Прегледај датотеку

@@ -1,35 +0,0 @@
1
-import org.junit.Test;
2
-import org.junit.Ignore;
3
-
4
-public class IncorrectKeyCipherTest {
5
-
6
-
7
-    @Test(expected = IllegalArgumentException.class)
8
-    public void cipherThrowsWithAllCapsKey() {
9
-        new Cipher("ABCDEF");
10
-    }
11
-
12
-    @Ignore
13
-    @Test(expected = IllegalArgumentException.class)
14
-    public void cipherThrowsWithAnyCapsKey() {
15
-        new Cipher("abcdEFg");
16
-    }
17
-
18
-    @Ignore
19
-    @Test(expected = IllegalArgumentException.class)
20
-    public void cipherThrowsWithNumericKey() {
21
-        new Cipher("12345");
22
-    }
23
-
24
-    @Ignore
25
-    @Test(expected = IllegalArgumentException.class)
26
-    public void cipherThrowsWithAnyNumericKey() {
27
-        new Cipher("abcd345ef");
28
-    }
29
-
30
-    @Ignore
31
-    @Test(expected = IllegalArgumentException.class)
32
-    public void cipherThrowsWithEmptyKey() {
33
-        new Cipher("");
34
-    }
35
-}

+ 0
- 62
exercises/simple-cipher/src/test/java/RandomKeyCipherTest.java Прегледај датотеку

@@ -1,62 +0,0 @@
1
-import org.junit.Before;
2
-import org.junit.Test;
3
-import org.junit.Ignore;
4
-
5
-import static org.junit.Assert.assertEquals;
6
-import static org.junit.Assert.assertTrue;
7
-
8
-public class RandomKeyCipherTest {
9
-
10
-    private Cipher cipher;
11
-
12
-    @Before
13
-    public void setup() {
14
-        this.cipher = new Cipher();
15
-    }
16
-
17
-
18
-    @Test
19
-    public void cipherKeyIsMadeOfLetters() {
20
-        assertTrue(cipher.getKey().matches("[a-z]+"));
21
-    }
22
-
23
-    @Ignore
24
-    @Test
25
-    public void defaultCipherKeyIs100Characters() {
26
-        assertEquals(100, cipher.getKey().length());
27
-    }
28
-
29
-    @Ignore
30
-    @Test
31
-    public void cipherKeysAreRandomlyGenerated() {
32
-        assertTrue(!(new Cipher().getKey().equals(cipher.getKey())));
33
-    }
34
-
35
-    /**
36
-     * Here we take advantage of the fact that plaintext of "aaa..." doesn't output the key. This is a critical problem
37
-     * with shift ciphers, some characters will always output the key verbatim.
38
-     */
39
-    @Ignore
40
-    @Test
41
-    public void cipherCanEncode() {
42
-        String expectedOutput = cipher.getKey().substring(0, 10);
43
-
44
-        assertEquals(expectedOutput, cipher.encode("aaaaaaaaaa"));
45
-    }
46
-
47
-    @Ignore
48
-    @Test
49
-    public void cipherCanDecode() {
50
-        String expectedOutput = "aaaaaaaaaa";
51
-
52
-        assertEquals(expectedOutput, cipher.decode(cipher.getKey().substring(0, 10)));
53
-    }
54
-
55
-    @Ignore
56
-    @Test
57
-    public void cipherIsReversible() {
58
-        String plainText = "abcdefghij";
59
-
60
-        assertEquals(plainText, cipher.decode(cipher.encode(plainText)));
61
-    }
62
-}

+ 41
- 0
exercises/simple-cipher/src/test/java/SimpleCipherStepOneTest.java Прегледај датотеку

@@ -0,0 +1,41 @@
1
+import org.junit.Before;
2
+import org.junit.Ignore;
3
+import org.junit.Test;
4
+
5
+import static org.junit.Assert.assertEquals;
6
+
7
+/**
8
+ * Step 1: Make a simple shift cipher
9
+ */
10
+public class SimpleCipherStepOneTest {
11
+    private Cipher cipherWithDefaultKey;
12
+
13
+    @Before
14
+    public void setup() {
15
+        cipherWithDefaultKey = new Cipher();
16
+    }
17
+
18
+    /**
19
+     * Here we take advantage of the fact that plaintext of "aaa..." doesn't output the key. This is a critical problem
20
+     * with shift ciphers, some characters will always output the key verbatim.
21
+     */
22
+    @Test
23
+    public void cipherCanEncode() {
24
+        String cipherText = cipherWithDefaultKey.getKey().substring(0, 10);
25
+        assertEquals(cipherText, cipherWithDefaultKey.encode("aaaaaaaaaa"));
26
+    }
27
+
28
+    @Ignore
29
+    @Test
30
+    public void cipherCanDecode() {
31
+        String cipherText = "aaaaaaaaaa";
32
+        assertEquals(cipherText, cipherWithDefaultKey.decode(cipherWithDefaultKey.getKey().substring(0, 10)));
33
+    }
34
+
35
+    @Ignore
36
+    @Test
37
+    public void cipherIsReversible() {
38
+        String plainText = "abcdefghij";
39
+        assertEquals(plainText, cipherWithDefaultKey.decode(cipherWithDefaultKey.encode(plainText)));
40
+    }
41
+}

+ 63
- 0
exercises/simple-cipher/src/test/java/SimpleCipherStepThreeTest.java Прегледај датотеку

@@ -0,0 +1,63 @@
1
+import org.junit.Ignore;
2
+import org.junit.Rule;
3
+import org.junit.Test;
4
+import org.junit.rules.ExpectedException;
5
+
6
+import static org.junit.Assert.*;
7
+
8
+/**
9
+ * Step 3: Generate random key if key isn't specified. Check key is right format
10
+ */
11
+public class SimpleCipherStepThreeTest {
12
+    @Rule
13
+    public ExpectedException expectedException = ExpectedException.none();
14
+
15
+    @Ignore
16
+    @Test
17
+    public void cipherKeyIsMadeOfLetters() {
18
+        assertTrue(new Cipher().getKey().matches("[a-z]+"));
19
+    }
20
+
21
+    @Ignore
22
+    @Test
23
+    public void defaultCipherKeyIs100Characters() {
24
+        assertEquals(100, new Cipher().getKey().length());
25
+    }
26
+
27
+    @Ignore
28
+    @Test
29
+    public void cipherKeysAreRandomlyGenerated() {
30
+        String newKey = new Cipher().getKey();
31
+        assertFalse("Cipher constructor without argument should generate a random key. No two calls to the" +
32
+                " constructor should generate the same key. Two calls to the constructor " +
33
+                "both returned key: " + newKey, newKey.equals(new Cipher().getKey()));
34
+    }
35
+
36
+    @Ignore
37
+    @Test
38
+    public void cipherThrowsWithAllCapsKey() {
39
+        expectedException.expect(IllegalArgumentException.class);
40
+        new Cipher("ABCDEF");
41
+    }
42
+
43
+    @Ignore
44
+    @Test
45
+    public void cipherThrowsWithAnyCapsKey() {
46
+        expectedException.expect(IllegalArgumentException.class);
47
+        new Cipher("abcdEFg");
48
+    }
49
+
50
+    @Ignore
51
+    @Test
52
+    public void cipherThrowsWithNumericKey() {
53
+        expectedException.expect(IllegalArgumentException.class);
54
+        new Cipher("12345");
55
+    }
56
+
57
+    @Ignore
58
+    @Test
59
+    public void cipherThrowsWithAnyNumericKey() {
60
+        expectedException.expect(IllegalArgumentException.class);
61
+        new Cipher("abcd345ef");
62
+    }
63
+}

+ 86
- 0
exercises/simple-cipher/src/test/java/SimpleCipherStepTwoTest.java Прегледај датотеку

@@ -0,0 +1,86 @@
1
+import org.junit.Before;
2
+import org.junit.Ignore;
3
+import org.junit.Rule;
4
+import org.junit.Test;
5
+import org.junit.rules.ExpectedException;
6
+
7
+import static org.junit.Assert.assertEquals;
8
+
9
+/**
10
+ * Step 2: Specify key and use that for shift distance : substitution cipher
11
+ */
12
+public class SimpleCipherStepTwoTest {
13
+    private Cipher cipherWithSetKey;
14
+    private static final String key = "abcdefghij";
15
+
16
+    @Rule
17
+    public ExpectedException expectedException = ExpectedException.none();
18
+
19
+    @Before
20
+    public void setup() {
21
+        cipherWithSetKey = new Cipher(key);
22
+    }
23
+
24
+    @Ignore
25
+    @Test
26
+    public void cipherKeepsTheSubmittedKey() {
27
+        assertEquals(key, cipherWithSetKey.getKey());
28
+    }
29
+
30
+    @Ignore
31
+    @Test
32
+    public void cipherThrowsWithEmptyKey() {
33
+        expectedException.expect(IllegalArgumentException.class);
34
+        new Cipher("");
35
+    }
36
+
37
+    @Ignore
38
+    @Test
39
+    public void cipherCanEncodeWithGivenKey() {
40
+        String cipherText = "abcdefghij";
41
+        assertEquals(cipherText, cipherWithSetKey.encode("aaaaaaaaaa"));
42
+    }
43
+
44
+    @Ignore
45
+    @Test
46
+    public void cipherCanDecodeWithGivenKey() {
47
+        String cipherText = "aaaaaaaaaa";
48
+        assertEquals(cipherText, cipherWithSetKey.decode("abcdefghij"));
49
+    }
50
+
51
+    @Ignore
52
+    @Test
53
+    public void cipherIsReversibleGivenKey() {
54
+        String plainText = "abcdefghij";
55
+        assertEquals(plainText, cipherWithSetKey.decode(cipherWithSetKey.encode("abcdefghij")));
56
+    }
57
+
58
+    @Ignore
59
+    @Test
60
+    public void cipherCanWrapEncode() {
61
+        String cipherText = "zabcdefghi";
62
+        assertEquals(cipherText, cipherWithSetKey.encode("zzzzzzzzzz"));
63
+    }
64
+
65
+    @Ignore
66
+    @Test
67
+    public void cipherCanEncodeMessageThatIsShorterThanTheKey() {
68
+        String cipherText = "abcde";
69
+        assertEquals(cipherText, cipherWithSetKey.encode("aaaaa"));
70
+    }
71
+
72
+    @Ignore
73
+    @Test
74
+    public void cipherCanDecodeMessageThatIsShorterThanTheKey() {
75
+        String cipherText = "aaaaa";
76
+        assertEquals(cipherText, cipherWithSetKey.decode("abcde"));
77
+    }
78
+
79
+    @Ignore
80
+    @Test
81
+    public void cipherCanDoubleShiftEncode() {
82
+        String plainText = "iamapandabear";
83
+        String cipherText = "qayaeaagaciai";
84
+        assertEquals(cipherText, new Cipher(plainText).encode(plainText));
85
+    }
86
+}

+ 0
- 11
exercises/simple-cipher/src/test/java/SimpleCipherTest.java Прегледај датотеку

@@ -1,11 +0,0 @@
1
-import org.junit.runner.RunWith;
2
-import org.junit.runners.Suite;
3
-
4
-@RunWith(Suite.class)
5
-@Suite.SuiteClasses({
6
-        RandomKeyCipherTest.class,
7
-        IncorrectKeyCipherTest.class,
8
-        SubstitutionCipherTest.class
9
-})
10
-public class SimpleCipherTest {
11
-}

+ 0
- 79
exercises/simple-cipher/src/test/java/SubstitutionCipherTest.java Прегледај датотеку

@@ -1,79 +0,0 @@
1
-import org.junit.Before;
2
-import org.junit.Test;
3
-import org.junit.Ignore;
4
-
5
-import static org.junit.Assert.assertEquals;
6
-
7
-public class SubstitutionCipherTest {
8
-
9
-    private static final String KEY = "abcdefghij";
10
-    private Cipher cipher;
11
-
12
-    @Before
13
-    public void setup() {
14
-        this.cipher = new Cipher(KEY);
15
-    }
16
-
17
-
18
-    @Test
19
-    public void cipherKeepsTheSubmittedKey() {
20
-        assertEquals(KEY, cipher.getKey());
21
-    }
22
-
23
-    @Ignore
24
-    @Test
25
-    public void cipherCanEncodeWithGivenKey() {
26
-        String expectedOutput = "abcdefghij";
27
-
28
-        assertEquals(expectedOutput, cipher.encode("aaaaaaaaaa"));
29
-    }
30
-
31
-    @Ignore
32
-    @Test
33
-    public void cipherCanDecodeWithGivenKey() {
34
-        String expectedOutput = "aaaaaaaaaa";
35
-
36
-        assertEquals(expectedOutput, cipher.decode("abcdefghij"));
37
-    }
38
-
39
-    @Ignore
40
-    @Test
41
-    public void cipherIsReversibleGivenKey() {
42
-        String plainText = "abcdefghij";
43
-
44
-        assertEquals(plainText, cipher.decode(cipher.encode("abcdefghij")));
45
-    }
46
-
47
-    @Ignore
48
-    @Test
49
-    public void cipherCanDoubleShiftEncode() {
50
-        String plainText = "iamapandabear";
51
-        String expectedOutput = "qayaeaagaciai";
52
-
53
-        assertEquals(expectedOutput, new Cipher(plainText).encode(plainText));
54
-    }
55
-
56
-    @Ignore
57
-    @Test
58
-    public void cipherCanWrapEncode() {
59
-        String expectedOutput = "zabcdefghi";
60
-
61
-        assertEquals(expectedOutput, cipher.encode("zzzzzzzzzz"));
62
-    }
63
-
64
-    @Ignore
65
-    @Test
66
-    public void cipherCanEncodeMessageThatIsShorterThanTheKey() {
67
-        String expectedOutput = "abcde";
68
-
69
-        assertEquals(expectedOutput, cipher.encode("aaaaa"));
70
-    }
71
-
72
-    @Ignore
73
-    @Test
74
-    public void cipherCanDecodeMessageThatIsShorterThanTheKey() {
75
-        String expectedOutput = "aaaaa";
76
-
77
-        assertEquals(expectedOutput, cipher.decode("abcde"));
78
-    }
79
-}