Hi Michael, Another way would be to make assert_equal work with all types and use the repr module to print out pretty representations of objects being compared.
To give you an example: https://github.com/jakub-/a8282cf2/blob/master/src/matchers.rs#L24 Regards, Jakub On Oct 6, 2013, at 20:03, Michael Williamson <[email protected]> wrote: > Hello, > > As an exercise, I was trying to create a generic assert_equal function. > I came up with this: > > > #[cfg(test)] > mod tests { > #[test] > fn incorrect_output() { > let output = ~"Hello"; > assert_equal("Hi", output); > } > > fn assert_equal<T: ToStr + Eq>(expected: &T, actual: &T) { > assert!(expected == actual, "expected %s, was %s", expected.to_str(), > actual.to_str()); > } > } > > > However, attempting to compile this using rustc 0.8 results in an error: > > > $ rustc assert_equal.rs --test -o tests > assert_equal.rs:6:21: 6:25 error: mismatched types: expected `&<V2>` but > found `&'static str` (expected &-ptr but found &'static str) > assert_equal.rs:6 assert_equal("Hi", output); > ^~~~ > assert_equal.rs:6:27: 6:33 error: mismatched types: expected `&<V2>` but > found `~str` (expected &-ptr but found ~str) > assert_equal.rs:6 assert_equal("Hi", output); > ^~~~~~ > assert_equal.rs:6:8: 6:20 error: cannot determine a type for this bounded > type parameter: unconstrained type > assert_equal.rs:6 assert_equal("Hi", output); > ^~~~~~~~~~~~ > > > Replacing assert_equal with a specialised definition for strings > results in an error-free compilation: > > > fn assert_equal(expected: &str, actual: &str) { > assert!(expected == actual, "expected %s, was %s", expected.to_str(), > actual.to_str()); > } > > > How would I go about creating fixing the generic version of > assert_equal, or am I barking up completely the wrong tree? > > Many thanks, and apologies if I'm sending this to the wrong list! > > Michael > _______________________________________________ > Rust-dev mailing list > [email protected] > https://mail.mozilla.org/listinfo/rust-dev _______________________________________________ Rust-dev mailing list [email protected] https://mail.mozilla.org/listinfo/rust-dev
