2015-08-03 10:19:23 +03:00
|
|
|
|
--
|
|
|
|
|
-- QuickCheck tests for Megaparsec's character parsers.
|
|
|
|
|
--
|
2016-01-09 15:56:33 +03:00
|
|
|
|
-- Copyright © 2015–2016 Megaparsec contributors
|
2015-08-03 10:19:23 +03:00
|
|
|
|
--
|
|
|
|
|
-- Redistribution and use in source and binary forms, with or without
|
|
|
|
|
-- modification, are permitted provided that the following conditions are
|
|
|
|
|
-- met:
|
|
|
|
|
--
|
|
|
|
|
-- * Redistributions of source code must retain the above copyright notice,
|
|
|
|
|
-- this list of conditions and the following disclaimer.
|
|
|
|
|
--
|
|
|
|
|
-- * Redistributions in binary form must reproduce the above copyright
|
|
|
|
|
-- notice, this list of conditions and the following disclaimer in the
|
|
|
|
|
-- documentation and/or other materials provided with the distribution.
|
|
|
|
|
--
|
2015-10-30 16:41:21 +03:00
|
|
|
|
-- THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS “AS IS” AND ANY
|
|
|
|
|
-- EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
|
|
|
|
-- WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
|
|
|
-- DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
|
|
|
|
-- DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
|
-- DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
|
-- OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
|
-- HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
|
|
|
|
|
-- STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
|
|
|
|
|
-- ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
|
-- POSSIBILITY OF SUCH DAMAGE.
|
2015-08-03 10:19:23 +03:00
|
|
|
|
|
2016-04-10 12:28:04 +03:00
|
|
|
|
{-# LANGUAGE CPP #-}
|
2015-08-12 20:00:03 +03:00
|
|
|
|
{-# OPTIONS -fno-warn-orphans #-}
|
|
|
|
|
|
2015-08-03 10:19:23 +03:00
|
|
|
|
module Char (tests) where
|
|
|
|
|
|
2015-08-08 21:42:47 +03:00
|
|
|
|
import Data.Char
|
2015-08-30 15:16:59 +03:00
|
|
|
|
import Data.List (findIndex, isPrefixOf)
|
2016-04-25 20:37:06 +03:00
|
|
|
|
import Data.List.NonEmpty (NonEmpty (..))
|
2016-04-24 16:23:03 +03:00
|
|
|
|
import qualified Data.List.NonEmpty as NE
|
2015-08-08 12:49:57 +03:00
|
|
|
|
|
2015-08-03 10:19:23 +03:00
|
|
|
|
import Test.Framework
|
2015-08-08 12:49:57 +03:00
|
|
|
|
import Test.Framework.Providers.QuickCheck2 (testProperty)
|
2015-08-17 18:58:59 +03:00
|
|
|
|
import Test.QuickCheck
|
2015-08-03 10:19:23 +03:00
|
|
|
|
|
|
|
|
|
import Text.Megaparsec.Char
|
2016-04-24 16:23:03 +03:00
|
|
|
|
import Text.Megaparsec.Error
|
2015-08-08 12:49:57 +03:00
|
|
|
|
|
|
|
|
|
import Util
|
2015-08-03 10:19:23 +03:00
|
|
|
|
|
2015-09-30 20:30:50 +03:00
|
|
|
|
#if !MIN_VERSION_base(4,8,0)
|
|
|
|
|
import Control.Applicative ((<$>))
|
|
|
|
|
#endif
|
|
|
|
|
|
2015-08-03 10:19:23 +03:00
|
|
|
|
tests :: Test
|
|
|
|
|
tests = testGroup "Character parsers"
|
2016-02-08 10:30:57 +03:00
|
|
|
|
[ testProperty "newline" prop_newline
|
|
|
|
|
, testProperty "crlf" prop_crlf
|
|
|
|
|
, testProperty "eol" prop_eol
|
|
|
|
|
, testProperty "tab" prop_tab
|
|
|
|
|
, testProperty "space" prop_space
|
|
|
|
|
, testProperty "controlChar" prop_controlChar
|
|
|
|
|
, testProperty "spaceChar" prop_spaceChar
|
|
|
|
|
, testProperty "upperChar" prop_upperChar
|
|
|
|
|
, testProperty "lowerChar" prop_lowerChar
|
|
|
|
|
, testProperty "letterChar" prop_letterChar
|
|
|
|
|
, testProperty "alphaNumChar" prop_alphaNumChar
|
|
|
|
|
, testProperty "printChar" prop_printChar
|
|
|
|
|
, testProperty "digitChar" prop_digitChar
|
|
|
|
|
, testProperty "hexDigitChar" prop_hexDigitChar
|
|
|
|
|
, testProperty "octDigitChar" prop_octDigitChar
|
|
|
|
|
, testProperty "markChar" prop_markChar
|
|
|
|
|
, testProperty "numberChar" prop_numberChar
|
|
|
|
|
, testProperty "punctuationChar" prop_punctuationChar
|
|
|
|
|
, testProperty "symbolChar" prop_symbolChar
|
|
|
|
|
, testProperty "separatorChar" prop_separatorChar
|
|
|
|
|
, testProperty "asciiChar" prop_asciiChar
|
|
|
|
|
, testProperty "latin1Char" prop_latin1Char
|
|
|
|
|
, testProperty "charCategory" prop_charCategory
|
|
|
|
|
, testProperty "char" prop_char
|
|
|
|
|
, testProperty "char'" prop_char'
|
|
|
|
|
, testProperty "anyChar" prop_anyChar
|
|
|
|
|
, testProperty "oneOf" prop_oneOf
|
|
|
|
|
, testProperty "oneOf'" prop_oneOf'
|
|
|
|
|
, testProperty "noneOf" prop_noneOf
|
|
|
|
|
, testProperty "noneOf'" prop_noneOf'
|
|
|
|
|
, testProperty "string" prop_string
|
|
|
|
|
, testProperty "string'" prop_string'_0
|
|
|
|
|
, testProperty "string' (case)" prop_string'_1 ]
|
2015-08-12 20:00:03 +03:00
|
|
|
|
|
|
|
|
|
instance Arbitrary GeneralCategory where
|
2016-04-24 16:23:03 +03:00
|
|
|
|
arbitrary = elements [minBound..maxBound]
|
2015-08-08 12:49:57 +03:00
|
|
|
|
|
2015-08-12 20:00:03 +03:00
|
|
|
|
prop_newline :: String -> Property
|
2016-04-24 16:23:03 +03:00
|
|
|
|
prop_newline = checkChar newline (== '\n') (tkn '\n')
|
2015-08-08 12:49:57 +03:00
|
|
|
|
|
2015-08-12 20:00:03 +03:00
|
|
|
|
prop_crlf :: String -> Property
|
2016-04-24 16:23:03 +03:00
|
|
|
|
prop_crlf = checkString crlf "\r\n" (==)
|
2015-08-08 12:49:57 +03:00
|
|
|
|
|
2015-08-12 20:00:03 +03:00
|
|
|
|
prop_eol :: String -> Property
|
|
|
|
|
prop_eol s = checkParser eol r s
|
2015-08-17 18:58:59 +03:00
|
|
|
|
where h = head s
|
|
|
|
|
r | s == "\n" = Right "\n"
|
|
|
|
|
| s == "\r\n" = Right "\r\n"
|
2016-04-24 16:23:03 +03:00
|
|
|
|
| null s = posErr 0 s [ueof, elabel "end of line"]
|
|
|
|
|
| h == '\n' = posErr 1 s [utok (s !! 1), eeof]
|
|
|
|
|
| h /= '\r' = posErr 0 s [utok h, elabel "end of line"]
|
|
|
|
|
| "\r\n" `isPrefixOf` s = posErr 2 s [utok (s !! 2), eeof]
|
|
|
|
|
| otherwise = posErr 0 s [ utoks (take 2 s)
|
|
|
|
|
, utok '\r'
|
|
|
|
|
, elabel "end of line" ]
|
2015-08-12 20:00:03 +03:00
|
|
|
|
|
|
|
|
|
prop_tab :: String -> Property
|
2016-04-24 16:23:03 +03:00
|
|
|
|
prop_tab = checkChar tab (== '\t') (tkn '\t')
|
2015-08-12 20:00:03 +03:00
|
|
|
|
|
|
|
|
|
prop_space :: String -> Property
|
|
|
|
|
prop_space s = checkParser space r s
|
2015-08-12 20:51:06 +03:00
|
|
|
|
where r = case findIndex (not . isSpace) s of
|
|
|
|
|
Just x ->
|
|
|
|
|
let ch = s !! x
|
|
|
|
|
in posErr x s
|
2016-04-24 16:23:03 +03:00
|
|
|
|
[ utok ch
|
|
|
|
|
, utok ch
|
|
|
|
|
, elabel "white space"
|
|
|
|
|
, eeof ]
|
2015-08-12 20:51:06 +03:00
|
|
|
|
Nothing -> Right ()
|
2015-08-08 21:42:47 +03:00
|
|
|
|
|
2015-08-12 20:00:03 +03:00
|
|
|
|
prop_controlChar :: String -> Property
|
2016-04-24 16:23:03 +03:00
|
|
|
|
prop_controlChar = checkChar controlChar isControl (lbl "control character")
|
2015-08-08 21:42:47 +03:00
|
|
|
|
|
2015-08-12 20:00:03 +03:00
|
|
|
|
prop_spaceChar :: String -> Property
|
2016-04-24 16:23:03 +03:00
|
|
|
|
prop_spaceChar = checkChar spaceChar isSpace (lbl "white space")
|
2015-08-08 21:42:47 +03:00
|
|
|
|
|
2015-08-12 20:00:03 +03:00
|
|
|
|
prop_upperChar :: String -> Property
|
2016-04-24 16:23:03 +03:00
|
|
|
|
prop_upperChar = checkChar upperChar isUpper (lbl "uppercase letter")
|
2015-08-08 21:42:47 +03:00
|
|
|
|
|
2015-08-12 20:00:03 +03:00
|
|
|
|
prop_lowerChar :: String -> Property
|
2016-04-24 16:23:03 +03:00
|
|
|
|
prop_lowerChar = checkChar lowerChar isLower (lbl "lowercase letter")
|
2015-08-08 21:42:47 +03:00
|
|
|
|
|
2015-08-12 20:00:03 +03:00
|
|
|
|
prop_letterChar :: String -> Property
|
2016-04-24 16:23:03 +03:00
|
|
|
|
prop_letterChar = checkChar letterChar isAlpha (lbl "letter")
|
2015-08-12 20:00:03 +03:00
|
|
|
|
|
|
|
|
|
prop_alphaNumChar :: String -> Property
|
2016-04-24 16:23:03 +03:00
|
|
|
|
prop_alphaNumChar = checkChar alphaNumChar isAlphaNum
|
|
|
|
|
(lbl "alphanumeric character")
|
2015-08-12 20:00:03 +03:00
|
|
|
|
|
|
|
|
|
prop_printChar :: String -> Property
|
2016-04-24 16:23:03 +03:00
|
|
|
|
prop_printChar = checkChar printChar isPrint (lbl "printable character")
|
2015-08-08 21:42:47 +03:00
|
|
|
|
|
2015-08-12 20:00:03 +03:00
|
|
|
|
prop_digitChar :: String -> Property
|
2016-04-24 16:23:03 +03:00
|
|
|
|
prop_digitChar = checkChar digitChar isDigit (lbl "digit")
|
2015-08-08 21:42:47 +03:00
|
|
|
|
|
2015-08-12 20:00:03 +03:00
|
|
|
|
prop_octDigitChar :: String -> Property
|
2016-04-24 16:23:03 +03:00
|
|
|
|
prop_octDigitChar = checkChar octDigitChar isOctDigit (lbl "octal digit")
|
2015-08-08 21:42:47 +03:00
|
|
|
|
|
2015-08-12 20:00:03 +03:00
|
|
|
|
prop_hexDigitChar :: String -> Property
|
2016-04-24 16:23:03 +03:00
|
|
|
|
prop_hexDigitChar = checkChar hexDigitChar isHexDigit (lbl "hexadecimal digit")
|
2015-08-08 21:42:47 +03:00
|
|
|
|
|
2015-08-12 20:00:03 +03:00
|
|
|
|
prop_markChar :: String -> Property
|
2016-04-24 16:23:03 +03:00
|
|
|
|
prop_markChar = checkChar markChar isMark (lbl "mark character")
|
2015-08-08 21:42:47 +03:00
|
|
|
|
|
2015-08-12 20:00:03 +03:00
|
|
|
|
prop_numberChar :: String -> Property
|
2016-04-24 16:23:03 +03:00
|
|
|
|
prop_numberChar = checkChar numberChar isNumber (lbl "numeric character")
|
2015-08-08 21:42:47 +03:00
|
|
|
|
|
2015-08-12 20:00:03 +03:00
|
|
|
|
prop_punctuationChar :: String -> Property
|
2016-04-24 16:23:03 +03:00
|
|
|
|
prop_punctuationChar = checkChar punctuationChar isPunctuation (lbl "punctuation")
|
2015-08-08 21:42:47 +03:00
|
|
|
|
|
2015-08-12 20:00:03 +03:00
|
|
|
|
prop_symbolChar :: String -> Property
|
2016-04-24 16:23:03 +03:00
|
|
|
|
prop_symbolChar = checkChar symbolChar isSymbol (lbl "symbol")
|
2015-08-12 20:00:03 +03:00
|
|
|
|
|
|
|
|
|
prop_separatorChar :: String -> Property
|
2016-04-24 16:23:03 +03:00
|
|
|
|
prop_separatorChar = checkChar separatorChar isSeparator (lbl "separator")
|
2015-08-12 20:00:03 +03:00
|
|
|
|
|
|
|
|
|
prop_asciiChar :: String -> Property
|
2016-04-24 16:23:03 +03:00
|
|
|
|
prop_asciiChar = checkChar asciiChar isAscii (lbl "ASCII character")
|
2015-08-12 20:00:03 +03:00
|
|
|
|
|
|
|
|
|
prop_latin1Char :: String -> Property
|
2016-04-24 16:23:03 +03:00
|
|
|
|
prop_latin1Char = checkChar latin1Char isLatin1 (lbl "Latin-1 character")
|
2015-08-12 20:00:03 +03:00
|
|
|
|
|
|
|
|
|
prop_charCategory :: GeneralCategory -> String -> Property
|
2016-04-24 16:23:03 +03:00
|
|
|
|
prop_charCategory cat = checkChar (charCategory cat) p (lbl $ categoryName cat)
|
2015-08-12 20:00:03 +03:00
|
|
|
|
where p c = generalCategory c == cat
|
2015-08-08 21:42:47 +03:00
|
|
|
|
|
|
|
|
|
prop_char :: Char -> String -> Property
|
2016-04-24 16:23:03 +03:00
|
|
|
|
prop_char c = checkChar (char c) (== c) (tkn c)
|
2015-08-08 21:42:47 +03:00
|
|
|
|
|
2015-09-06 12:11:18 +03:00
|
|
|
|
prop_char' :: Char -> String -> Property
|
|
|
|
|
prop_char' c s = checkParser (char' c) r s
|
|
|
|
|
where h = head s
|
|
|
|
|
l | isLower c = [c, toUpper c]
|
|
|
|
|
| isUpper c = [c, toLower c]
|
|
|
|
|
| otherwise = [c]
|
2016-04-24 16:23:03 +03:00
|
|
|
|
r | null s = posErr 0 s $ ueof : (etok <$> l)
|
2015-09-06 12:11:18 +03:00
|
|
|
|
| length s == 1 && (h `elemi` l) = Right h
|
2016-04-24 16:23:03 +03:00
|
|
|
|
| h `notElemi` l = posErr 0 s $ utok h : (etok <$> l)
|
|
|
|
|
| otherwise = posErr 1 s [utok (s !! 1), eeof]
|
2015-09-06 12:11:18 +03:00
|
|
|
|
|
2015-08-08 21:42:47 +03:00
|
|
|
|
prop_anyChar :: String -> Property
|
2016-04-24 16:23:03 +03:00
|
|
|
|
prop_anyChar = checkChar anyChar (const True) (lbl "character")
|
2015-08-08 21:42:47 +03:00
|
|
|
|
|
2015-08-12 20:00:03 +03:00
|
|
|
|
prop_oneOf :: String -> String -> Property
|
|
|
|
|
prop_oneOf a = checkChar (oneOf a) (`elem` a) Nothing
|
|
|
|
|
|
2015-09-06 12:11:18 +03:00
|
|
|
|
prop_oneOf' :: String -> String -> Property
|
|
|
|
|
prop_oneOf' a = checkChar (oneOf' a) (`elemi` a) Nothing
|
|
|
|
|
|
2015-08-12 20:00:03 +03:00
|
|
|
|
prop_noneOf :: String -> String -> Property
|
|
|
|
|
prop_noneOf a = checkChar (noneOf a) (`notElem` a) Nothing
|
|
|
|
|
|
2015-09-06 12:11:18 +03:00
|
|
|
|
prop_noneOf' :: String -> String -> Property
|
|
|
|
|
prop_noneOf' a = checkChar (noneOf' a) (`notElemi` a) Nothing
|
|
|
|
|
|
2015-08-08 21:42:47 +03:00
|
|
|
|
prop_string :: String -> String -> Property
|
2016-04-24 16:23:03 +03:00
|
|
|
|
prop_string a = checkString (string a) a (==)
|
2015-09-04 15:12:59 +03:00
|
|
|
|
|
2015-10-04 21:10:59 +03:00
|
|
|
|
prop_string'_0 :: String -> String -> Property
|
2016-04-24 16:23:03 +03:00
|
|
|
|
prop_string'_0 a = checkString (string' a) a casei
|
2015-10-04 21:10:59 +03:00
|
|
|
|
|
|
|
|
|
-- | Randomly change the case in the given string.
|
|
|
|
|
|
|
|
|
|
fuzzyCase :: String -> Gen String
|
2015-10-28 14:51:35 +03:00
|
|
|
|
fuzzyCase s = zipWith f s <$> vector (length s)
|
2015-10-04 21:10:59 +03:00
|
|
|
|
where f k True = if isLower k then toUpper k else toLower k
|
|
|
|
|
f k False = k
|
|
|
|
|
|
|
|
|
|
prop_string'_1 :: String -> Property
|
|
|
|
|
prop_string'_1 a = forAll (fuzzyCase a) $ \s ->
|
2016-04-24 16:23:03 +03:00
|
|
|
|
checkString (string' a) a casei s
|
2015-09-06 12:11:18 +03:00
|
|
|
|
|
|
|
|
|
-- | Case-insensitive equality test for characters.
|
|
|
|
|
|
|
|
|
|
casei :: Char -> Char -> Bool
|
|
|
|
|
casei x y = toLower x == toLower y
|
|
|
|
|
|
|
|
|
|
-- | Case-insensitive 'elem'.
|
|
|
|
|
|
|
|
|
|
elemi :: Char -> String -> Bool
|
|
|
|
|
elemi c = any (casei c)
|
|
|
|
|
|
|
|
|
|
-- | Case-insensitive 'notElem'.
|
|
|
|
|
|
|
|
|
|
notElemi :: Char -> String -> Bool
|
|
|
|
|
notElemi c = not . elemi c
|
2016-04-24 16:23:03 +03:00
|
|
|
|
|
2016-04-25 20:37:06 +03:00
|
|
|
|
tkn :: Char -> Maybe (ErrorItem Char)
|
|
|
|
|
tkn = Just . Tokens . (:|[])
|
2016-04-24 16:23:03 +03:00
|
|
|
|
|
2016-04-25 20:37:06 +03:00
|
|
|
|
lbl :: String -> Maybe (ErrorItem Char)
|
2016-04-24 16:23:03 +03:00
|
|
|
|
lbl = Just . Label . NE.fromList
|