Tests for CIS 194 homework assignments (1 to 6)#8
Conversation
| @@ -1,15 +1,44 @@ | |||
| module Main | |||
| ( main | |||
There was a problem hiding this comment.
I think we should keep this to be explicit about what the module exports
| ] | ||
| main = hspec $ do | ||
|
|
||
| describe "toDigits" $ do |
There was a problem hiding this comment.
Not sure if it's important, but what about the case for numbers starting with 0 like 01234
There was a problem hiding this comment.
The signature of toDigits function is toDigits :: Integer -> [Integer]. Haskell don't use leading zeros, so toDigits 01234 == toDigits 1234.
homework01/test/examples/Main.hs
Outdated
| it "returns an empty list for negative inputs" $ | ||
| toDigits (-17) `shouldBe` [] | ||
|
|
||
| describe "toDigitsRev" $ |
There was a problem hiding this comment.
What does this function makes if a negative number is passed as argument? and like before, not sure if important but what if the number starts with 0
There was a problem hiding this comment.
Checking homework 01, toDigitsRev function has the same tests as toDigits function, I'm going to add the corresponding tests.
| it "converts positive integers to a list of digits reversed" $ | ||
| toDigitsRev 1234 `shouldBe` [4, 3, 2, 1] | ||
|
|
||
| describe "doubleEveryOther" $ do |
There was a problem hiding this comment.
Not sure if important, but what happens with an empty list?
There was a problem hiding this comment.
I'm not sure if it's necessary to add a test with an empty lists as input, because the exercise doesn't indicate this kind of input. I know that is necessary to test all cases (valid and invalid inputs) but this can produce confusion in a Haskell beginner when he test doubleEveryOther function and don't pass a test with an empty list.
| @@ -1,15 +1,39 @@ | |||
| module Main | |||
| ( main | |||
There was a problem hiding this comment.
Again, I think it's a better idea to keep specific what the module exports
| @@ -1,15 +1,61 @@ | |||
| module Main | |||
| ( main | |||
There was a problem hiding this comment.
same as above, let's keep being specific about what a module exports
homework05/test/examples/Main.hs
Outdated
| it "evaluates expression without parenthesis" $ | ||
| evalStr "2+3*4" `shouldBe` Just 14 | ||
|
|
||
| it "evaluates wrong expression" $ |
There was a problem hiding this comment.
can we make a case for a negative number too?
There was a problem hiding this comment.
I'm going to modify this tests to consider operations with negative numbers.
| let testSat = parseExp lit add mul "(3 * 4) + (-5)" :: Maybe Mod7 | ||
| testSat `shouldBe` Just (Mod7 0) | ||
|
|
||
| describe "compile" $ |
There was a problem hiding this comment.
parseExp function in Parser.hs file handles this case, producing Nothing for the inputs which are not well-formed expressions.
| @@ -1,15 +1,45 @@ | |||
| module Main | |||
| ( main | |||
There was a problem hiding this comment.
same as above about what the module exports
homework05/test/examples/Main.hs
Outdated
|
|
||
| describe "reify" $ | ||
| it "makes an instance of Expr to ExprT type" $ | ||
| reify (mul (add (lit 2) (lit 3)) (lit 4)) `shouldBe` ExprT.Mul (ExprT.Add (ExprT.Lit 2) (ExprT.Lit 3)) (ExprT.Lit 4) |
There was a problem hiding this comment.
please, split this line to make it more readable
flandrade
left a comment
There was a problem hiding this comment.
@jmorocho Good job. I left some comments. Could you please take a look? Thanks for your contribution!
In addition, could you please run hlint?
homework02/test/examples/Main.hs
Outdated
| describe "whatWentWrong" $ | ||
| it "returns a list of the messages corresponding to any errors with a severity of 50 or greater, sorted by timestamp" $ | ||
| pendingWith " whatWentWrong returns IO [String]" | ||
| -- testWhatWentWrong parse whatWentWrong "src/sample.log" `shouldBe` ["Way too many pickles","Bad pickle-flange interaction detected","Flange failed!"] |
There was a problem hiding this comment.
@jmorocho Could you please remove this comment? Thanks.
homework01/test/examples/Main.hs
Outdated
|
|
||
| it "gets a list of moves whit 3 discs" $ | ||
| hanoi 3 "a" "b" "c" `shouldBe` [("a","b"),("a","c"),("b","c"),("a","b"),("c","a"),("c","b"),("a","b")] | ||
|
No newline at end of file |
There was a problem hiding this comment.
@jmorocho Could you please add a EOF space?
homework02/test/examples/Main.hs
Outdated
| it "returns a list of the messages corresponding to any errors sorted by timestamp" $ | ||
| pendingWith "whatWentWrong returns IO [String]" | ||
| -- testWhatWentWrong parse whatWentWrong "src/sample.log" | ||
| -- `shouldBe` ["Way too many pickles","Bad pickle-flange interaction detected","Flange failed!"] |
There was a problem hiding this comment.
@jmorocho Could you please remove this comment?
There was a problem hiding this comment.
I have written the test of this function. Check please.
homework01/test/examples/Main.hs
Outdated
| main = hspec $ do | ||
|
|
||
| describe "toDigits" $ do | ||
| it "converts positive integers to a list of digits" $ |
There was a problem hiding this comment.
@jmorocho Some of these describe a context. Could you please verify and add context? http://hspec.github.io/writing-specs.html
describe "toDigits" $ do
context "when digits are positive integers" $ do
it "converts to a list of digits" $ doThere was a problem hiding this comment.
You are right, the context is when digits are positive integers. I going to change this.
There was a problem hiding this comment.
@jmorocho Good job so far. I left some comments regarding the use of context. This might be helpful: http://lmws.net/describe-vs-context-in-rspec
homework01/test/examples/Main.hs
Outdated
| sumDigits [16, 7, 12, 5] `shouldBe` 22 | ||
|
|
||
| describe "validate" $ do | ||
| it "returns True if an Integer is a valid credit card number" $ |
There was a problem hiding this comment.
@jmorocho What about the following?
context "when the credit card number is valid
it "returns True"There was a problem hiding this comment.
I changed the context in case of valid an invalid credit card number.
homework03/test/examples/Main.hs
Outdated
| it "finds local maxima and gets 1 elements" $ | ||
| localMaxima [2,3,4,1,5] `shouldBe` [4] | ||
|
|
||
| it "finds local maxima and not gets elements" $ |
There was a problem hiding this comment.
@jmorocho "find local maxima and doesn't get elements"?
In addition, I think we're missing some context functions here. Why in some case do you get one element or two?
context "list.... "
it "gets 2 elements"There was a problem hiding this comment.
Context added in case that exists at least one local maximum or none.
homework04/test/examples/Main.hs
Outdated
| it "returns xor from a list with an odd number of True values" $ | ||
| xor [False, True, False] `shouldBe` True | ||
|
|
||
| it "returns xor from a list with an even number of True values" $ |
There was a problem hiding this comment.
@jmorocho context when list has an even number of elements?
homework05/test/examples/Main.hs
Outdated
| eval (ExprT.Mul (ExprT.Add (ExprT.Lit 2) (ExprT.Lit 3)) (ExprT.Lit (-4))) `shouldBe` (-20) | ||
|
|
||
| describe "evalStr" $ do | ||
| it "evaluates expression with parenthesis" $ |
There was a problem hiding this comment.
@jmorocho What about the following?
context "when an expression has parenthesis" $ do
it "evaluates the expression"There was a problem hiding this comment.
Added two nested contexts because when the expression is well-formed (with or without parentheses), the function return value of the expression and when the expression is not well-formed (with or without parentheses), return Nothing.
homework05/test/examples/Main.hs
Outdated
| `shouldBe` ExprT.Mul (ExprT.Add (ExprT.Lit 2) (ExprT.Lit 3)) (ExprT.Lit (-4)) | ||
|
|
||
| describe "parseExp" $ | ||
| context "when provided with multiple data types" $ do |
There was a problem hiding this comment.
@jmorocho I think it's better to have multiple contexts here. For example:
context "when provided with an Integer data type" $ do
it "evaluates the expression"What do you think?
There was a problem hiding this comment.
Your are right, this test have multiple contexts (Integer, Bool, etc.).
homework05/test/examples/Main.hs
Outdated
| compile "(3*4)" `shouldBe` ((Just [PushI 3,PushI 4,StackVM.Mul]) :: Maybe Program) | ||
|
|
||
| describe "withVars" $ do | ||
| it "evaluates all expression if variable exist in a operation of Expr class" $ |
There was a problem hiding this comment.
@jmorocho What about the following?
context "when the provided variable exists in an operation of Expr class" $ do
it "evaluates the expression"
homework01/test/examples/Main.hs
Outdated
| it "converts to a list of digits reversed" $ | ||
| toDigitsRev 9876 `shouldBe` [6, 7, 8, 9] | ||
|
|
||
| it "returns an empty list for 0" $ |
There was a problem hiding this comment.
@jmorocho I think we're missing some context functions. Perhaps:
context "when the digit is 0"
it "returns an empty list"
homework01/test/examples/Main.hs
Outdated
| toDigitsRev (-1) `shouldBe` [] | ||
|
|
||
| describe "doubleEveryOther" $ do | ||
| it "doubles every other number beginning from the right (even number of elements)" $ |
There was a problem hiding this comment.
@jmorocho Perhaps:
context "when the list has an even number of elements"
it "doubles every other number beginning from the right" Could you please verify for each case of doubleEveryOther?
I have added tests based in homeworks (1 to 6) of CIS 194 course. The reason for this pull request is that all tests that use
doctestpackage exists. Thedoctestpackage definition is:It can cause confusion at the time of run tests, because usually tests source code are in other files and folders. I think that a better practice is to use a testing package for this tests. I choosed
hspecpackage because I found this package in some Stack Builders repositories. Thehspecpackage definition is:I have used
hspecpackage to migratedoctesttests to this testing framework.Please check these tests and write any commentary if is necessary.