Hi all.
For study I have to create my own crate, but although long researching I've got no idea. I've been programming RUST for half a year, so I'd call myself an advanced beginner. Has anyone got an idea or a wish for a crate I could develop?
What a coincidence! I just had an idea for a small crate yesterday! would have implemented it myself if I haven't had other projects to maintain
Some of my functions return rather elaborate structs like
struct Foo {
bar: Bar
bazes: Vec<Baz>
}
When testing such functions, I need to compare that the result is what I wanted to get. However, constructing such functions by hand is hard. So what I do is the following:
let foo = function_under_test();
assert_eq!(
format!("{:?}", foo),
r#"here I write Foo's debug representation"#
)
This approach has a couple of drawbacks:
- Debug string is white space sensitive, it can get pretty large and I can't split it over several lines or indent it
- I can't get a nice report about what exactly was different.
I think this can be handled with a function of the following signature:
fn assert_ron<T: Serialize>(actual: T, expected: &str) {
let actual_ron = serialize_to_ron(actual);
let expected_ron = parse_ron(expected);
smart_diff_rons(actual_ron, expected_ron);
}
Where expected is a string with ron notation.
The call site might look like this:
assert_ron(get_scene(), r#"
Scene(
materials: {
"metal": (
reflectivity: 1.0,
),
"plastic": (
reflectivity: 0.5,
),
},
entities: [
(
name: "hero",
material: "metal",
),
(
name: "monster",
material: "plastic",
),
],
)
"#)
And the result might look like this
mismatch in entities[0].name.
Expected: `"hero"`
Got: "villain"
I've even written something similar for Cargo, there's a function which finds mismatch in JSON objects:
https://github.com/rust-lang/cargo/blob/f6ef6349b18f70243428aefd122edda80da7dab1/tests/cargotest/support/mod.rs#L642
Thanks for your suggestion, this idea sounds interesting.
But what do you think: what size will this crate have?
I understand that the functions serialize_to_ron(actual)
and parse_ron(expected)
already exist. So the only thing to do would be smart_diff_rons(actual_ron, expected_ron)
, wouldn't it?
The point is: this study project is planned to be finished within a time of 5 weeks. So do you (or any other user) have any further suggestions for expansion of this crate, so that it will be enough work for 5 weeks?
Thank you in advance