hspec

How to select a value in a range with QuickCheck?

▼魔方 西西 提交于 2020-03-22 09:47:08
问题 I have the following code I am using for creating a challenge on the following site : codewars describe "Random cases" $ do it "It should handle random test cases" $ property $ prop_check where prop_check (Positive x) = solution x == ref_sol x --- ref_sol function I would like to set the value of x in prop_check to be a positive int greater than 4 and at max a five-digit number (no more than five digits, i.e : max value = 99999). How would I go about approaching it ? 回答1: You can use

Unit-testing the undefined evaluated in lazy expression in Haskell

强颜欢笑 提交于 2020-01-04 09:03:53
问题 Writing a unit test in Haskell where an expression should fail when undefined is encountered is a bit tricky. I tried the following with HSpec: module Main where import Test.Hspec import Control.Exception (evaluate) main :: IO () main = hspec $ do describe "Test" $ do it "test case" $ do evaluate (take 1 $ map (+1) [undefined, 2, 3]) `shouldThrow` anyException to no avail. It reports me did not get expected exception: SomeException If I evaluate the same expression in REPL, I'd get: [***

Unit testing IO actions with Hspec

你。 提交于 2019-12-30 11:31:50
问题 I have found other questions on similar lines but nothing that answers my question in this particular scenario. Furthermore, there seem to be few resources which succinctly cover the subject of unit testing IO actions in Haskell. Let's say I have this typeclass for my database communication: data Something = Something String deriving Show class MonadIO m => MonadDB m where getSomething :: String -> m Something getSomething s = do ... -- assume a DB call is made and an otherwise valid function

Unit testing IO actions with Hspec

南笙酒味 提交于 2019-12-30 11:31:17
问题 I have found other questions on similar lines but nothing that answers my question in this particular scenario. Furthermore, there seem to be few resources which succinctly cover the subject of unit testing IO actions in Haskell. Let's say I have this typeclass for my database communication: data Something = Something String deriving Show class MonadIO m => MonadDB m where getSomething :: String -> m Something getSomething s = do ... -- assume a DB call is made and an otherwise valid function

HSpec Nothing expectation failing to compile

给你一囗甜甜゛ 提交于 2019-12-19 04:22:48
问题 I'm learning Haskell and I've written this function: safeHead :: [a] -> Maybe a safeHead [] = Nothing safeHead (x:xs) = Just x I'm now trying to test it with HSpec: import Test.Hspec main :: IO () main = hspec spec spec :: Spec spec = describe "safeHead" $ it "should return Nothing for empty list" $ safeHead [] `shouldBe` Nothing But that fails to compile: Error:(14, 19) ghc: No instance for (Eq a0) arising from a use of ‘shouldBe’ The type variable ‘a0’ is ambiguous Note: there are several

hspec defined tests invoked with stack throw an error when test file is defined as a module

孤人 提交于 2019-12-11 00:27:43
问题 I'm trying to get my head around the reason why the test file containing unit-tests which is defined as a module fails when run with stack build --test . Say having a simple test module defined from scratch with: stack new test-module cd test-module vim package.yaml # remove "executables" section, add "hspec" as tests dependency Following "getting started" instructions from Hspec documentation I've modified files such as: Step 1: Describe your desired behavior -- file test/Spec.hs module

Use HSpec and QuickCheck to verify Data.Monoid properties

余生颓废 提交于 2019-12-06 02:37:01
问题 I'm trying to use HSpec and QuickCheck to verify properties of Monoids (associativity and identity element). I am going to verify particular instances, but would like to keep most of the code polymorphic. This is what I came up with after several hours: module Test where import Test.Hspec import Test.QuickCheck import Data.Monoid instance (Arbitrary a) => Arbitrary (Sum a) where arbitrary = fmap Sum arbitrary instance (Arbitrary a) => Arbitrary (Product a) where arbitrary = fmap Product

Select which test to run with Hspec and stack

那年仲夏 提交于 2019-12-04 18:26:12
问题 I've written a series of test, using the automatic spec discovery feature of Hspec. I'm also using stack as my build tool. My test directory has the the Spec.hs file, along with the test files for the different modules of my application (e.g. Module0Spec.hs , Module1Spec.hs ). Now, when I start writing a new test module, or when I want to re-run a failed test after code changes, I'd like to be able to run only a given test module. Is there any way in which either stack or Hspec allow to do

Use HSpec and QuickCheck to verify Data.Monoid properties

非 Y 不嫁゛ 提交于 2019-12-04 09:21:55
I'm trying to use HSpec and QuickCheck to verify properties of Monoids (associativity and identity element). I am going to verify particular instances, but would like to keep most of the code polymorphic. This is what I came up with after several hours: module Test where import Test.Hspec import Test.QuickCheck import Data.Monoid instance (Arbitrary a) => Arbitrary (Sum a) where arbitrary = fmap Sum arbitrary instance (Arbitrary a) => Arbitrary (Product a) where arbitrary = fmap Product arbitrary prop_Monoid_mappend_mempty_x x = mappend mempty x === x sumMonoidSpec = it "mappend mempty x = x"

Unit testing IO actions with Hspec

隐身守侯 提交于 2019-12-01 11:25:36
I have found other questions on similar lines but nothing that answers my question in this particular scenario. Furthermore, there seem to be few resources which succinctly cover the subject of unit testing IO actions in Haskell. Let's say I have this typeclass for my database communication: data Something = Something String deriving Show class MonadIO m => MonadDB m where getSomething :: String -> m Something getSomething s = do ... -- assume a DB call is made and an otherwise valid function instance MonadDB IO and this function which uses it: getIt :: MonadDB m => m (Int, Something) getIt =