-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgohocr_test.go
88 lines (78 loc) · 1.92 KB
/
gohocr_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
package gohocr
import (
"fmt"
"io/ioutil"
"os"
"testing"
)
func TestParse(t *testing.T) {
_, err := Parse("./test/test.hocr")
if err != nil {
panic("Parse failed")
}
}
// {"Lang":"eng","Content":"Lorem","Direction":"ltr","Title":"bbox 299 432 422 465; x_wconf 97","ID":"word_1_4"}
func TestFields(t *testing.T) {
page, err := Parse("./test/test.hocr")
if err != nil {
panic("Parse failed")
}
word := page.Words[3]
if word.Content != "Lorem" {
panic(fmt.Sprintf("Have: %s -- Expected: Lorem", word.Content))
}
if word.ID != "word_1_4" {
panic(fmt.Sprintf("Have: %s -- Expected: word_1_4", word.ID))
}
if word.Lang != "eng" {
panic(fmt.Sprintf("Have: %s -- Expected: eng", word.Lang))
}
if word.Title != "bbox 299 432 422 465; x_wconf 97" {
panic(fmt.Sprintf("Have: %s -- Expected: bbox 299 432 422 465; x_wconf 97", word.Title))
}
if word.Direction != "ltr" {
panic(fmt.Sprintf("Have: %s -- Expected: ltr", word.Direction))
}
}
func TestWrongPath(t *testing.T) {
_, err := Parse("./foo/test.hocr")
if err == nil {
panic("Parse should have failed with wrong path")
}
}
func TestNotAHOCR(t *testing.T) {
_, err := Parse("./test/notahocr.hocr")
if err == nil {
panic("Parse should have failed with invalid hocr")
}
}
func TestOSFilePoiner(t *testing.T) {
file, err := os.Open("./test/test.hocr")
if err != nil {
panic("os.Open should return valid pointer")
}
_, err = Parse(file)
if err != nil {
panic("Parse failed")
}
}
func TestByteSlice(t *testing.T) {
xmlFile, err := os.Open("./test/test.hocr")
if err != nil {
panic("os.Open should return valid pointer")
}
byteValue, err := ioutil.ReadAll(xmlFile)
if err != nil {
panic("ioutil.ReadAll should return a valid byte slice")
}
_, err = Parse(byteValue)
if err != nil {
panic("Parse failed")
}
}
func TestInvalidInput(t *testing.T) {
_, err := Parse(1)
if err == nil {
panic("Parse should have returned error with invalid input")
}
}