Skip to content

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 1 commit into from
Aug 7, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion exercises/crypto-square/package.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,4 @@ tests:
source-dirs: test
dependencies:
- crypto-square
- HUnit
- hspec
4 changes: 0 additions & 4 deletions exercises/crypto-square/src/CryptoSquare.hs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ module CryptoSquare
, normalizeCiphertext
, normalizePlaintext
, plaintextSegments
, squareSize
) where

ciphertext :: String -> String
Expand All @@ -15,8 +14,5 @@ normalizeCiphertext = undefined
normalizePlaintext :: String -> String
normalizePlaintext = undefined

squareSize :: String -> Int
squareSize = undefined

plaintextSegments :: String -> [String]
plaintextSegments = undefined
1 change: 0 additions & 1 deletion exercises/crypto-square/src/Example.hs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
module CryptoSquare ( normalizePlaintext
, squareSize
, plaintextSegments
, ciphertext
, normalizeCiphertext ) where
Expand Down
149 changes: 91 additions & 58 deletions exercises/crypto-square/test/Tests.hs
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.
Copy link
Member

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

Copy link
Member

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?

Copy link
Contributor Author

@rbasso rbasso Aug 6, 2016

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)?

Copy link
Member

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!

Copy link
Contributor Author

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!

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know it is probably boring to review so many similar Hspec PRs, but we almost there.

I think to be commended more is your persistence in doing all of them, actually!

Copy link
Contributor Author

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... 😁


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 "

-}