packages feed

fluent-1.0.0: test/ReferenceSpec.hs

module ReferenceSpec (spec) where

import Prelude

spec :: Spec
spec = do
    describe "message" do
        it "stands for its value" $
            translate
                "bar"
                [fluent|
                  foo = Foo
                  bar = { foo } Bar
                |]
                `shouldBe` Right "Foo Bar"
        it "stands for one of its attributes" $
            translate
                "bar"
                [fluent|
                  foo = Foo
                      .attr = Attribute
                  bar = { foo.attr }
                |]
                `shouldBe` Right "Attribute"
        it "passes its arguments on" $
            translate
                "bar"
                ("arg", "Bar" :: Text)
                [fluent|
                  foo = { $arg }
                  bar = { foo }
                |]
                `shouldBe` Right "Bar"
        it "reaches through a chain of references" $
            translate
                "baz"
                [fluent|
                  foo = Foo
                  bar = { foo }
                  baz = { bar }
                |]
                `shouldBe` Right "Foo"
        it "is missing" $
            translate "bar" [fluent|bar = { foo }|]
                `shouldBe` Left "Message not found: foo"
        it "has no value of its own" $
            translate
                "bar"
                [fluent|
                  foo =
                      .attr = Attribute
                  bar = { foo }
                |]
                `shouldBe` Left "Message has no value: \"foo\""
        it "has no such attribute" $
            translate
                "bar"
                [fluent|
                  foo = Foo
                  bar = { foo.attr }
                |]
                `shouldBe` Left "Attribute not found: (\"foo\",\"attr\")"

    describe "term" do
        it "stands for its value" $
            translate
                "foo"
                [fluent|
                  -term = Term
                  foo = { -term } Foo
                |]
                `shouldBe` Right "Term Foo"
        it "chooses a variant by one of its attributes" $
            translate
                "foo"
                [fluent|
                  -term = Term
                      .attr = key
                  foo = { -term.attr ->
                     *[key] Attribute
                  }
                |]
                `shouldBe` Right "Attribute"
        it "takes arguments of its own" $
            translate "genitive" brand `shouldBe` Right "Firefox's feature"
        it "matches a variant key by the argument it is given" $
            translate
                "foo"
                [fluent|
                  -brand = { $case ->
                      [genitive] Firefox's
                     *[nominative] Firefox
                    }
                  foo = { -brand(case: "genitive") } feature
                |]
                `shouldBe` Right "Firefox's feature"
        it "falls back to its default without arguments" $
            translate "plain" brand `shouldBe` Right "Firefox feature"
        it "cannot be reached as a message" $
            translate
                "foo"
                [fluent|
                  -term = Term
                  foo = { term }
                |]
                `shouldBe` Left "Message not found: term"
        it "does not read the arguments of the message that refers to it" $
            translate
                "foo"
                ("arg", "Bar" :: Text)
                [fluent|
                  -term = { $arg }
                  foo = { -term }
                |]
                `shouldBe` Left "Variable not found: \"arg\""
        it "is missing" $
            translate "foo" [fluent|foo = { -term }|]
                `shouldBe` Left "Term not found: \"term\""

brand :: Resource
brand =
    [fluent|
      -brand = { $case ->
          [genitive] Firefox's
         *[nominative] Firefox
        }
      genitive = { -brand(case: "genitive") } feature
      plain = { -brand } feature
    |]