module Data.Text.ParagraphLayout.PlainSpec (spec) where
import Data.List (intersperse)
import Data.Text.Glyphize (Font)
import Test.Hspec
import Test.Hspec.Golden
import System.FilePath ((</>))
import Data.Text.ParagraphLayout.FontLoader
import Data.Text.ParagraphLayout.ParagraphData
import Data.Text.ParagraphLayout.Plain
prettyShow :: ParagraphLayout -> String
prettyShow (ParagraphLayout pr sls) = showParagraphLayout where
showParagraphLayout = concat
[ "ParagraphLayout {paragraphRect = "
, show pr
, ", spanLayouts = ["
, newline
, showSpanLayouts
, newline
, "]}"
, newline
]
showSpanLayouts = concat $ intersperse commaNewline $ map showSpanLayout sls
showSpanLayout (SpanLayout boxes) = concat
[ indent1
, "SpanLayout ["
, concat $ map showBox boxes
, "]"
]
showBox (r, glyphs) = concat
[ "("
, show r
, commaNewline
, indent2
, "["
, showGlyphs glyphs
, "]"
, newline
, indent1
, ")"
]
showGlyphs = concat . intersperse (commaNewline ++ indent2) . map show
indent1 = " "
indent2 = indent1 ++ indent1
newline = "\n"
commaNewline = "," ++ newline
shouldBeGolden :: ParagraphLayout -> FilePath -> Golden ParagraphLayout
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
}
emptyLayout :: ParagraphLayout
emptyLayout = ParagraphLayout (Rect 0 0 0 0) []
emptySpanLayout :: ParagraphLayout
emptySpanLayout = ParagraphLayout (Rect 0 0 0 0) [SpanLayout []]
opts :: Font -> ParagraphOptions
opts font = ParagraphOptions font (Relative 1.5) 20000
spec :: Spec
spec = do
-- Note: This font does not contain Japanese glyphs.
describe "layoutPlain" $ before loadUbuntuRegular $ do
it "handles input with no spans" $ \font -> do
let result = layoutPlain $ emptyParagraph $ opts font
result `shouldBe` emptyLayout
it "handles one span with no text" $ \font -> do
let result = layoutPlain $ emptySpanParagraph $ opts font
result `shouldBe` emptySpanLayout
it "handles Czech hello" $ \font -> do
let result = layoutPlain $ czechHelloParagraph $ opts font
result `shouldBeGolden` "czechHelloParagraph"
it "handles mixed languages in LTR layout" $ \font -> do
let result = layoutPlain $ mixedLanguageLTRParagraph $ opts font
result `shouldBeGolden` "mixedLanguageLTRParagraph"