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

Tests for CIS 194 homework assignments (7 to 11) #9

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion homework07/editor.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ test-suite examples
base
, editor
, mtl
, doctest >= 0.10
, hspec
default-language: Haskell2010


Expand Down
2 changes: 1 addition & 1 deletion homework07/stack.yaml
Original file line number Diff line number Diff line change
@@ -1 +1 @@
resolver: lts-5.10
resolver: lts-7.14
115 changes: 103 additions & 12 deletions homework07/test/examples/Main.hs
Original file line number Diff line number Diff line change
@@ -1,16 +1,107 @@
module Main
( main
)
where

-- doctest
import qualified Test.DocTest as DocTest
( main )
where

import JoinList hiding (main)
import Scrabble
import Buffer
import Editor
import Sized
import Test.Hspec

main :: IO ()
main =
DocTest.doctest
[ "-isrc"
, "src/JoinList.hs"
, "src/Scrabble.hs"
]
main = hspec $ do

describe "(+++) (append function)" $ do
context "when provided with Empty and Single values" $
it "returns new JoinList" $
(+++) Empty (Single (Size 1) "a") `shouldBe` Append (Size 1) Empty (Single (Size 1) "a")

context "when provided with Single and Append values" $
it "returns new JoinList" $
(+++) (Single (Size 1) "a") (Append (Size 2) Empty Empty)
`shouldBe` Append (Size 3) (Single (Size 1) "a") (Append (Size 2) Empty Empty)

context "when provided with Empty and Append values" $
it "returns new JoinList" $
(+++) Empty (Append (Size 2) (Single (Size 1) "a") (Single (Size 2) "b"))
`shouldBe` Append (Size 2) Empty (Append (Size 2) (Single (Size 1) "a") (Single (Size 2) "b"))

describe "indexJ" $ do
context "when provided with Empty value" $
it "returns Nothing" $
indexJ 10 (Empty :: JoinList Size String) `shouldBe` Nothing

context "when provided with Single value" $ do
context "when index is equals to zero" $
it "returns a Just value" $
indexJ 0 (Single (Size 1) "a") `shouldBe` Just "a"

context "when index is out of range" $
it "returns Nothing" $
indexJ 1 (Single (Size 1) "a") `shouldBe` Nothing

context "when provided with Append value" $ do
context "when index is in range" $
it "returns a Just value" $
indexJ 0 (Append (Size 2) (Single (Size 1) "a") (Single (Size 2) "b"))
`shouldBe` Just "a"

context "when index is out of range" $
it "returns Nothing" $
indexJ 2 (Append (Size 2) (Single (Size 1) "a") (Single (Size 2) "b"))
`shouldBe` Nothing

describe "dropJ" $ do
context "when provided with Empty value" $
it "returns Empty" $
dropJ 10 (Empty :: JoinList Size String) `shouldBe` Empty

context "when provided with Single value" $ do
context "when number of elements to drop is zero" $
it "returns original JoinList" $
dropJ 0 (Single (Size 1) "a") `shouldBe` (Single (Size 1) "a")

context "when number of elements to drop is valid" $
it "returns Empty" $
dropJ 1 (Single (Size 1) "a") `shouldBe` Empty

context "when provided with Append value" $ do
context "when number of elements to drop is zero" $
it "returns original JoinList" $
dropJ 0 (Append (Size 2) (Single (Size 1) "a") (Single (Size 2) "b"))
`shouldBe` (Append (Size 2) (Single (Size 1) "a") (Single (Size 2) "b"))

context "when number of elements to drop is valid" $
it "drops the first n elements from a JoinList" $
dropJ 1 (Append (Size 2) (Single (Size 1) "a") (Single (Size 2) "b"))
`shouldBe` Single (Size 2) "b"

describe "takeJ" $ do
context "when provided with Empty value" $
it "returns Empty" $
takeJ 10 (Empty :: JoinList Size String) `shouldBe` Empty

context "when provided with Single value" $ do
context "when number of elements to take is zero" $
it "returns Empty value" $
takeJ 0 (Single (Size 1) "a") `shouldBe` Empty

context "when number of elements to take is valid" $
it "returns original JoinList" $
takeJ 1 (Single (Size 1) "a") `shouldBe` (Single (Size 1) "a")

