Skip to content
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

Merged
merged 3 commits into from
Oct 25, 2019
Merged

Add property tests for the Validation type #216

merged 3 commits into from
Oct 25, 2019

Conversation

astynax
Copy link
Contributor

@astynax astynax commented Oct 23, 2019

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 in Applicative class.

Checklist:

HLint

  • I've changed the exposed interface (add new reexports, remove reexports, rename reexported things, etc.).
    • I've updated hlint.dhall accordingly to my changes (add new rules for the new imports, remove old ones, when they are outdated, etc.).
    • I've generated the new .hlint.yaml file (see this instructions).

General

  • I've updated the CHANGELOG with the short description of my latest changes.
  • All new and existing tests pass.
  • I keep the code style used in the files I've changed (see style-guide for more details).
  • I've used the stylish-haskell file.
  • My change requires the documentation updates.
    • I've updated the documentation accordingly.
  • I've added the [ci skip] text to the docs-only related commit's name.

Copy link
Contributor

@hint-man hint-man bot left a 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.

test/Test/Relude/Extra/Validation/Property.hs Outdated Show resolved Hide resolved
prop_monoidRightIdentity :: Property
prop_monoidRightIdentity = property $ do
x <- forAll $ asValidation genSmallText
x <> mempty === x
Copy link
Contributor

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

Suggested change
x <> mempty === x
x === x

prop_monoidLeftIdentity :: Property
prop_monoidLeftIdentity = property $ do
x <- forAll $ asValidation genSmallText
mempty <> x === x
Copy link
Contributor

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

Suggested change
mempty <> x === x
x === x

prop_applicativeIdentity :: Property
prop_applicativeIdentity = property $ do
vx <- forAll $ asValidation genSmallText
(pure id <*> vx) === vx
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggestion: Use <$>

Suggested change
(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))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggestion: Use <$>

Suggested change
(pure (.) <*> vf <*> vg <*> vx) === (vf <*> (vg <*> vx))
(((.) <$> vf) <*> vg <*> vx) === (vf <*> (vg <*> vx))

test/Test/Relude/Extra/Validation/Property.hs Outdated Show resolved Hide resolved
prop_applicativeInterchange = property $ do
vf <- forAllWith (const "f") $ asValidation genFunction
x <- forAll genSmallInt
(vf <*> pure x) === (pure ($ x) <*> vf)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggestion: Use <$>

Suggested change
(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
Copy link
Contributor

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

Suggested change
(x <|> empty) === x
(x) === x

prop_alternativeLeftIdentity :: Property
prop_alternativeLeftIdentity = property $ do
x <- forAll $ asValidation genSmallText
(empty <|> x) === x
Copy link
Contributor

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

Suggested change
(empty <|> x) === x
(x) === x

@astynax
Copy link
Contributor Author

astynax commented Oct 23, 2019

@chshersh will I need to suppress such warnings from the bot? In this particular case code contains lines like empty <|> by purpose :)

@chshersh
Copy link
Contributor

@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 hedgehog so it's unavoidable that we will have all these warnings...

@vrom911
Copy link
Member

vrom911 commented Oct 24, 2019

We can use HLint ignore pragmas to not yell on these particular cases 🙂

Copy link
Contributor

@chshersh chshersh left a 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
Copy link
Contributor

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

Suggested change
, Test.Relude.Extra.Validation.Property
Test.Relude.Extra.Validation.Property

Comment on lines 2 to 4
Copyright: (c) 2016 Stephen Diehl
(c) 2016-2018 Serokell
(c) 2018-2019 Kowainik
Copy link
Contributor

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

Comment on lines 21 to 22
validationTestList :: [Group]
validationTestList =
Copy link
Contributor

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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor typo 🙂

Suggested change
-- 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)
Copy link
Contributor

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

Suggested change
(pure f <*> (pure x :: Validation [Text] Int)) === pure (f x)
(pure f <*> pure x) === pure @(Validation [Text]) (f x)

Copy link
Contributor

@chshersh chshersh left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, looks great!

Copy link
Member

@vrom911 vrom911 left a 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.

@vrom911 vrom911 merged commit 9e7fbfa into kowainik:master Oct 25, 2019
@astynax astynax deleted the validaion-props branch October 25, 2019 06:29
@chshersh chshersh added the Hacktoberfest https://hacktoberfest.digitalocean.com/ label Nov 5, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Hacktoberfest https://hacktoberfest.digitalocean.com/ tests
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Implement property-based tests for Validation
3 participants