packages feed

fluent-1.0.0: test/CycleSpec.hs

module CycleSpec (spec) where

import Control.Exception (evaluate)
import System.Timeout (timeout)
import Prelude

spec :: Spec
spec = do
    it "is no circle where a message refers to an attribute of its own" $
        translate
            "foo"
            [fluent|
              foo = { foo.attr } and more
                  .attr = Attribute
            |]
            `shouldBe` Right "Attribute and more"
    it "is no circle where a message refers to another one twice" $
        translate
            "foo"
            [fluent|
              bar = Bar
              foo = { bar } { bar }
            |]
            `shouldBe` Right "Bar Bar"
    it "answers when two messages refer to each other" $
        answers
            [fluent|
              foo = { bar }
              bar = { foo }
            |]
    it "answers when two terms refer to each other" $
        answers
            [fluent|
              -a = { -b }
              -b = { -a }
              foo = { -a }
            |]
    it "answers when a message refers to itself" $
        answers [fluent|foo = { foo }|]
    it "answers around a longer cycle" $
        answers
            [fluent|
              a = { b }
              b = { c }
              c = { a }
              foo = { a }
            |]

-- | Whether looking up @foo@ answers at all, one way or the other.
answers :: Resource -> Expectation
answers resource = do
    answered <- timeout 100000 . evaluate . length . show $ asked
    answered `shouldSatisfy` (/= Nothing)
  where
    asked :: Either String Text
    asked = translate "foo" resource