context "when provided with Append value" $ do
context "when number of elements to take is zero" $
it "returns Empty value" $
takeJ 0 (Append (Size 2) (Single (Size 1) "a") (Single (Size 2) "b"))
`shouldBe` Empty

context "when number of elements to take is valid" $
it "takes the first n elements from a JoinList" $
takeJ 1 (Append (Size 2) (Single (Size 1) "a") (Single (Size 2) "b"))
`shouldBe` Append (Size 1) (Single (Size 1) "a") Empty

describe "scoreLine" $
it "returns JoinList with scrabble score" $
scoreLine "haskell" `shouldBe` Single (Score 14) "haskell"
5 changes: 4 additions & 1 deletion homework08/party.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ cabal-version: >= 1.10

library
hs-source-dirs: src
exposed-modules: Party
exposed-modules:
Party
Employee
build-depends:
containers
, base >= 4.7 && < 5
Expand All @@ -24,6 +26,7 @@ test-suite examples
, containers
, party
, doctest >= 0.10
, hspec
default-language: Haskell2010


Expand Down
2 changes: 1 addition & 1 deletion homework08/stack.yaml
Original file line number Diff line number Diff line change
@@ -1 +1 @@
resolver: lts-5.10
resolver: lts-7.14
56 changes: 45 additions & 11 deletions homework08/test/examples/Main.hs
Original file line number Diff line number Diff line change
@@ -1,15 +1,49 @@
module Main
( main
)
where

-- doctest
import qualified Test.DocTest as DocTest
( main )
where

import Employee
import qualified Party as P
import Data.Tree
import Test.Hspec

main :: IO ()
main =
DocTest.doctest
[ "-isrc"
, "src/Party.hs"
]
main = hspec $ do

describe "glCons" $
it "adds an Employee to the GuestList" $ do
let guestList = GL [Emp "Stan" 9, Emp "Bob" 2] 11
P.glCons (Emp "Joe" 5) guestList
`shouldBe` GL [Emp "Stan" 9, Emp "Bob" 2, Emp "Joe" 5] 16

describe "moreFun" $
it "returns the GuestList with more fun" $ do
let firstGuestList = GL [Emp "Stan" 9, Emp "Bob" 2] 11
let secondGuestList = GL [Emp "John" 1, Emp "Sue" 5] 6
P.moreFun firstGuestList secondGuestList
`shouldBe` GL [Emp "Stan" 9, Emp "Bob" 2] 11

describe "treeFold" $
it "folds function with a tree" $ do
let first = Node { rootLabel = 1::Integer, subForest = [] }
let second = Node { rootLabel = 2::Integer, subForest = [] }
let third = Node { rootLabel = 3::Integer, subForest = [] }
let tree = Node { rootLabel = 4::Integer, subForest = [first, second, third] }
P.treeFold (\x xs -> x + (sum xs)) [0] tree `shouldBe` 10

describe "nextLevel" $
it "returns the best guest list both with and without the given boss" $ do
let firstGuestList = GL [Emp "Stan" 9, Emp "Bob" 2] 16
let secondGuestList = GL [Emp "John" 1, Emp "Sue" 5] 6
P.nextLevel (Emp "Joe" 5) [(firstGuestList, secondGuestList)]
`shouldBe` (GL [Emp "John" 1, Emp "Sue" 5, Emp "Joe" 5] 11, GL [Emp "Stan" 9, Emp "Bob" 2] 16)

describe "maxFun" $
it "returns a fun-maximizing guest list" $
P.maxFun testCompany `shouldBe` GL [Emp "Sarah" 17] 17

describe "main" $
it "prints out a formatted guest list" $ do
mainResult <- P.main
formattedGuestList <- putStrLn $ "Total fun: 268\nFrancis Deluzain\nHenri Bishop\nMargareth Adix\n"
mainResult `shouldBe` formattedGuestList
2 changes: 1 addition & 1 deletion homework10/aparser.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ test-suite examples
build-depends:
base
, aparser
, doctest >= 0.10
, hspec
default-language: Haskell2010


Expand Down
2 changes: 1 addition & 1 deletion homework10/stack.yaml
Original file line number Diff line number Diff line change
@@ -1 +1 @@
resolver: lts-4.1
resolver: lts-7.14
94 changes: 83 additions & 11 deletions homework10/test/examples/Main.hs
Original file line number Diff line number Diff line change
@@ -1,15 +1,87 @@
module Main
( main
)
where

