~jaro/balkon

ref: f72b5805e6f857d963bf025d464a86a80b9374b1 balkon/test/Data/Text/ParagraphLayout/Internal/BiDiReorderSpec.hs -rw-r--r-- 6.3 KiB
f72b5805Jaro Add stress test for Heisenbug hunting. 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
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
module Data.Text.ParagraphLayout.Internal.BiDiReorderSpec where

import Data.Foldable (toList)
import Data.List.NonEmpty (NonEmpty, fromList)
import Data.Word (Word8)

import Test.Hspec
import Data.Text.ParagraphLayout.Internal.BiDiLevels
import Data.Text.ParagraphLayout.Internal.BiDiReorder

ne :: [a] -> NonEmpty a
ne = fromList

data ExampleItem = ExampleItem Word8 String
    deriving (Eq, Show)

instance WithLevel ExampleItem where
    level (ExampleItem lvl _) = lvl

toString :: ExampleItem -> String
toString (ExampleItem _ str) = str

toStrings :: Foldable t => t ExampleItem -> [String]
toStrings = map toString . toList

singleItemLevel :: Level -> NonEmpty ExampleItem
singleItemLevel lvl = ne
    [ ExampleItem lvl "item"
    ]

flatLevel :: Level -> NonEmpty ExampleItem
flatLevel lvl = ne
    [ ExampleItem lvl "start"
    , ExampleItem lvl "mid"
    , ExampleItem lvl "end"
    ]

-- | Example 1 from <https://www.unicode.org/reports/tr9/#L2>,
-- split into words and individual punctuation marks.
example1Words :: NonEmpty ExampleItem
example1Words = ne
    [ ExampleItem 0 "car"
    , ExampleItem 0 "means"
    , ExampleItem 1 "CAR"
    , ExampleItem 0 "."
    ]

-- | Example 2 from <https://www.unicode.org/reports/tr9/#L2>,
-- split into words and individual punctuation marks.
example2Words :: NonEmpty ExampleItem
example2Words = ne
    [ ExampleItem 2 "car"
    , ExampleItem 1 "MEANS"
    , ExampleItem 1 "CAR"
    , ExampleItem 1 "."
    ]

-- | Example 3 from <https://www.unicode.org/reports/tr9/#L2>,
-- split into words and individual punctuation marks.
example3Words :: NonEmpty ExampleItem
example3Words = ne
    [ ExampleItem 0 "he"
    , ExampleItem 0 "said"
    , ExampleItem 0 "“"
    , ExampleItem 2 "car"
    , ExampleItem 1 "MEANS"
    , ExampleItem 1 "CAR"
    , ExampleItem 0 "."
    , ExampleItem 0 "”"
    , ExampleItem 0 "“"
    , ExampleItem 1 "IT"
    , ExampleItem 1 "DOES"
    , ExampleItem 0 ","
    , ExampleItem 0 "”"
    , ExampleItem 0 "she"
    , ExampleItem 0 "agreed"
    , ExampleItem 0 "."
    ]

-- | Example 4 from <https://www.unicode.org/reports/tr9/#L2>,
-- split into words and individual punctuation marks.
example4Words :: NonEmpty ExampleItem
example4Words = ne
    [ ExampleItem 1 "DID"
    , ExampleItem 1 "YOU"
    , ExampleItem 1 "SAY"
    , ExampleItem 1 "’"
    , ExampleItem 2 "he"
    , ExampleItem 2 "said"
    , ExampleItem 2 "“"
    , ExampleItem 4 "car"
    , ExampleItem 3 "MEANS"
    , ExampleItem 3 "CAR"
    , ExampleItem 2 "”"
    , ExampleItem 1 "‘"
    , ExampleItem 1 "?"
    ]

