| 1 |
{-# LANGUAGE TemplateHaskell #-} |
| 2 |
|
| 3 |
{-| Unittests for Attoparsec support for unicode -} |
| 4 |
|
| 5 |
{- |
| 6 |
|
| 7 |
Copyright (C) 2012 Google Inc. |
| 8 |
All rights reserved. |
| 9 |
|
| 10 |
Redistribution and use in source and binary forms, with or without |
| 11 |
modification, are permitted provided that the following conditions are |
| 12 |
met: |
| 13 |
|
| 14 |
1. Redistributions of source code must retain the above copyright notice, |
| 15 |
this list of conditions and the following disclaimer. |
| 16 |
|
| 17 |
2. Redistributions in binary form must reproduce the above copyright |
| 18 |
notice, this list of conditions and the following disclaimer in the |
| 19 |
documentation and/or other materials provided with the distribution. |
| 20 |
|
| 21 |
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS |
| 22 |
IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED |
| 23 |
TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
| 24 |
PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR |
| 25 |
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
| 26 |
EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 27 |
PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
| 28 |
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
| 29 |
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
| 30 |
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 31 |
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 32 |
|
| 33 |
-} |
| 34 |
|
| 35 |
module Test.Ganeti.Attoparsec (testAttoparsec) where |
| 36 |
|
| 37 |
import Test.HUnit |
| 38 |
|
| 39 |
import Test.Ganeti.TestHelper |
| 40 |
|
| 41 |
import qualified Data.Attoparsec.Text as A |
| 42 |
import Data.Attoparsec.Text (Parser) |
| 43 |
import Data.Text (pack, unpack) |
| 44 |
|
| 45 |
-- | Unicode test string, first part. |
| 46 |
part1 :: String |
| 47 |
part1 = "äßĉ" |
| 48 |
|
| 49 |
-- | Unicode test string, second part. |
| 50 |
part2 :: String |
| 51 |
part2 = "ðèق" |
| 52 |
|
| 53 |
-- | Simple parser able to split a string in two parts, name and |
| 54 |
-- value, separated by a '=' sign. |
| 55 |
simpleParser :: Parser (String, String) |
| 56 |
simpleParser = do |
| 57 |
n <- A.takeTill (\c -> A.isHorizontalSpace c || c == '=') |
| 58 |
A.skipWhile A.isHorizontalSpace |
| 59 |
_ <- A.char '=' |
| 60 |
A.skipWhile A.isHorizontalSpace |
| 61 |
v <- A.takeTill A.isEndOfLine |
| 62 |
return (unpack n, unpack v) |
| 63 |
|
| 64 |
{-# ANN case_unicodeParsing "HLint: ignore Use camelCase" #-} |
| 65 |
-- | Tests whether a Unicode string is still Unicode after being |
| 66 |
-- parsed. |
| 67 |
case_unicodeParsing :: Assertion |
| 68 |
case_unicodeParsing = |
| 69 |
case A.parseOnly simpleParser text of |
| 70 |
Right (name, value) -> do |
| 71 |
assertEqual "name part" part1 name |
| 72 |
assertEqual "value part" part2 value |
| 73 |
Left msg -> assertFailure $ "Failed to parse: " ++ msg |
| 74 |
where text = Data.Text.pack $ part1 ++ " = \t" ++ part2 |
| 75 |
|
| 76 |
testSuite "Attoparsec" |
| 77 |
[ 'case_unicodeParsing |
| 78 |
] |