~jaro/balkon

ref: 033116be7da3e02163292c8f5fee8bee39308f86 balkon/test/Data/Text/ParagraphLayout/PlainSpec.hs -rw-r--r-- 4.5 KiB
033116beJaro Remove I8/Int distinction. 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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
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.Fragment
import Data.Text.ParagraphLayout.LineHeight
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 frags) = concat
        [ indent1
        , "SpanLayout ["
        , concat $ intersperse ", " $ map showFrag frags
        , "]"
        ]
    showFrag (Fragment r pen glyphs) = concat
        [ "Fragment {fragmentRect = "
        , show r
        , ", "
        , "fragmentPen = "
        , show pen
        , ", "
        , "fragmentGlyphs ="
        , newline
        , 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 Normal 8000

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"
        it "handles normal line height" $ \font -> do
            let result = layoutPlain $ trivialParagraph $ (opts font) {
                paragraphLineHeight = Normal
            }
            result `shouldBeGolden` "lineHeightNormal"
        it "handles larger line height" $ \font -> do
            let result = layoutPlain $ trivialParagraph $ (opts font) {
                paragraphLineHeight = Absolute 1600
            }
            result `shouldBeGolden` "lineHeightLarger"
        it "handles smaller line height" $ \font -> do
            let result = layoutPlain $ trivialParagraph $ (opts font) {
                paragraphLineHeight = Absolute 599
            }
            result `shouldBeGolden` "lineHeightSmaller"
        it "wraps by characters when line is ultra narrow" $ \font -> do
            let result = layoutPlain $ czechHelloParagraph $ (opts font) {
                paragraphMaxWidth = 100
            }
            result `shouldBeGolden` "czechHelloParagraphUltraNarrow"
        -- TODO: Update the golden samples when word breaking is implemented.
        it "wraps lorem ipsum at 20em" $ \font -> do
            let result = layoutPlain $ loremIpsumParagraph $ (opts font) {
                paragraphMaxWidth = 20000
            }
            result `shouldBeGolden` "loremIpsum20em"
        it "wraps lorem ipsum at 100em" $ \font -> do
            let result = layoutPlain $ loremIpsumParagraph $ (opts font) {
                paragraphMaxWidth = 100000
            }
            result `shouldBeGolden` "loremIpsum100em"