Simple Test
Tests are functions, marked with the #[test]
attribute. They are not included into the compiled program, and are only used for testing purposes.
module examples::simple_tests {
/// A point in 2D space. Has an `x` and `y` coordinate.
public struct Point has copy, drop {
x: u8,
y: u8,
}
/// Create a point.
public fun new(x: u8, y: u8): Point { Point { x, y } }
/// Move a point to a new location.
public fun move_xy(p: &mut Point, x: u8, y: u8) {
p.x = x;
p.y = y;
}
/// Get the x coordinate of a point.
public fun x(p: &Point): u8 { p.x }
/// Get the y coordinate of a point.
public fun y(p: &Point): u8 { p.y }
#[test]
// The function marked with `#[test]` is a test. The name of the function
// will be shown in the test output, so it should be descriptive.
//
// The function doesn't take any arguments, and doesn't return anything.
fun test_point_new_and_move() {
let mut p1 = new(1, 2);
// normally, a test should contain correctness assertions
assert!(x(&p1) == 1, 0);
assert!(y(&p1) == 2, 1);
move_xy(&mut p1, 3, 4);
// while not a requirement, abort codes should be unique,
// it's easier to debug when they are
assert!(x(&p1) == 3, 2);
assert!(y(&p1) == 4, 3);
}
}