-- | Example 4 from <https://www.unicode.org/reports/tr9/#L2>,
-- split into individual characters.
example4Characters :: NonEmpty ExampleItem
example4Characters = ne
    [ ExampleItem 1 "D"
    , ExampleItem 1 "I"
    , ExampleItem 1 "D"
    , ExampleItem 1 " "
    , ExampleItem 1 "Y"
    , ExampleItem 1 "O"
    , ExampleItem 1 "U"
    , ExampleItem 1 " "
    , ExampleItem 1 "S"
    , ExampleItem 1 "A"
    , ExampleItem 1 "Y"
    , ExampleItem 1 " "
    , ExampleItem 1 "’"
    , ExampleItem 1 ">"
    , ExampleItem 2 "h"
    , ExampleItem 2 "e"
    , ExampleItem 2 " "
    , ExampleItem 2 "s"
    , ExampleItem 2 "a"
    , ExampleItem 2 "i"
    , ExampleItem 2 "d"
    , ExampleItem 2 " "
    , ExampleItem 2 "“"
    , ExampleItem 2 "<"
    , ExampleItem 4 "c"
    , ExampleItem 4 "a"
    , ExampleItem 4 "r"
    , ExampleItem 3 " "
    , ExampleItem 3 "M"
    , ExampleItem 3 "E"
    , ExampleItem 3 "A"
    , ExampleItem 3 "N"
    , ExampleItem 3 "S"
    , ExampleItem 3 " "
    , ExampleItem 3 "C"
    , ExampleItem 3 "A"
    , ExampleItem 3 "R"
    , ExampleItem 2 "="
    , ExampleItem 2 "”"
    , ExampleItem 1 "="
    , ExampleItem 1 "‘"
    , ExampleItem 1 "?"
    ]

spec :: Spec
spec = do

    describe "reorder" $ do

        it "passes through single item at level 0" $ do
            toStrings (reorder $ singleItemLevel 0) `shouldBe` ["item"]

        it "passes through single item at level 1" $ do
            toStrings (reorder $ singleItemLevel 1) `shouldBe` ["item"]

        it "passes through single item at level 2" $ do
            toStrings (reorder $ singleItemLevel 2) `shouldBe` ["item"]

        it "passes through single item at level 3" $ do
            toStrings (reorder $ singleItemLevel 3) `shouldBe` ["item"]

        it "does not reverse at level 0" $ do
            toStrings (reorder $ flatLevel 0) `shouldBe` ["start", "mid", "end"]

        it "reverses at level 1" $ do
            toStrings (reorder $ flatLevel 1) `shouldBe` ["end", "mid", "start"]

        it "does not reverse at level 2" $ do
            toStrings (reorder $ flatLevel 2) `shouldBe` ["start", "mid", "end"]

        it "reverses at level 3" $ do
            toStrings (reorder $ flatLevel 3) `shouldBe` ["end", "mid", "start"]

        it "reorders UAX #9 example 1 as words" $
            toStrings (reorder example1Words) `shouldBe`
                [ "car"
                , "means"
                , "CAR"
                , "."
                ]

        it "reorders UAX #9 example 2 as words" $
            toStrings (reorder example2Words) `shouldBe`
                [ "."
                , "CAR"
                , "MEANS"
                , "car"
                ]

        it "reorders UAX #9 example 3 as words" $
            toStrings (reorder example3Words) `shouldBe`
                [ "he"
                , "said"
                , "“"
                , "CAR"
                , "MEANS"
                , "car"
                , "."
                , "”"
                , "“"
                , "DOES"
                , "IT"
                , ","
                , "”"
                , "she"
                , "agreed"
                , "."
                ]

        it "reorders UAX #9 example 4 as words" $
            toStrings (reorder example4Words) `shouldBe`
                [ "?"
                , "‘"
                , "he"
                , "said"
                , "“"
                , "CAR"
                , "MEANS"
                , "car"
                , "”"
                , "’"
                , "SAY"
                , "YOU"
                , "DID"
                ]

        it "reorders UAX #9 example 4 as characters" $
            concat (toStrings (reorder example4Characters)) `shouldBe`
                "?‘=he said “<RAC SNAEM car=”>’ YAS UOY DID"