windjammer_runtime/
testing.rs

1//! Testing utilities
2//!
3//! Windjammer's `std::testing` module maps to these functions.
4
5/// Assert that a condition is true
6pub fn assert(condition: bool, message: &str) {
7    if !condition {
8        panic!("Assertion failed: {}", message);
9    }
10}
11
12/// Assert that two values are equal
13pub fn assert_eq<T: PartialEq + std::fmt::Debug>(left: T, right: T, message: &str) {
14    if left != right {
15        panic!(
16            "Assertion failed: {}\n  left: {:?}\n right: {:?}",
17            message, left, right
18        );
19    }
20}
21
22/// Assert that two values are not equal
23pub fn assert_ne<T: PartialEq + std::fmt::Debug>(left: T, right: T, message: &str) {
24    if left == right {
25        panic!(
26            "Assertion failed: {}\n  values should not be equal: {:?}",
27            message, left
28        );
29    }
30}
31
32/// Fail a test with a message
33pub fn fail(message: &str) -> ! {
34    panic!("Test failed: {}", message);
35}
36
37/// Check if code panics
38pub fn should_panic<F: FnOnce() + std::panic::UnwindSafe>(f: F) -> bool {
39    std::panic::catch_unwind(f).is_err()
40}
41
42#[cfg(test)]
43mod tests {
44    use super::*;
45
46    #[test]
47    fn test_assert_true() {
48        assert(true, "should not panic");
49    }
50
51    #[test]
52    #[should_panic]
53    fn test_assert_false() {
54        assert(false, "should panic");
55    }
56
57    #[test]
58    fn test_assert_eq_pass() {
59        assert_eq(5, 5, "should be equal");
60    }
61
62    #[test]
63    #[should_panic]
64    fn test_assert_eq_fail() {
65        assert_eq(5, 10, "should panic");
66    }
67
68    #[test]
69    fn test_should_panic() {
70        let panics = should_panic(|| {
71            panic!("test panic");
72        });
73        assert!(panics);
74
75        let no_panic = should_panic(|| {
76            // do nothing
77        });
78        assert!(!no_panic);
79    }
80}