Open la10736 opened 1 year ago
files_arg
too and rewrite tests to use mockall
files
json_path
?The proposed rendering is just wrong :cry: . In order to avoid modules name duplication every rendering should start with the function name. So, in the previous described case we'll have something like the the following expanded code (some details are omitted):
mod age_is_greater_eq_than_18 {
use super::*;
#[derive(Deserialize)]
struct __JsonObjDeserialized {
age: u16
}
mod resources {
use super::*;
mod tests {
use super::*;
mod people_json {
use super::*;
#[test]
fn data_1() {build
let __JsonObjDeserialized {
age
} = serde_json::from_str(#"{
"name": "John Doe",
"age": 23
}"#
).expect(#"I cannot deserialize '"{
"name": "John Doe",
"age": 23
}"' to extract fields"#);
assert!(age>=18);
}
#[test]
fn data_2() {build
let __JsonObjDeserialized {
age
} = serde_json::from_str(#"{
"name": "Andy Brown",
"age": 45
}"#
).expect(#"I cannot deserialize '"{
"name": "Andy Brown",
"age": 45
}"' to extract fields"#);
assert!(age>=18);
}
}
}
}
}
mod use_remap {
use super::*;
#[derive(Deserialize)]
struct __JsonObjDeserialized {
#[serde(rename = "age")]
a: u16
}
/// Rest of the code is like the previous case where use `a` instead of `age` in the test
}
mod use_remap_from_serde {
/// The same code like in `use_remap`
}
mod use_object {
mod resources {
use super::*;
mod tests {
use super::*;
mod people_json {
use super::*;
#[test]
fn data_1() {build
let user = serde_json::from_str(#"{
"name": "John Doe",
"age": 23
}"#
).expect(#"I cannot deserialize '"{
"name": "John Doe",
"age": 23
}"'"#);
assert!(user.age>=18);
}
#[test]
fn data_2() {build
let user = serde_json::from_str(#"{
"name": "Andy Brown",
"age": 45
}"#
).expect(#"I cannot deserialize '"{
"name": "Andy Brown",
"age": 45
}"'"#);
assert!(user.age>=18);
}
}
}
}
}
The big picture here is to generate tests from some kind of serialization protocols. But I would start from a narrow case to expand it in a future.
The idea is define a glob path to get the json files that can contains one object or an array of objects. And write a test for each object.
In this ticket we manage just an MVP, later we'll extend it.
#[json(glob_path)]
: function attribute to identify the json files. All followingserde
attributes will be applied to the struct used to deserialize data for test if any.#[field]
: argument attribute that we should deserialize from the json, the optional argumentname
can be used to map the json field to this argument. In this case allserde
's attribute of this field will be used as field's attribute in deserialized struct.#[data]
: argument attribute to define one argument where the type implementserde::Deserialize
and can be deserialized from the json object in the filesMinimal Example
file in
resources/test/people.json
More Info
_UP
): in this case the functions pathresources::tests::people_json::age_is_greater_eq_than_18
and the names aredata_1
anddata_2
. If json contains a sigle root object the last module is omitted and its name will be used as names.#[json]
attribute for a function#[values]
and#[case]