~jaro/balkon

ref: 41c384ab0c26afa46ec3cbed9565161f9c1898e0 balkon/test/Data/Text/ParagraphLayout/Internal/BiDiLevelsSpec.hs -rw-r--r-- 3.0 KiB
41c384abJaro Remove paragraphLineHeight from Rich interface. 1 year, 5 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
module Data.Text.ParagraphLayout.Internal.BiDiLevelsSpec where

import Data.Text (Text)
import Data.Text.Glyphize (Direction (DirLTR, DirRTL))

import Test.Hspec
import Data.Text.ParagraphLayout.Internal.BiDiLevels
import Data.Text.ParagraphLayout.RunLengthEncoding
import Data.Text.ParagraphLayout.TextData

-- | Test that `textLevels` produces the levels of the lengths given in
-- "Data.Text.ParagraphLayout.TextData" and values given here.
shouldHaveLevels :: (Direction, a, Text, [Int]) -> [Level] -> SpecWith ()
shouldHaveLevels (dir, _, text, lens) levels = it description $
    result `shouldBe` TextLevels (runLengthDecode rls)
    where
        result = textLevels dir text
        rls = zip lens levels
        description = case rls of
            [] -> "should be empty"
            [(_, lvl)] -> "should all have level " ++ show lvl
            _ -> "should have multiple levels"

-- | Override the paragraph direction of sample data.
setDirection :: Direction -> (Direction, a, b, c) -> (Direction, a, b, c)
setDirection dir (_, a, b, c) = (dir, a, b, c)

-- | Override the paragraph direction of sample data to LTR.
setLTR :: (Direction, a, b, c) -> (Direction, a, b, c)
setLTR = setDirection DirLTR

-- | Override the paragraph direction of sample data to RTL.
setRTL :: (Direction, a, b, c) -> (Direction, a, b, c)
setRTL = setDirection DirRTL

spec :: Spec
spec = do

    describe "textLevels" $ do

        -- Empty input should produce empty output.
        -- Infinite list of level 0 is also acceptable.
        describe "on English input" $
            englishEmpty `shouldHaveLevels` []

        -- Empty input should produce empty output.
        -- Infinite list of level 1 is also acceptable.
        describe "on Arabic empty" $
            arabicEmpty `shouldHaveLevels` []

        -- All LTR text without numbers should always stay at the base level.
        describe "on English word in LTR" $
            setLTR englishWord `shouldHaveLevels` [0]

        -- All characters in a RTL paragraph must be at least at level 1.
        describe "on English word in RTL" $
            setRTL englishWord `shouldHaveLevels` [2]

        describe "on Arabic word in LTR" $
            setLTR arabicHello `shouldHaveLevels` [1]

        describe "on Arabic word in RTL" $
            setRTL arabicHello `shouldHaveLevels` [1]

        describe "on Serbian mixed script" $
            serbianMixedScript `shouldHaveLevels` [0]

        describe "on mixed direction with base LTR" $
            mixedDirectionSimple DirLTR `shouldHaveLevels` [0, 1, 0]

        describe "on mixed direction with base RTL" $
            mixedDirectionSimple DirRTL `shouldHaveLevels` [2, 1, 2]

        describe "on Arabic around English" $
            arabicAroundEnglish `shouldHaveLevels` [1, 2, 1, 2, 1]

        describe "on English around Arabic" $
            englishAroundArabic `shouldHaveLevels` [0, 1, 0]

        describe "on numbers in RTL run in LTR" $
            mixedDirectionNumbers `shouldHaveLevels` [0, 1, 2, 1]