forked from AbdullahDiaa/garabic
-
Notifications
You must be signed in to change notification settings - Fork 1
/
garabic_test.go
284 lines (259 loc) · 8.16 KB
/
garabic_test.go
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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
package garabic
import (
"fmt"
"io/ioutil"
"reflect"
"testing"
)
const succeed = "\u2705"
const failed = "\u274C"
// TestRemoveHarakat.
func TestRemoveHarakat(t *testing.T) {
t.Log("Given an arabic string it should be normalized")
{
for i, tt := range removeHarakatTestCases {
normalized := RemoveHarakat(tt.input)
t.Logf("\tTest: %d\t Normalizing %s", i, tt.input)
if normalized != tt.expected {
t.Errorf("\t%s\t(%s)\tShould be normalized to %s, got %s instead", failed, tt.description, tt.expected, normalized)
} else {
t.Logf("\t%s\t(%s)\tShould be normalized to %s", succeed, tt.description, tt.expected)
}
}
}
}
// TestNormalizeBigText.
func TestNormalizeBigText(t *testing.T) {
originalArabicText, err := ioutil.ReadFile("test_data/bigText.txt")
if err != nil {
t.Errorf("\t%s\t Reading file failed with error:(%s)\t", failed, err)
}
preNormalizedArabicText, err := ioutil.ReadFile("test_data/normalizedBigText.txt")
if err != nil {
t.Errorf("\t%s\t Reading prenormalized file failed with error:(%s)\t", failed, err)
}
// Try to normalize test.
normalized := Normalize(string(originalArabicText))
if normalized != string(preNormalizedArabicText) {
t.Errorf("\t%s\t Normalized text doesn't match [length of the normalized version: %d\t length of the original prenormalized version: %d\t", failed, len(normalized), len(preNormalizedArabicText))
}
t.Logf("\t%s\t Should normalize all text file\t", succeed)
}
// TestNormalize.
func TestNormalize(t *testing.T) {
t.Log("Given an arabic string it should be normalized")
{
for i, tt := range normalizeTestCases {
normalized := Normalize(tt.input)
t.Logf("\tTest: %d\t Normalizing %s", i, tt.input)
if normalized != tt.expected {
t.Errorf("\t%s\t(%s)\tShould be normalized to %s, got %s instead", failed, tt.description, tt.expected, normalized)
} else {
t.Logf("\t%s\t(%s)\tShould be normalized to %s", succeed, tt.description, tt.expected)
}
}
}
}
// TestDeleteRune.
func TestDeleteRune(t *testing.T) {
testCases := []struct {
description string
input []rune
index int
expected []rune
}{
{
description: "Deleting rune with index 0 that exists in the array",
input: []rune{'م', 'ح'},
index: 0,
expected: []rune{'ح'},
},
{
description: "Deleting rune with index 3 that exists in the array",
input: []rune{'م', 'ا', 'د', 'ة', 'ن'},
index: 3,
expected: []rune{'م', 'ا', 'د', 'ن'},
},
{
description: "Deleting rune with index 0 that doesn't exist in the array",
input: []rune{},
index: 0,
expected: []rune{},
},
{
description: "Deleting rune with index 0 that's more than array length",
input: []rune{'أ', 'ب'},
index: 10,
expected: []rune{'أ', 'ب'},
},
}
t.Log("Given a slice of runes and a position of a rune, it should be deleted from the slice while keeping order")
{
for i, tt := range testCases {
input := tt.input
input = deleteRune(input, tt.index)
t.Logf("\tTest: %d\t Deleting rune %d from %v", i, tt.index, tt.input)
if !reflect.DeepEqual(input, tt.expected) {
t.Errorf(
"\t%s\t(%s)\tShould be %v (len %d, cap %d), got %v (len %d, cap %d) instead",
failed, tt.description, tt.expected, len(tt.expected), cap(tt.expected), input, len(input), cap(input),
)
} else {
t.Logf("\t%s\t(%s)\tShould be %v", succeed, tt.description, tt.expected)
}
}
}
}
// TestSpellNumber.
func TestSpellNumber(t *testing.T) {
t.Log("Given a number it should be return readable string of it in arabic")
{
for i, tt := range spellNumberTestCases {
textOfNum := SpellNumber(tt.input)
t.Logf("\tTest: %d\t Spelling Number %d", i, tt.input)
if textOfNum != tt.expected {
t.Errorf("\t%s\t\tShould be converted to %s, got %s instead", failed, tt.expected, textOfNum)
} else {
t.Logf("\t%s\t\tShould be converted to %s", succeed, tt.expected)
}
}
}
}
// TestTashkeel.
func TestTashkeel(t *testing.T) {
t.Log("Given an arabic string, diacritics should be added correctly")
{
for i, tt := range tashkeelTestCases {
withDiacritics := Tashkeel(tt.input)
t.Logf("\tTest: %d\t Adding diacritics to %s", i, tt.input)
if withDiacritics != tt.expected {
t.Errorf("\t%s\t(%s)\tShould be updated to %s, got %s instead", failed, tt.description, tt.expected, withDiacritics)
} else {
t.Logf("\t%s\t(%s)\tShould be updated to %s", succeed, tt.description, tt.expected)
}
}
}
}
// TestShape.
func TestShape(t *testing.T) {
t.Log("Given an arabic string, shaping will be fixed for rendering")
{
for i, tt := range shapingTestCases {
shapedArabicText := Shape(tt.input)
t.Logf("\tTest: %d\t Shaping: %s", i, tt.input)
if shapedArabicText != tt.expected {
t.Errorf("\t%s\t(%s)\tShould be updated to \n'%s'\n, got \n\"%s\"\n instead", failed, tt.description, tt.expected, shapedArabicText)
} else {
t.Logf("\t%s\t(%s)\tShould be updated to %s", succeed, tt.description, tt.expected)
}
}
}
}
func TestIsArabicLetter(t *testing.T) {
t.Log("Given a letter, check if it's an arabic letter")
{
for i, tt := range arabicLetterTestCases {
t.Logf("\tTest: %d\t Checking if letter %s is arabic", i, string(tt.input))
if IsArabicLetter(tt.input) != tt.expected {
t.Errorf("\t%s\t(%s)\tShould return %t, got %t instead", failed, tt.description, tt.expected, IsArabicLetter(tt.input))
} else {
t.Logf("\t%s\t(%s)\tShould be %t", succeed, tt.description, tt.expected)
}
}
}
}
func TestIsArabic(t *testing.T) {
t.Log("Given a text, check if it's arabic")
{
for i, tt := range arabicTextTestCases {
t.Logf("\tTest: %d\t Checking if letter %s is arabic", i, string(tt.input))
if IsArabic(tt.input) != tt.expected {
t.Errorf("\t%s\t(%s)\tShould return %t, got %t instead", failed, tt.description, tt.expected, IsArabic(tt.input))
} else {
t.Logf("\t%s\t(%s)\tShould be %t", succeed, tt.description, tt.expected)
}
}
}
}
func TestToArabicDigits(t *testing.T) {
t.Log("Given a text, convert all english digits in it into arabic digits")
{
for i, tt := range arabicNumbersTestCases {
t.Logf("\tTest: %d\t Checking if letter %s is arabic", i, string(tt.input))
if ToArabicDigits(tt.input) != tt.expected {
t.Errorf("\t%s\t(%s)\tShould return %s, got %s instead", failed, tt.description, tt.expected, ToArabicDigits(tt.input))
} else {
t.Logf("\t%s\t(%s)\tShould be %s", succeed, tt.description, tt.expected)
}
}
}
}
func TestToEnglishDigits(t *testing.T) {
t.Log("Given a text, convert all arabic digits in it into english digits")
{
for i, tt := range englishNumbersTestCases {
t.Logf("\tTest: %d\t Checking if letter %s is arabic", i, string(tt.input))
if ToEnglishDigits(tt.input) != tt.expected {
t.Errorf("\t%s\t(%s)\tShould return %s, got %s instead", failed, tt.description, tt.expected, ToEnglishDigits(tt.input))
} else {
t.Logf("\t%s\t(%s)\tShould be %s", succeed, tt.description, tt.expected)
}
}
}
}
func BenchmarkNormalize(b *testing.B) {
for i := 0; i < b.N; i++ {
for _, c := range normalizeTestCases {
Normalize(c.input)
}
}
}
func BenchmarkRemoveHarakat(b *testing.B) {
for i := 0; i < b.N; i++ {
for _, c := range removeHarakatTestCases {
RemoveHarakat(c.input)
}
}
}
func BenchmarkNormalizeBigText(b *testing.B) {
originalArabicText, err := ioutil.ReadFile("test_data/bigText.txt")
if err != nil {
b.Errorf("\t%s\t Reading file failed with error:(%s)\t", failed, err)
}
for i := 0; i < b.N; i++ {
Normalize(string(originalArabicText))
}
}
func ExampleNormalize() {
normalized := Normalize("أحمد")
fmt.Println(normalized)
// Output:
// احمد
}
func ExampleRemoveHarakat() {
normalized := RemoveHarakat("سَنواتٌ")
fmt.Println(normalized)
// Output:
// سنوات
}
func ExampleSpellNumber() {
numberInWords := SpellNumber(100)
fmt.Println(numberInWords)
// Output:
// مئة
}
func ExampleIsArabicLetter() {
fmt.Println(IsArabicLetter('ص'))
// Output:
// true
}
func ExampleToArabicDigits() {
fmt.Println(ToArabicDigits("عام 2021"))
// Output:
// عام ٢٠٢١
}
func ExampleToEnglishDigits() {
fmt.Println(ToEnglishDigits("عام ٢٠٢١"))
// Output:
// عام 2021
}