-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathutfutil_test.go
146 lines (122 loc) · 3.78 KB
/
utfutil_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
package utfutil_test
import (
"bufio"
"io"
"io/ioutil"
"log"
"os"
"path/filepath"
"testing"
"github.com/TomOnTime/utfutil"
)
func TestReadFile(t *testing.T) {
expected, err := ioutil.ReadFile(filepath.Join("testdata", "calblur8.htm"))
if err != nil {
log.Fatal(err)
}
// The test files were generated with:
// for i in $(iconv -l|grep UTF) ; do
// iconv -f UTF-8 -t $i calblur8.htm > calblur8.htm.$i
// done
for _, tst := range []struct {
works bool // is combination is expected to work?
ume utfutil.EncodingHint
name string
}{
// Assume missing BOM means UTF8
{true, utfutil.UTF8, "calblur8.htm.UTF-8"}, // No BOM
{true, utfutil.UTF8, "calblur8.htm.UTF-16"}, // BOM=fffe
{false, utfutil.UTF8, "calblur8.htm.UTF-16LE"}, // no BOM
{false, utfutil.UTF8, "calblur8.htm.UTF-16BE"}, // no BOM
// Assume missing BOM means UFT16LE
{false, utfutil.UTF16LE, "calblur8.htm.UTF-8"}, // No BOM
{true, utfutil.UTF16LE, "calblur8.htm.UTF-16"}, // BOM=fffe
{true, utfutil.UTF16LE, "calblur8.htm.UTF-16LE"}, // no BOM
{false, utfutil.UTF16LE, "calblur8.htm.UTF-16BE"}, // no BOM
// Assume missing BOM means UFT16BE
{false, utfutil.UTF16BE, "calblur8.htm.UTF-8"}, // No BOM
{true, utfutil.UTF16BE, "calblur8.htm.UTF-16"}, // BOM=fffe
{false, utfutil.UTF16BE, "calblur8.htm.UTF-16LE"}, // no BOM
{true, utfutil.UTF16BE, "calblur8.htm.UTF-16BE"}, // no BOM
} {
actual, err := utfutil.ReadFile(filepath.Join("testdata", tst.name), tst.ume)
if err != nil {
log.Fatal(err)
}
if tst.works {
if string(expected) == string(actual) {
t.Log("SUCCESS:", tst.ume, tst.name)
} else {
t.Errorf("FAIL: %v/%v: expected %#v got %#v\n", tst.ume, tst.name, string(expected)[:4], actual[:4])
}
} else {
if string(expected) != string(actual) {
t.Logf("SUCCESS: %v/%v: failed as expected.", tst.ume, tst.name)
} else {
t.Errorf("FAILUREish: %v/%v: unexpected success!", tst.ume, tst.name)
}
}
}
}
func TestReadAndCloseFile(t *testing.T) {
file := filepath.Join("testdata", "calblur8.htm.UTF-8")
_, err := utfutil.ReadFile(file, utfutil.UTF8)
if err != nil {
log.Fatal(err)
}
f, err := os.OpenFile(file, os.O_RDONLY|os.O_EXCL, 0)
if err != nil {
t.Errorf("FAIL: Unable to open file in exclusive mode after reading, handle must still be open\n")
}
f.Close()
t.Log("SUCCESS: Closed file after reading")
}
func TestReadAndCloseScanner(t *testing.T) {
file := filepath.Join("testdata", "calblur8.htm.UTF-8")
scanner, err := utfutil.NewScanner(file, utfutil.UTF8)
if err != nil {
log.Fatal(err)
}
for scanner.Scan() {
scanner.Text()
}
if err := scanner.Close(); err != nil {
t.Errorf("FAIL: Unable to close file handle after scan")
}
f, err := os.OpenFile(file, os.O_RDONLY|os.O_EXCL, 0)
if err != nil {
t.Errorf("FAIL: Unable to open file in exclusive mode after reading, handle must still be open")
}
if err := f.Close(); err != nil {
t.Errorf("FAIL: Unable to close file handle after reading")
}
t.Logf("SUCCESS: Read and closed file handle")
}
func TestReadAndCloseFileReader(t *testing.T) {
file := filepath.Join("testdata", "calblur8.htm.UTF-8")
fr, err := utfutil.OpenFile(file, utfutil.UTF8)
if err != nil {
log.Fatal(err)
}
r := bufio.NewReader(fr)
for {
_, _, err := r.ReadLine()
if err != nil {
if err == io.EOF {
break
}
log.Fatal(err)
}
}
if err := fr.Close(); err != nil {
t.Errorf("FAIL: Unable to close file handle after reading")
}
f, err := os.OpenFile(file, os.O_RDONLY|os.O_EXCL, 0)
if err != nil {
t.Errorf("FAIL: Unable to open file in exclusive mode after reading, handle must still be open\n")
}
if err := f.Close(); err != nil {
t.Errorf("FAIL: Unable to close file handle after reading")
}
t.Logf("SUCCESS: Read and closed file handle")
}