on master help sign in
Test reading of integral types
f5347a7Bryan O'Sullivan
tests/Properties.hs+28−1
@@ -7,7 +7,8 @@ import Test.QuickCheck.Monadic
7import Text.Show.Functions ()
7import Text.Show.Functions ()
8
8
9import qualified Data.Bits as Bits (shiftL, shiftR)
9import qualified Data.Bits as Bits (shiftL, shiftR)
10+import Numeric (showHex)
10−import Data.Char (chr, isLower, isSpace, isUpper, ord)
11+import Data.Char (chr, isDigit, isHexDigit, isLower, isSpace, isUpper, ord)
11import Data.Monoid (Monoid(..))
12import Data.Monoid (Monoid(..))
12import Data.String (fromString)
13import Data.String (fromString)
13import Debug.Trace (trace)
14import Debug.Trace (trace)
@@ -21,6 +22,8 @@ import qualified Data.Text.Lazy.IO as TL
21import qualified Data.Text.Lazy.Internal as TL
22import qualified Data.Text.Lazy.Internal as TL
22import qualified Data.Text.Lazy.Builder as TB
23import qualified Data.Text.Lazy.Builder as TB
23import qualified Data.Text.Encoding as E
24import qualified Data.Text.Encoding as E
25+import Data.Text.Read as T
26+import Data.Text.Lazy.Read as TL
24import Data.Text.Encoding.Error
27import Data.Text.Encoding.Error
25import Control.Exception (SomeException, bracket, catch, evaluate, try)
28import Control.Exception (SomeException, bracket, catch, evaluate, try)
26import Data.Text.Foreign
29import Data.Text.Foreign
@@ -658,6 +661,23 @@ t_builderAssociative s1 s2 s3 =
658 b2 = TB.fromText (packS s2)
661 b2 = TB.fromText (packS s2)
659 b3 = TB.fromText (packS s3)
662 b3 = TB.fromText (packS s3)
660
663
664+-- Reading.
665+
666+t_decimal (n::Int) s =
667+ T.signed T.decimal (T.pack (show n) `T.append` t) == Right (n,t)
668+ where t = T.dropWhile isDigit s
669+tl_decimal (n::Int) s =
670+ TL.signed TL.decimal (TL.pack (show n) `TL.append` t) == Right (n,t)
671+ where t = TL.dropWhile isDigit s
672+t_hexadecimal (n::Positive Int) s ox =
673+ T.hexadecimal (T.concat [p, T.pack (showHex n ""), t]) == Right (n,t)
674+ where t = T.dropWhile isHexDigit s
675+ p = if ox then "0x" else ""
676+tl_hexadecimal (n::Positive Int) s ox =
677+ TL.hexadecimal (TL.concat [p, TL.pack (showHex n ""), t]) == Right (n,t)
678+ where t = TL.dropWhile isHexDigit s
679+ p = if ox then "0x" else ""
680+
661-- Input and output.
681-- Input and output.
662
682
663-- Work around lack of Show instance for TextEncoding.
683-- Work around lack of Show instance for TextEncoding.
@@ -1139,6 +1159,13 @@ tests = [
1139 testProperty "t_builderAssociative" t_builderAssociative
1159 testProperty "t_builderAssociative" t_builderAssociative
1140 ],
1160 ],
1141
1161
1162+ testGroup "read" [
1163+ testProperty "t_decimal" t_decimal,
1164+ testProperty "tl_decimal" tl_decimal,
1165+ testProperty "t_hexadecimal" t_hexadecimal,
1166+ testProperty "tl_hexadecimal" tl_hexadecimal
1167+ ],
1168+
1142 testGroup "input-output" [
1169 testGroup "input-output" [
1143 testProperty "t_write_read" t_write_read,
1170 testProperty "t_write_read" t_write_read,
1144 testProperty "tl_write_read" tl_write_read,
1171 testProperty "tl_write_read" tl_write_read,