packages feed

phino-0.0.157 / test / MarginSpec.hs

{-# LANGUAGE OverloadedStrings #-}-- SPDX-FileCopyrightText: Copyright (c) 2025 Objectionary.com-- SPDX-License-Identifier: MITmodule MarginSpec whereimport ASTimport Bytes (numToBts, strToBts)import CSTimport Control.Monad (forM_)import Data.Text qualified as Timport Lining (LineFormat (..), toSingleLine)import Margin (withLines, withMargin)import Render (render)import Test.Hspec (Spec, describe, it, shouldBe, shouldNotBe)bigLabel :: AttributebigLabel = AtLabel "aVeryLongAttributeNameThatWontFitOnOneLine"bigFormation :: ExpressionbigFormation = ExFormation [BiTau bigLabel ExRoot, BiTau AtRho ExRoot]nestedFormation :: ExpressionnestedFormation =  ExFormation    [ BiTau (AtLabel "x") ExRoot    , BiTau (AtLabel "y") (ExFormation [BiTau bigLabel ExRoot, BiTau AtRho ExRoot])    ]longCalleeShortArg :: ExpressionlongCalleeShortArg = ExApplication bigFormation (ArTau (AtLabel "y") (ExDispatch ExXi AtRho))shortCalleeLongArg :: ExpressionshortCalleeLongArg = ExApplication (ExDispatch ExRoot (AtLabel "x")) (ArTau (AtLabel "y") bigFormation)wideStringFormation :: ExpressionwideStringFormation =  ExFormation    [ BiTau (AtLabel "b") (DataString (strToBts "你好世界你好世界"))    , BiTau (AtLabel "c") (ExFormation [])    ]narrowStringFormation :: ExpressionnarrowStringFormation =  ExFormation    [ BiTau (AtLabel "b") (DataString (strToBts "abcdabcd"))    , BiTau (AtLabel "c") (ExFormation [])    ]wide :: Expressionwide =  ExFormation    [ BiTau        (AtLabel "r")        (ExApplication (ExDispatch callee (AtLabel "h大家大家大家大家")) (ArTau (AtLabel "cc") ExRoot))    ]narrow :: Expressionnarrow =  ExFormation    [ BiTau        (AtLabel "r")        (ExApplication (ExDispatch callee (AtLabel "haaaaaaaaaaaaaaaa")) (ArTau (AtLabel "cc") ExRoot))    ]callee :: Expressioncallee =  ExFormation    [ BiTau (AtLabel "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa") (ExFormation [])    , BiTau (AtLabel "bb") (ExFormation [])    ]blocks :: Expressionblocks =  ExFormation    [ BiTau (AtLabel "b") (DataString (strToBts "\11035\11035\11035\11035\11035\11035\11035\11035"))    , BiTau (AtLabel "c") (ExFormation [])    ]marks :: Expressionmarks =  ExFormation    [ BiTau (AtLabel "b") (DataString (strToBts "e\769e\769e\769e\769e\769e\769e\769e\769"))    , BiTau (AtLabel "c") (ExFormation [])    ]precomposed :: Expressionprecomposed =  ExFormation    [ BiTau (AtLabel "b") (DataString (strToBts "\233\233\233\233\233\233\233\233"))    , BiTau (AtLabel "c") (ExFormation [])    ]spec :: Specspec = do  describe "withMargin on EX_FORMATION" $ do    it "leaves an empty formation untouched at any margin" $      withMargin 0 (expressionToCST (ExFormation [])) `shouldBe` expressionToCST (ExFormation [])    forM_      [        ( "keeps a formation on one line when it fits the margin"        , 100        , nestedFormation        , "⟦ x ↦ Φ, y ↦ ⟦ aVeryLongAttributeNameThatWontFitOnOneLine ↦ Φ, ρ ↦ Φ ⟧ ⟧"        )      ,        ( "wraps a formation across lines when it does not fit the margin"        , 1        , nestedFormation        , "⟦\n  x ↦ Φ,\n  y ↦ ⟦\n    aVeryLongAttributeNameThatWontFitOnOneLine ↦ Φ,\n    ρ ↦ Φ\n  ⟧\n⟧"        )      ]      (\(desc, margin, expression, expected) -> it desc (render (withMargin margin (expressionToCST expression)) `shouldBe` expected))  describe "withMargin leaves data primitives untouched" $ do    it "a number literal is never wrapped" $      let cst = expressionToCST (DataNumber (numToBts 42))       in withMargin 0 cst `shouldBe` cst    it "a string literal is never wrapped" $      let cst = expressionToCST (DataString (strToBts "hello"))       in withMargin 0 cst `shouldBe` cst  describe "withMargin on EX_APPLICATION" $    forM_      [        ( "keeps the whole application on one line when it all fits"        , 100        , longCalleeShortArg        , "⟦ aVeryLongAttributeNameThatWontFitOnOneLine ↦ Φ, ρ ↦ Φ ⟧( y ↦ ρ )"        )      ,        ( "wraps only the callee formation when the callee alone still fits alongside the argument"        , 10        , longCalleeShortArg        , "⟦\n  aVeryLongAttributeNameThatWontFitOnOneLine ↦ Φ,\n  ρ ↦ Φ\n⟧( y ↦ ρ )"        )      ,        ( "wraps both the callee and the argument when neither fits alongside the other"        , 1        , longCalleeShortArg        , "⟦\n  aVeryLongAttributeNameThatWontFitOnOneLine ↦ Φ,\n  ρ ↦ Φ\n⟧(\n  y ↦ ρ\n)"        )      ,        ( "keeps a short callee on one line and wraps only the argument"        , 65        , shortCalleeLongArg        , "Φ.x(\n  y ↦ ⟦ aVeryLongAttributeNameThatWontFitOnOneLine ↦ Φ, ρ ↦ Φ ⟧\n)"        )      ,        ( "wraps the argument formation itself when it does not fit even on its own line"        , 1        , shortCalleeLongArg        , "Φ.x(\n  y ↦ ⟦\n    aVeryLongAttributeNameThatWontFitOnOneLine ↦ Φ,\n    ρ ↦ Φ\n  ⟧\n)"        )      ]      (\(desc, margin, expression, expected) -> it desc (render (withMargin margin (expressionToCST expression)) `shouldBe` expected))  describe "withMargin on positional (AA_EXPRS) application arguments" $    forM_      [ (100, "Φ.x( ⟦ aVeryLongAttributeNameThatWontFitOnOneLine ↦ Φ, ρ ↦ Φ ⟧ )")      ,        ( 1        , "Φ.x(\n  ⟦\n    aVeryLongAttributeNameThatWontFitOnOneLine ↦ Φ,\n    ρ ↦ Φ\n  ⟧\n)"        )      ]      ( \(margin, expected) ->          it ("margin " ++ show margin) $            let ex = ExApplication (ExDispatch ExRoot (AtLabel "x")) (ArAlpha (Alpha 0) (ExFormation [BiTau bigLabel ExRoot, BiTau AtRho ExRoot]))             in render (withMargin margin (expressionToCST ex)) `shouldBe` expected      )  describe "withMargin on mixed tau/alpha (AA_TAUS with PA_ALPHA) application arguments" $    forM_      [ (100, "Φ.x( a ↦ Φ, α5 ↦ ⟦ aVeryLongAttributeNameThatWontFitOnOneLine ↦ Φ, ρ ↦ Φ ⟧ )")      ,        ( 1        , "Φ.x(\n  a ↦ Φ,\n  α5 ↦ ⟦\n    aVeryLongAttributeNameThatWontFitOnOneLine ↦ Φ,\n    ρ ↦ Φ\n  ⟧\n)"        )      ]      ( \(margin, expected) ->          it ("margin " ++ show margin) $            let ex = ExApplication (ExApplication (ExDispatch ExRoot (AtLabel "x")) (ArTau (AtLabel "a") ExRoot)) (ArAlpha (Alpha 5) bigFormation)             in render (withMargin margin (expressionToCST ex)) `shouldBe` expected      )  describe "withMargin on a manually built AA_TAU / APP_BINDING argument" $ do    let manual =          EX_APPLICATION            (EX_DISPATCH (EX_GLOBAL Φ) NO_SPACE (AT_LABEL "x"))            NO_SPACE            EOL            (TAB 1)            (AA_TAU (APP_BINDING (PA_TAU (AT_LABEL "aVeryLongAttributeNameThatWontFitOnOneLine") ARROW (EX_GLOBAL Φ))))            EOL            (TAB 0)            1    it "keeps it on one line when it fits" $      render (withMargin 100 manual) `shouldBe` "Φ.x( aVeryLongAttributeNameThatWontFitOnOneLine ↦ Φ )"    it "wraps it when it does not fit" $      render (withMargin 1 manual) `shouldBe` "Φ.x(\n  aVeryLongAttributeNameThatWontFitOnOneLine ↦ Φ\n)"  describe "withMargin on EX_DISPATCH" $    forM_      [        ( "recurses into the dispatched-upon expression"        , 1        , ExDispatch bigFormation (AtLabel "z")        , "⟦\n  aVeryLongAttributeNameThatWontFitOnOneLine ↦ Φ,\n  ρ ↦ Φ\n⟧.z"        )      ]      (\(desc, margin, expression, expected) -> it desc (render (withMargin margin (expressionToCST expression)) `shouldBe` expected))  describe "withMargin on a one-binding formation" $    forM_      [        ( "keeps the sugar on one line when it fits the margin"        , 100        , ExFormation [BiTau (AtLabel "x") shortCalleeLongArg]        , "Φ.x( y ↦ ⟦ aVeryLongAttributeNameThatWontFitOnOneLine ↦ Φ, ρ ↦ Φ ⟧ ):x"        )      ,        ( "wraps the asset of the sugar and keeps the attribute after it"        , 1        , ExFormation [BiTau (AtLabel "x") shortCalleeLongArg]        , "Φ.x(\n  y ↦ ⟦\n    aVeryLongAttributeNameThatWontFitOnOneLine ↦ Φ,\n    ρ ↦ Φ\n  ⟧\n):x"        )      ]      (\(desc, margin, expression, expected) -> it desc (render (withMargin margin (expressionToCST expression)) `shouldBe` expected))  describe "withMargin on EX_PHI_MEET" $ do    it "produces the same result whether the margin is 1 or 1000" $      let meet = expressionToCST (ExPhiMeet Nothing 3 bigFormation)       in withMargin 1 meet `shouldBe` withMargin 1000 meet    it "collapses its body via toSingleLine regardless of the margin" $      let meet = expressionToCST (ExPhiMeet Nothing 3 bigFormation)       in withMargin 1 meet `shouldBe` EX_PHI_MEET Nothing 3 (toSingleLine (expressionToCST bigFormation))  describe "withMargin on EX_PHI_AGAIN" $    it "threads the margin into its body" $ do      let again = expressionToCST (ExPhiAgain Nothing 3 bigFormation)      withMargin 1 again `shouldNotBe` withMargin 1000 again  describe "withMargin measures display width rather than code points" $ do    it "wraps a formation whose string has wide characters that push it past the margin" $      render (withMargin 30 (expressionToCST wideStringFormation))        `shouldBe` "⟦\n  b ↦ \"你好世界你好世界\",\n  c ↦ ⟦⟧\n⟧"    it "keeps a same length narrow string on one line within the same margin" $      render (withMargin 30 (expressionToCST narrowStringFormation))        `shouldBe` "⟦ b ↦ \"abcdabcd\", c ↦ ⟦⟧ ⟧"  describe "withLines" $    forM_ terms $ \(desc, expression) ->      forM_ margins $ \margin -> do        it ("flattens " ++ desc ++ " at margin " ++ show margin ++ " as the margin pass would") $          withLines SINGLELINE margin (expressionToCST expression)            `shouldBe` toSingleLine (withMargin margin (expressionToCST expression))        it ("wraps " ++ desc ++ " at margin " ++ show margin ++ " as the margin pass does") $          withLines MULTILINE margin (expressionToCST expression)            `shouldBe` withMargin margin (expressionToCST expression)  describe "withMargin measures the last line of a call by display width" $    it "breaks the argument of a call whose callee ends in wide characters" $      rows wide `shouldBe` rows narrow  describe "withMargin measures every wide character as two columns" $    it "wraps a string of wide characters the way it wraps the same count of CJK ones" $      rows blocks `shouldBe` rows wideStringFormation  describe "withMargin gives a zero-width character no column" $ do    it "keeps a string of combining marks on one line, as its precomposed twin stays" $      rows marks `shouldBe` rows precomposed    it "keeps that string on one line within the margin" $      rows marks `shouldBe` 1  where    terms :: [(String, Expression)]    terms =      [ ("an empty formation", ExFormation [])      , ("a wide formation", bigFormation)      , ("a nested formation", nestedFormation)      , ("a long callee with a short argument", longCalleeShortArg)      , ("a short callee with a long argument", shortCalleeLongArg)      , ("a phi meet", ExPhiMeet Nothing 3 bigFormation)      , ("a phi again", ExPhiAgain Nothing 3 bigFormation)      , ("a number literal", DataNumber (numToBts 42))      , ("a string literal", DataString (strToBts "hello"))      ]    margins :: [Int]    margins = [0, 1, 40, 80, 1000]    rows :: Expression -> Int    rows expression = length (T.lines (render (withMargin 30 (expressionToCST expression))))