summaryrefslogtreecommitdiff
path: root/libraries/base/tests/T13896.hs
blob: 8b4c47f44a8d5f4e5fbbf0c3bf9fda619af8476c (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
import GHC.ResponseFile

assertEqual :: (Eq a, Show a) => a -> a -> IO ()
assertEqual x y = if x == y
                  then return ()
                  else error $ "assertEqual: " ++ show x ++ " /= " ++ show y

-- Migrated from Haddock.

-- The first two elements are
--   1) a list of 'args' to encode and
--   2) a single string of the encoded args
-- The 3rd element is just a description for the tests.
testStrs :: [(([String], String), String)]
testStrs =
  [ ((["a simple command line"],
      "a\\ simple\\ command\\ line\n"),
     "the white-space, end with newline")

  , ((["arg 'foo' is single quoted"],
      "arg\\ \\'foo\\'\\ is\\ single\\ quoted\n"),
     "the single quotes as well")

  , ((["arg \"bar\" is double quoted"],
      "arg\\ \\\"bar\\\"\\ is\\ double\\ quoted\n"),
     "the double quotes as well" )

  , ((["arg \"foo bar\" has embedded whitespace"],
      "arg\\ \\\"foo\\ bar\\\"\\ has\\ embedded\\ whitespace\n"),
     "the quote-embedded whitespace")

  , ((["arg 'Jack said \\'hi\\'' has single quotes"],
      "arg\\ \\'Jack\\ said\\ \\\\\\'hi\\\\\\'\\'\\ has\\ single\\ quotes\n"),
     "the escaped single quotes")

  , ((["arg 'Jack said \\\"hi\\\"' has double quotes"],
      "arg\\ \\'Jack\\ said\\ \\\\\\\"hi\\\\\\\"\\'\\ has\\ double\\ quotes\n"),
     "the escaped double quotes")

  , ((["arg 'Jack said\\r\\n\\t \\\"hi\\\"' has other whitespace"],
      "arg\\ \\'Jack\\ said\\\\r\\\\n\\\\t\\ \\\\\\\"hi\\\\\\\"\\'\\ has\\ \
           \other\\ whitespace\n"),
     "the other whitespace")

  , (([ "--prologue=.\\dist\\.\\haddock-prologue3239114604.txt"
      , "--title=HaddockNewline-0.1.0.0: This has a\n\
        \newline yo."
      , "-BC:\\Program Files\\Haskell Platform\\lib"],
      "--prologue=.\\\\dist\\\\.\\\\haddock-prologue3239114604.txt\n\
      \--title=HaddockNewline-0.1.0.0:\\ This\\ has\\ a\\\n\
      \newline\\ yo.\n\
      \-BC:\\\\Program\\ Files\\\\Haskell\\ Platform\\\\lib\n"),
     "an actual haddock response file snippet with embedded newlines")
  ]

main :: IO ()
main = do
  -- Test escapeArgs
  mapM_ (\((ss1,s2),des) -> escapeArgs ss1 `assertEqual` s2) testStrs

  -- Test unescapeArgs
  mapM_ (\((ss1,s2),des) -> unescapeArgs s2 `assertEqual` ss1) testStrs

  -- Given unescaped quotes, it should pass-through,
  -- without escaping everything inside

  (filter (not . null) $
    unescapeArgs "this\\ is\\ 'not escape\\d \"inside\"'\\ yo\n")
    `assertEqual`
    ["this is not escaped \"inside\" yo"]

  (filter (not . null) $
    unescapeArgs "this\\ is\\ \"not escape\\d 'inside'\"\\ yo\n")
    `assertEqual`
    ["this is not escaped 'inside' yo"]