packages feed

fluent-icu-1.0.0: test/NumberSpec.hs

module NumberSpec (spec) where

import Prelude

spec :: Spec
spec = do
    it "groups the digits the way the locale groups them" $
        translate "foo" ("arg", NumberValue 1234567 numberOptions) resource `shouldBe` Right "1,234,567"
    it "leaves the grouping out when it is turned off" $
        translate "foo" ("arg", NumberValue 1234567 numberOptions{useGrouping = False}) resource
            `shouldBe` Right "1234567"
    it "pads the integer part to the given width" $
        translate "foo" ("arg", NumberValue 5 numberOptions{minimumIntegerDigits = 3}) resource
            `shouldBe` Right "005"
    it "limits the fraction digits to the maximum given" $
        translate "foo" ("arg", NumberValue 1.5 numberOptions{maximumFractionDigits = Just 0}) resource
            `shouldBe` Right "2"
    it "limits the significant digits it is given" $
        translate
            "foo"
            ("arg", NumberValue 1234 numberOptions{maximumSignificantDigits = Just 2})
            resource
            `shouldBe` Right "1,200"
    it "is shown as a percentage" $
        translate "foo" ("arg", NumberValue 0.25 numberOptions{style = Percent}) resource
            `shouldBe` Right "25%"
    it "is an amount of a currency" $
        translate "foo" ("arg", NumberValue 12.5 numberOptions{style = Currency "EUR"}) resource
            `shouldBe` Right "€12.50"
    it "spells the currency's name out, not its symbol" $
        translate
            "foo"
            ("arg", NumberValue 12.5 numberOptions{style = Currency "EUR", currencyDisplay = Name})
            resource
            `shouldBe` Right "12.50 euros"
    it "is an amount of a unit" $
        translate "foo" ("arg", NumberValue 5 numberOptions{style = Unit "kilometre-per-hour"}) resource
            `shouldBe` Right "5 km/h"
    it "is an amount of a unit of one thing per another" $
        translate "foo" ("arg", NumberValue 5 numberOptions{style = Unit "litre-per-kilometre"}) resource
            `shouldBe` Right "5 l/km"
    it "shows the unit's symbol when narrow, its name when long" $ do
        translate
            "foo"
            ("arg", NumberValue 5 numberOptions{style = Unit "litre", unitDisplay = Narrow})
            resource
            `shouldBe` Right "5l"
        translate
            "foo"
            ("arg", NumberValue 5 numberOptions{style = Unit "litre", unitDisplay = Long})
            resource
            `shouldBe` Right "5 litres"
    it "accepts a unit identifier written in British spelling" $ do
        translate "foo" ("arg", NumberValue 5 numberOptions{style = Unit "litre"}) resource
            `shouldBe` Right "5 l"
        translate "foo" ("arg", NumberValue 5 numberOptions{style = Unit "millilitre"}) resource
            `shouldBe` Right "5 ml"
        translate "foo" ("arg", NumberValue 5 numberOptions{style = Unit "square-kilometre"}) resource
            `shouldBe` Right "5 km²"
    it "keeps the fraction digits a unit is given" $
        translate
            "foo"
            ("arg", NumberValue 5 numberOptions{style = Unit "megabyte", minimumFractionDigits = 1})
            resource
            `shouldBe` Right "5.0 MB"
    it "spells the unit the way the locale spells it, whatever it is called" $ do
        translate
            "foo"
            ("arg", NumberValue 5 numberOptions{style = Unit "kilometre", unitDisplay = Long})
            resource
            `shouldBe` Right "5 kilometres"
        translate
            "foo"
            ("arg", NumberValue 5 numberOptions{style = Unit "litre", unitDisplay = Long})
            deCH
            resource
            `shouldBe` Right "5 Liter"
    it "rejects a unit it does not know" $
        translate "foo" ("arg", NumberValue 5 numberOptions{style = Unit "banana"}) resource
            `shouldBe` Left "NUMBER: U_NUMBER_SKELETON_SYNTAX_ERROR"
    it "uses the first known locale in the chain" $ do
        translate "foo" ("arg", NumberValue 1234.5 numberOptions) [deCH, enGB] resource
            `shouldBe` Right "1'234.5"
        translate "foo" ("arg", NumberValue 1234.5 numberOptions) [enGB, deCH] resource
            `shouldBe` Right "1,234.5"
        translate "foo" ("arg", NumberValue 1234.5 numberOptions) [unknown, deCH] resource
            `shouldBe` Right "1'234.5"
    it "falls back to an ancestor locale when the exact one is unknown" $ do
        translate "foo" ("arg", NumberValue 1234.5 numberOptions) deSince1996 resource
            `shouldBe` Right "1'234.5"
        translate "foo" ("arg", NumberValue 1234.5 numberOptions) deInAnUnknownScript resource
            `shouldBe` Right "1.234,5"

resource :: Resource
resource = [fluent|foo = { $arg }|]

enGB, deCH, deSince1996, deInAnUnknownScript, unknown :: LocaleName
enGB = "en-GB"
deCH = "de-CH"
deSince1996 = "de-CH-1996"
deInAnUnknownScript = "de-Qaai-XX"
unknown = "xx"