summaryrefslogtreecommitdiff
path: root/Haskell-book/24/ParserExercises/test/Spec/Main.hs
blob: 23781c6bf956a8a95fe9385329f46e67cd27928b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
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"