Skip to content

Commit

Permalink
Add basic unit test
Browse files Browse the repository at this point in the history
This just ensures that the output is as expected. It does not exercise
the extent of the Tangibl scanning beyond one token.
  • Loading branch information
battesonb committed Jun 10, 2023
1 parent e0b006c commit 7db7840
Show file tree
Hide file tree
Showing 7 changed files with 32 additions and 43 deletions.
41 changes: 4 additions & 37 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 0 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@ default = ["console_error_panic_hook"]

[dependencies]
js-sys = "0.3.60"
serde = { version = "1.0.144", features = ["derive"] }
serde-wasm-bindgen = "0.4.3"
topcodes = { version = "0.1.0" }
wasm-bindgen = "0.2.63"

Expand Down
3 changes: 1 addition & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
mod utils;

use serde::{Deserialize, Serialize};
use topcodes::{Scanner, TopCode};
use topcodes::Scanner;
use wasm_bindgen::prelude::*;

#[wasm_bindgen]
Expand Down
Binary file added tests/assets/start.bin
Binary file not shown.
Binary file added tests/assets/start.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
9 changes: 9 additions & 0 deletions tests/assets/to_bytes.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#!/usr/bin/env python3

from PIL import Image
from io import BytesIO

with Image.open("start.png") as img:
buffer = img.tobytes("raw")
with open("start.bin", "wb") as binfile:
binfile.write(buffer)
20 changes: 18 additions & 2 deletions tests/web.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,27 @@
#![cfg(target_arch = "wasm32")]

extern crate wasm_bindgen_test;
use topcodes_wasm::scan;
use wasm_bindgen::JsValue;
use wasm_bindgen_test::*;

wasm_bindgen_test_configure!(run_in_browser);

#[wasm_bindgen_test]
fn can_scan() {
assert_eq!(1 + 1, 2);
fn can_scan() -> Result<(), Box<dyn std::error::Error>> {
let bytes = include_bytes!("./assets/start.bin");
let width = 64;
let height = 64;
// Assert that the bin file has a rgba bytes for the right dimensions.
assert_eq!(bytes.len(), width * height * 4);
let buffer = js_sys::Uint8ClampedArray::new_with_length(width as u32 * height as u32 * 4);
buffer.copy_from(bytes);

let result = scan(&buffer, width, height).to_vec();

let expected: Vec<JsValue> = vec![JsValue::from_str(
"{\"code\":61,\"unit\":7.6,\"orientation\":-0.07249829200591831,\"x\":31.5,\"y\":30.5}",
)];
assert_eq!(expected, result);
Ok(())
}

0 comments on commit 7db7840

Please sign in to comment.