{-# LANGUAGE OverloadedStrings #-}-- SPDX-FileCopyrightText: Copyright (c) 2025 Objectionary.com-- SPDX-License-Identifier: MITmodule BuilderSpec whereimport ASTimport Builderimport Control.Monadimport Data.Either (isLeft)import Data.Map.Strict qualified as Mapimport Data.Text qualified as Timport Matcherimport Test.Hspec (Example (Arg), Expectation, Spec, SpecWith, anyException, describe, it, shouldBe, shouldSatisfy, shouldThrow)test :: (Show a, Eq a) => (a -> Subst -> Either String a) -> [(String, a, [(T.Text, MetaValue)], Either String a)] -> SpecWith (Arg Expectation)test function useCases = forM_ useCases $ \(desc, expr, mp, res) -> it desc $ function expr (Subst (Map.fromList mp)) `shouldBe` resspec :: Specspec = do describe "buildExpression" $ test buildExpression [ ( "Q.!t => (!t >> x) => Q.x" , ExDispatch ExRoot (AtMeta "t") , [("t", MvAttribute (AtLabel "x"))] , Right (ExDispatch ExRoot (AtLabel "x")) ) , ( "Q.c(!t -> !e) => (!t >> x, !e >> $.y.z) => Q.c(x -> $.y.z)" , ExApplication (ExDispatch ExRoot (AtLabel "c")) (ArTau (AtMeta "t") (ExMeta "e")) , [("t", MvAttribute (AtLabel "x")), ("e", MvExpression (ExDispatch (ExDispatch ExXi (AtLabel "y")) (AtLabel "z")))] , Right (ExApplication (ExDispatch ExRoot (AtLabel "c")) (ArTau (AtLabel "x") (ExDispatch (ExDispatch ExXi (AtLabel "y")) (AtLabel "z")))) ) , ( "[[!t -> $.x, !B]] => (!t >> y, !B >> [[b -> ?, L> Func]]) => [[y -> $.x, b -> ?, L> Func]]" , ExFormation [BiTau (AtMeta "t") (ExDispatch ExXi (AtLabel "x")), BiMeta "B"] , [("t", MvAttribute (AtLabel "y")), ("B", MvBindings [BiVoid (AtLabel "b"), BiLambda (Function "Func")])] , Right ( ExFormation [ BiTau (AtLabel "y") (ExDispatch ExXi (AtLabel "x")) , BiVoid (AtLabel "b") , BiLambda (Function "Func") ] ) ) , ( "Q.!t => () => X" , ExDispatch ExRoot (AtMeta "t") , [] , Left "meta 't' is either does not exist or refers to an inappropriate term" ) , ( "!e0(!t1 -> !e1, !t2 => !e2) => (!e0 >> [[]], !t1 >> x, !e1 >> Q, !t2 >> y, !e2 >> $) => [[]](x -> Q, y -> $)" , ExApplication (ExApplication (ExMeta "e0") (ArTau (AtMeta "t1") (ExMeta "e1"))) (ArTau (AtMeta "t2") (ExMeta "e2")) , [ ("e0", MvExpression (ExFormation [])) , ("t1", MvAttribute (AtLabel "x")) , ("e1", MvExpression ExRoot) , ("t2", MvAttribute (AtLabel "y")) , ("e2", MvExpression ExXi) ] , Right (ExApplication (ExApplication (ExFormation []) (ArTau (AtLabel "x") ExRoot)) (ArTau (AtLabel "y") ExXi)) ) , ( "⟦!t ↦ ∅, !B⟧.!t => (!t >> t, !B >> ⟦ x ↦ ξ.t ⟧ ) => ⟦ t ↦ ∅, x ↦ ξ.t ⟧.t" , ExDispatch (ExFormation [BiVoid (AtMeta "t"), BiMeta "B"]) (AtMeta "t") , [ ("t", MvAttribute (AtLabel "t")) , ("B", MvBindings [BiTau (AtLabel "x") (ExDispatch ExXi (AtLabel "t"))]) ] , Right ( ExDispatch ( ExFormation [ BiVoid (AtLabel "t") , BiTau (AtLabel "x") (ExDispatch ExXi (AtLabel "t")) ] ) (AtLabel "t") ) ) ] describe "buildExpressions" $ do it "!e => [(!e >> Q.x), (!e >> $.y)] => [Q.x, $.y]" $ do built <- buildExpressionsThrows (ExMeta "e") [ substSingle "e" (MvExpression (ExDispatch ExRoot (AtLabel "x"))) , substSingle "e" (MvExpression (ExDispatch ExXi (AtLabel "y"))) ] built `shouldBe` [ExDispatch ExRoot (AtLabel "x"), ExDispatch ExXi (AtLabel "y")] it "!e => [(!e1 >> Q.x)] => X" $ buildExpressionsThrows (ExMeta "e") [substSingle "e1" (MvExpression (ExDispatch ExRoot (AtLabel "x")))] `shouldThrow` anyException describe "build with duplicate attributes in bindings" $ do it "build binding with duplicates" $ buildBinding (BiMeta "B") (substSingle "B" (MvBindings [BiVoid AtRho, BiVoid AtRho])) `shouldSatisfy` isLeft it "build formation with duplicates" $ buildExpression (ExMeta "e") (substSingle "e" (MvExpression (ExFormation [BiVoid AtRho, BiVoid AtRho]))) `shouldSatisfy` isLeft