module Main (main) where import qualified Data.ByteString as BS import Data.List (intersperse) import Data.Text.Glyphize (Font, GlyphInfo, GlyphPos, createFace, createFont) import Data.Text.Lazy (pack) import Test.Hspec import Test.Hspec.Golden import System.FilePath (()) import Data.Text.ParagraphLayout type LayoutOutput = [[(GlyphInfo,GlyphPos)]] czechHello :: Font -> Span czechHello font = Span { spanText = pack "Ahoj, světe!" , spanFont = font , spanLanguage = Just "cs" } prettyShow :: LayoutOutput -> String prettyShow = showOutput where showOutput rs = concat ["[\n", showRuns rs, "\n]\n"] showRuns = concat . intersperse ",\n" . map showRun showRun gs = concat [indent1, "[\n", showGlyphs gs, "\n", indent1, "]"] showGlyphs = concat . intersperse ",\n" . map showGlyph showGlyph g = concat [indent2, show g] indent1 = " " indent2 = indent1 ++ indent1 shouldBeGolden :: LayoutOutput -> FilePath -> Golden LayoutOutput shouldBeGolden output_ name = Golden { output = output_ , encodePretty = show , writeToFile = \path -> writeFile path . prettyShow , readFromFile = \path -> readFile path >>= return . read , goldenFile = ".golden" name "golden" , actualFile = Just (".golden" name "actual") , failFirstTime = False } main :: IO () main = do ttf <- BS.readFile ("assets" "fonts" "ubuntu" "Ubuntu-R.ttf") let face = createFace ttf 0 let font = createFont face hspec $ do describe "layout" $ do it "handles empty input" $ do layout [] `shouldBe` [] it "handles Czech hello" $ do layout [czechHello font] `shouldBeGolden` "czechHello"