From d9c4617427486a68c3bf869d902021eeed1f4816 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jegors=20=C4=8Cemisovs?= Date: Sat, 14 Nov 2026 20:56:57 +0200 Subject: [PATCH 1/3] Generate simple-cipher's test suite from the canonical data Add .meta/template.j2 and regenerate the suite in Scala 3 syntax, with the twelve canonical cases. New are "Can wrap on decode", the two cases with a message longer than the key, and the random key's lowercase check; the random-key encode and decode are now separate tests. The canonical values that are expressions on the cipher are written out in Scala. Keep the three invalid-key tests at the end of the template: they are not in the canonical data, but the instructions ask for a key of lowercase letters. Rewrite the example in Scala 3. It repeats the key for longer messages, and its random key is 100 letters drawn from all 26, as the instructions ask. Give the empty stub the `key`, `encode` and `decode` the tests call. Co-Authored-By: Claude Opus 5.5 --- .../simple-cipher/.meta/Example.scala | 35 ++---- .../practice/simple-cipher/.meta/config.json | 3 +- .../practice/simple-cipher/.meta/template.j2 | 49 +++++++++ .../simple-cipher/src/main/scala/Cipher.scala | 6 ++ .../src/test/scala/CipherTest.scala | 101 +++++++++++------- 5 files changed, 127 insertions(+), 67 deletions(-) create mode 100644 exercises/practice/simple-cipher/.meta/template.j2 diff --git a/exercises/practice/simple-cipher/.meta/Example.scala b/exercises/practice/simple-cipher/.meta/Example.scala index f5fbdf2d1..8a479ff7c 100644 --- a/exercises/practice/simple-cipher/.meta/Example.scala +++ b/exercises/practice/simple-cipher/.meta/Example.scala @@ -1,32 +1,17 @@ import scala.util.Random -case class Cipher(keyOpt: Option[String]) { - require(keyOpt.isEmpty || keyOpt.get.matches("^[a-z]+$")) +case class Cipher(keyOpt: Option[String]): + require(keyOpt.forall(_.matches("[a-z]+")), "the key must be lowercase letters") - private val alphabet = ('a' to 'z').toVector - private lazy val randomKey = Seq.fill(26)((Random.nextInt(25) + 'a').toChar).mkString + // Without a key, a random one of 100 lowercase letters. + val key: String = keyOpt.getOrElse(Seq.fill(100)(('a' + Random.nextInt(26)).toChar).mkString) - val key = keyOpt.getOrElse(randomKey) + def encode(text: String): String = shift(text, 1) - def encode(s: String): String = - (for (i <- 0 until s.length) yield encodeChar(s, i)).mkString + def decode(text: String): String = shift(text, -1) - private def encodeChar(plainText: String, index: Int) = { - var alphaIdx = alphabet.indexOf(plainText(index)) + alphabet.indexOf(key(index)) + // Each letter moves by the key's letter at the same position; the key repeats as often as needed. + private def shift(text: String, direction: Int): String = + text.zipWithIndex.map((letter, i) => rotate(letter, direction * (key(i % key.length) - 'a'))).mkString - if (alphaIdx >= alphabet.length) alphaIdx -= alphabet.length - - alphabet(alphaIdx) - } - - def decode(s: String): String = - (for (i <- 0 until s.length) yield decodeChar(s, i)).mkString - - private def decodeChar(cipherText: String, index: Int) = { - var alphaIdx = alphabet.indexOf(cipherText(index)) - alphabet.indexOf(key(index)) - - if (alphaIdx < 0) alphaIdx += alphabet.length - - alphabet(alphaIdx) - } -} + private def rotate(letter: Char, by: Int): Char = ('a' + Math.floorMod(letter - 'a' + by, 26)).toChar diff --git a/exercises/practice/simple-cipher/.meta/config.json b/exercises/practice/simple-cipher/.meta/config.json index 251fdda70..7b550aebb 100644 --- a/exercises/practice/simple-cipher/.meta/config.json +++ b/exercises/practice/simple-cipher/.meta/config.json @@ -5,7 +5,8 @@ "contributors": [ "ErikSchierboom", "ppartarr", - "rajeshpg" + "rajeshpg", + "rabestro" ], "files": { "solution": [ diff --git a/exercises/practice/simple-cipher/.meta/template.j2 b/exercises/practice/simple-cipher/.meta/template.j2 new file mode 100644 index 000000000..c1d52f580 --- /dev/null +++ b/exercises/practice/simple-cipher/.meta/template.j2 @@ -0,0 +1,49 @@ +{%- import "macros.j2" as macros -%} +{#- Some canonical values are expressions on the cipher (its random key, or encode's result); + they are written out in Scala here. -#} +{{ macros.header("Cipher") }} +{% for case in cases %} +{% set input = case.input %} +{% if case.property == "key" %} +{% set subject = "cipher.key" %} +{% elif case.property == "encode" %} +{% set subject = "cipher.encode(" ~ (input.plaintext | scala) ~ ")" %} +{% elif input.ciphertext == "cipher.encode" %} +{% set subject = "cipher.decode(cipher.encode(" ~ (input.plaintext | scala) ~ "))" %} +{% elif input.ciphertext.startswith("cipher.key") %} +{% set subject = "cipher.decode(cipher.key.substring(0, " ~ (case.expected | length) ~ "))" %} +{% else %} +{% set subject = "cipher.decode(" ~ (input.ciphertext | scala) ~ ")" %} +{% endif %} +{% if case.property == "key" %} +{% set expected = None %} +{% elif case.expected.startswith("cipher.key") %} +{% set expected = "cipher.key.substring(0, " ~ (input.plaintext | length) ~ ")" %} +{% else %} +{% set expected = case.expected | scala %} +{% endif %} + +{{ macros.test_open(case) }} +{% if not loop.first %} + pending +{% endif %} + val cipher = Cipher({{ "Some(" ~ (input.key | scala) ~ ")" if input.key is defined else "None" }}) +{% if case.property == "key" %} + cipher.key should fullyMatch regex {{ case.expected.match | scala }} +{% else %} +{{ macros.assert_be(subject, expected) }} +{% endif %} +{% endfor %} + + // Not in the canonical data: the instructions ask for a key of lowercase letters only. + test("Invalid key - contains caps"): + pending + assertThrows[IllegalArgumentException](Cipher(Some("ABCD"))) + + test("Invalid key - contains numerics"): + pending + assertThrows[IllegalArgumentException](Cipher(Some("123"))) + + test("Invalid key - is empty"): + pending + assertThrows[IllegalArgumentException](Cipher(Some(""))) diff --git a/exercises/practice/simple-cipher/src/main/scala/Cipher.scala b/exercises/practice/simple-cipher/src/main/scala/Cipher.scala index e69de29bb..48440bd73 100644 --- a/exercises/practice/simple-cipher/src/main/scala/Cipher.scala +++ b/exercises/practice/simple-cipher/src/main/scala/Cipher.scala @@ -0,0 +1,6 @@ +case class Cipher(keyOpt: Option[String]): + def key: String = ??? + + def encode(text: String): String = ??? + + def decode(text: String): String = ??? diff --git a/exercises/practice/simple-cipher/src/test/scala/CipherTest.scala b/exercises/practice/simple-cipher/src/test/scala/CipherTest.scala index 5385211d2..61dd4c9c6 100644 --- a/exercises/practice/simple-cipher/src/test/scala/CipherTest.scala +++ b/exercises/practice/simple-cipher/src/test/scala/CipherTest.scala @@ -1,65 +1,84 @@ import org.scalatest.funsuite.AnyFunSuite import org.scalatest.matchers.should.Matchers +/** Generated from the canonical data in problem-specifications. + * + * Do not edit by hand: change .meta/template.j2 and run bin/generate-tests. + */ +class CipherTest extends AnyFunSuite with Matchers: -/** @version created manually **/ -class CipherTest extends AnyFunSuite with Matchers { + test("Random key cipher - Can encode"): + val cipher = Cipher(None) + cipher.encode("aaaaaaaaaa") should be(cipher.key.substring(0, 10)) - test("Random key cipher - can encode/decode") { - // Here we take advantage of the fact that plaintext of "aaa..." - // outputs the key. This is a critical problem with shift ciphers, some - // characters will always output the key verbatim. + test("Random key cipher - Can decode"): + pending + val cipher = Cipher(None) + cipher.decode(cipher.key.substring(0, 10)) should be("aaaaaaaaaa") + test( + "Random key cipher - Is reversible. I.e., if you apply decode in a encoded result, you must see the same plaintext encode parameter as a result of the decode method", + ): + pending val cipher = Cipher(None) - cipher.encode("aaaaaaaaaa") should be (cipher.key.substring(0, 10)) - cipher.decode(cipher.key.substring(0, 10)) should be ("aaaaaaaaaa") - } + cipher.decode(cipher.encode("abcdefghij")) should be("abcdefghij") - test("Invalid key - contains caps") { + test("Random key cipher - Key is made only of lowercase letters"): pending - intercept[IllegalArgumentException] { - Cipher(Some("ABCD")) - } - } + val cipher = Cipher(None) + cipher.key should fullyMatch regex "^[a-z]+$" - test("Invalid key - contains numerics") { + test("Substitution cipher - Can encode"): pending - intercept[IllegalArgumentException] { - Cipher(Some("123")) - } - } + val cipher = Cipher(Some("abcdefghij")) + cipher.encode("aaaaaaaaaa") should be("abcdefghij") - test("Invalid key - is empty") { + test("Substitution cipher - Can decode"): pending - intercept[IllegalArgumentException] { - Cipher(Some("")) - } - } + val cipher = Cipher(Some("abcdefghij")) + cipher.decode("abcdefghij") should be("aaaaaaaaaa") - test("Substitution cipher - can encode") { + test( + "Substitution cipher - Is reversible. I.e., if you apply decode in a encoded result, you must see the same plaintext encode parameter as a result of the decode method", + ): pending - Cipher(Some("abcdefghij")).encode("aaaaaaaaaa") should be ("abcdefghij") - } + val cipher = Cipher(Some("abcdefghij")) + cipher.decode(cipher.encode("abcdefghij")) should be("abcdefghij") - test("Substitution cipher - can decode") { + test("Substitution cipher - Can double shift encode"): pending - Cipher(Some("abcdefghij")).decode("abcdefghij") should be ("aaaaaaaaaa") - } + val cipher = Cipher(Some("iamapandabear")) + cipher.encode("iamapandabear") should be("qayaeaagaciai") - test("Substitution cipher - is reversible") { + test("Substitution cipher - Can wrap on encode"): pending val cipher = Cipher(Some("abcdefghij")) - cipher.decode(cipher.encode("abcdefghij")) should be ("abcdefghij") - } + cipher.encode("zzzzzzzzzz") should be("zabcdefghi") - test("Substitution cipher - can double shift") { + test("Substitution cipher - Can wrap on decode"): pending - val cipher = Cipher(Some("iamapandabear")) - cipher.encode("iamapandabear") should be ("qayaeaagaciai") - } + val cipher = Cipher(Some("abcdefghij")) + cipher.decode("zabcdefghi") should be("zzzzzzzzzz") + + test("Substitution cipher - Can encode messages longer than the key"): + pending + val cipher = Cipher(Some("abc")) + cipher.encode("iamapandabear") should be("iboaqcnecbfcr") + + test("Substitution cipher - Can decode messages longer than the key"): + pending + val cipher = Cipher(Some("abc")) + cipher.decode("iboaqcnecbfcr") should be("iamapandabear") + + // Not in the canonical data: the instructions ask for a key of lowercase letters only. + test("Invalid key - contains caps"): + pending + assertThrows[IllegalArgumentException](Cipher(Some("ABCD"))) + + test("Invalid key - contains numerics"): + pending + assertThrows[IllegalArgumentException](Cipher(Some("123"))) - test("Substitution cipher - can wrap") { + test("Invalid key - is empty"): pending - Cipher(Some("abcdefghij")).encode("zzzzzzzzzz") should be ("zabcdefghi") - } -} + assertThrows[IllegalArgumentException](Cipher(Some(""))) From e6a6dfe9d12c15f68a1be5979c3ee6049947234f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jegors=20=C4=8Cemisovs?= Date: Sat, 14 Nov 2026 21:21:15 +0200 Subject: [PATCH 2/3] simple-cipher: write the random-key cases out in the template The four random-key cases have expressions on the cipher as their canonical values, which made the template translate them case by case. Write those tests out by hand instead, and generate only the cases with a key. The rendered suite is unchanged. Co-Authored-By: Claude Opus 5.5 --- .../practice/simple-cipher/.meta/template.j2 | 57 ++++++++++--------- 1 file changed, 29 insertions(+), 28 deletions(-) diff --git a/exercises/practice/simple-cipher/.meta/template.j2 b/exercises/practice/simple-cipher/.meta/template.j2 index c1d52f580..f198aaf48 100644 --- a/exercises/practice/simple-cipher/.meta/template.j2 +++ b/exercises/practice/simple-cipher/.meta/template.j2 @@ -1,37 +1,38 @@ {%- import "macros.j2" as macros -%} -{#- Some canonical values are expressions on the cipher (its random key, or encode's result); - they are written out in Scala here. -#} +{#- The random-key cases are written out by hand: their canonical values are expressions on the cipher. + The cases with a key are generated. -#} {{ macros.header("Cipher") }} -{% for case in cases %} -{% set input = case.input %} -{% if case.property == "key" %} -{% set subject = "cipher.key" %} -{% elif case.property == "encode" %} -{% set subject = "cipher.encode(" ~ (input.plaintext | scala) ~ ")" %} -{% elif input.ciphertext == "cipher.encode" %} -{% set subject = "cipher.decode(cipher.encode(" ~ (input.plaintext | scala) ~ "))" %} -{% elif input.ciphertext.startswith("cipher.key") %} -{% set subject = "cipher.decode(cipher.key.substring(0, " ~ (case.expected | length) ~ "))" %} -{% else %} -{% set subject = "cipher.decode(" ~ (input.ciphertext | scala) ~ ")" %} -{% endif %} -{% if case.property == "key" %} -{% set expected = None %} -{% elif case.expected.startswith("cipher.key") %} -{% set expected = "cipher.key.substring(0, " ~ (input.plaintext | length) ~ ")" %} -{% else %} -{% set expected = case.expected | scala %} -{% endif %} + + test("Random key cipher - Can encode"): + val cipher = Cipher(None) + cipher.encode("aaaaaaaaaa") should be(cipher.key.substring(0, 10)) + + test("Random key cipher - Can decode"): + pending + val cipher = Cipher(None) + cipher.decode(cipher.key.substring(0, 10)) should be("aaaaaaaaaa") + + test( + "Random key cipher - Is reversible. I.e., if you apply decode in a encoded result, you must see the same plaintext encode parameter as a result of the decode method", + ): + pending + val cipher = Cipher(None) + cipher.decode(cipher.encode("abcdefghij")) should be("abcdefghij") + + test("Random key cipher - Key is made only of lowercase letters"): + pending + val cipher = Cipher(None) + cipher.key should fullyMatch regex "^[a-z]+$" +{% for case in cases if case.input.key is defined %} +{% set text = case.input.plaintext if case.property == "encode" else case.input.ciphertext %} {{ macros.test_open(case) }} -{% if not loop.first %} pending -{% endif %} - val cipher = Cipher({{ "Some(" ~ (input.key | scala) ~ ")" if input.key is defined else "None" }}) -{% if case.property == "key" %} - cipher.key should fullyMatch regex {{ case.expected.match | scala }} + val cipher = Cipher(Some({{ case.input.key | scala }})) +{% if text == "cipher.encode" %} + cipher.decode(cipher.encode({{ case.input.plaintext | scala }})) should be({{ case.expected | scala }}) {% else %} -{{ macros.assert_be(subject, expected) }} + cipher.{{ case.property }}({{ text | scala }}) should be({{ case.expected | scala }}) {% endif %} {% endfor %} From c58b2b7686a80c9c9f913619d039e440124cca11 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jegors=20=C4=8Cemisovs?= Date: Sat, 14 Nov 2026 21:30:22 +0200 Subject: [PATCH 3/3] simple-cipher: generate the random-key cases from the canonical data too Go back to one loop over the canonical cases, as review asked, with one branch per kind of case: the key check, "is reversible", a case with a key, and encoding or decoding with the random key. The rendered suite is unchanged. Co-Authored-By: Claude Opus 5.5 --- .../practice/simple-cipher/.meta/template.j2 | 44 +++++++------------ 1 file changed, 15 insertions(+), 29 deletions(-) diff --git a/exercises/practice/simple-cipher/.meta/template.j2 b/exercises/practice/simple-cipher/.meta/template.j2 index f198aaf48..b649f8cd2 100644 --- a/exercises/practice/simple-cipher/.meta/template.j2 +++ b/exercises/practice/simple-cipher/.meta/template.j2 @@ -1,38 +1,24 @@ {%- import "macros.j2" as macros -%} -{#- The random-key cases are written out by hand: their canonical values are expressions on the cipher. - The cases with a key are generated. -#} +{#- Without a key the cipher picks a random one, so those cases check against `cipher.key`. -#} {{ macros.header("Cipher") }} - - test("Random key cipher - Can encode"): - val cipher = Cipher(None) - cipher.encode("aaaaaaaaaa") should be(cipher.key.substring(0, 10)) - - test("Random key cipher - Can decode"): - pending - val cipher = Cipher(None) - cipher.decode(cipher.key.substring(0, 10)) should be("aaaaaaaaaa") - - test( - "Random key cipher - Is reversible. I.e., if you apply decode in a encoded result, you must see the same plaintext encode parameter as a result of the decode method", - ): - pending - val cipher = Cipher(None) - cipher.decode(cipher.encode("abcdefghij")) should be("abcdefghij") - - test("Random key cipher - Key is made only of lowercase letters"): - pending - val cipher = Cipher(None) - cipher.key should fullyMatch regex "^[a-z]+$" -{% for case in cases if case.input.key is defined %} -{% set text = case.input.plaintext if case.property == "encode" else case.input.ciphertext %} +{% for case in cases %} +{% set input = case.input %} {{ macros.test_open(case) }} +{% if not loop.first %} pending - val cipher = Cipher(Some({{ case.input.key | scala }})) -{% if text == "cipher.encode" %} - cipher.decode(cipher.encode({{ case.input.plaintext | scala }})) should be({{ case.expected | scala }}) +{% endif %} + val cipher = Cipher({{ "Some(" ~ (input.key | scala) ~ ")" if input.key is defined else "None" }}) +{% if case.property == "key" %} + cipher.key should fullyMatch regex {{ case.expected.match | scala }} +{% elif input.get("ciphertext") == "cipher.encode" %} + cipher.decode(cipher.encode({{ input.plaintext | scala }})) should be({{ case.expected | scala }}) +{% elif input.key is defined %} + cipher.{{ case.property }}({{ (input.plaintext if case.property == "encode" else input.ciphertext) | scala }}) should be({{ case.expected | scala }}) +{% elif case.property == "encode" %} + cipher.encode({{ input.plaintext | scala }}) should be(cipher.key.substring(0, {{ input.plaintext | length }})) {% else %} - cipher.{{ case.property }}({{ text | scala }}) should be({{ case.expected | scala }}) + cipher.decode(cipher.key.substring(0, {{ case.expected | length }})) should be({{ case.expected | scala }}) {% endif %} {% endfor %}