Skip to content

Commit 83e8cb9

Browse files
RonakLakhotiaFridaTveit
authored andcommitted
[exercism#1475] Update Cipher Tests (exercism#1639)
* Update version file v2.2.0 --> v2.3.0 * Add exceptions for value not found * Update tests * Remove unrelated changes * Remove checkstyle violations * Make Code quality changes * Make design changes * Fix code quality issues
1 parent 8816d8b commit 83e8cb9

6 files changed

Lines changed: 109 additions & 155 deletions

File tree

‎exercises/simple-cipher/.meta/src/reference/java/Cipher.java‎

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,15 +33,28 @@ public String getKey() {
3333

3434
public String encode(String plainText) {
3535
StringBuilder ciphertext = new StringBuilder(plainText.length());
36+
String sameLengthKey = getSufficientlyLongKeyForPlaintext(plainText, key);
3637

37-
for (int index = 0; index < Math.min(plainText.length(), key.length()); index++) {
38-
ciphertext.append(encodeCharacter(plainText, index));
38+
for (int index = 0; index < plainText.length(); index++) {
39+
ciphertext.append(encodeCharacter(plainText, index, sameLengthKey));
3940
}
4041

4142
return ciphertext.toString();
4243
}
4344

44-
private char encodeCharacter(String plainText, int index) {
45+
private String getSufficientlyLongKeyForPlaintext(String plainText, String key) {
46+
StringBuilder longKeyBuilder = new StringBuilder(key.length());
47+
longKeyBuilder.append(key);
48+
if (plainText.length() > key.length()) {
49+
int difference = plainText.length() - key.length();
50+
for (int i = 0; i < difference; i = i + key.length()) {
51+
longKeyBuilder.append(key);
52+
}
53+
}
54+
return longKeyBuilder.toString();
55+
}
56+
57+
private char encodeCharacter(String plainText, int index, String key) {
4558
int alphabetIdx = ALPHABET.indexOf(plainText.toCharArray()[index]) + ALPHABET.indexOf(key.toCharArray()[index]);
4659

4760
if (alphabetIdx >= ALPHABET.length()) {
@@ -62,8 +75,8 @@ public String decode(String cipherText) {
6275
}
6376

6477
private char decodeCharacter(String cipherText, int index) {
65-
int alphabetIdx = ALPHABET.indexOf(cipherText.toCharArray()[index]) -
66-
ALPHABET.indexOf(key.toCharArray()[index]);
78+
int alphabetIdx = ALPHABET
79+
.indexOf(cipherText.toCharArray()[index]) - ALPHABET.indexOf(key.toCharArray()[index]);
6780

6881
if (alphabetIdx < 0) {
6982
alphabetIdx += ALPHABET.length();
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
2.0.0

‎exercises/simple-cipher/src/test/java/SimpleCipherStepOneTest.java‎

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
import org.junit.Test;
44

55
import static org.junit.Assert.assertEquals;
6+
import static org.junit.Assert.assertTrue;
7+
8+
import java.util.regex.Pattern;
69

710
/**
811
* Step 1: Make a simple shift cipher
@@ -21,8 +24,9 @@ public void setup() {
2124
*/
2225
@Test
2326
public void cipherCanEncode() {
27+
String plainText = "aaaaaaaaaa";
2428
String cipherText = cipherWithDefaultKey.getKey().substring(0, 10);
25-
assertEquals(cipherText, cipherWithDefaultKey.encode("aaaaaaaaaa"));
29+
assertEquals(cipherText, cipherWithDefaultKey.encode(plainText));
2630
}
2731

2832
@Ignore("Remove to run test")
@@ -38,4 +42,11 @@ public void cipherIsReversible() {
3842
String plainText = "abcdefghij";
3943
assertEquals(plainText, cipherWithDefaultKey.decode(cipherWithDefaultKey.encode(plainText)));
4044
}
45+
46+
@Ignore("Remove to run test")
47+
@Test
48+
public void keyIsLowercaseLetters() {
49+
String key = cipherWithDefaultKey.getKey();
50+
assertTrue(Pattern.matches("^[a-z]+$", key));
51+
}
4152
}

‎exercises/simple-cipher/src/test/java/SimpleCipherStepThreeTest.java‎

Lines changed: 0 additions & 63 deletions
This file was deleted.
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
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+
import org.junit.Before;
9+
10+
public class SimpleCipherStepTwoSubsitutionTest {
11+
@Rule
12+
public ExpectedException expectedException = ExpectedException.none();
13+
14+
private Cipher cipherWithDefaultKey;
15+
private static final String key = "abcdefghij";
16+
17+
@Before
18+
public void setup() {
19+
cipherWithDefaultKey = new Cipher(key);
20+
}
21+
22+
@Ignore("Remove to run test")
23+
@Test
24+
public void cipherCanEncode() {
25+
String plainText = "aaaaaaaaaa";
26+
String cipherText = "abcdefghij";
27+
assertEquals(cipherText, cipherWithDefaultKey.encode(plainText));
28+
}
29+
30+
@Ignore("Remove to run test")
31+
@Test
32+
public void cipherCanDecode() {
33+
String plainText = "abcdefghij";
34+
String cipherText = "aaaaaaaaaa";
35+
assertEquals(cipherText, cipherWithDefaultKey.decode(plainText));
36+
}
37+
38+
@Ignore("Remove to run test")
39+
@Test
40+
public void cipherIsReversibleGivenKey() {
41+
String plainText = "abcdefghij";
42+
assertEquals(plainText, cipherWithDefaultKey.decode(cipherWithDefaultKey.encode(plainText)));
43+
}
44+
45+
@Ignore("Remove to run test")
46+
@Test
47+
public void cipherCanDoubleShiftEncode() {
48+
String plainText = "iamapandabear";
49+
String cipherText = "qayaeaagaciai";
50+
assertEquals(cipherText, new Cipher(plainText).encode(plainText));
51+
}
52+
53+
@Ignore("Remove to run test")
54+
@Test
55+
public void cipherCanWrapEncode() {
56+
String plainText = "zzzzzzzzzz";
57+
String cipherText = "zabcdefghi";
58+
assertEquals(cipherText, cipherWithDefaultKey.encode(plainText));
59+
}
60+
61+
@Ignore("Remove to run test")
62+
@Test
63+
public void cipherCanWrapDecode() {
64+
String plainText = "zabcdefghi";
65+
String cipherText = "zzzzzzzzzz";
66+
assertEquals(cipherText, cipherWithDefaultKey.decode(plainText));
67+
}
68+
69+
@Ignore("Remove to run test")
70+
@Test
71+
public void cipherMessageLongerThanKey() {
72+
String plainText = "iamapandabear";
73+
String key = "abc";
74+
String cipherText = "iboaqcnecbfcr";
75+
assertEquals(cipherText, new Cipher(key).encode(plainText));
76+
}
77+
78+
}

‎exercises/simple-cipher/src/test/java/SimpleCipherStepTwoTest.java‎

Lines changed: 0 additions & 86 deletions
This file was deleted.

0 commit comments

Comments
 (0)