Statistics
| Branch: | Tag: | Revision:

root / htest / Test / Ganeti / Attoparsec.hs @ 43b3b5c1

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.QuickCheck
29

    
30
import Test.Ganeti.TestHelper
31
import Test.Ganeti.TestCommon
32

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

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

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

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

    
56
-- | Tests whether a Unicode string is still Unicode after being
57
-- parsed.
58
prop_parserSupportsUnicode :: Property
59
prop_parserSupportsUnicode = case A.parseOnly simpleParser text of
60
    Right (name, value) -> (name ==? part1) .&&. (value ==? part2)
61
    Left msg -> failTest $ "Failed to parse: " ++ msg
62
  where text = Data.Text.pack $ part1 ++ "  = \t" ++ part2
63

    
64
testSuite "Attoparsec"
65
          [ 'prop_parserSupportsUnicode
66
          ]