-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.js
225 lines (190 loc) · 5.73 KB
/
test.js
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
/* Copyright 2021 Nick White.
* Use of this source code is governed by the AGPLv3
* license that can be found in the LICENSE file. */
'use strict'
var inbrowser = setinbrowser()
var window
var document
/* Test the titletobbox() function */
function testtitletobbox() {
var cases = [
{"name": "tesseract ocr_line example",
"in": "bbox 559 1651 2429 1740; textangle 180; x_size 86; x_descenders 22; x_ascenders 23",
"out": {x: 559, y: 1651, width: 1870, height: 89}},
{"name": "tesseract ocrx_word example",
"in": "bbox 1936 1902 2173 1990; x_wconf 92",
"out": {x: 1936, y: 1902, width: 237, height: 88}},
{"name": "tesseract ocr_carea example",
"in": "bbox 552 560 2451 3801",
"out": {x: 552, y: 560, width: 1899, height: 3241}},
{"name": "tesseract ocr_page example",
"in": 'image "/tmp/t.png"; bbox 0 0 3456 4677; ppageno 0',
"out": {x: 0, y: 0, width: 3456, height: 4677}}
]
var err = ""
for(let i of cases) {
let got = titletobbox(i.in)
if(JSON.stringify(got) != JSON.stringify(i.out)) {
err += "✖ titletobbox(): error in case '" + i.name + "'\n" +
" expected: " + JSON.stringify(i.out) + "\n" +
" got : " + JSON.stringify(got) + "\n"
} else {
err += "✔ titletobox() passed for '" + i.name + "'\n"
}
}
return err
}
/* Test the titletoimgname() function */
function testtitletoimgname() {
var cases = [
{"name": "tesseract ocr_page example 1",
"in": "image \"/tmp/1647_MAGNEN_PlacitaLogicae/0060_bin0.5.png\"; bbox 0 0 1881 2500; ppageno 0",
"out": "0060_bin0.5.png"},
{"name": "tesseract ocr_page example 2",
"in": "image \"/tmp/Alcoranus_test_2/0001_bin0.0.png\"; bbox 0 0 8533 13432; ppageno 0",
"out": "0001_bin0.0.png"},
{"name": "basic example",
"in": "image \"/tmp/Alcoranus_test_2/0001_bin0.0.png\"",
"out": "0001_bin0.0.png"},
{"name": "single quotes",
"in": "image '/tmp/Alcoranus_test_2/0001_bin0.0.png'",
"out": "0001_bin0.0.png"},
{"name": "bbox first",
"in": "bbox 0 0 8533 13432; image \"/tmp/Alcoranus_test_2/0001_bin0.0.png\"; ppageno 0",
"out": "0001_bin0.0.png"}
]
var err = ""
for(let i of cases) {
let got = titletoimgname(i.in)
if(got != i.out) {
err += "✖ titletoimgname(): error in case '" + i.name + "'\n" +
" expected: " + i.out + "\n" +
" got : " + got + "\n"
} else {
err += "✔ titletoimgname() passed for '" + i.name + "'\n"
}
}
return err
}
/* Tests that addopened() adds an hocr to the DOM successfully.
* Note that openhocr() (which calls this in proofreader.js) can't
* be tested directly as the file input element can't be manipulated
* by javascript for security reasons. */
function testaddopened() {
var cases = [
{"name": "hocr example 1",
"in": hocr1,
"out": {"carea_num": 5, "line_num": 83}}
]
for(let i of cases) {
var blob = new window.Blob([i.in])
var fr = new window.FileReader
fr.onload = function(e) {
addopened(e)
var err = ""
var got = {}
got.carea_num = document.getElementsByClassName('ocr_carea').length
got.line_num = document.getElementsByClassName('ocr_line').length
if(JSON.stringify(got) != JSON.stringify(i.out)) {
err += "✖ addopened(): error in case '" + i.name + "'\n" +
" expected: " + JSON.stringify(i.out) + "\n" +
" got : " + JSON.stringify(got) + "\n"
}
status(err)
if(err == "") {
status("✔ addopened() passed for '" + i.name + "'\n")
}
var hocr = document.getElementById('hocr')
hocr.innerHTML = ''
}
fr.readAsText(blob)
}
return ''
}
/* Runs all tests requiring DOM manipulation */
function rundomtests(document) {
var err = ""
err += testaddopened()
return err
}
/* Run all tests and return any errors, or an empty string if all
* tests pass */
function runalltests() {
var err = ""
err += testtitletobbox()
err += testtitletoimgname()
err += domtestsetup()
return err
}
/* If in browser, just call rundomtests(), otherwise set up the
* nodejs libraries jsdom and fs, load proofreader.html into a
* dom, and call rundomtests() with it. Returns an empty string
* on success, or the error message on failure. */
function domtestsetup() {
if(inbrowser) {
return rundomtests(document)
}
try {
var fs = require('fs')
} catch(e) {
console.log('Skipping DOM tests as fs library could not be loaded (is nodejs installed?)')
return ''
}
try {
var jsdom = require('jsdom')
} catch(e) {
console.log('Skipping DOM tests as jsdom library could not be loaded (is node-jsdom installed?)')
return ''
}
try {
var d = fs.readFileSync('proofreader.html', 'utf8')
} catch(e) {
return 'Error reading proofreader.html for DOM tests'
}
window = (new jsdom.JSDOM(d)).window
document = window.document
return rundomtests(window.document)
}
/* Tests whether this javascript is being run from a browser */
function setinbrowser() {
try {
if(typeof document !== 'undefined') {
return 1
}
} catch(e) {
}
return 0
}
/* Run tests and print the results with status() */
function start() {
var err = runalltests()
status(err)
}
/* Run tests and print the results to a new pre element */
function browserstart() {
var pre = document.createElement('pre')
pre.id = 'status'
pre.style = 'padding: 1ex 1em'
var f = document.getElementById('footer')
document.body.insertBefore(pre, f)
do {
f.style = 'display: none'
} while((f = f.nextSibling) != null)
start()
}
/* prints status messages on console (terminal) or in browser as appropriate */
function status(status) {
var s = status.trim()
if(s == '') {
return
}
if(!inbrowser) {
console.log(s)
return
}
var pre = document.getElementById('status')
pre.textContent += '\n' + s
}
if(!inbrowser) {
start()
}