Skip to content
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

Diamond: Implement property-based tests #843

Merged
merged 31 commits into from
Oct 1, 2019
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
3bfe932
Implement basic property based test for diamond
chiroptical Sep 7, 2019
3090372
Update package.yaml dependencies and bump version number
chiroptical Sep 7, 2019
9cc8d51
Use `elements` from QuickCheck
chiroptical Sep 7, 2019
a1916de
Add spacing between PBTs and unit tests
chiroptical Sep 7, 2019
95314a5
Use singular name for the `Gen Char` function
chiroptical Sep 7, 2019
57a29dd
Minor commit to fix current comments
chiroptical Sep 7, 2019
359f554
Length of a diamond should be odd
chiroptical Sep 7, 2019
66645f4
Top and bottom of a diamond should be equal
chiroptical Sep 7, 2019
279e775
Check the middle of the diamond for correctness
chiroptical Sep 7, 2019
bf290f0
Check dimensionality of diamond is correct
chiroptical Sep 7, 2019
f875947
Clarify test descriptions, use maintainable character generator, add …
chiroptical Sep 13, 2019
b7d89c0
Implement start-end-char test, clean up fromMaybe from individual tests
chiroptical Sep 20, 2019
aa37333
Use elements over suchThat for genAlphaChar
chiroptical Sep 20, 2019
642e1e0
Add QuickCheck to solution package.yaml
chiroptical Sep 26, 2019
299d14b
Replace partition with filter
chiroptical Sep 26, 2019
1e9ee3c
Avoid incomplete implementation checkFirstAndLastCharEq, inline funct…
chiroptical Sep 27, 2019
18371a8
Minor stylistic update for shrinkNonAlphaChar
chiroptical Sep 27, 2019
f89bd5a
Update exercises/diamond/test/Tests.hs
chiroptical Sep 27, 2019
4554d26
Filter non-space characters from rows instead of only letter
chiroptical Sep 27, 2019
b253952
Merge branch 'diamond_property_based_tests' of github.com:barrymoo/ha…
chiroptical Sep 27, 2019
f7b4085
Inline topEqualToBottom and remove unnecessary checkMiddle
chiroptical Sep 27, 2019
eb61cce
Inline checkCorrectDimensions, add a friendly error message upon failure
chiroptical Sep 27, 2019
51088de
Clean up to eliminate CI warnings
sshine Sep 27, 2019
d268994
Fix example solution after adding property-based tests
sshine Sep 27, 2019
cebda94
Also remove 'ord' as import
sshine Sep 27, 2019
6dc6a97
diamond: unindent imports
Sep 30, 2019
ef86530
Reorder imports alphabetically
sshine Oct 1, 2019
2978792
Align style of import with existing imports
sshine Oct 1, 2019
078b8fd
Reindent let-in
sshine Oct 1, 2019
ca1b6f4
Reorder indents: J < N, n < u
sshine Oct 1, 2019
6fcd7a8
Reindent if-then-else
sshine Oct 1, 2019
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
3 changes: 2 additions & 1 deletion exercises/diamond/package.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: diamond
version: 1.1.0.4
version: 1.1.0.5

dependencies:
- base
Expand All @@ -19,3 +19,4 @@ tests:
dependencies:
- diamond
- hspec
- QuickCheck
chiroptical marked this conversation as resolved.
Show resolved Hide resolved
45 changes: 43 additions & 2 deletions exercises/diamond/test/Tests.hs
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,32 @@
import Data.Foldable (for_)
import Test.Hspec (Spec, describe, it, shouldBe)
import Test.Hspec.Runner (configFastFail, defaultConfig, hspecWith)
import Test.QuickCheck (Gen, elements, forAll, choose)
import Data.List ((\\))
import Data.Maybe (isNothing, fromMaybe)
import Data.Char (ord)
import Diamond (diamond)

main :: IO ()
main = hspecWith defaultConfig {configFastFail = True} specs

specs :: Spec
specs = describe "diamond" $ for_ cases test
where
specs = describe "diamond" $ do
it "non-Alpha characters should produce `Nothing`" $
chiroptical marked this conversation as resolved.
Show resolved Hide resolved
forAll genNonAlphaChar $
isNothing . diamond

it "Length of a diamond should be odd" $
forAll genAlphaChar $ odd . length . fromMaybe [""] . diamond
chiroptical marked this conversation as resolved.
Show resolved Hide resolved

it "Top and bottom of a diamond should be equal" $
forAll genAlphaChar $ topEqualToBottom . fromMaybe [""] . diamond
chiroptical marked this conversation as resolved.
Show resolved Hide resolved

it "Check the middle of the diamond is correct" $
forAll genAlphaChar $ \c -> checkMiddle c . fromMaybe [""] $ diamond c

for_ cases test
chiroptical marked this conversation as resolved.
Show resolved Hide resolved
where
test Case{..} = it description assertion
where
assertion = diamond input `shouldBe` Just expected
Expand Down Expand Up @@ -107,3 +124,27 @@ cases = [ Case { description = "Degenerate case with a single 'A' row"
" A "]
}
]

genNonAlphaChar :: Gen Char
genNonAlphaChar = elements nonAlphaChars
where nonAlphaChars = ['\0' .. '\127'] \\ (['A' .. 'Z'] ++ ['a' .. 'z'])
chiroptical marked this conversation as resolved.
Show resolved Hide resolved
chiroptical marked this conversation as resolved.
Show resolved Hide resolved

genAlphaChar :: Gen Char
genAlphaChar = choose ('A', 'Z')

topLength :: [String] -> Int
topLength = (`div` 2) . length

topEqualToBottom :: [String] -> Bool
topEqualToBottom xs = take len xs == take len (reverse xs)
where
len = topLength xs

checkMiddle :: Char -> [String] -> Bool
checkMiddle c xs = getMiddle xs == middle
where
position = ord c - ord 'A'
getMiddle ys = ys !! topLength ys
leftSide = c : replicate position ' '
rightSide = tail . reverse $ leftSide
middle = leftSide ++ rightSide
chiroptical marked this conversation as resolved.
Show resolved Hide resolved