haskell - testing functions that return a Maybe Monad -
say have function:
safehead :: [a] -> maybe safehead [] = nothing safehead xs = $ head xs
and test:
describe "example.safehead" $ "returns head" $ safehead [1,2,3] `shouldbe` 1 "returns nothing empty list" $ safehead [] `shouldbe` nothing
this produces:
no instance (eq a0) arising use of ‘shouldbe’ type variable ‘a0’ ambiguous note: there several potential instances: instance (eq a, eq b) => eq (either b) -- defined in ‘data.either’ instance forall (k :: box) (s :: k). eq (data.proxy.proxy s) -- defined in ‘data.proxy’ instance (ghc.arr.ix i, eq e) => eq (ghc.arr.array e) -- defined in ‘ghc.arr’ ...plus 88 others in second argument of ‘($)’, namely ‘safehead [] `shouldbe` nothing’ in stmt of 'do' block: "returns nothing empty list" $ safehead [] `shouldbe` nothing in second argument of ‘($)’, namely ‘do { "returns head" $ { safehead [...] `shouldbe` 1 }; "returns nothing empty list" $ safehead [] `shouldbe` nothing }’
why? , how can fix it?
as user2407038 commented, compiler doesn't know how instantiate a
. fix proposed best 1 -- should specify type of a
explicitly.
but completeness i'd note, there other solution, extended default rules:
{-# language extendeddefaultrules #-} describe "example.safehead" $ "returns head" $ safehead [1,2,3] `shouldbe` 1 "returns nothing empty list" $ safehead [] `shouldbe` nothing
the extension modifies the standard defaulting rules include more cases, e.g. eq
type class.
added: after thinking reconsider answer bit. results of unit testing of polymorphic functions should not depend of particular way instantiate type variables. extended default rules right thing in tests? never used them real code, can't sure, worth thinking about.
Comments
Post a Comment