{-# LANGUAGE OverloadedStrings #-}-- SPDX-FileCopyrightText: Copyright (c) 2025 Objectionary.com-- SPDX-License-Identifier: MITmodule ConditionSpec whereimport AST (Attribute (AtDelta, AtLabel, AtLambda, AtMeta), Binding (BiMeta), Expression (ExDispatch, ExMeta, ExRoot))import Conditionimport Control.Exception (SomeException)import Control.Monad (forM_)import Data.Either (isLeft, isRight)import Data.List (isInfixOf)import Test.Hspec (Spec, describe, expectationFailure, it, shouldBe, shouldSatisfy, shouldThrow)import Yaml qualified as Yspec :: Specspec = do describe "just parses" $ forM_ [ "in (!t, !B)" , " not (in (!t1, !B)) " , "eq(1, 1)" , "gt(length(!B), 1)" , "or(eq(1,1),eq(length(!B),-2),eq(!e1,!e2),eq(!t1,x),eq(Q.org.eolang,[[ x -> 2 ]]))" , "nf([[ x -> !e ]].x)" , "absolute(!e1)" , "matches(\"hello(\\\"\\u0000)\", !e)" , "part-of ( [[ x -> 1 ]] , !B ) " , "not(formation(!n1))" ] (\expr -> it expr (parseCondition expr `shouldSatisfy` isRight)) describe "parses correctly" $ forM_ [ ("in(!t1, !B1)", Y.In [AtMeta "t1"] [BiMeta "B1"]) , ("not(in(!t1,!B1))", Y.Not (Y.In [AtMeta "t1"] [BiMeta "B1"])) , ("eq(1,-2)", Y.Eq (Y.CmpNum (Y.Literal 1)) (Y.CmpNum (Y.Literal (-2)))) , ("eq(2,length(!B1))", Y.Eq (Y.CmpNum (Y.Literal 2)) (Y.CmpNum (Y.Length (BiMeta "B1")))) , ("gt(length(!B1),1)", Y.Gt (Y.CmpNum (Y.Length (BiMeta "B1"))) (Y.CmpNum (Y.Literal 1))) , ("or(absolute(!e1), nf(Q.x))", Y.Or [Y.Absolute (ExMeta "e1"), Y.NF (ExDispatch ExRoot (AtLabel "x"))]) , ("and(matches(\"hi\", !e1),part-of(!e1, !B1))", Y.And [Y.Matches "hi" (ExMeta "e1"), Y.PartOf (ExMeta "e1") (BiMeta "B1")]) , ("not(formation(!n1))", Y.Not (Y.IsFormation (ExMeta "n1"))) , ("in(Δ, !B1)", Y.In [AtDelta] [BiMeta "B1"]) , ("in(λ, !B1)", Y.In [AtLambda] [BiMeta "B1"]) , ("in([Δ, λ], [!B1, !B2])", Y.In [AtDelta, AtLambda] [BiMeta "B1", BiMeta "B2"]) ] (\(expr, res) -> it expr (parseCondition expr `shouldBe` Right res)) describe "does not parse" $ forM_ [ "some()" , "in(!t, !t)" , "or(or(), or())" , "eq(!t1, !e2)" , "eq(!i1, 1)" , "gt(length(!B), 𝑖1)" , "eq(3, domain(!B))" , "gt(domain(!B), 1)" ] (\expr -> it expr (parseCondition expr `shouldSatisfy` isLeft)) describe "refuses a condition that compares different kinds" $ do it "names the two kinds it cannot compare" $ case parseCondition "eq(!e1, 5)" of Left err -> "compares an expression with a number" `isInfixOf` err `shouldBe` True Right _ -> expectationFailure "expected a refusal" it "accepts a quoted argument as an expression" $ parseCondition "eq(!e1, \"5\")" `shouldSatisfy` isRight describe "parseConditionThrows" $ it "throws with a descriptive message on unparsable input" $ parseConditionThrows "some()" `shouldThrow` (\exc -> "Couldn't parse given condition" `isInfixOf` show (exc :: SomeException))