|
| 1 | +script "TestCiphers" |
| 2 | + |
| 3 | +on TestSetup |
| 4 | + local tSourceFile |
| 5 | + put the filename of me into tSourceFile |
| 6 | + set the itemdelimiter to slash |
| 7 | + put "src/PuzzleTools.livecodescript" into item -3 to -1 of tSourceFile |
| 8 | + start using stack tSourceFile |
| 9 | +end TestSetup |
| 10 | + |
| 11 | +private command TestAssertUserThrows pDesc, pHandler, pError |
| 12 | + local tError |
| 13 | + try |
| 14 | + dispatch pHandler to me |
| 15 | + catch tError |
| 16 | + end try |
| 17 | + |
| 18 | + TestAssert pDesc, tError is pError |
| 19 | + |
| 20 | + if tError is not pError then |
| 21 | + TestDiagnostic "Expected error:" && pError |
| 22 | + TestDiagnostic "Actual error:" && tError |
| 23 | + end if |
| 24 | +end TestAssertUserThrows |
| 25 | + |
| 26 | +on LetterToNumberThrow |
| 27 | + get toNumber("'") |
| 28 | +end LetterToNumberThrow |
| 29 | + |
| 30 | +on TestLetterToNumber |
| 31 | + local tIndex |
| 32 | + put 0 into tIndex |
| 33 | + repeat for each char tChar in "ABCDEFGHIJKLMNOPQRSTUVWXYZ" |
| 34 | + add 1 to tIndex |
| 35 | + TestAssert "uppercase letter" && tChar && "to number correct", \ |
| 36 | + toNumber(tChar) is tIndex |
| 37 | + end repeat |
| 38 | + |
| 39 | + put 0 into tIndex |
| 40 | + repeat for each char tChar in "abcdefghijklmnopqrstuvwxyz" |
| 41 | + add 1 to tIndex |
| 42 | + TestAssert "lowercase letter" && tChar && "to number correct", \ |
| 43 | + toNumber(tChar) is tIndex |
| 44 | + end repeat |
| 45 | + |
| 46 | + TestAssertUserThrows "non alphabetic input to toNumber function throws", \ |
| 47 | + "LetterToNumberThrow", "char not alphabetical" |
| 48 | +end TestLetterToNumber |
| 49 | + |
| 50 | +on TestLetterFromNumber |
| 51 | + local tIndex |
| 52 | + put 0 into tIndex |
| 53 | + repeat for each char tChar in "ABCDEFGHIJKLMNOPQRSTUVWXYZ" |
| 54 | + add 1 to tIndex |
| 55 | + TestAssert "uppercase letter" && tChar && "from number correct", \ |
| 56 | + fromNumber(tIndex) is tChar |
| 57 | + end repeat |
| 58 | + |
| 59 | + put 0 into tIndex |
| 60 | + repeat for each char tChar in "abcdefghijklmnopqrstuvwxyz" |
| 61 | + add 1 to tIndex |
| 62 | + TestAssert "lowercase letter" && tChar && "from number correct", \ |
| 63 | + fromNumber(tIndex) is tChar |
| 64 | + end repeat |
| 65 | +end TestLetterFromNumber |
| 66 | + |
| 67 | +command _TestCaesarCipher pDesc, pSource, pShift, pExpected |
| 68 | + local tResult, tSuccess |
| 69 | + put caesarShift(pSource, pShift) into tResult |
| 70 | + put tResult is pExpected into tSuccess |
| 71 | + TestAssert pDesc, tSuccess |
| 72 | + |
| 73 | + // If the test failed then log the actual output of caesarShift |
| 74 | + if not tSuccess then |
| 75 | + TestDiagnostic pDesc && "failed" |
| 76 | + TestDiagnostic "Expected result:" && pExpected |
| 77 | + TestDiagnostic "Actual result:" && tResult |
| 78 | + end if |
| 79 | +end _TestCaesarCipher |
| 80 | + |
| 81 | +on TestCaesarCipher |
| 82 | + _TestCaesarCipher "abjurer <-> nowhere (13)", "abjurer", 13, "nowhere" |
| 83 | + _TestCaesarCipher "inkier <-> purply (7)", "inkier", 7, "purply" |
| 84 | + _TestCaesarCipher "fusion <-> layout (6)", "fusion", 6, "layout" |
| 85 | + _TestCaesarCipher "manful <-> thumbs (7)", "manful", 7, "thumbs" |
| 86 | + _TestCaesarCipher "primero <-> sulphur (3)", "primero", 3, "sulphur" |
| 87 | + _TestCaesarCipher "steeds <-> tuffet (1)", "steeds", 1, "tuffet" |
| 88 | +end TestCaesarCipher |
| 89 | + |
| 90 | +on TestVigenereCipher |
| 91 | + TestAssert "LIVECODE encoded using vigenere cipher with key TEST is EMNXVSVX", \ |
| 92 | + vigenereShift("LIVECODE","TEST") is "EMNXVSVX" |
| 93 | +end TestVigenereCipher |
| 94 | + |
0 commit comments