2019-09-26 00:09:23 +03:00
|
|
|
{-# LANGUAGE OverloadedStrings #-}
|
2018-06-25 18:55:32 +03:00
|
|
|
module Numeric.Spec
|
2019-09-26 00:09:23 +03:00
|
|
|
( testTree
|
2018-06-25 18:55:32 +03:00
|
|
|
) where
|
|
|
|
|
2019-09-26 00:09:23 +03:00
|
|
|
import Data.Either
|
|
|
|
import Data.Text (Text)
|
|
|
|
import Data.Foldable (traverse_)
|
|
|
|
import Numeric.Exts
|
|
|
|
import qualified Test.Tasty as Tasty
|
|
|
|
import Test.Tasty.HUnit
|
2018-06-25 18:55:32 +03:00
|
|
|
|
2019-09-26 00:09:23 +03:00
|
|
|
type Fixture = (Text, Integer)
|
2018-06-25 18:55:32 +03:00
|
|
|
|
2019-09-26 00:09:23 +03:00
|
|
|
python :: [Fixture]
|
|
|
|
python = [ ("-1", (negate 1))
|
|
|
|
, ("0xDEAD", 0xDEAD)
|
|
|
|
, ("0XDEAD", 0xDEAD)
|
|
|
|
, ("1j", 1)
|
|
|
|
, ("0o123", 83)
|
|
|
|
, ("0O123", 83)
|
|
|
|
, ("0b001", 1)
|
|
|
|
, ("0B001", 1)
|
|
|
|
, ("1_1", 11) -- underscore syntax is Python 3 only
|
|
|
|
, ("0B1_1", 3)
|
|
|
|
, ("0O1_1", 9)
|
|
|
|
, ("0L", 0)
|
|
|
|
]
|
2018-06-25 18:55:32 +03:00
|
|
|
|
2019-09-26 00:09:23 +03:00
|
|
|
ruby :: [Fixture]
|
|
|
|
ruby = [ ("0xa_bcd_ef0_123_456_789", 0xabcdef0123456789)
|
2018-06-25 18:55:32 +03:00
|
|
|
, ("01234567", 342391)
|
|
|
|
]
|
|
|
|
|
2019-09-26 00:09:23 +03:00
|
|
|
testTree :: Tasty.TestTree
|
|
|
|
testTree =
|
|
|
|
let go = traverse_ (\(s, v) -> parseInteger s @?= Right v)
|
|
|
|
in Tasty.testGroup "Numeric.Exts"
|
|
|
|
[ testCase "handles Python integers" (go python)
|
|
|
|
, testCase "handles Ruby integers" (go ruby)
|
|
|
|
, testCase "doesn't accept floats" (isLeft (parseInteger "1.5") @? "Accepted floating-point")
|
|
|
|
, testCase "doesn't accept empty string" (isLeft (parseInteger "") @? "Accepted integer")
|
|
|
|
]
|