-- doctest
import qualified Test.DocTest as DocTest
( main )
where

import Control.Applicative
import AParser
import Data.Char
import Test.Hspec

main :: IO ()
main =
DocTest.doctest
[ "-isrc"
, "src/AParser.hs"
]
main = hspec $ do

describe "fmap (Parser)" $
it "returns the result of fmap in Parser value" $ do
let concatStr str = Just(str, str ++ "haskell")
let parserStr = map toUpper <$> Parser concatStr
runParser parserStr "abc" `shouldBe` Just ("ABC","abchaskell")

describe "pure (Parser)" $
it "returns pure value of Applicative instance for Parser" $ do
let parser = pure "ABC"
runParser parser "abc" `shouldBe` Just ("ABC","abc")

describe "<*> (Parser)" $
it "returns sequential application of Applicative instance for Parser" $ do
let concatStr str = Just(str, str ++ "haskell")
let pureParser = pure $ map toUpper
let parserStr = pureParser <*> Parser concatStr
runParser parserStr "abc" `shouldBe` Just ("ABC","abchaskell")

describe "abParser" $ do
context "when the first characters of the input are 'a' and 'b'" $
it "returns a Just value with (a,b) tuple and the input without this characters" $
runParser abParser "abcdef" `shouldBe` Just (('a','b'),"cdef")

context "when the first characters of the input aren't 'a' and 'b'" $
it "returns Nothing" $
runParser abParser "aebcdf" `shouldBe` Nothing

describe "abParser_" $ do
context "when the first characters of the input are 'a' and 'b'" $
it "returns a Just value with empty tuple and the input without 'a' and 'b' characters" $
runParser abParser_ "abcdef" `shouldBe` Just ((),"cdef")

context "when the first characters of the input aren't 'a' and 'b'" $
it "returns Nothing" $
runParser abParser_ "aebcdf" `shouldBe` Nothing

describe "intPair" $ do
context "when the input contains two integer values separated by a space" $
it "returns the integer values in a list" $
runParser intPair "12 34" `shouldBe` Just ([12,34],"")

context "when the input does not contains two integer values separated by a space" $
it "returns Nothing" $
runParser intPair "a b" `shouldBe` Nothing

describe "empty (Parser)" $
it "returns Nothing" $ do
let parser = empty :: Parser [Integer]
runParser parser "abc" `shouldBe` Nothing

describe "<|> (Parser)" $ do
context "when the first parser succeeds" $
it "returns the result of the first parser" $ do
let firstParser = runParser intPair "12 34"
let secondParser = runParser intPair "56 78"
(firstParser <|> secondParser) `shouldBe` Just ([12,34],"")

context "when the first parser fails" $
it "returns the result of the second parser" $ do
let firstParser = runParser intPair "a b"
let secondParser = runParser intPair "56 78"
(firstParser <|> secondParser) `shouldBe` Just ([56,78],"")

describe "intOrUppercase" $ do
context "when the first characters of the input are numbers" $
it "returns a Just value with empty tuple and the input without numbers" $
runParser intOrUppercase "342abcd" `shouldBe` Just ((),"abcd")

context "when the first characters of the input are uppercase" $
it "returns a Just value with empty tuple and the input without first character" $
runParser intOrUppercase "XYZ" `shouldBe` Just ((),"YZ")

context "when the first characters of the input aren't numbers or uppercase" $
it "returns Nothing" $
runParser intOrUppercase "foo" `shouldBe` Nothing
2 changes: 1 addition & 1 deletion homework11/sexpr.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ test-suite examples
build-depends:
base
, sexpr
, doctest >= 0.10
, hspec
default-language: Haskell2010


Expand Down
4 changes: 2 additions & 2 deletions homework11/src/SExpr.hs
Original file line number Diff line number Diff line change
Expand Up @@ -74,13 +74,13 @@ type Ident =
data Atom
= N Integer
| I Ident
deriving Show
deriving (Show, Eq)


data SExpr
= A Atom
| Comb [SExpr]
deriving Show
deriving (Show, Eq)


-- |
Expand Down
2 changes: 1 addition & 1 deletion homework11/stack.yaml
Original file line number Diff line number Diff line change
@@ -1 +1 @@
resolver: lts-4.1
resolver: lts-7.14
Loading