-
-
Notifications
You must be signed in to change notification settings - Fork 81
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
Add property tests for the Validation type #216
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do you know why your PR is still not approved? Because I chose not to approve it. But they will.
prop_monoidRightIdentity :: Property | ||
prop_monoidRightIdentity = property $ do | ||
x <- forAll $ asValidation genSmallText | ||
x <> mempty === x |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Warning: Monoid law, right identity
x <> mempty === x | |
x === x |
prop_monoidLeftIdentity :: Property | ||
prop_monoidLeftIdentity = property $ do | ||
x <- forAll $ asValidation genSmallText | ||
mempty <> x === x |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Warning: Monoid law, left identity
mempty <> x === x | |
x === x |
prop_applicativeIdentity :: Property | ||
prop_applicativeIdentity = property $ do | ||
vx <- forAll $ asValidation genSmallText | ||
(pure id <*> vx) === vx |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Suggestion: Use <$>
(pure id <*> vx) === vx | |
(id <$> vx) === vx |
vf <- forAllWith (const "f") $ asValidation genFunction | ||
vg <- forAllWith (const "g") $ asValidation genFunction | ||
vx <- forAll $ asValidation genSmallInt | ||
(pure (.) <*> vf <*> vg <*> vx) === (vf <*> (vg <*> vx)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Suggestion: Use <$>
(pure (.) <*> vf <*> vg <*> vx) === (vf <*> (vg <*> vx)) | |
(((.) <$> vf) <*> vg <*> vx) === (vf <*> (vg <*> vx)) |
prop_applicativeInterchange = property $ do | ||
vf <- forAllWith (const "f") $ asValidation genFunction | ||
x <- forAll genSmallInt | ||
(vf <*> pure x) === (pure ($ x) <*> vf) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Suggestion: Use <$>
(vf <*> pure x) === (pure ($ x) <*> vf) | |
(vf <*> pure x) === (($ x) <$> vf) |
prop_alternativeRightIdentity :: Property | ||
prop_alternativeRightIdentity = property $ do | ||
x <- forAll $ asValidation genSmallText | ||
(x <|> empty) === x |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Warning: Alternative law, right identity
(x <|> empty) === x | |
(x) === x |
prop_alternativeLeftIdentity :: Property | ||
prop_alternativeLeftIdentity = property $ do | ||
x <- forAll $ asValidation genSmallText | ||
(empty <|> x) === x |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Warning: Alternative law, left identity
(empty <|> x) === x | |
(x) === x |
@chshersh will I need to suppress such warnings from the bot? In this particular case code contains lines like |
@astynax Yeah, I know that this code is on purpose. Ideally, there should be a good library that already implements all required test-suites for laws for the standard typeclasses. Unfortunately, I'm not aware of such library for |
We can use HLint |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@astynax Thanks for your work! Looks good and very useful 👍 I have a few comments, but they are mostly stylistic.
relude.cabal
Outdated
@@ -183,7 +183,7 @@ test-suite relude-test | |||
main-is: Spec.hs | |||
|
|||
other-modules: Test.Relude.Property | |||
|
|||
, Test.Relude.Extra.Validation.Property |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Commas are redundant in module definitions
, Test.Relude.Extra.Validation.Property | |
Test.Relude.Extra.Validation.Property |
Copyright: (c) 2016 Stephen Diehl | ||
(c) 2016-2018 Serokell | ||
(c) 2018-2019 Kowainik |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I believe there's no need to specify everyone here, since it's a brand new module. So this can be just:
Copyright: (c) 2019 Kowainik
validationTestList :: [Group] | ||
validationTestList = |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's rename List
suffix to Laws
or Group
to have better semantics
a `op` (b `op` c) === (a `op` b) `op` c | ||
|
||
---------------------------------------------------------------------------- | ||
-- Semogroup instance properties |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Minor typo 🙂
-- Semogroup instance properties | |
-- Semigroup instance properties |
prop_applicativeHomomorphism = property $ do | ||
f <- forAllWith (const "f") genFunction | ||
x <- forAll genSmallInt | ||
(pure f <*> (pure x :: Validation [Text] Int)) === pure (f x) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this can be written a bit cleaner with type applications
(pure f <*> (pure x :: Validation [Text] Int)) === pure (f x) | |
(pure f <*> pure x) === pure @(Validation [Text]) (f x) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks, looks great!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the contribution!
I will silence the HLint warnings in a separate PR.
Resolves #176
I added property tests for basic laws of
Semigroup
/Monoid
/Applicative
/Alternative
.A copule of tests checks that
<*
and*>
work just like default implementations inApplicative
class.Checklist:
HLint
hlint.dhall
accordingly to my changes (add new rules for the new imports, remove old ones, when they are outdated, etc.)..hlint.yaml
file (see this instructions).General
stylish-haskell
file.[ci skip]
text to the docs-only related commit's name.