From 84f9c76357b7b7a2d88316f31d7a39c1ed335bd0 Mon Sep 17 00:00:00 2001 From: Jaro Date: Tue, 18 Apr 2023 23:36:35 +0200 Subject: [PATCH] Prepare for test modules sharing PrettyShow. --- balkon.cabal | 1 + test/Data/Text/ParagraphLayout/PrettyShow.hs | 143 +++++++++++++++++++ test/Data/Text/ParagraphLayoutSpec.hs | 133 +---------------- 3 files changed, 145 insertions(+), 132 deletions(-) create mode 100644 test/Data/Text/ParagraphLayout/PrettyShow.hs diff --git a/balkon.cabal b/balkon.cabal index 3d48cd6..1d2815b 100644 --- a/balkon.cabal +++ b/balkon.cabal @@ -195,6 +195,7 @@ test-suite balkon-test Data.Text.ParagraphLayout.Internal.TextContainerSpec, Data.Text.ParagraphLayout.Internal.ZipperSpec, Data.Text.ParagraphLayout.ParagraphData, + Data.Text.ParagraphLayout.PrettyShow, Data.Text.ParagraphLayout.RectSpec, Data.Text.ParagraphLayout.SpanData diff --git a/test/Data/Text/ParagraphLayout/PrettyShow.hs b/test/Data/Text/ParagraphLayout/PrettyShow.hs new file mode 100644 index 0000000..2b9ff33 --- /dev/null +++ b/test/Data/Text/ParagraphLayout/PrettyShow.hs @@ -0,0 +1,143 @@ +module Data.Text.ParagraphLayout.PrettyShow + ( PrettyShow + , prettyShow + , Pages (..) + , ShapedRuns (..) + ) +where + +import Data.Text.ParagraphLayout +import Data.Text.ParagraphLayout.Internal.Fragment (ShapedRun) + +class PrettyShow a where + prettyShow :: a -> String + +newtype ShapedRuns = ShapedRuns { getShapedRuns :: [ShapedRun] } + deriving (Eq) + +instance PrettyShow ShapedRuns where + prettyShow (ShapedRuns xs) = concat + [ "[" + , concat $ indentedList indent0 $ map (prettyShow . ShapedRun') xs + , "]" + ] + +newtype ShapedRun' = ShapedRun' ShapedRun + +instance PrettyShow ShapedRun' where + prettyShow (ShapedRun' (x, y, glyphs)) = concat + [ "(" + , show x + , "," + , show y + , "," + , newline + , indent1 + , "[" + , concat $ indentedList indent1 $ map show glyphs + , "]" + , newline + , ")" + ] + +type Page = (PageContinuity, ParagraphLayout) + +newtype Pages = Pages { getPages :: [Page] } + deriving (Eq) + +instance PrettyShow Pages where + prettyShow (Pages ps) = concat + [ "[" + , concat $ indentedList indent0 $ map (prettyShow . Page') ps + , "]" + ] + +newtype Page' = Page' Page + +instance PrettyShow Page' where + prettyShow (Page' (c, pl)) = concat + [ "(" + , show c + , ", " + , prettyShow pl + , ")" + ] + +instance PrettyShow ParagraphLayout where + prettyShow (ParagraphLayout pr sls) = concat + [ "ParagraphLayout {paragraphRect = " + , show pr + , commaSpace + , "spanLayouts = [" + , newline + , indent1 + , concat $ indentedList indent1 $ map prettyShow sls + , newline + , "]}" + , newline + ] + +instance PrettyShow SpanLayout where + prettyShow (SpanLayout frags) = concat + [ "SpanLayout [" + , concat $ inlineList $ map prettyShow frags + , "]" + ] + +instance PrettyShow Fragment where + prettyShow (Fragment r pen glyphs) = concat + [ "Fragment {fragmentRect = " + , show r + , commaSpace + , "fragmentPen = " + , show pen + , commaSpace + , "fragmentGlyphs =" + , newline + , indent2 + , "[" + , concat $ indentedList indent2 $ map show glyphs + , "]" + , newline + , indent1 + , "}" + ] + +inlineList :: [String] -> [String] +inlineList items = suffixInit commaSpace items + +indentedList :: String -> [String] -> [String] +indentedList indent items = prefixTail indent $ suffixInit commaNewline items + +suffixInit :: String -> [String] -> [String] +suffixInit suffix = mapInit (++ suffix) + +mapInit :: (a -> a) -> [a] -> [a] +mapInit _ [] = [] +mapInit _ [x] = [x] +mapInit f (x : y : ys) = f x : mapInit f (y : ys) + +prefixTail :: String -> [String] -> [String] +prefixTail prefix = mapTail (prefix ++) + +mapTail :: (a -> a) -> [a] -> [a] +mapTail _ [] = [] +mapTail f (x : xs) = x : (map f xs) + +indent0 :: String +indent0 = "" + +indent1 :: String +indent1 = " " + +indent2 :: String +indent2 = indent1 ++ indent1 + +newline :: String +newline = "\n" + +commaSpace :: String +commaSpace = ", " + +commaNewline :: String +commaNewline = "," ++ newline diff --git a/test/Data/Text/ParagraphLayoutSpec.hs b/test/Data/Text/ParagraphLayoutSpec.hs index 8ab1b46..5024305 100644 --- a/test/Data/Text/ParagraphLayoutSpec.hs +++ b/test/Data/Text/ParagraphLayoutSpec.hs @@ -7,144 +7,13 @@ import Test.Hspec.Golden import System.FilePath (()) import Data.Text.ParagraphLayout import Data.Text.ParagraphLayout.FontLoader -import Data.Text.ParagraphLayout.Internal.Fragment import Data.Text.ParagraphLayout.Internal.Plain.ParagraphLayout import Data.Text.ParagraphLayout.ParagraphData +import Data.Text.ParagraphLayout.PrettyShow import Data.Text.ParagraphLayout.Rect -class PrettyShow a where - prettyShow :: a -> String - -newtype ShapedRuns = ShapedRuns { getShapedRuns :: [ShapedRun] } - deriving (Eq) - -instance PrettyShow ShapedRuns where - prettyShow (ShapedRuns xs) = concat - [ "[" - , concat $ indentedList indent0 $ map (prettyShow . ShapedRun') xs - , "]" - ] - -newtype ShapedRun' = ShapedRun' ShapedRun - -instance PrettyShow ShapedRun' where - prettyShow (ShapedRun' (x, y, glyphs)) = concat - [ "(" - , show x - , "," - , show y - , "," - , newline - , indent1 - , "[" - , concat $ indentedList indent1 $ map show glyphs - , "]" - , newline - , ")" - ] - type Page = (PageContinuity, ParagraphLayout) -newtype Pages = Pages { getPages :: [Page] } - deriving (Eq) - -instance PrettyShow Pages where - prettyShow (Pages ps) = concat - [ "[" - , concat $ indentedList indent0 $ map (prettyShow . Page') ps - , "]" - ] - -newtype Page' = Page' Page - -instance PrettyShow Page' where - prettyShow (Page' (c, pl)) = concat - [ "(" - , show c - , ", " - , prettyShow pl - , ")" - ] - -instance PrettyShow ParagraphLayout where - prettyShow (ParagraphLayout pr sls) = concat - [ "ParagraphLayout {paragraphRect = " - , show pr - , commaSpace - , "spanLayouts = [" - , newline - , indent1 - , concat $ indentedList indent1 $ map prettyShow sls - , newline - , "]}" - , newline - ] - -instance PrettyShow SpanLayout where - prettyShow (SpanLayout frags) = concat - [ "SpanLayout [" - , concat $ inlineList $ map prettyShow frags - , "]" - ] - -instance PrettyShow Fragment where - prettyShow (Fragment r pen glyphs) = concat - [ "Fragment {fragmentRect = " - , show r - , commaSpace - , "fragmentPen = " - , show pen - , commaSpace - , "fragmentGlyphs =" - , newline - , indent2 - , "[" - , concat $ indentedList indent2 $ map show glyphs - , "]" - , newline - , indent1 - , "}" - ] - -inlineList :: [String] -> [String] -inlineList items = suffixInit commaSpace items - -indentedList :: String -> [String] -> [String] -indentedList indent items = prefixTail indent $ suffixInit commaNewline items - -suffixInit :: String -> [String] -> [String] -suffixInit suffix = mapInit (++ suffix) - -mapInit :: (a -> a) -> [a] -> [a] -mapInit _ [] = [] -mapInit _ [x] = [x] -mapInit f (x : y : ys) = f x : mapInit f (y : ys) - -prefixTail :: String -> [String] -> [String] -prefixTail prefix = mapTail (prefix ++) - -mapTail :: (a -> a) -> [a] -> [a] -mapTail _ [] = [] -mapTail f (x : xs) = x : (map f xs) - -indent0 :: String -indent0 = "" - -indent1 :: String -indent1 = " " - -indent2 :: String -indent2 = indent1 ++ indent1 - -newline :: String -newline = "\n" - -commaSpace :: String -commaSpace = ", " - -commaNewline :: String -commaNewline = "," ++ newline - class ShouldBeGolden a where shouldBeGolden :: a -> FilePath -> Golden a -- 2.30.2