1+ -- |
2+ -- Module : Test.Hspec.Codewars
3+ -- Description : Utility functions for testing on Codewars with Hspec
4+ -- License : MIT
5+
16{-# LANGUAGE RecordWildCards #-}
27module Test.Hspec.Codewars (
38 Hidden (.. ),
@@ -55,7 +60,13 @@ declToDesc decl = case Q.importSpecs decl of
5560treatPrelude :: [ImportDesc ] -> [ImportDesc ]
5661treatPrelude 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+
5970instance 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"
8094solutionShouldHide :: Hidden -> Expectation
8195solutionShouldHide = hidden . pure
8296
97+ -- | Check that solution hides all of given modules and symbols.
98+ --
99+ -- > solutionShouldHideAll [FromModule "Prelude" "head", Module "Data.Set"]
83100solutionShouldHideAll :: [Hidden ] -> Expectation
84101solutionShouldHideAll = hidden
85102
103+ -- | Create approximately equal expectation with margin.
104+ --
105+ -- > shouldBeApprox' = shouldBeApproxPrec 1e-9
86106shouldBeApproxPrec :: (Fractional a , Ord a , Show a ) => a -> a -> a -> Expectation
87107shouldBeApproxPrec 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
97117infix 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)
98124shouldBeApprox :: (Fractional a , Ord a , Show a ) => a -> a -> Expectation
99125shouldBeApprox = shouldBeApproxPrec 1e-6
0 commit comments