packages feed

fluent-1.0.0: test/SelectSpec.hs

module SelectSpec (spec) where

import Prelude

spec :: Spec
spec = do
    it "picks the variant whose key matches" $
        translate "foo" ("arg", "a" :: Text) letters `shouldBe` Right "A"
    it "picks the default variant when no key matches" $
        translate "foo" ("arg", "c" :: Text) letters `shouldBe` Right "B"
    it "picks the default variant when the selector is missing" $
        translate "foo" letters `shouldBe` Right "B"
    it "picks the variant whose number key matches" $
        translate "foo" ("arg", 1 :: Int) numbers `shouldBe` Right "One"
    it "picks a number key over a category" $
        translate "foo" ("arg", 0 :: Int) numbers `shouldBe` Right "Zero"
    it "picks an exact key over a category that is written before it" $
        translate
            "foo"
            ("arg", 0 :: Int)
            [fluent|
              foo = { $arg ->
                 *[other] Other
                  [0] Zero
                }
            |]
            `shouldBe` Right "Zero"
    it "matches a number key against a fraction" $
        translate "foo" ("arg", 1.5 :: Scientific) numbers `shouldBe` Right "Other"
    it "holds a select of its own in the variant it picks" $
        translate
            "foo"
            ("outer", "x" :: Text)
            ("inner", "y" :: Text)
            [fluent|
              foo = { $outer ->
                  [x] { $inner ->
                      [y] XY
                     *[z] XZ
                    }
                 *[o] O
                }
            |]
            `shouldBe` Right "XY"
    it "counts a number that is below zero by how far it is from it" $
        translate
            "foo"
            ("n", -1 :: Int)
            [fluent|
              foo = { $n ->
                  [one] one
                 *[other] other
                }
            |]
            `shouldBe` Right "one"
    it "cannot select on a value of several parts" $
        translate
            "foo"
            [fluent|
              -term = Term
                  .attr = a{ "b" }
              foo = { -term.attr ->
                  [ab] AB
                 *[c] C
                }
            |]
            `shouldBe` Left "Selector is not a number or identifier"
    it "selects on a string literal" $
        translate
            "foo"
            [fluent|
              foo = { "a" ->
                  [a] A
                 *[b] B
                }
            |]
            `shouldBe` Right "A"
    it "selects on a number literal" $
        translate
            "foo"
            [fluent|
              foo = { 1 ->
                  [1] One
                 *[other] Other
                }
            |]
            `shouldBe` Right "One"
    it "selects on an attribute of a term" $
        translate
            "foo"
            [fluent|
              -term = Term
                  .gender = feminine
              foo = { -term.gender ->
                  [feminine] She
                 *[masculine] He
                }
            |]
            `shouldBe` Right "She"
    it "holds a placeable in the variant it picks" $
        translate
            "foo"
            ("arg", "a" :: Text)
            [fluent|
              foo = { $arg ->
                  [a] A { $arg }
                 *[b] B
                }
            |]
            `shouldBe` Right "A a"
    it "holds several lines in the variant it picks" $
        translate
            "foo"
            [fluent|
              foo = { "a" ->
                  [a]
                      One
                      Two
                 *[b] B
                }
            |]
            `shouldBe` Right "One\nTwo"
    it "stands between texts" $
        translate
            "foo"
            ("arg", "a" :: Text)
            [fluent|
              foo = Foo { $arg ->
                  [a] A
                 *[b] B
                } Baz
            |]
            `shouldBe` Right "Foo A Baz"

letters :: Resource
letters =
    [fluent|
      foo = { $arg ->
          [a] A
         *[b] B
        }
    |]

numbers :: Resource
numbers =
    [fluent|
      foo = { $arg ->
          [0] Zero
          [1] One
         *[other] Other
        }
    |]