forked from InsightSoftwareConsortium/itk-wasm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
readMeshTest.js
107 lines (97 loc) · 3.24 KB
/
readMeshTest.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
import test from 'tape'
import axios from 'axios'
import readMeshArrayBuffer from 'readMeshArrayBuffer'
import readMeshBlob from 'readMeshBlob'
import readMeshFile from 'readMeshFile'
import IntTypes from 'IntTypes'
import FloatTypes from 'FloatTypes'
const fileName = 'cow.vtk'
const testFilePath = 'base/build/ExternalData/test/Input/' + fileName
const verifyMesh = (t, mesh) => {
t.is(mesh.meshType.dimension, 3)
t.is(mesh.meshType.pointComponentType, FloatTypes.Float32)
t.is(mesh.meshType.cellComponentType, IntTypes.UInt32)
t.is(mesh.meshType.pointPixelType, 1)
t.is(mesh.meshType.cellPixelType, 1)
t.is(mesh.numberOfPoints, 2903)
t.is(mesh.numberOfCells, 3263)
t.end()
}
test('readMeshArrayBuffer reads an ArrayBuffer', (t) => {
return axios.get(testFilePath, { responseType: 'arraybuffer' })
.then(function (response) {
return readMeshArrayBuffer(null, response.data, 'cow.vtk')
})
.then(function ({ mesh, webWorker }) {
webWorker.terminate()
verifyMesh(t, mesh)
})
})
// Todo: fix me
// test('readMeshFile reads a Blob without a file extension', (t) => {
// return axios.get(testFilePath, {responseType: 'blob'})
// .then(function (response) {
// return readMeshBlob(response.data, 'cow')
// })
// .then(function (mesh) {
// verifyMesh(t, mesh)
// })
// })
test('readMeshBlob reads a Blob', (t) => {
return axios.get(testFilePath, { responseType: 'blob' })
.then(function (response) {
return readMeshBlob(null, response.data, 'cow.vtk')
})
.then(function ({ mesh, webWorker }) {
webWorker.terminate()
verifyMesh(t, mesh)
})
})
test('readMeshFile reads a File', (t) => {
return axios.get(testFilePath, { responseType: 'blob' }).then(function (response) {
const jsFile = new window.File([response.data], fileName)
return jsFile
})
.then(function (jsFile) {
return readMeshFile(null, jsFile)
})
.then(function ({ mesh, webWorker }) {
webWorker.terminate()
verifyMesh(t, mesh)
})
})
test('readMeshFile re-uses a WebWorker', (t) => {
return axios.get(testFilePath, { responseType: 'blob' }).then(function (response) {
const jsFile = new window.File([response.data], fileName)
return jsFile
})
.then(function (jsFile) {
return readMeshFile(null, jsFile)
})
.then(function ({ webWorker }) {
axios.get(testFilePath, { responseType: 'blob' }).then(function (response) {
const jsFile = new window.File([response.data], fileName)
return jsFile
})
.then(function (jsFile) {
return readMeshFile(webWorker, jsFile)
.then(function ({ mesh, webWorker }) {
webWorker.terminate()
verifyMesh(t, mesh)
})
})
})
})
test('readMeshFile throws a catchable error for an invalid file', (t) => {
const invalidArray = new Uint8Array([21, 4, 4, 4, 4, 9, 5, 0, 82, 42])
const invalidBlob = new window.Blob([invalidArray])
const invalidFile = new window.File([invalidBlob], 'invalid.file')
return readMeshFile(null, invalidFile).then(function ({ mesh }) {
t.fail('should not have successfully read the mesh')
t.end()
}).catch(function (error) {
t.pass(String(error))
t.pass('thrown an error that was caught')
t.end()
})
})