module Data.Text.ParagraphLayoutSpec (spec) where
import Data.List (intersperse)
import Data.Text.Glyphize (GlyphInfo, GlyphPos)
import Test.Hspec
import Test.Hspec.Golden
import System.FilePath ((</>))
import Data.Text.ParagraphLayout
import Data.Text.ParagraphLayout.FontLoader
import Data.Text.ParagraphLayout.SpanData
type LayoutOutput = [[(GlyphInfo,GlyphPos)]]
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
}
spec :: Spec
spec = do
describe "layout" $ before loadUbuntuRegular $ do
it "handles input with no spans" $ \_ -> do
layout [] `shouldBe` []
it "handles one span with no text" $ \font -> do
layout [emptySpan font] `shouldBe` []
it "handles Czech hello" $ \font -> do
layout [czechHello font] `shouldBeGolden` "czechHello"