1
1
mirror of https://github.com/github/semantic.git synced 2024-11-25 11:04:00 +03:00
semantic/test/Numeric/Spec.hs

44 lines
1.2 KiB
Haskell
Raw Normal View History

2019-09-26 00:09:23 +03:00
{-# LANGUAGE OverloadedStrings #-}
module Numeric.Spec
2019-09-26 00:09:23 +03:00
( testTree
) 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
2019-09-26 00:09:23 +03:00
type Fixture = (Text, Integer)
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)
]
2019-09-26 00:09:23 +03:00
ruby :: [Fixture]
ruby = [ ("0xa_bcd_ef0_123_456_789", 0xabcdef0123456789)
, ("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")
]