Skip to content

Commit 3bc69ef

Browse files
authored
Add tests for Chapter 1 (#21)
1 parent 7f244f0 commit 3bc69ef

File tree

7 files changed

+134
-1
lines changed

7 files changed

+134
-1
lines changed

.github/workflows/ci.yml

+9
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,15 @@ jobs:
6262
run: |
6363
cabal v2-test doctest-chapter1 --enable-tests --test-show-details=direct
6464
65+
- name: Chapter 1 - Tests
66+
run: |
67+
cabal run learn4haskell-test --enable-tests -- -m "Chapter1Normal"
68+
69+
- name: Chapter 1 - Tests - Advanced
70+
continue-on-error: true
71+
run: |
72+
cabal run learn4haskell-test --enable-tests -- -m "Chapter1Advanced"
73+
6574
chapter2:
6675
name: Chapter Two
6776
runs-on: ubuntu-16.04

learn4haskell.cabal

+5
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,12 @@ test-suite learn4haskell-test
5858
type: exitcode-stdio-1.0
5959
hs-source-dirs: test
6060
main-is: Spec.hs
61+
other-modules: Test.Chapter1
62+
Test.Chapter2
63+
Test.Chapter3
64+
Test.Chapter4
6165
build-depends: learn4haskell
66+
, hspec ^>= 2.7.4
6267
ghc-options: -threaded
6368
-rtsopts
6469
-with-rtsopts=-N

test/Spec.hs

+12-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,16 @@
11
module Main (main) where
22

3+
import Test.Hspec (hspec)
4+
5+
import Test.Chapter1
6+
import Test.Chapter2
7+
import Test.Chapter3
8+
import Test.Chapter4
9+
310

411
main :: IO ()
5-
main = putStrLn "Tests are not implemented"
12+
main = hspec $ do
13+
chapter1
14+
chapter2
15+
chapter3
16+
chapter4

test/Test/Chapter1.hs

+69
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
{-# OPTIONS_GHC -Wno-type-defaults #-}
2+
3+
module Test.Chapter1
4+
( chapter1
5+
) where
6+
7+
import Test.Hspec (Spec, describe, it, shouldBe)
8+
9+
import Chapter1
10+
11+
12+
chapter1 :: Spec
13+
chapter1 = describe "Chapter1" $ do
14+
chapter1normal
15+
chapter1advanced
16+
17+
chapter1normal :: Spec
18+
chapter1normal = describe "Chapter1Normal" $ do
19+
describe "Task4: next" $ do
20+
it "returns next Int for 42" $ next 42 `shouldBe` 43
21+
it "returns next Int for negative" $ next (-5) `shouldBe` (-4)
22+
it "returns next Int for 0" $ next 0 `shouldBe` 1
23+
describe "Task5: lastDigit" $ do
24+
it "last digit of 0" $ lastDigit 0 `shouldBe` 0
25+
it "last digit of 0 < x < 10" $ lastDigit 5 `shouldBe` 5
26+
it "last digit of 10 < x < 100" $ lastDigit 34 `shouldBe` 4
27+
it "last digit of 100 < x < 1000" $ lastDigit 341 `shouldBe` 1
28+
it "last digit of big num" $ lastDigit 1234789 `shouldBe` 9
29+
it "last digit of negative" $ lastDigit (-12) `shouldBe` 2
30+
describe "Task6: closestToZero" $ do
31+
it "both positive, 1st wins" $ closestToZero 100 200 `shouldBe` 100
32+
it "both positive, 2nd wins" $ closestToZero 200 100 `shouldBe` 100
33+
it "both negative, 2nd wins" $ closestToZero (-200) (-100) `shouldBe` (-100)
34+
it "both negative, 2nd wins" $ closestToZero (-100) (-200) `shouldBe` (-100)
35+
it "with 0, 1nd wins" $ closestToZero 0 (-200) `shouldBe` 0
36+
it "with 0, 2nd wins" $ closestToZero 10 0 `shouldBe` 0
37+
it "equals" $ closestToZero 42 42 `shouldBe` 42
38+
it "positive, negative, pos wins" $ closestToZero 11 (-12) `shouldBe` 11
39+
it "positive, negative, neg wins" $ closestToZero 12 (-11) `shouldBe` (-11)
40+
describe "Task7: mid" $ do
41+
it "positives up " $ mid 10 20 30 `shouldBe` 20
42+
it "positives down" $ mid 30 20 10 `shouldBe` 20
43+
it "negatives down" $ mid (-10) (-20) (-30) `shouldBe` (-20)
44+
it "negatives up " $ mid (-30) (-20) (-10) `shouldBe` (-20)
45+
it "all equal" $ mid 1 1 1 `shouldBe` 1
46+
it "all equal, except 1" $ mid 1 1 2 `shouldBe` 1
47+
describe "Task8: isVowel" $ do
48+
it "true for vowels" $ all isVowel "aeiou" `shouldBe` True
49+
it "false for non-vowels" $ isVowel 'c' `shouldBe` False
50+
it "false for symbol" $ isVowel '+' `shouldBe` False
51+
describe "Task9: sumLast2" $ do
52+
it "sumLast2 0" $ sumLast2 0 `shouldBe` 0
53+
it "sumLast2 0 < 10" $ sumLast2 9 `shouldBe` 9
54+
it "sumLast2 10 < 100" $ sumLast2 56 `shouldBe` 11
55+
it "sumLast2 100 < 1000" $ sumLast2 987 `shouldBe` 15
56+
it "sumLast2 0 > -10" $ sumLast2 (-9) `shouldBe` 9
57+
it "sumLast2 -10 > -100" $ sumLast2 (-56) `shouldBe` 11
58+
it "sumLast2 -100 > -1000" $ sumLast2 (-987) `shouldBe` 15
59+
60+
chapter1advanced :: Spec
61+
chapter1advanced = describe "Chapter1Advanced" $ do
62+
it "first digit 0" $ firstDigit 0 `shouldBe` 0
63+
it "first digit 0 < 10" $ firstDigit 9 `shouldBe` 9
64+
it "first digit 10 < 100" $ firstDigit 58 `shouldBe` 5
65+
it "first digit 100 < 1000" $ firstDigit 158 `shouldBe` 1
66+
it "first digit big" $ firstDigit 467321 `shouldBe` 4
67+
it "first digit 0 > -10" $ firstDigit (-9) `shouldBe` 9
68+
it "first digit -10 > -100" $ firstDigit (-58) `shouldBe` 5
69+
it "first digit -100 > -1000" $ firstDigit (-158) `shouldBe` 1

test/Test/Chapter2.hs

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
module Test.Chapter2
2+
( chapter2
3+
) where
4+
5+
import Test.Hspec (Spec, describe, it, shouldBe)
6+
7+
import Chapter2
8+
9+
10+
chapter2 :: Spec
11+
chapter2 = describe "Chapter2" $ do
12+
describe "Chapter2Normal" $ it "" $ True `shouldBe` True
13+
describe "Chapter2Advanced" $ it "" $ True `shouldBe` True

test/Test/Chapter3.hs

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
module Test.Chapter3
2+
( chapter3
3+
) where
4+
5+
import Test.Hspec (Spec, describe, it, shouldBe)
6+
7+
import Chapter3
8+
9+
10+
chapter3 :: Spec
11+
chapter3 = describe "Chapter3" $ do
12+
describe "Chapter3Normal" $ it "" $ True `shouldBe` True
13+
describe "Chapter3Advanced" $ it "" $ True `shouldBe` True

test/Test/Chapter4.hs

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
module Test.Chapter4
2+
( chapter4
3+
) where
4+
5+
import Test.Hspec (Spec, describe, it, shouldBe)
6+
7+
import Chapter3
8+
9+
10+
chapter4 :: Spec
11+
chapter4 = describe "Chapter4" $ do
12+
describe "Chapter4Normal" $ it "" $ True `shouldBe` True
13+
describe "Chapter4Advanced" $ it "" $ True `shouldBe` True

0 commit comments

Comments
 (0)