Testing Conway's Game of Life¶
Now that we have our Rust implementation of the Game of Life rendering in the browser with JavaScript, let's talk about testing our Rust-generated WebAssembly functions.
We are going to test our tick
function to make sure that it gives us the output that we expect.
Next, we'll want to create some setter and getter functions inside our existing impl Universe
block in the wasm_game_of_life/src/lib.rs
file. We are going to create a set_width
and a set_height
function so we can create Universe
s of different sizes.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
|
We are going to create another impl Universe
block inside our wasm_game_of_life/src/lib.rs
file without the #[wasm_bindgen]
attribute. There are a few functions we need for testing that we don't want to expose to our JavaScript. Rust-generated WebAssembly functions cannot return borrowed references. Try compiling the Rust-generated WebAssembly with the attribute and take a look at the errors you get.
We are going to write the implementation of get_cells
to get the contents of the cells
of a Universe
. We'll also write a set_cells
function so we can set cells
in a specific row and column of a Universe
to be Alive.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
|
Now we're going to create our test in the wasm_game_of_life/tests/web.rs
file.
Before we do that, there is already one working test in the file. You can confirm that the Rust-generated WebAssembly test is working by running wasm-pack test --chrome --headless
in the wasm-game-of-life
directory. You can also use the --firefox
, --safari
, and --node
options to test your code in those browsers.
In the wasm_game_of_life/tests/web.rs
file, we need to export our wasm_game_of_life
crate and the Universe
type.
1 2 |
|
In the wasm_game_of_life/tests/web.rs
file we'll want to create some spaceship builder functions.
We'll want one for our input spaceship that we'll call the tick
function on and we'll want the expected spaceship we will get after one tick. We picked the cells that we want to initialize as Alive
to create our spaceship in the input_spaceship
function. The position of the spaceship in the expected_spaceship
function after the tick of the input_spaceship
was calculated manually. You can confirm for yourself that the cells of the input spaceship after one tick is the same as the expected spaceship.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
|
Now we will write the implementation for our test_tick
function. First, we create an instance of our input_spaceship()
and our expected_spaceship()
. Then, we call tick
on the input_universe
. Finally, we use the assert_eq!
macro to call get_cells()
to ensure that input_universe
and expected_universe
have the same Cell
array values. We add the #[wasm_bindgen_test]
attribute to our code block so we can test our Rust-generated WebAssembly code and use wasm-pack test
to test the WebAssembly code.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
|
Run the tests within the wasm-game-of-life
directory by running wasm-pack test --firefox --headless
.