~jaro/balkon

ref: 633d18066bdebdd14f5ae6116741dcf7963327ce balkon/test/Data/Text/ParagraphLayoutSpec.hs -rw-r--r-- 1.6 KiB
633d1806Jaro Separate tests by module. 1 year, 8 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
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"