File: Main.hs

package info (click to toggle)
haskell-irc-core 2.12-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 188 kB
  • sloc: haskell: 2,143; makefile: 3
file content (260 lines) | stat: -rw-r--r-- 8,105 bytes parent folder | download | duplicates (2)
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
{-# Language OverloadedStrings #-}
{-|
Module      : Main
Description : Tests for the irc-core library
Copyright   : (c) Eric Mertens, 2016
License     : ISC
Maintainer  : emertens@gmail.com

This module test IRC message parsing.

-}
module Main (main) where

import qualified Data.Text as Text
import           Data.Hashable
import           Data.Semigroup
import           Irc.RawIrcMsg
import           Irc.UserInfo
import           Irc.Identifier
import           System.Exit
import           Test.HUnit
import           Text.Read

main :: IO a
main =
  do outcome <- runTestTT tests
     if errors outcome == 0 && failures outcome == 0
       then exitSuccess
       else exitFailure

tests :: Test
tests = test [ irc0, irc2, irc15, ircWithPrefix, ircWithTags,
               parseUserInfos, renderUserInfos, renderIrc,
               badRawMsgs, userInfoFields, identifierInstances ]

-- | Check that we can handle commands without parameters
irc0 :: Test
irc0 = test [ assertEqual "" goal (parseRawIrcMsg alt) | alt <- alternatives ]
  where
    goal = Just (rawIrcMsg "COMMAND" [])
    alternatives =
      [ "COMMAND"
      , "COMMAND "
      , "COMMAND  "
      ]

-- | Check that we can handle commands with two parameters and an assortment of spacing
irc2 :: Test
irc2 = test [ assertEqual "" goal (parseRawIrcMsg alt) | alt <- alternatives ]
  where
    goal = Just (rawIrcMsg "COMMAND" ["param1","param2"])
    alternatives =
      [ "COMMAND param1 param2"
      , "COMMAND   param1 param2"
      , "COMMAND   param1   param2"
      , "COMMAND param1   param2"
      , "COMMAND param1   param2   "
      , "COMMAND param1   :param2"
      , "COMMAND param1 :param2"
      ]

-- | Check that we max out at 15 parameters
irc15 :: Test
irc15 = test
  [ assertEqual "" goal (parseRawIrcMsg raw1)
  , assertEqual "" goal (parseRawIrcMsg raw2)
  ]
  where
    goal   = Just (rawIrcMsg "001" (params ++ ["last  two"]))
    params = map (Text.pack . show) [1 .. 14 :: Int]
    raw1 = "001 " <> Text.unwords params <> " last  two"
    raw2 = "001 " <> Text.unwords params <> "  :last  two"

ircWithPrefix :: Test
ircWithPrefix = test
  [ assertEqual ""
      (Just (rawIrcMsg "254" ["glguytest", "57555", "channels formed"])
            { _msgPrefix = Just (UserInfo "morgan.example.net" "" "") })
      (parseRawIrcMsg ":morgan.example.net 254 glguytest 57555 :channels formed")
  ]

ircWithTags :: Test
ircWithTags = test
  [ assertEqual "without prefix"
      (Just (rawIrcMsg "CMD" [])
            { _msgTags = [TagEntry "time" "value"] })
      (parseRawIrcMsg "@time=value CMD")

  , assertEqual "with prefix"
      (Just (rawIrcMsg "CMD" [])
            { _msgTags = [TagEntry "time" "value"]
            , _msgPrefix = Just (UserInfo "prefix" "user" "host") })
      (parseRawIrcMsg "@time=value :prefix!user@host CMD")

  , assertEqual "two tags"
      (Just (rawIrcMsg "CMD" [])
            { _msgTags = [TagEntry "time" "value", TagEntry "this" "\n\rand\\ ;that"] })
      (parseRawIrcMsg "@time=value;this=\\n\\rand\\\\\\s\\:that CMD")

  , assertEqual "don't escape keys"
      (Just (rawIrcMsg "CMD" [])
            { _msgTags = [TagEntry "this\\s" "value"] })
      (parseRawIrcMsg "@this\\s=value CMD")

  , assertEqual "optional key"
      (Just (rawIrcMsg "CMD" [])
            { _msgTags = [TagEntry "this" ""] })
      (parseRawIrcMsg "@this CMD")

  , assertEqual "optional keys"
      (Just (rawIrcMsg "CMD" [])
            { _msgTags = [TagEntry "this" "", TagEntry "that" ""] })
      (parseRawIrcMsg "@this;that CMD")

  ]

renderUserInfos :: Test
renderUserInfos = test

  [ assertEqual "missing user and hostname"
      "glguy"
      (renderUserInfo (UserInfo "glguy" "" ""))

  , assertEqual "example cloak"
      "glguy!~glguy@haskell/developer/glguy"
      (renderUserInfo (UserInfo "glguy" "~glguy" "haskell/developer/glguy"))

  , assertEqual "missing user"
      "glguy@haskell/developer/glguy"
      (renderUserInfo (UserInfo "glguy" "" "haskell/developer/glguy"))

  , assertEqual "missing host"
      "glguy!~glguy"
      (renderUserInfo (UserInfo "glguy" "~glguy" ""))

  , assertEqual "extra @ goes into host"
      "nick!user@server@name"
      (renderUserInfo (UserInfo "nick" "user" "server@name"))

  , assertEqual "servername in nick"
      "morgan.example.net"
      (renderUserInfo (UserInfo "morgan.example.net" "" ""))
  ]

userInfoFields :: Test
userInfoFields = test
  [ assertEqual "nickfield" "nick" (userNick (UserInfo "nick" "user" "host"))
  , assertEqual "userfield" "user" (userName (UserInfo "nick" "user" "host"))
  , assertEqual "hostfield" "host" (userHost (UserInfo "nick" "user" "host"))
  ]

parseUserInfos :: Test
parseUserInfos = test

  [ assertEqual "missing user and hostname"
      (UserInfo "glguy" "" "")
      (parseUserInfo "glguy")

  , assertEqual "example cloak"
      (UserInfo "glguy" "~glguy" "haskell/developer/glguy")
      (parseUserInfo "glguy!~glguy@haskell/developer/glguy")

  , assertEqual "missing user"
      (UserInfo "glguy" "" "haskell/developer/glguy")
      (parseUserInfo "glguy@haskell/developer/glguy")

  , assertEqual "missing host"
      (UserInfo "glguy" "~glguy" "")
      (parseUserInfo "glguy!~glguy")

  , assertEqual "extra @ goes into host"
      (UserInfo "nick" "user" "server@name")
      (parseUserInfo "nick!user@server@name")

  , assertEqual "servername in nick"
      (UserInfo "morgan.example.net" "" "")
      (parseUserInfo "morgan.example.net")
  ]

renderIrc :: Test
renderIrc = test
  [ assertEqual ""
      ":morgan.example.net 254 glguytest 57555 :channels formed\r\n"
      (renderRawIrcMsg
          (rawIrcMsg "254" ["glguytest", "57555", "channels formed"])
          { _msgPrefix = Just (UserInfo "morgan.example.net" "" "") })

  , assertEqual ""
      "254 glguytest 57555 :channels formed\r\n"
      (renderRawIrcMsg
          (rawIrcMsg "254" ["glguytest", "57555", "channels formed"]))

  , assertEqual ""
      "CMD param:with:colon\r\n"
      (renderRawIrcMsg (rawIrcMsg "CMD" ["param:with:colon"]))

  , assertEqual ""
      "CMD ::param\r\n"
      (renderRawIrcMsg (rawIrcMsg "CMD" [":param"]))

  , assertEqual ""
      "CMD\r\n"
      (renderRawIrcMsg (rawIrcMsg "CMD" []))

  , assertEqual "two tags"
      "@time=value;this=\\n\\rand\\\\\\s\\:that CMD\r\n"
      (renderRawIrcMsg (rawIrcMsg "CMD" [])
            { _msgTags = [TagEntry "time" "value",
                          TagEntry "this" "\n\rand\\ ;that"] })

  , assertEqual "empty tags"
      "@time;magic CMD\r\n"
      (renderRawIrcMsg (rawIrcMsg "CMD" [])
            { _msgTags = [TagEntry "time" "",
                          TagEntry "magic" ""] })

  , assertEqual "empty last argument 1"
      "CMD :\r\n"
      (renderRawIrcMsg (rawIrcMsg "CMD" [""]))

  , assertEqual "empty last argument 2"
      "CMD X :\r\n"
      (renderRawIrcMsg (rawIrcMsg "CMD" ["X", ""]))

  ]

badRawMsgs :: Test
badRawMsgs = test
  [ assertEqual "bad prefix"
      Nothing
      (parseRawIrcMsg ": CMD")
  , assertEqual "empty string"
      Nothing
      (parseRawIrcMsg "")
  , assertEqual "whitespace"
      Nothing
      (parseRawIrcMsg "   ")
  , assertEqual "only prefix"
      Nothing
      (parseRawIrcMsg ":glguy!glguy@glguy")
  , assertEqual "only tags"
      Nothing
      (parseRawIrcMsg "@glguy=tester")
  ]

identifierInstances :: Test
identifierInstances = test
  [ assertEqual "read" (Just ("GLGUY"::Identifier)) (readMaybe "\"glguy\"")
  , assertEqual "read2" (Just ("Glguy"::Identifier)) (readMaybe "\"glguy\"")
  , assertEqual "show1" "\"GLguy\"" (show ("GLguy"::Identifier))
  , assertEqual "show2" "\"glguy\"" (show ("glguy"::Identifier))
  , assertBool "hash"  $ hash ("glguy"::Identifier) ==
                         hash ("GLGUY"::Identifier)
  , assertBool "lt1"   $ "glguy"  < ("tester" :: Identifier)
  , assertBool "gt1"   $ "tester" > ("glguy" :: Identifier)
  , assertBool "lt2"   $ "GLGUY"  < ("tester" :: Identifier)
  , assertBool "gt2"   $ "TESTER" > ("glguy" :: Identifier)
  , assertBool "pre"   $ idPrefix "gl" "GLGUY"
  , assertBool "pre"   $ not $ idPrefix "glguy" "gl"
  ]