Skip to content

Commit

Permalink
secret-handshake: use cases from x-common
Browse files Browse the repository at this point in the history
This removes the string cases; the function-under-test is now only
obligated to accept integers.

Note that the now-deleted HINTS.md accepted that this API design was
bad, so it is good to get rid of it.
  • Loading branch information
petertseng authored and rbasso committed Feb 1, 2017
1 parent 785a3e5 commit 0a37a4d
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 52 deletions.
1 change: 0 additions & 1 deletion config.json
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,6 @@
"slug": "secret-handshake",
"difficulty": 3,
"topics": [
"instance custom"
]
},
{
Expand Down
1 change: 0 additions & 1 deletion exercises/secret-handshake/.meta/DONT-TEST-STUB

This file was deleted.

6 changes: 0 additions & 6 deletions exercises/secret-handshake/HINTS.md

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
module SecretHandshake (handshake) where

import Numeric (readInt)
import Data.Bits (testBit)

data Action = Wink
Expand All @@ -10,7 +9,7 @@ data Action = Wink
| Reverse
deriving (Show, Eq, Enum, Bounded)

handshake :: Handshake a => a -> [String]
handshake :: Int -> [String]
handshake = foldr f [] . toActions
where f Reverse acc = reverse acc
f action acc = toString action:acc
Expand All @@ -21,22 +20,5 @@ handshake = foldr f [] . toActions
Jump -> "jump"
Reverse -> undefined

class Handshake a where
toActions :: a -> [Action]

instance Handshake Int where
toActions n = [act | act <- Reverse:[Wink .. Jump], n `testBit` fromEnum act]

class BinParsable a where
listToInt :: [a] -> Int

instance BinParsable Char where
listToInt s = case readInt 2 isBin parseBin s of
[(n, "")] -> n
_ -> 0
where isBin c = c == '0' || c == '1'
parseBin = fromEnum . ('1'==)


instance BinParsable a => Handshake [a] where
toActions = toActions . listToInt
toActions :: Int -> [Action]
toActions n = [act | act <- Reverse:[Wink .. Jump], n `testBit` fromEnum act]
1 change: 1 addition & 0 deletions exercises/secret-handshake/src/SecretHandshake.hs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
module SecretHandshake (handshake) where

handshake :: Int -> [String]
handshake = error "You need to implement this function."
43 changes: 20 additions & 23 deletions exercises/secret-handshake/test/Tests.hs
Original file line number Diff line number Diff line change
Expand Up @@ -9,43 +9,40 @@ main = hspecWith defaultConfig {configFastFail = True} specs
specs :: Spec
specs = describe "secret-handshake" $ do

-- As of 2016-09-12, there was no reference file
-- for the test cases in `exercism/x-common`.
-- Test cases adapted from `exercism/x-common/secret-handshake` on 2017-01-31.

it "1 to wink" $ do
it "wink for 1" $
handshake (1 :: Int) `shouldBe` ["wink"]
handshake "1" `shouldBe` ["wink"]

it "10 to double blink" $ do
it "double blink for 10" $
handshake (2 :: Int) `shouldBe` ["double blink"]
handshake "10" `shouldBe` ["double blink"]

it "100 to close your eyes" $ do
it "close your eyes for 100" $
handshake (4 :: Int) `shouldBe` ["close your eyes"]
handshake "100" `shouldBe` ["close your eyes"]

it "1000 to jump" $ do
it "jump for 1000" $
handshake (8 :: Int) `shouldBe` ["jump"]
handshake "1000" `shouldBe` ["jump"]

it "11 to wink and double blink" $ do
it "combine two actions" $
handshake (3 :: Int) `shouldBe` ["wink", "double blink"]
handshake "11" `shouldBe` ["wink", "double blink"]

it "10011 to double blink and wink" $ do
it "reverse two actions" $
handshake (19 :: Int) `shouldBe` ["double blink", "wink"]
handshake "10011" `shouldBe` ["double blink", "wink"]

it "11111 to jump, close your eyes, double blink, and wink" $ do
it "reversing one action gives the same action" $
handshake (24 :: Int) `shouldBe` ["jump"]

it "reversing no actions still gives no actions" $
handshake (16 :: Int) `shouldBe` []

it "all possible actions" $
handshake (15 :: Int) `shouldBe` ["wink", "double blink", "close your eyes", "jump"]

it "reverse all possible actions" $
handshake (31 :: Int) `shouldBe` ["jump", "close your eyes", "double blink", "wink"]
handshake "11111" `shouldBe` ["jump", "close your eyes", "double blink", "wink"]

it "zero" $ do
it "do nothing for zero" $
handshake (0 :: Int) `shouldBe` []
handshake "0" `shouldBe` []

it "gibberish" $
handshake "piggies" `shouldBe` []

it "partial gibberish" $
handshake "1piggies" `shouldBe` []
it "do nothing if lower 5 bits not set" $
handshake (32 :: Int) `shouldBe` []

0 comments on commit 0a37a4d

Please sign in to comment.