Skip to content

Commit 039e34c

Browse files
authored
Merge pull request #2 from kazk/add-docs
Add docs
2 parents b46019c + 7a1e600 commit 039e34c

File tree

2 files changed

+61
-1
lines changed

2 files changed

+61
-1
lines changed

README.md

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,37 @@
11
# Hspec Codewars
22

33
Utility functions for testing on Codewars with Hspec.
4+
5+
## Functions
6+
7+
### Blacklisting
8+
9+
```haskell
10+
solutionShouldHide :: Hidden -> Expectation
11+
12+
-- solutionShouldHide $ FromModule "Prelude" "head"
13+
```
14+
Check that solution hides a module or a symbol from a module.
15+
16+
```haskell
17+
solutionShouldHideAll :: [Hidden] -> Expectation
18+
19+
-- solutionShouldHideAll [FromModule "Prelude" "head", Module "Data.Set"]
20+
```
21+
Check that solution hides all of given modules and symbols.
22+
23+
### Approximate Equality
24+
25+
```haskell
26+
shouldBeApprox :: (Fractional a, Ord a, Show a) => a -> a -> Expectation
27+
28+
-- sqrt 2.0 `shouldBeApprox` (1.4142135 :: Double)
29+
```
30+
Predefined approximately equal expectation with error margin `1e-6`.
31+
32+
```haskell
33+
shouldBeApproxPrec :: (Fractional a, Ord a, Show a) => a -> a -> a -> Expectation
34+
35+
-- shouldBeApprox' = shouldBeApproxPrec 1e-9
36+
```
37+
Create approximately equal expectation with margin.

src/Test/Hspec/Codewars.hs

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
-- |
2+
-- Module : Test.Hspec.Codewars
3+
-- Description : Utility functions for testing on Codewars with Hspec
4+
-- License : MIT
5+
16
{-# LANGUAGE RecordWildCards #-}
27
module Test.Hspec.Codewars (
38
Hidden(..),
@@ -55,7 +60,13 @@ declToDesc decl = case Q.importSpecs decl of
5560
treatPrelude :: [ImportDesc] -> [ImportDesc]
5661
treatPrelude xs = if any (\x -> mName x == "Prelude") xs then xs else ImportAll "Prelude" : xs
5762

58-
data Hidden = Module {moduleName :: String} | FromModule {moduleName :: String, symbolName :: String} deriving (Eq)
63+
data Hidden
64+
-- | Module to be hidden
65+
= Module {moduleName :: String}
66+
-- | Symbol from a module to be hidden
67+
| FromModule {moduleName :: String, symbolName :: String}
68+
deriving (Eq)
69+
5970
instance Show Hidden where
6071
show (Module{..}) = moduleName
6172
show (FromModule{..}) = moduleName ++ "." ++ symbolName
@@ -77,12 +88,21 @@ hidden hiddens = do
7788
let message = "Import declarations must hide " ++ show hiddens
7889
assertBool message $ null failures
7990

91+
-- | Check that solution hides a module or a symbol from a module.
92+
--
93+
-- > solutionShouldHide $ FromModule "Prelude" "head"
8094
solutionShouldHide :: Hidden -> Expectation
8195
solutionShouldHide = hidden . pure
8296

97+
-- | Check that solution hides all of given modules and symbols.
98+
--
99+
-- > solutionShouldHideAll [FromModule "Prelude" "head", Module "Data.Set"]
83100
solutionShouldHideAll :: [Hidden] -> Expectation
84101
solutionShouldHideAll = hidden
85102

103+
-- | Create approximately equal expectation with margin.
104+
--
105+
-- > shouldBeApprox' = shouldBeApproxPrec 1e-9
86106
shouldBeApproxPrec :: (Fractional a, Ord a, Show a) => a -> a -> a -> Expectation
87107
shouldBeApproxPrec margin actual expected =
88108
if abs (actual - expected) < abs margin * max 1 (abs expected)
@@ -95,5 +115,11 @@ shouldBeApproxPrec margin actual expected =
95115
"\n but got: ", show actual]
96116

97117
infix 1 `shouldBeApprox`
118+
119+
-- | Predefined approximately equal expectation.
120+
-- @actual \`shouldBeApprox\` expected@ sets the expectation that @actual@ is
121+
-- approximately equal to @expected@ within the margin of @1e-6@.
122+
--
123+
-- > sqrt 2.0 `shouldBeApprox` (1.4142135 :: Double)
98124
shouldBeApprox :: (Fractional a, Ord a, Show a) => a -> a -> Expectation
99125
shouldBeApprox = shouldBeApproxPrec 1e-6

0 commit comments

Comments
 (0)