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 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326
|
{-# LANGUAGE CPP #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE TemplateHaskell #-}
-- Copyright (C) 2012 John Millikin <jmillikin@gmail.com>
--
-- See license.txt for details
module OptionsTests.Tokenize
( suite_Tokenize
) where
import Test.Chell
import Options.Types
import Options.Tokenize
suite_Tokenize :: Suite
suite_Tokenize = suite "tokenize"
[ test_Empty
, test_NoFlag
, test_ShortFlag
, test_ShortFlagUnknown
, test_ShortFlagMissing
, test_ShortFlagUnary
, test_ShortFlagDuplicate
, test_LongFlag
, test_LongFlagUnknown
, test_LongFlagMissing
, test_LongFlagUnary
, test_LongFlagDuplicate
, test_EndFlags
, test_Subcommand
, test_SubcommandUnknown
, test_Unicode
]
commandDefs :: OptionDefinitions
commandDefs = OptionDefinitions
[ OptionInfo (OptionKey "test.a") ['a'] ["long-a"] "default" False False "" Nothing Nothing ""
, OptionInfo (OptionKey "test.x") ['x'] ["long-x"] "default" True False "" Nothing Nothing ""
, OptionInfo (OptionKey "test.y") ['y'] ["long-y"] "default" True False "" Nothing Nothing ""
, OptionInfo (OptionKey "test.z") ['z'] ["long-z"] "default" True False "" Nothing Nothing ""
]
[]
subcommandDefs :: OptionDefinitions
subcommandDefs = OptionDefinitions
[ OptionInfo (OptionKey "test.a") ['a'] ["long-a"] "default" False False "" Nothing Nothing ""
, OptionInfo (OptionKey "test.b") ['b'] ["long-b"] "default" False False "" Nothing Nothing ""
, OptionInfo (OptionKey "test.x") ['x'] ["long-x"] "default" True False "" Nothing Nothing ""
, OptionInfo (OptionKey "test.y") ['y'] ["long-y"] "default" True False "" Nothing Nothing ""
, OptionInfo (OptionKey "test.z") ['z'] ["long-z"] "default" True False "" Nothing Nothing ""
]
[ ("sub1",
[ OptionInfo (OptionKey "sub.d") ['d'] ["long-d"] "default" False False "" Nothing Nothing ""
, OptionInfo (OptionKey "sub.e") ['e'] ["long-e"] "default" True False "" Nothing Nothing ""
])
, ("sub2",
[ OptionInfo (OptionKey "sub.d") ['d'] ["long-d"] "default" True False "" Nothing Nothing ""
, OptionInfo (OptionKey "sub.e") ['e'] ["long-e"] "default" True False "" Nothing Nothing ""
])
]
unicodeDefs :: OptionDefinitions
unicodeDefs = OptionDefinitions
[ OptionInfo (OptionKey "test.a") ['\12354'] ["long-\12354"] "default" False False "" Nothing Nothing ""
]
[]
test_Empty :: Test
test_Empty = assertions "empty" $ do
let (subcmd, eTokens) = tokenize commandDefs []
$expect (equal Nothing subcmd)
$assert (right eTokens)
let Right (Tokens tokens args) = eTokens
$expect (equalTokens [] tokens)
$expect (equal [] args)
test_NoFlag :: Test
test_NoFlag = assertions "no-flag" $ do
let (subcmd, eTokens) = tokenize commandDefs ["-", "foo", "bar"]
$expect (equal Nothing subcmd)
$assert (right eTokens)
let Right (Tokens tokens args) = eTokens
$expect (equalTokens [] tokens)
$expect (equal ["-", "foo", "bar"] args)
test_ShortFlag :: Test
test_ShortFlag = assertions "short-flag" $ do
do
let (subcmd, eTokens) = tokenize commandDefs ["-a", "foo", "bar"]
$expect (equal Nothing subcmd)
$assert (right eTokens)
let Right (Tokens tokens args) = eTokens
$expect (equalTokens [("test.a", Token "-a" "foo")] tokens)
$expect (equal ["bar"] args)
do
let (subcmd, eTokens) = tokenize commandDefs ["-afoo", "bar"]
$expect (equal Nothing subcmd)
$assert (right eTokens)
let Right (Tokens tokens args) = eTokens
$expect (equalTokens [("test.a", Token "-a" "foo")] tokens)
$expect (equal ["bar"] args)
test_ShortFlagUnknown :: Test
test_ShortFlagUnknown = assertions "short-flag-unknown" $ do
let (subcmd, eTokens) = tokenize commandDefs ["-c", "foo", "bar"]
$expect (equal Nothing subcmd)
$assert (left eTokens)
let Left err = eTokens
$expect (equal "Unknown flag -c" err)
test_ShortFlagMissing :: Test
test_ShortFlagMissing = assertions "short-flag-missing" $ do
let (subcmd, eTokens) = tokenize commandDefs ["-a"]
$expect (equal Nothing subcmd)
$assert (left eTokens)
let Left err = eTokens
$expect (equal "The flag -a requires a parameter." err)
test_ShortFlagUnary :: Test
test_ShortFlagUnary = assertions "short-flag-unary" $ do
do
let (subcmd, eTokens) = tokenize commandDefs ["-x", "foo", "bar"]
$expect (equal Nothing subcmd)
$assert (right eTokens)
let Right (Tokens tokens args) = eTokens
$expect (equalTokens [("test.x", TokenUnary "-x")] tokens)
$expect (equal ["foo", "bar"] args)
do
let (subcmd, eTokens) = tokenize commandDefs ["-xy", "foo", "bar"]
$expect (equal Nothing subcmd)
$assert (right eTokens)
let Right (Tokens tokens args) = eTokens
$expect (equalTokens [("test.x", TokenUnary "-x"), ("test.y", TokenUnary "-y")] tokens)
$expect (equal ["foo", "bar"] args)
test_ShortFlagDuplicate :: Test
test_ShortFlagDuplicate = assertions "short-flag-duplicate" $ do
do
let (subcmd, eTokens) = tokenize commandDefs ["-x", "-x"]
$expect (equal Nothing subcmd)
$assert (right eTokens)
let Right (Tokens tokens args) = eTokens
$expect (equalTokens [("test.x", TokenUnary "-x"), ("test.x", TokenUnary "-x")] tokens)
do
let (subcmd, eTokens) = tokenize commandDefs ["-afoo", "-a", "foo"]
$expect (equal Nothing subcmd)
$assert (right eTokens)
let Right (Tokens tokens args) = eTokens
$expect (equalTokens [("test.a", Token "-a" "foo"), ("test.a", Token "-a" "foo")] tokens)
do
let (subcmd, eTokens) = tokenize commandDefs ["-afoo", "-afoo"]
$expect (equal Nothing subcmd)
$assert (right eTokens)
let Right (Tokens tokens args) = eTokens
$expect (equalTokens [("test.a", Token "-a" "foo"), ("test.a", Token "-a" "foo")] tokens)
test_LongFlag :: Test
test_LongFlag = assertions "long-flag" $ do
do
let (subcmd, eTokens) = tokenize commandDefs ["--long-a", "foo", "bar"]
$expect (equal Nothing subcmd)
$assert (right eTokens)
let Right (Tokens tokens args) = eTokens
$expect (equalTokens [("test.a", Token "--long-a" "foo")] tokens)
$expect (equal ["bar"] args)
do
let (subcmd, eTokens) = tokenize commandDefs ["--long-a=foo", "bar"]
$expect (equal Nothing subcmd)
$assert (right eTokens)
let Right (Tokens tokens args) = eTokens
$expect (equalTokens [("test.a", Token "--long-a" "foo")] tokens)
$expect (equal ["bar"] args)
test_LongFlagUnknown :: Test
test_LongFlagUnknown = assertions "long-flag-unknown" $ do
do
let (subcmd, eTokens) = tokenize commandDefs ["--long-c", "foo", "bar"]
$expect (equal Nothing subcmd)
$assert (left eTokens)
let Left err = eTokens
$expect (equal "Unknown flag --long-c" err)
do
let (subcmd, eTokens) = tokenize commandDefs ["--long-c=foo", "bar"]
$expect (equal Nothing subcmd)
$assert (left eTokens)
let Left err = eTokens
$expect (equal "Unknown flag --long-c" err)
test_LongFlagMissing :: Test
test_LongFlagMissing = assertions "long-flag-missing" $ do
let (subcmd, eTokens) = tokenize commandDefs ["--long-a"]
$expect (equal Nothing subcmd)
$assert (left eTokens)
let Left err = eTokens
$expect (equal "The flag --long-a requires a parameter." err)
test_LongFlagUnary :: Test
test_LongFlagUnary = assertions "long-flag-unary" $ do
do
let (subcmd, eTokens) = tokenize commandDefs ["--long-x", "foo", "bar"]
$expect (equal Nothing subcmd)
$assert (right eTokens)
let Right (Tokens tokens args) = eTokens
$expect (equalTokens [("test.x", TokenUnary "--long-x")] tokens)
$expect (equal ["foo", "bar"] args)
do
let (subcmd, eTokens) = tokenize commandDefs ["--long-x=foo", "bar"]
$expect (equal Nothing subcmd)
$assert (right eTokens)
let Right (Tokens tokens args) = eTokens
$expect (equalTokens [("test.x", Token "--long-x" "foo")] tokens)
$expect (equal ["bar"] args)
test_LongFlagDuplicate :: Test
test_LongFlagDuplicate = assertions "long-flag-duplicate" $ do
do
let (subcmd, eTokens) = tokenize commandDefs ["-x", "--long-x"]
$expect (equal Nothing subcmd)
$assert (right eTokens)
let Right (Tokens tokens args) = eTokens
$expect (equalTokens [("test.x", TokenUnary "-x"), ("test.x", TokenUnary "--long-x")] tokens)
do
let (subcmd, eTokens) = tokenize commandDefs ["-afoo", "--long-a", "foo"]
$expect (equal Nothing subcmd)
$assert (right eTokens)
let Right (Tokens tokens args) = eTokens
$expect (equalTokens [("test.a", Token "-a" "foo"), ("test.a", Token "--long-a" "foo")] tokens)
do
let (subcmd, eTokens) = tokenize commandDefs ["-afoo", "--long-a=foo"]
$expect (equal Nothing subcmd)
$assert (right eTokens)
let Right (Tokens tokens args) = eTokens
$expect (equalTokens [("test.a", Token "-a" "foo"), ("test.a", Token "--long-a" "foo")] tokens)
test_EndFlags :: Test
test_EndFlags = assertions "end-flags" $ do
let (subcmd, eTokens) = tokenize commandDefs ["foo", "--", "-a", "bar"]
$expect (equal Nothing subcmd)
$assert (right eTokens)
let Right (Tokens tokens args) = eTokens
$expect (equalTokens [] tokens)
$expect (equal ["foo", "-a", "bar"] args)
test_Subcommand :: Test
test_Subcommand = assertions "subcommand" $ do
do
let (subcmd, eTokens) = tokenize subcommandDefs ["-x", "sub1", "-d", "foo", "--long-e", "bar"]
$expect (equal (Just "sub1") subcmd)
$assert (right eTokens)
let Right (Tokens tokens args) = eTokens
$expect (equalTokens [("test.x", TokenUnary "-x"), ("sub.d", Token "-d" "foo"), ("sub.e", TokenUnary "--long-e")] tokens)
$expect (equal ["bar"] args)
do
let (subcmd, eTokens) = tokenize subcommandDefs ["-x", "sub2", "-d", "foo", "--long-e", "bar"]
$expect (equal (Just "sub2") subcmd)
$assert (right eTokens)
let Right (Tokens tokens args) = eTokens
$expect (equalTokens [("test.x", TokenUnary "-x"), ("sub.d", TokenUnary "-d"), ("sub.e", TokenUnary "--long-e")] tokens)
$expect (equal ["foo", "bar"] args)
test_SubcommandUnknown:: Test
test_SubcommandUnknown = assertions "subcommand-unknown" $ do
let (subcmd, eTokens) = tokenize subcommandDefs ["foo"]
$expect (equal Nothing subcmd)
$assert (left eTokens)
let Left err = eTokens
$expect (equal "Unknown subcommand \"foo\"." err)
test_Unicode :: Test
test_Unicode = assertions "unicode" $ do
#if defined(OPTIONS_ENCODING_UTF8)
let shortArgs = ["-\227\129\130", "foo", "bar"]
let longArgs = ["--long-\227\129\130=foo", "bar"]
#else
let shortArgs = ["-\12354", "foo", "bar"]
let longArgs = ["--long-\12354=foo", "bar"]
#endif
do
let (subcmd, eTokens) = tokenize unicodeDefs shortArgs
$expect (equal Nothing subcmd)
case eTokens of
Left err -> error err
Right _ -> return ()
$assert (right eTokens)
let Right (Tokens tokens args) = eTokens
$expect (equalTokens [("test.a", Token "-\12354" "foo")] tokens)
$expect (equal ["bar"] args)
do
let (subcmd, eTokens) = tokenize unicodeDefs longArgs
$expect (equal Nothing subcmd)
$assert (right eTokens)
let Right (Tokens tokens args) = eTokens
$expect (equalTokens [("test.a", Token "--long-\12354" "foo")] tokens)
$expect (equal ["bar"] args)
equalTokens :: [(String, Token)] -> [([OptionKey], Token)] -> Assertion
equalTokens tokens = equal ([([OptionKey k], t) | (k, t) <- tokens])
|