-
Notifications
You must be signed in to change notification settings - Fork 5
/
enca_test.go
106 lines (89 loc) · 2.04 KB
/
enca_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
package enca
import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strings"
"testing"
)
func Test_GetAvailableLanguages(t *testing.T) {
if len(GetAvailableLanguages()) == 0 {
t.Error("List of available languages must not be empty")
}
}
func Test_EncaFromString(t *testing.T) {
analyzer, err := New("zh")
if err != nil {
t.Errorf("Unable to create new analyzer: %v", err)
} else {
encoding, err := analyzer.FromString("美国各州选民今天开始正式投票。据信,", NAME_STYLE_HUMAN)
defer analyzer.Free()
// Output:
// UTF-8
if err != nil {
t.Errorf("Unable to get encoding: %v", err)
} else {
if encoding != "UTF-8" {
t.Errorf("Encoding must be 'UTF-8'")
}
}
}
}
func Test_EncaFromBytes(t *testing.T) {
pwd, err := os.Getwd()
if err != nil {
t.Errorf("Unable to get current working directory: %v", err)
}
var (
ea *EncaAnalyser
parts []string
lang string
tests map[string][]string = make(map[string][]string)
content []byte
)
visit := func(path string, f os.FileInfo, err error) error {
if !f.IsDir() {
parts = strings.Split(f.Name(), "-")
if len(parts) < 2 {
return fmt.Errorf("Unable to process file '%s'", path)
}
lang = parts[0]
if lang == "none" {
lang = "__"
}
if _, ok := tests[lang]; ok {
tests[lang] = append(tests[lang], path)
} else {
tests[lang] = []string{path}
}
}
return err
}
err = filepath.Walk(fmt.Sprintf("%s%stest-data", pwd, string(filepath.Separator)), visit)
if err != nil {
t.Error(err)
}
if len(tests) == 0 {
t.Errorf("Tests are empty")
}
for lang, files := range tests {
ea, err = New(lang)
if err != nil {
t.Errorf("Unable to create new analyzer: %v", err)
break
}
for _, file := range files {
content, err = ioutil.ReadFile(file)
if err != nil {
t.Errorf("Unable to read file '%s': %v", file, err)
} else {
_, err = ea.FromBytes(content, NAME_STYLE_ENCA)
if err != nil {
t.Errorf("Unable to detect encoding of '%s': %v", file, err)
}
}
}
ea.Free()
}
}