-
-
Notifications
You must be signed in to change notification settings - Fork 195
crypto-square: Rewrite tests to use hspec with fail-fast. #249
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,4 +17,4 @@ tests: | |
source-dirs: test | ||
dependencies: | ||
- crypto-square | ||
- HUnit | ||
- hspec |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,60 +1,93 @@ | ||
import Test.HUnit ((@=?), runTestTT, Test(..), Counts(..)) | ||
import System.Exit (ExitCode(..), exitWith) | ||
import CryptoSquare (normalizePlaintext, | ||
squareSize, | ||
plaintextSegments, | ||
ciphertext, | ||
normalizeCiphertext) | ||
|
||
exitProperly :: IO Counts -> IO () | ||
exitProperly m = do | ||
counts <- m | ||
exitWith $ if failures counts /= 0 || errors counts /= 0 then ExitFailure 1 else ExitSuccess | ||
import Test.Hspec (Spec, describe, it, shouldBe) | ||
import Test.Hspec.Runner (configFastFail, defaultConfig, hspecWith) | ||
|
||
import CryptoSquare | ||
( ciphertext | ||
, normalizeCiphertext | ||
, normalizePlaintext | ||
, plaintextSegments | ||
) | ||
|
||
main :: IO () | ||
main = exitProperly $ runTestTT $ TestList | ||
[ TestLabel "normalizePlaintext" $ TestList normalizePlaintextTests | ||
, TestLabel "squareSize" $ TestList squareSizeTests | ||
, TestLabel "plaintextSegments" $ TestList plaintextSegmentsTests | ||
, TestLabel "ciphertext" $ TestList ciphertextTests | ||
, TestLabel "normalizeCiphertext" $ TestList normalizeCiphertextTests | ||
] | ||
|
||
normalizePlaintextTests :: [Test] | ||
normalizePlaintextTests = map TestCase | ||
[ "splunk" @=? normalizePlaintext "s#!@$%plunk" | ||
, "123go" @=? normalizePlaintext "1, 2, 3 GO!" | ||
] | ||
|
||
squareSizeTests :: [Test] | ||
squareSizeTests = map TestCase | ||
[ 2 @=? squareSize "1234" | ||
, 3 @=? squareSize "123456789" | ||
, 4 @=? squareSize "123456789abc" ] | ||
|
||
plaintextSegmentsTests :: [Test] | ||
plaintextSegmentsTests = map TestCase | ||
[ ["neverv", "exthin", "eheart", "withid", "lewoes"] @=? | ||
plaintextSegments "Never vex thine heart with idle woes." | ||
, ["zomg", "zomb", "ies"] @=? | ||
plaintextSegments "ZOMG! ZOMBIES!!!" | ||
] | ||
|
||
ciphertextTests :: [Test] | ||
ciphertextTests = map TestCase | ||
[ "tasneyinicdsmiohooelntuillibsuuml" @=? | ||
ciphertext "Time is an illusion. Lunchtime doubly so." | ||
, "wneiaweoreneawssciliprerlneoidktcms" @=? | ||
ciphertext "We all know interspecies romance is weird." | ||
] | ||
|
||
normalizeCiphertextTests :: [Test] | ||
normalizeCiphertextTests = map TestCase | ||
[ "msemo aanin dnin ndla etlt shui" @=? | ||
normalizeCiphertext "Madness, and then illumination." | ||
, "vrel aepe mset paoo irpo" @=? | ||
normalizeCiphertext "Vampires are people too!" | ||
, "imtgdvs fearwer mayoogo anouuio ntnnlvt wttddes aohghn sseoau" @=? | ||
normalizeCiphertext "If man was meant to stay on the ground god would \ | ||
\have given us roots" | ||
] | ||
main = hspecWith defaultConfig {configFastFail = True} specs | ||
|
||
specs :: Spec | ||
specs = describe "crypto-square" $ do | ||
|
||
-- Test cases adapted from `exercism/x-common/crypto-square.json` | ||
-- on 2016-08-02. Some deviations exist and are noted in comments. | ||
|
||
describe "normalizePlaintext" $ do | ||
|
||
it "Lowercase" $ | ||
normalizePlaintext "Hello" | ||
`shouldBe` "hello" | ||
|
||
it "Remove spaces" $ | ||
normalizePlaintext "Hi there" | ||
`shouldBe` "hithere" | ||
|
||
it "Remove punctuation" $ | ||
normalizePlaintext "@1, 2%, 3 Go!" | ||
`shouldBe` "123go" | ||
|
||
describe "plaintextSegments" $ do | ||
|
||
it "empty plaintext results in an empty rectangle" $ | ||
plaintextSegments "" | ||
`shouldBe` [] | ||
|
||
it "4 character plaintext results in an 2x2 rectangle" $ | ||
plaintextSegments "Ab Cd" | ||
`shouldBe` [ "ab" | ||
, "cd" ] | ||
|
||
it "9 character plaintext results in an 3x3 rectangle" $ | ||
plaintextSegments "This is fun!" | ||
`shouldBe` [ "thi" | ||
, "sis" | ||
, "fun" ] | ||
|
||
it "54 character plaintext results in an 8x7 rectangle" $ | ||
plaintextSegments "If man was meant to stay on the ground, god would have given us roots." | ||
`shouldBe` [ "ifmanwas" | ||
, "meanttos" | ||
, "tayonthe" | ||
, "groundgo" | ||
, "dwouldha" | ||
, "vegivenu" | ||
, "sroots" ] | ||
|
||
describe "ciphertext" $ do | ||
|
||
-- The function described by the reference file in `x-common` | ||
-- as `encoded` is called `ciphertext` in this track. | ||
|
||
it "empty plaintext results in an empty encode" $ | ||
ciphertext "" | ||
`shouldBe` "" | ||
|
||
it "Non-empty plaintext results in the combined plaintext segments" $ | ||
ciphertext "If man was meant to stay on the ground, god would have given us roots." | ||
`shouldBe` "imtgdvsfearwermayoogoanouuiontnnlvtwttddesaohghnsseoau" | ||
|
||
describe "normalizeCiphertext" $ do | ||
|
||
-- The function described by the reference file in `x-common` | ||
-- as `ciphertext` is called `normalizeCiphertext` in this track. | ||
|
||
it "empty plaintext results in an empty ciphertext" $ | ||
normalizeCiphertext "" | ||
`shouldBe` "" | ||
|
||
it "9 character plaintext results in 3 chunks of 3 characters" $ | ||
normalizeCiphertext "This is fun!" | ||
`shouldBe` "tsf hiu isn" | ||
|
||
{- In this track the encoded text chunks are not padded with spaces. | ||
|
||
it "54 character plaintext results in 7 chunks, the last two padded with spaces" $ | ||
normalizeCiphertext "If man was meant to stay on the ground, god would have given us roots." | ||
`shouldBe` "imtgdvs fearwer mayoogo anouuio ntnnlvt wttddes aohghn sseoau " | ||
|
||
-} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
do you think they should be?
I realize it would probably make the example implementation a bit more complex
To make it clear, I don't care enough to insist on one way or the other
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we instead keep the old test that expects
imtgdvs fearwer mayoogo anouuio ntnnlvt wttddes aohghn sseoau
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think that this exercise is not very idiomatic and deserves a major rewrite, with better functions name and, maybe, using lists of strings. But for now, I am just trying to document the differences between the track and x-common.
I'm against that last test. The multiple spaces between the words make sense only if we look at the string as a matrix - and this is probably the reason why there is space padding in the exercise - but in Haskell we would never represent a matrix as a single string.
What do you think? Should we enforce non-padding (old test), padding (json) or accept both (PR)?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Given that you have plans pending, I'm fine with leaving this commented as it already is. The student can uncomment it if truly desired, and there is a test for the perfect square case above. As long as there is a note in either #194 or a new issue describing the change planned!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Issue opened! Thanks again, @petertseng!
I know it is probably boring to review so many similar Hspec PRs, but we almost there. 😄
As long as there is a note in either #194 or a new issue describing the change planned!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think to be commended more is your persistence in doing all of them, actually!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I had a lot of fun rewriting the first 10 exercises... 😁