packages feed

fluent-1.1.0: test/ValueSpec.hs

module ValueSpec (spec) where

import Data.Int (Int16, Int64, Int8)
import Data.Word (Word16, Word64, Word8)
import Foreign.C.Types (CDouble, CInt)
import Language.Fluent.Value qualified as Value
import Numeric.Natural (Natural)
import Prelude

spec :: Spec
spec = do
    describe "integer" do
        it "is formatted as a number" $ do
            written (-5 :: Int8) `shouldBe` Right "-5"
            written (200 :: Word8) `shouldBe` Right "200"
            written (-300 :: Int16) `shouldBe` Right "-300"
            written (7 :: Word16) `shouldBe` Right "7"
            written (9 :: Natural) `shouldBe` Right "9"
        it "is formatted as a number if it has no instance" $
            written (Value.fromIntegral (-3 :: CInt)) `shouldBe` Right "-3"
        it "keeps every digit of the largest" $ do
            written (maxBound @Int64) `shouldBe` Right "9,223,372,036,854,775,807"
            written (maxBound @Word64) `shouldBe` Right "18,446,744,073,709,551,615"

    describe "floating point number" do
        it "is formatted with the digits that identify it" $ do
            written (0.1 :: Double) `shouldBe` Right "0.1"
            written (0.1 :: Float) `shouldBe` Right "0.1"
            written (Value.fromFloatDigits (1.5 :: CDouble)) `shouldBe` Right "1.5"
        it "is not a number if it is NaN" $ do
            written (0 / 0 :: Double) `shouldBe` Left "NaN"
            written (0 / 0 :: Float) `shouldBe` Left "NaN"
        it "is not a number if it is infinite" $ do
            written (1 / 0 :: Double) `shouldBe` Left "Infinity"
            written (-(1 / 0) :: Float) `shouldBe` Left "-Infinity"
        it "is not a number to a function that expects one" $
            translate "foo" ("arg", 0 / 0 :: Double) [fluent|foo = { NUMBER($arg) }|]
                `shouldBe` Left "NaN"
        it "is only an error where it is used" $
            translate "foo" ("arg", 0 / 0 :: Double) [fluent|foo = Hello|] `shouldBe` Right "Hello"

written :: (ToValue v) => v -> Either String Text
written v = translate "foo" ("arg", toValue v) [fluent|foo = { $arg }|]