Skip to content

Commit

Permalink
Add property tests to resistor-color-duo
Browse files Browse the repository at this point in the history
  • Loading branch information
tejasbubane committed Apr 18, 2020
1 parent b5de2ed commit c5b4115
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 6 deletions.
29 changes: 29 additions & 0 deletions exercises/resistor-color-duo/.meta/hints.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,32 @@ value :: (Color, Color) -> Int

that only accepts two colors and produces the resistor's numeric value
component of its resistance.

**Bonus - Property Testing:**

This exercise is a good opportunity to write some [property tests](https://en.wikipedia.org/wiki/Property_testing). If you haven't written any before here are some resources:

1. [School of Haskell](https://www.schoolofhaskell.com/user/pbv/an-introduction-to-quickcheck-testing).
2. [Real world Haskell](http://book.realworldhaskell.org/read/testing-and-quality-assurance.html)

We have provided one sample property test in the [test file](test/Tests.hs). We use the [QuickCheck](https://hackage.haskell.org/package/QuickCheck) library, but there are [others](http://hackage.haskell.org/package/hedgehog) as well, feel free to play around.

Few properties that can be tested:

* `value (x, y) >= 10` for all x except Black and all y

* `show (value (x, y)) == reverse (show (value (y, x)))` for all x, y except Black.

* (Color, Color) is isomorphic to [0..99] (or: all colors have a unique value).

* Associativity: Kind of tricky because you can't get the value of a single Color:

```haskell
value (k, x) <= value (k, y) && value (k, y) <= value (k, z)
value (k, x) <= value (k, z)

value (x, k) <= value (y, k) && value (y, k) <= value (z, k)
value (k, x) <= value (k, z)
```

Can you think of any other properties?
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,4 @@ tests:
dependencies:
- resistor-color-duo
- hspec
- QuickCheck
3 changes: 2 additions & 1 deletion exercises/resistor-color-duo/package.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: resistor-color-duo
version: 2.1.0.3
version: 2.1.0.4

dependencies:
- base
Expand All @@ -19,3 +19,4 @@ tests:
dependencies:
- resistor-color-duo
- hspec
- QuickCheck
23 changes: 18 additions & 5 deletions exercises/resistor-color-duo/test/Tests.hs
Original file line number Diff line number Diff line change
@@ -1,19 +1,32 @@
{-# OPTIONS_GHC -fno-warn-type-defaults #-}
{-# OPTIONS_GHC -fno-warn-orphans #-}
{-# LANGUAGE RecordWildCards #-}

import Data.Foldable (for_)
import Test.Hspec (Spec, describe, it, shouldBe)
import Test.Hspec.Runner (configFastFail, defaultConfig, hspecWith)
import Data.Foldable (for_)
import Test.Hspec (Spec, describe, context, it, shouldBe)
import Test.Hspec.Runner (configFastFail, defaultConfig, hspecWith)
import Test.QuickCheck (property)
import Test.QuickCheck.Gen (oneof)
import Test.QuickCheck.Arbitrary

import ResistorColors (Color(..), value)

instance Arbitrary Color where
arbitrary = oneof $ map return [Black, Brown, Red, Orange, Yellow, Green, Blue, Violet, Grey, White]

main :: IO ()
main = hspecWith defaultConfig {configFastFail = True} specs

specs :: Spec
specs = describe "value" $ for_ cases test
where
specs = describe "value" $ do
context "equality tests" $ for_ cases test

context "property tests" $
it "all values starting with Black are single digit" $ property $
\color -> value (Black, color) < 10
-- Add more property tests here

where
test Case{..} = it explanation assertion
where
explanation = unwords [show input, "-", description]
Expand Down

0 comments on commit c5b4115

Please sign in to comment.