Statistics
| Branch: | Tag: | Revision:

root / test / hs / Test / Ganeti / Attoparsec.hs @ efa6dd08

History | View | Annotate | Download (2 kB)

1
{-# LANGUAGE TemplateHaskell #-}
2

    
3
{-| Unittests for Attoparsec support for unicode -}
4

    
5
{-
6

    
7
Copyright (C) 2012 Google Inc.
8

    
9
This program is free software; you can redistribute it and/or modify
10
it under the terms of the GNU General Public License as published by
11
the Free Software Foundation; either version 2 of the License, or
12
(at your option) any later version.
13

    
14
This program is distributed in the hope that it will be useful, but
15
WITHOUT ANY WARRANTY; without even the implied warranty of
16
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17
General Public License for more details.
18

    
19
You should have received a copy of the GNU General Public License
20
along with this program; if not, write to the Free Software
21
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
22
02110-1301, USA.
23

    
24
-}
25

    
26
module Test.Ganeti.Attoparsec (testAttoparsec) where
27

    
28
import Test.HUnit
29

    
30
import Test.Ganeti.TestHelper
31

    
32
import qualified Data.Attoparsec.Text as A
33
import Data.Attoparsec.Text (Parser)
34
import Data.Text (pack, unpack)
35

    
36
-- | Unicode test string, first part.
37
part1 :: String
38
part1 = "äßĉ"
39

    
40
-- | Unicode test string, second part.
41
part2 :: String
42
part2 = "ðèق"
43

    
44
-- | Simple parser able to split a string in two parts, name and
45
-- value, separated by a '=' sign.
46
simpleParser :: Parser (String, String)
47
simpleParser = do
48
  n <- A.takeTill (\c -> A.isHorizontalSpace c || c == '=')
49
  A.skipWhile A.isHorizontalSpace
50
  _ <- A.char '='
51
  A.skipWhile A.isHorizontalSpace
52
  v <- A.takeTill A.isEndOfLine
53
  return (unpack n, unpack v)
54

    
55
{-# ANN case_unicodeParsing "HLint: ignore Use camelCase" #-}
56
-- | Tests whether a Unicode string is still Unicode after being
57
-- parsed.
58
case_unicodeParsing :: Assertion
59
case_unicodeParsing =
60
  case A.parseOnly simpleParser text of
61
    Right (name, value) -> do
62
      assertEqual "name part" part1 name
63
      assertEqual "value part" part2 value
64
    Left msg -> assertFailure $ "Failed to parse: " ++ msg
65
  where text = Data.Text.pack $ part1 ++ "  = \t" ++ part2
66

    
67
testSuite "Attoparsec"
68
          [ 'case_unicodeParsing
69
          ]