Skip to content

Commit c57cbe4

Browse files
committed
Add first unit test (can be run from node or in browser)
1 parent b9315e8 commit c57cbe4

18 files changed

+1584
-45
lines changed

.eslintignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
dist/
2+
build/

.eslintrc.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
module.exports = {
2+
extends: 'eslint:recommended',
3+
env: {
4+
es6: true
5+
},
6+
parserOptions: {
7+
ecmaVersion: 2018,
8+
sourceType: 'module'
9+
},
10+
rules: {
11+
'no-console': 'off'
12+
}
13+
}

CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,4 +38,4 @@ file(GLOB opencv_lib_core "${OPENCV_DIR}/build_wasm/lib/libopencv_core.a")
3838
target_link_libraries(hello ${opencv_lib_core})
3939

4040
# Specify linker arguments
41-
set_target_properties(hello PROPERTIES LINK_FLAGS "-s ENVIRONMENT=web -s EXTRA_EXPORTED_RUNTIME_METHODS=['cwrap']")
41+
set_target_properties(hello PROPERTIES LINK_FLAGS "-s EXTRA_EXPORTED_RUNTIME_METHODS=['cwrap']")

dist/bundle.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/bundle.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/hello.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
<div class="container">
1414
<div cass="row">
1515
<div class="col-md-12">
16-
<div class="version pull-right">(version: 0.0.8)</div>
16+
<div class="version pull-right">(version: 0.0.9)</div>
1717
</div>
1818
</div>
1919
<div cass="row">

dist/tests.html

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
4+
<head>
5+
<meta charset="utf-8" />
6+
<title>Tests</title>
7+
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
8+
<link rel="stylesheet" href="https://unpkg.com/mocha/mocha.css" />
9+
<script src="https://unpkg.com/mocha/mocha.js"></script>
10+
<script src="https://unpkg.com/chai/chai.js"></script>
11+
</head>
12+
13+
<body>
14+
<div id="mocha"></div>
15+
<script src="/tests.js"></script>
16+
<script src="/hello.js"></script>
17+
</body>
18+
19+
</html>

dist/tests.js

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
let expect
2+
let npmcanvas
3+
let helloModule
4+
5+
const IS_NODE = typeof process === 'object' && typeof require === 'function'
6+
7+
if (IS_NODE) {
8+
expect = require('chai').expect
9+
npmcanvas = require('canvas')
10+
} else {
11+
expect = chai.expect
12+
mocha.setup('bdd')
13+
window.Module = {
14+
onRuntimeInitialized: () => {
15+
helloModule = window.Module
16+
mocha.run()
17+
}
18+
}
19+
}
20+
21+
const getImageDataNode = async () => {
22+
const fs = require('fs').promises
23+
const png = await fs.readFile('src/sudoku-puzzle.png')
24+
return new Promise(resolve => {
25+
const img = new npmcanvas.Image()
26+
img.onload = () => {
27+
const canvas = npmcanvas.createCanvas(img.width, img.height)
28+
const ctx = canvas.getContext('2d')
29+
ctx.drawImage(img, 0, 0)
30+
const imageData = ctx.getImageData(0, 0, canvas.width, canvas.height)
31+
resolve(imageData)
32+
}
33+
img.src = png
34+
})
35+
}
36+
37+
const getImageDataBrowser = async () =>
38+
new Promise(resolve => {
39+
const img = new Image()
40+
img.onload = () => {
41+
const canvas = document.createElement('canvas')
42+
canvas.width = img.width
43+
canvas.height = img.height
44+
const ctx = canvas.getContext('2d')
45+
ctx.drawImage(img, 0, 0)
46+
const imageData = ctx.getImageData(0, 0, canvas.width, canvas.height)
47+
resolve(imageData)
48+
}
49+
img.src = '/sudoku-puzzle.png'
50+
})
51+
52+
const getImageData = () =>
53+
IS_NODE ? getImageDataNode() : getImageDataBrowser()
54+
55+
describe('tests', () => {
56+
57+
before(() => {
58+
if (IS_NODE) {
59+
return new Promise(resolve => {
60+
const Module = require('../build/hello.js')
61+
Module.onRuntimeInitialized = () => {
62+
helloModule = Module
63+
resolve()
64+
}
65+
})
66+
}
67+
})
68+
69+
const expectAlmost = (actual, expected) => {
70+
const TOLERANCE = 1
71+
const lowerBound = expected - TOLERANCE
72+
const upperBound = expected + TOLERANCE
73+
expect(actual).to.be.within(lowerBound, upperBound)
74+
}
75+
76+
it('processImage', async () => {
77+
78+
const ident = 'processImage'
79+
const returnType = 'number'
80+
const argTypes = ['array', 'number', 'number']
81+
const processImage = helloModule.cwrap(ident, returnType, argTypes)
82+
83+
const imageData = await getImageData()
84+
const { data, width, height } = imageData
85+
const addr = processImage(data, width, height)
86+
const returnDataAddr = addr / helloModule.HEAP32.BYTES_PER_ELEMENT
87+
const returnData = helloModule.HEAP32.slice(returnDataAddr, returnDataAddr + 8)
88+
89+
const [bbx, bby, bbw, bbh, imgw, imgh, imgd] = returnData
90+
91+
expectAlmost(bbx, 29)
92+
expectAlmost(bby, 43)
93+
expectAlmost(bbw, 266)
94+
expectAlmost(bbh, 267)
95+
96+
expect(imgw).to.equal(324)
97+
expect(imgh).to.equal(324)
98+
expect(imgd).to.equal(1)
99+
100+
helloModule._free(addr)
101+
})
102+
})

0 commit comments

Comments
 (0)