packages feed

phino-0.0.173 / test / XMIRSpec.hs

{-# LANGUAGE DeriveAnyClass #-}{-# LANGUAGE DeriveGeneric #-}{-# LANGUAGE DuplicateRecordFields #-}{-# LANGUAGE OverloadedStrings #-}-- SPDX-FileCopyrightText: Copyright (c) 2025 Objectionary.com-- SPDX-License-Identifier: MITmodule XMIRSpec whereimport AST (Attribute (AtLabel, AtRho), Binding (BiMeta, BiTau, BiVoid), Expression (ExFormation))import Control.Exception (SomeException, displayException, try)import Control.Monad (forM_, unless, void)import Data.Aesonimport Data.Char (isDigit)import Data.List (intercalate)import Data.Map qualified as Mimport Data.Text qualified as Timport Data.Time.Clock.POSIX (posixSecondsToUTCTime)import Data.Yaml qualified as Yamlimport Files (allPathsIn)import GHC.Generics (Generic)import Parser (Form (MONOFORM), parseExpressionThrows, parseExpressionThrows')import Printer (printExpression)import System.FilePath (makeRelative)import Test.Hspec (Spec, anyException, describe, expectationFailure, it, runIO, shouldBe, shouldContain, shouldNotContain, shouldReturn, shouldThrow)import Text.XML (Document (..), Element (..), Node (NodeElement), Prologue (..))import Text.XML.Cursor qualified as Cimport XMIR (XmirContext (XmirContext), defaultXmirContext, escapeXML, expressionToXMIR, parseXMIRThrows, printXMIR, renameAtoms, toName, xmirAtoms, xmirTime, xmirToPhi)data ParsePack = ParsePack  { failure :: Maybe Bool  , xmir :: String  , phi :: String  }  deriving (Generic, Show, FromJSON)data PrintPack = PrintPack  { phi :: String  , xpaths :: [String]  }  deriving (Generic, Show, FromJSON)parsePack :: FilePath -> IO ParsePackparsePack = Yaml.decodeFileThrowprintPack :: FilePath -> IO PrintPackprintPack = Yaml.decodeFileThrowdata Predicate  = AttrEquals String String  | ChildText String String  | ChildExists String [Predicate]  | PositionIs Int  | AndPred Predicate Predicate  deriving (Show)data Step = Step String [Predicate]  deriving (Show)xpath :: String -> [Step]xpath ('/' : rest) = steps restxpath _ = []steps :: String -> [Step]steps "" = []steps str =  let (step, rest) = span (\c -> c /= '/' && c /= '[') str      (preds, remaining) = parsePredicate rest   in Step step preds : steps (dropWhile (== '/') remaining)parsePredicate :: String -> ([Predicate], String)parsePredicate ('[' : rest) =  let (inner, after) = splitBracket rest      pred' = parsePredicateInner inner      (more, final) = parsePredicate after   in (pred' : more, final)parsePredicate str = ([], str)splitBracket :: String -> (String, String)splitBracket = go (0 :: Int) ""  where    go :: Int -> String -> String -> (String, String)    go _ acc "" = (reverse acc, "")    go 0 acc (']' : rest) = (reverse acc, rest)    go n acc ('[' : rest) = go (n + 1) ('[' : acc) rest    go n acc (']' : rest) = go (n - 1) (']' : acc) rest    go n acc (c : rest) = go n (c : acc) restparsePredicateInner :: String -> PredicateparsePredicateInner str  | " and " `isInfixOf'` str =      let parts = splitAnd str       in foldr1 AndPred (map parsePredicateInner parts)  | all isDigit str = PositionIs (read str)  | '@' : rest <- str = parseAttrPred rest  | otherwise = parseChildPred str  where    isInfixOf' needle haystack = needle `elem` tails haystack    tails [] = [[]]    tails s@(_ : xs) = s : tails xssplitAnd :: String -> [String]splitAnd = go (0 :: Int) ""  where    go :: Int -> String -> String -> [String]    go _ acc "" = [reverse acc | not (null acc)]    go n acc ('[' : rest) = go (n + 1) ('[' : acc) rest    go n acc (']' : rest) = go (n - 1) (']' : acc) rest    go 0 acc (' ' : 'a' : 'n' : 'd' : ' ' : rest) = reverse acc : go 0 "" rest    go n acc (c : rest) = go n (c : acc) restparseAttrPred :: String -> PredicateparseAttrPred str =  let (name, rest) = break (== '=') str      val = extractQuoted (drop 1 rest)   in AttrEquals name valparseChildPred :: String -> PredicateparseChildPred str  | '[' `elem` str =      let (name, rest) = break (== '[') str          (preds, _) = parsePredicate rest       in ChildExists name preds  | '=' `elem` str =      let (name, rest) = break (== '=') str          val = extractQuoted (drop 1 rest)       in ChildText name val  | otherwise = ChildExists str []extractQuoted :: String -> StringextractQuoted (q : rest)  | q == '"' || q == '\'' = takeWhile (/= q) restextractQuoted s = sevaluate :: Document -> [Step] -> [C.Cursor]evaluate doc [] = [C.fromDocument doc]evaluate doc (Step name preds : rest) =  let root = C.fromDocument doc      rootName = elementName (documentRoot doc)   in if rootName == toName name        then foldl applyStep (applyPredicates [root] preds) rest        else []applyStep :: [C.Cursor] -> Step -> [C.Cursor]applyStep curs (Step name preds) = do  cur <- curs  child <- cur C.$/ C.element (toName name)  applyPredicates [child] predsapplyPredicates :: [C.Cursor] -> [Predicate] -> [C.Cursor]applyPredicates = foldl applyPredicateapplyPredicate :: [C.Cursor] -> Predicate -> [C.Cursor]applyPredicate curs pred' = case pred' of  AttrEquals name val -> filter (hasAttrValue name val) curs  ChildText name val -> filter (hasChildText name val) curs  ChildExists name nested -> filter (hasChild name nested) curs  PositionIs n -> take 1 (drop (n - 1) curs)  AndPred p1 p2 -> applyPredicate (applyPredicate curs p1) p2hasAttrValue :: String -> String -> C.Cursor -> BoolhasAttrValue name val cur = C.attribute (toName name) cur == [T.pack val]hasChildText :: String -> String -> C.Cursor -> BoolhasChildText name val cur =  let children = cur C.$/ C.element (toName name)   in any (hasTextContent val) childrenhasTextContent :: String -> C.Cursor -> BoolhasTextContent val cur =  let txt = concatMap T.unpack (cur C.$/ C.content)   in txt == valhasChild :: String -> [Predicate] -> C.Cursor -> BoolhasChild name nested cur =  let children = cur C.$/ C.element (toName name)   in not (null (applyPredicates children nested))matches :: Document -> String -> Boolmatches doc path = not (null (evaluate doc (xpath path)))spec :: Specspec = do  describe "XMIR parsing packs" $ do    let resources = "test-resources/xmir-parsing-packs"    packs <- runIO (allPathsIn resources)    forM_      packs      ( \pth -> it (makeRelative resources pth) $ do          pack <- parsePack pth          let ParsePack{phi = phi'} = pack              xmir' = do                doc <- parseXMIRThrows (xmir pack)                xmirToPhi doc          case failure pack of            Just True -> xmir' `shouldThrow` anyException            _ -> do              xmir'' <- xmir'              phi'' <- parseExpressionThrows' MONOFORM phi'              xmir'' `shouldBe` phi''      )  describe "round-trips non-program tops as XMIR (#1076)" $    forM_      [ "[[ x -> ? ]]"      , "[[ ^ -> 5 ]]"      , "[[ x -> 4, L> L_number_plus, ^ -> [[ y -> 5 ]] ]]"      , "[[ a -> T ]]"      , "[[ a -> $ ]]"      ]      ( \phi' -> it phi' $ do          expr <- parseExpressionThrows phi'          doc <- expressionToXMIR expr defaultXmirContext          doc' <- parseXMIRThrows (printXMIR doc)          back <- xmirToPhi doc'          back `shouldBe` expr      )  describe "derived λ function name" $ do    it "spells itself in the alphabet the parser accepts" $ do      doc <- parseXMIRThrows "<object><o name=\"foo\"><o name=\"l🌵ab12\"><o base=\"∅\" name=\"v0\"/><o name=\"λ\"/></o></o></object>"      expr <- xmirToPhi doc      parseExpressionThrows (printExpression expr) `shouldReturn` expr    it "stays unique when two paths spell alike, and keeps the type of each atom" $ do      doc <- parseXMIRThrows "<object><o name=\"top\"><o name=\"as-int\"><o atom=\"Φ.number\" name=\"λ\"/></o><o name=\"as_int\"><o atom=\"Φ.string\" name=\"λ\"/></o></o></object>"      xmirAtoms doc `shouldReturn` M.fromList [("L_top_as_int", "Φ.number"), ("L_top_as_int_2", "Φ.string")]  describe "types of the voids of an atom in XMIR" $ do    let alloc :: String        alloc = "<object><o name=\"alloc\"><o base=\"∅\" name=\"size\" type=\"Φ.number\"/><o args=\"Φ.chunk\" base=\"∅\" name=\"scope\"/><o base=\"∅\" name=\"free\"/><o atom=\"Φ.bytes\" name=\"λ\"/></o></object>"        printed :: IO String        printed = do          doc <- parseXMIRThrows alloc          expr <- xmirToPhi doc          atoms <- xmirAtoms doc          printXMIR <$> expressionToXMIR expr (XmirContext True True False (const "") atoms)    it "keeps @type of a void through a round trip" $      printed >>= (`shouldContain` "<o base=\"∅\" name=\"size\" type=\"Φ.number\"/>")    it "keeps @args of a void through a round trip" $      printed >>= (`shouldContain` "<o args=\"Φ.chunk\" base=\"∅\" name=\"scope\"/>")    it "adds nothing to a void without a type" $      printed >>= (`shouldContain` "<o base=\"∅\" name=\"free\"/>")    it "keeps the type when the λ function is renamed" $ do      doc <- parseXMIRThrows alloc      atoms <- xmirAtoms doc      expr <- parseExpressionThrows "⟦ alloc ↦ ⟦ size ↦ ∅, scope ↦ ∅, free ↦ ∅, λ ⤍ Fn1 ⟧ ⟧"      result <- expressionToXMIR expr (renameAtoms [("L_alloc", "Fn1")] (XmirContext True True False (const "") atoms))      printXMIR result `shouldContain` "<o base=\"∅\" name=\"size\" type=\"Φ.number\"/>"  describe "atom result types in XMIR" $ do    let atom :: String        atom = "<object><o name=\"number\"><o name=\"plus\"><o base=\"∅\" name=\"b\"/><o atom=\"Φ.number\" name=\"λ\"/></o></o></object>"    it "survives a round trip through a λ marker" $ do      doc <- parseXMIRThrows atom      expr <- xmirToPhi doc      atoms <- xmirAtoms doc      result <- expressionToXMIR expr (XmirContext True True False (const "") atoms)      let printed = printXMIR result      printed `shouldContain` "atom=\"Φ.number\""      printed `shouldNotContain` "<o name=\"λ\">"    it "names the λ function after its locator, not after the type" $ do      expr <- parseXMIRThrows atom >>= xmirToPhi      printExpression expr `shouldContain` "L_number_plus"      printExpression expr `shouldNotContain` "Φ.number"      parseExpressionThrows (printExpression expr) `shouldReturn` expr    it "keeps the type keyed by the λ function name" $      (parseXMIRThrows atom >>= xmirAtoms) `shouldReturn` M.fromList [("L_number_plus", "Φ.number")]    it "keeps the type of a λ marker that carries its name" $      ( parseXMIRThrows "<object><o name=\"x\"><o atom=\"?\" name=\"λ\">Foo</o></o></object>"          >>= xmirAtoms      )        `shouldReturn` M.fromList [("Foo", "?")]  describe "--hide-rho in XMIR" $ do    it "drops every bound ρ from the printed document" $ do      expr <- parseExpressionThrows "[[ x -> 4, ^ -> [[ y -> 5 ]] ]]"      doc <- expressionToXMIR expr (XmirContext True False True (const "") M.empty)      let printed = printXMIR doc      printed `shouldContain` "name=\"x\""      printed `shouldNotContain` "name=\"ρ\""    it "drops an application argument bound to ρ instead of refusing it" $ do      expr <- parseExpressionThrows "[[ m -> Q.a(^ -> [[]]) ]]"      doc <- expressionToXMIR expr (XmirContext True False True (const "") M.empty)      printXMIR doc `shouldContain` "base=\"Φ.a\""  describe "prohibit to convert to XMIR" $    forM_      [ "[[ ]]"      , "T"      , "Q.x.y.z"      , "\"Hello\""      , "Q"      , "$"      , "[[ x -> [[ !t1 -> 5 ]] ]]"      , "[[ org -> [[ z -> ?, L> Package ]] ]]"      ]      ( \phi' -> it phi' $ do          expr <- parseExpressionThrows phi'          expressionToXMIR expr defaultXmirContext `shouldThrow` anyException      )  describe "XMIR printing packs" $ do    let resources = "test-resources/xmir-printing-packs"    packs <- runIO (allPathsIn resources)    forM_      packs      ( \pth ->          it (makeRelative resources pth) $ do            pack <- printPack pth            let PrintPack{phi = phi', xpaths = xpaths'} = pack            expr <- parseExpressionThrows' MONOFORM phi'            xmir' <- expressionToXMIR expr defaultXmirContext            let failed = filter (not . matches xmir') xpaths'            unless              (null failed)              (expectationFailure ("Failed xpaths:\n - " ++ intercalate "\n - " failed ++ "\nXMIR is:\n" ++ printXMIR xmir'))      )  describe "XMIR round-trip" $    forM_      [ ("keeps λ function name and bound ρ", "[[ k -> [[ x -> ?, L> Lorg_eolang_number_plus, ^ -> [[ y -> ? ]] ]] ]]")      , ("keeps Δ data bound to a named attribute", "[[ k -> [[ a -> [[ D> 01-02 ]], ^ -> [[ D> 03-04 ]] ]] ]]")      , ("keeps Δ data in a dispatched formation", "[[ k -> [[ D> 01-02 ]].plus ]]")      , ("keeps Δ data behind a sibling binding", "[[ top -> [[ a -> [[]], D> FF- ]] ]]")      , ("keeps Δ data between sibling bindings", "[[ top -> [[ a -> [[]], D> 01-02, b -> [[]] ]] ]]")      , ("keeps a bare 'Q' bound to a named attribute", "[[ x -> Q ]]")      , ("keeps a formation bound to φ", "[[ k -> [[ @ -> [[ L> S8 ]] ]] ]]")      , ("keeps byte data and sibling bindings in an application argument", "[[ x -> Q.y(a -> [[ D> 01-02, b -> Q.z ]]) ]]")      , ("keeps data after an application argument's sibling binding", "[[ top -> [[ @ -> ⊥(a -> [[ x -> [[]], D> 00-FF ]]) ]] ]]")      , ("keeps data between an application argument's sibling bindings", "[[ top -> [[ @ -> ⊥(a -> [[ x -> [[]], D> 00-FF, y -> [[]] ]]) ]] ]]")      ]      ( \(desc, source) -> it desc $ do          expr <- parseExpressionThrows source          xmir' <- expressionToXMIR expr defaultXmirContext          back <- xmirToPhi xmir'          back `shouldBe` expr      )  describe "XMIR exception messages" $    forM_      [        ( "explains an unsupported top-level expression"        , do            expr <- parseExpressionThrows "[[ x -> !e1 ]]"            try (void (expressionToXMIR expr defaultXmirContext)) :: IO (Either SomeException ())        , ["XMIR does not support such top-level expression"]        )      ,        ( "explains an unsupported nested expression"        , do            expr <- parseExpressionThrows "[[ x -> [[ y -> !e1 ]] ]]"            try (void (expressionToXMIR expr defaultXmirContext)) :: IO (Either SomeException ())        , ["XMIR does not support such expression"]        )      ,        ( "refuses an application argument bound to ρ"        , do            expr <- parseExpressionThrows "[[ top -> Q.a(^ -> [[]]) ]]"            try (void (expressionToXMIR expr defaultXmirContext)) :: IO (Either SomeException ())        , ["XMIR does not support such expression", "ρ ↦"]        )      ,        ( "refuses an application of a formation, which XMIR has no shape for"        , do            expr <- parseExpressionThrows "[[ x -> [[ a -> ? ]](a -> Q.y) ]]"            try (void (expressionToXMIR expr defaultXmirContext)) :: IO (Either SomeException ())        , ["XMIR does not support such expression", "a ↦ Φ.y"]        )      ,        ( "refuses a meta attribute of an application argument"        , do            expr <- parseExpressionThrows "[[ a -> Q.f(!t -> $) ]]"            try (void (expressionToXMIR expr defaultXmirContext)) :: IO (Either SomeException ())        , ["XMIR does not support such expression", "𝜏 ↦ ξ"]        )      ,        ( "refuses a dispatch of a meta attribute"        , do            expr <- parseExpressionThrows "[[ a -> Q.f.!t1 ]]"            try (void (expressionToXMIR expr defaultXmirContext)) :: IO (Either SomeException ())        , ["XMIR does not support such expression", "Φ.f.𝜏1"]        )      ,        ( "names the attribute that is empty"        , try (void (parseXMIRThrows "<object><o name=\"a\" base=\"\"/></object>" >>= xmirToPhi)) :: IO (Either SomeException ())        , ["The attribute 'base' is not expected to be empty"]        )      ,        ( "refuses an application argument given by its position"        , try (void (parseXMIRThrows "<object><o name=\"x\" base=\"Φ.f\"><o base=\"Φ.y\"/></o></object>" >>= xmirToPhi)) :: IO (Either SomeException ())        , ["Application argument must have @as attribute, since a positional argument is not supported"]        )      ,        ( "refuses an application argument given by an index"        , try (void (parseXMIRThrows "<object><o name=\"x\" base=\"Φ.f\"><o as=\"α0\" base=\"Φ.y\"/></o></object>" >>= xmirToPhi)) :: IO (Either SomeException ())        , ["The positional @as='α0' is not supported"]        )      ,        ( "explains an unsupported binding"        , try (void (expressionToXMIR (ExFormation [BiTau (AtLabel "x") (ExFormation [BiMeta "n", BiVoid AtRho]), BiVoid AtRho]) defaultXmirContext)) ::            IO (Either SomeException ())        , ["XMIR does not support such bindings"]        )      ,        ( "explains a parse failure"        , try (void (parseXMIRThrows "not-xml-at-all <<<")) :: IO (Either SomeException ())        , ["Couldn't parse given XMIR"]        )      ,        ( "explains an invalid XMIR structure, including the offending element"        , do            doc <- parseXMIRThrows "<object><o name=\"app\"><o/></o></object>"            try (void (xmirToPhi doc)) :: IO (Either SomeException ())        , ["Couldn't traverse though given XMIR", "XMIR:"]        )      ]      ( \(desc, action, messages) -> it desc $ do          result <- action          case result of            Left err -> mapM_ (displayException err `shouldContain`) messages            Right () -> expectationFailure "expected an exception"      )  describe "xmirTime" $ do    it "keeps every nanosecond of the time" $      xmirTime (posixSecondsToUTCTime 1790000000.123456789) `shouldBe` "2026-09-21T14:13:20.123456789Z"    it "never carries the fraction past nine digits" $      xmirTime (posixSecondsToUTCTime 1790000000.99999995) `shouldBe` "2026-09-21T14:13:20.999999950Z"  describe "escapeXML" $    it "escapes an apostrophe alongside the other reserved characters" $      escapeXML "it's a & <b> \"quote\"" `shouldBe` "it&apos;s a &amp; &lt;b&gt; &quot;quote&quot;"  describe "XMIR document structure" $ do    it "produces an empty prologue and epilogue" $ do      expr <- parseExpressionThrows "[[ x -> 5 ]]"      Document prologue _ epilogue <- expressionToXMIR expr defaultXmirContext      prologue `shouldBe` Prologue [] Nothing []      epilogue `shouldBe` []    it "renders package metas with empty attributes and a matching part" $ do      expr <- parseExpressionThrows "[[ org -> [[ eolang -> [[ foo -> [[ x -> 5 ]], L> Package ]], L> Package ]] ]]"      xmir' <- expressionToXMIR expr defaultXmirContext      let root = C.fromDocument xmir'      case root C.$/ C.element (toName "metas") of        [metasCur] -> case C.node metasCur of          NodeElement metasEl -> elementAttributes metasEl `shouldBe` M.empty          _ -> expectationFailure "expected <metas> to be an element"        _ -> expectationFailure "expected exactly one <metas> element"      case root C.$/ C.element (toName "metas") C.&/ C.element (toName "meta") of        [metaCur] -> case C.node metaCur of          NodeElement metaEl -> elementAttributes metaEl `shouldBe` M.empty          _ -> expectationFailure "expected <meta> to be an element"        _ -> expectationFailure "expected exactly one <meta> element"      let parts = root C.$/ C.element (toName "metas") C.&/ C.element (toName "meta") C.&/ C.element (toName "part") C.&/ C.content      parts `shouldBe` ["org.eolang"]  describe "XMIR comments" $ do    let commentedContext :: XmirContext        commentedContext = XmirContext True False False (const "") M.empty    it "includes a decimal comment for a number when comments aren't omitted" $ do      expr <- parseExpressionThrows "[[ x -> 5 ]]"      xmir' <- expressionToXMIR expr commentedContext      printXMIR xmir' `shouldContain` "<!-- 5.0 -->"    it "includes a quoted comment for a string when comments aren't omitted" $ do      expr <- parseExpressionThrows "[[ x -> \"foo\" ]]"      xmir' <- expressionToXMIR expr commentedContext      printXMIR xmir' `shouldContain` "<!-- \"foo\" -->"    it "keeps text on both sides of a comment inside an object" $ do      split <- parseXMIRThrows "<object><o name=\"x\">foo<!-- note -->bar</o></object>"      joined <- parseXMIRThrows "<object><o name=\"x\">foobar</o></object>"      actual <- xmirToPhi split      expected <- xmirToPhi joined      actual `shouldBe` expected  describe "XMIR printing edge cases" $ do    it "wraps a chained dispatch on a formation literal with a @base attribute" $ do      expr <- parseExpressionThrows "[[ x -> [[ y -> 5 ]].plus.minus ]]"      xmir' <- expressionToXMIR expr defaultXmirContext      let root = C.fromDocument xmir'          xCur = root C.$/ C.element (toName "o")          outer = filter (\cur -> C.attribute (toName "base") cur == [".minus"]) xCur          inner =            concatMap              (filter (\cur -> C.attribute (toName "base") cur == [".plus"]) . (C.$/ C.element (toName "o")))              xCur      length outer `shouldBe` 1      length inner `shouldBe` 1    it "renders a void φ binding as a nested formation" $ do      expr <- parseExpressionThrows "[[ x -> [[ @ -> ? ]] ]]"      xmir' <- expressionToXMIR expr defaultXmirContext      let root = C.fromDocument xmir'          nested = root C.$/ C.element (toName "o") C.&/ C.element (toName "o")          phiVoid =            filter              (\cur -> C.attribute (toName "name") cur == ["φ"] && C.attribute (toName "base") cur == ["∅"])              nested      length phiVoid `shouldBe` 1    it "renders a formation bound to φ without an empty @base (#1189)" $ do      expr <- parseExpressionThrows "[[ x -> [[ @ -> [[ L> S8 ]] ]] ]]"      xmir' <- expressionToXMIR expr defaultXmirContext      let phis =            filter              (\cur -> C.attribute (toName "name") cur == ["φ"] && null (C.attribute (toName "base") cur))              (C.fromDocument xmir' C.$/ C.element (toName "o") C.&/ C.element (toName "o"))      length phis `shouldBe` 1    it "renders a bare global reference as the top-level value" $ do      expr <- parseExpressionThrows "[[ x -> Q ]]"      xmir' <- expressionToXMIR expr defaultXmirContext      let root = C.fromDocument xmir'          xCur = filter (\cur -> C.attribute (toName "base") cur == ["Φ"]) (root C.$/ C.element (toName "o"))      length xCur `shouldBe` 1    it "omits @base when an application argument is a bare formation" $ do      expr <- parseExpressionThrows "[[ foo -> Q.bar(x -> 5, yu -> [[ z -> ? ]]) ]]"      xmir' <- expressionToXMIR expr defaultXmirContext      let root = C.fromDocument xmir'          args = root C.$/ C.element (toName "o") C.&/ C.element (toName "o")          namedArg = filter (\cur -> C.attribute (toName "as") cur == ["x"]) args          formationArg = filter (\cur -> C.attribute (toName "as") cur == ["yu"]) args      case namedArg of        [argCur] -> C.attribute (toName "base") argCur `shouldBe` ["Φ.number"]        _ -> expectationFailure "expected exactly one 'x' argument"      case formationArg of        [argCur] -> C.attribute (toName "base") argCur `shouldBe` []        _ -> expectationFailure "expected exactly one argument named yu"    it "escapes XML special characters in attribute values" $ do      expr <- parseExpressionThrows "[[ a&b -> \"<quoted>\" ]]"      xmir' <- expressionToXMIR expr defaultXmirContext      let out = printXMIR xmir'      out `shouldContain` "name=\"a&amp;b\""      xmirToPhi xmir' `shouldReturn` expr  describe "XMIR malformed input containing a processing instruction" $    it "embeds a processing instruction verbatim when rendering the offending element" $ do      doc <-        parseXMIRThrows          "<object><o name=\"x\" base=\"∅\"/><?a-pi some-data?><o name=\"y\" base=\"∅\"/></object>"      result <- try (xmirToPhi doc) :: IO (Either SomeException Expression)      case result of        Left exc -> displayException exc `shouldContain` "Couldn't traverse though given XMIR"        Right _ -> expectationFailure "expected an exception"  describe "XMIR name of a test" $ do    it "accepts a formation named with a plus, the way EO names a test" $      void (parseXMIRThrows "<object><o name=\"app\"><o name=\"+works\"/></o></object>" >>= xmirToPhi)    it "accepts a formation named with a minus, the way EO names a test that must fail" $      void (parseXMIRThrows "<object><o name=\"app\"><o name=\"-stops\"/></o></object>" >>= xmirToPhi)    it "prints 𝜑 of a test name that reads back the same" $ do      expr <- parseXMIRThrows "<object><o name=\"app\"><o name=\"+works\" base=\"Φ.x\"/><o name=\"-stops\"/></o></object>" >>= xmirToPhi      parseExpressionThrows (printExpression expr) `shouldReturn` expr    it "still refuses the rest of the name when it is not a label" $      (parseXMIRThrows "<object><o name=\"app\"><o name=\"+Works\"/></o></object>" >>= xmirToPhi)        `shouldThrow` anyException