fluent-1.0.0: test/BuiltinSpec.hs
module BuiltinSpec (spec) where
import Data.Foldable (for_)
import Data.Text qualified as Text
import Data.Time (UTCTime (..), fromGregorian, secondsToDiffTime)
import Prelude
spec :: Spec
spec = do
describe "NUMBER" do
it "formats a number by the given options" $
withNumber [fluent|foo = { NUMBER($arg, minimumFractionDigits: 2) }|]
`shouldBe` Right "3.00"
it "formats a percentage by the given options" $
withNumber [fluent|foo = { NUMBER($arg, style: "percent") }|]
`shouldBe` Right "3%"
it "formats a currency by the given options" $
withNumber [fluent|foo = { NUMBER($arg, style: "currency", currency: "EUR") }|]
`shouldBe` Right "3EUR"
it "formats a unit by the given options" $
withNumber [fluent|foo = { NUMBER($arg, style: "unit", unit: "u") }|]
`shouldBe` Right "3u"
it "selects by the plural category of the number" $
withNumber
[fluent|
foo = { NUMBER($arg, minimumFractionDigits: 1) ->
[one] one
*[other] other
}
|]
`shouldBe` Right "other"
describe "DATETIME" do
let widths = Text.show <$> [minBound @Width .. maxBound]
let digits = Text.show <$> [minBound @Digits .. maxBound]
for_ ((,) <$> (widths <> digits) <*> digits) \(month, year) -> do
let resource =
either error id . parseResource . mconcat . mconcat $
[ ["foo = { DATETIME($arg, "]
, ["month: \"", month, "\", "]
, ["year: \"", year, "\""]
, [") }"]
]
it (Text.unpack $ "with month " <> month <> " and year " <> year) $
withTime resource `shouldBe` Right "2026-08-10"
withNumber :: Resource -> Either String Text
withNumber = translate "foo" ("arg", 3 :: Int)
withTime :: Resource -> Either String Text
withTime = translate "foo" ("arg", UTCTime date time)
where
date = fromGregorian 2026 8 10
time = secondsToDiffTime $ 12 * 3600 + 34 * 60 + 56