Add remaining haskell book exercises
This commit is contained in:
14
Haskell-book/24/ParserExercises/test/LogTest/Main.hs
Normal file
14
Haskell-book/24/ParserExercises/test/LogTest/Main.hs
Normal file
@@ -0,0 +1,14 @@
|
||||
module Main where
|
||||
|
||||
import LogParser
|
||||
import Test.QuickCheck
|
||||
import Text.Trifecta
|
||||
|
||||
maybeSuccess :: Text.Trifecta.Result a -> Maybe a
|
||||
maybeSuccess (Text.Trifecta.Success a) = Just a
|
||||
maybeSuccess _ = Nothing
|
||||
|
||||
main :: IO ()
|
||||
main = do
|
||||
quickCheck ((\s -> (maybeSuccess $ parseString parseStatement mempty (show s)) == (Just s)) :: Statement -> Bool)
|
||||
quickCheck ((\s -> (maybeSuccess $ parseString parseLogEntry mempty (show s)) == (Just s)) :: LogEntry -> Bool)
|
||||
107
Haskell-book/24/ParserExercises/test/Spec/Main.hs
Normal file
107
Haskell-book/24/ParserExercises/test/Spec/Main.hs
Normal file
@@ -0,0 +1,107 @@
|
||||
import SemVer
|
||||
import Base10Integer
|
||||
import PhoneNumber
|
||||
import IPAddress
|
||||
import Test.Hspec
|
||||
import Text.Trifecta
|
||||
|
||||
maybeSuccess :: Result a -> Maybe a
|
||||
maybeSuccess (Success a) = Just a
|
||||
maybeSuccess _ = Nothing
|
||||
|
||||
parseIP :: String -> Maybe IPAddress
|
||||
parseIP s = o
|
||||
where r = parseString parseIP4 mempty s
|
||||
o = case r of
|
||||
(Success o) -> Just o
|
||||
_ -> Nothing
|
||||
|
||||
parseIP6' :: String -> Maybe IPAddress6
|
||||
parseIP6' s = o
|
||||
where r = parseString parseIP6 mempty s
|
||||
o = case r of
|
||||
(Success o) -> Just o
|
||||
_ -> Nothing
|
||||
|
||||
main :: IO ()
|
||||
main = hspec $ do
|
||||
describe "parseSemVer" $ do
|
||||
it "parses minimum SemVer" $ do
|
||||
let got = maybeSuccess $ parseString parseSemVer mempty "2.1.1"
|
||||
in got `shouldBe` Just (SemVer 2 1 1 [] [])
|
||||
it "parses release field" $ do
|
||||
let got = maybeSuccess $ parseString parseSemVer mempty "1.0.0-x.7.z.92"
|
||||
expected = Just $ SemVer 1 0 0 [NOSS "x", NOSI 7, NOSS "z", NOSI 92] []
|
||||
in got `shouldBe` expected
|
||||
|
||||
describe "parseDigit" $ do
|
||||
it "parses the first digit of '123'" $ do
|
||||
let got = maybeSuccess $ parseString parseDigit mempty "123"
|
||||
expected = Just '1'
|
||||
in got `shouldBe` expected
|
||||
it "fails on 'abc'" $ do
|
||||
let got = maybeSuccess $ parseString parseDigit mempty "abc"
|
||||
expected = Nothing
|
||||
in got `shouldBe` expected
|
||||
|
||||
describe "base10Integer" $ do
|
||||
it "parses the integer in '123abc'" $ do
|
||||
let got = maybeSuccess $ parseString base10Integer mempty "123abc"
|
||||
expected = Just 123
|
||||
in got `shouldBe` expected
|
||||
it "fails on 'abc'" $ do
|
||||
let got = maybeSuccess $ parseString base10Integer mempty "abc"
|
||||
expected = Nothing
|
||||
in got `shouldBe` expected
|
||||
|
||||
describe "base10Integer'" $ do
|
||||
it "parses negative numbers" $ do
|
||||
let got = maybeSuccess $ parseString base10Integer' mempty "-123abc"
|
||||
expected = Just (-123)
|
||||
in got `shouldBe` expected
|
||||
|
||||
describe "parsePhone" $ do
|
||||
it "parses '123-456-7890'" $ do
|
||||
let actual = maybeSuccess $ parseString parsePhone mempty "123-456-7890"
|
||||
expected = Just $ PhoneNumber 123 456 7890
|
||||
in actual `shouldBe` expected
|
||||
it "parses '1234567890'" $ do
|
||||
let actual = maybeSuccess $ parseString parsePhone mempty "1234567890"
|
||||
expected = Just $ PhoneNumber 123 456 7890
|
||||
in actual `shouldBe` expected
|
||||
it "parses '(123) 456-7890'" $ do
|
||||
let actual = maybeSuccess $ parseString parsePhone mempty "(123) 456-7890"
|
||||
expected = Just $ PhoneNumber 123 456 7890
|
||||
in actual `shouldBe` expected
|
||||
it "parses '1-123-456-7890'" $ do
|
||||
let actual = maybeSuccess $ parseString parsePhone mempty "1-123-456-7890"
|
||||
expected = Just $ PhoneNumber 123 456 7890
|
||||
in actual `shouldBe` expected
|
||||
|
||||
describe "parseIP4" $ do
|
||||
it "parses localhost" $ do
|
||||
let actual = maybeSuccess $ parseString parseIP4 mempty "127.0.0.1"
|
||||
expected = Just $ IPAddress 2130706433
|
||||
in actual `shouldBe` expected
|
||||
|
||||
describe "parseIP6" $ do
|
||||
it "parses localhost" $ do
|
||||
let actual = maybeSuccess $ parseString parseIP6 mempty "::1"
|
||||
expected = Just $ IPAddress6 0 1
|
||||
in actual `shouldBe` expected
|
||||
|
||||
describe "ipV4ToIpV6" $
|
||||
it "should work" $ do
|
||||
(show . ipV4ToIpV6 <$> parseIP "124.155.107.12") `shouldBe` Just "0:0:0:0:0:ffff:7c9b:6b0c"
|
||||
(show . ipV4ToIpV6 <$> parseIP "192.168.0.1") `shouldBe` Just "0:0:0:0:0:ffff:c0a8:1"
|
||||
|
||||
describe "show" $ do
|
||||
it "should show IPAddress6 properly" $ do
|
||||
(show <$> parseIP6' "ff39:0:0:0:2f2:b3ff:f23d:8d5") `shouldBe` Just "ff39:0:0:0:2f2:b3ff:f23d:8d5"
|
||||
(show <$> parseIP6' "9ff3:EA8::8A:30:2F0C:1F7A") `shouldBe` Just "9ff3:ea8:0:0:8a:30:2f0c:1f7a"
|
||||
(show <$> parseIP6' "::ffff:abc:fed9") `shouldBe` Just "0:0:0:0:0:ffff:abc:fed9"
|
||||
|
||||
it "should show IPAddress properly" $ do
|
||||
(show <$> parseIP "152.163.254.3") `shouldBe` Just "152.163.254.3"
|
||||
(show <$> parseIP "224.165.197.142") `shouldBe` Just "224.165.197.142"
|
||||
(show <$> parseIP "124.155.107.12") `shouldBe` Just "124.155.107.12"
|
||||
Reference in New Issue
Block a user