mirror of
https://github.com/Noratrieb/haesli.git
synced 2026-01-14 19:55:03 +01:00
random testing
This commit is contained in:
parent
2455e95d45
commit
c0bfcb4089
7 changed files with 1649 additions and 1247 deletions
|
|
@ -1,5 +1,6 @@
|
|||
mod parser;
|
||||
mod write;
|
||||
mod random;
|
||||
|
||||
use crate::parser::codegen_parser;
|
||||
use crate::write::codegen_write;
|
||||
|
|
@ -7,6 +8,7 @@ use heck::ToUpperCamelCase;
|
|||
use std::fs;
|
||||
use std::iter::Peekable;
|
||||
use strong_xml::XmlRead;
|
||||
use crate::random::codegen_random;
|
||||
|
||||
#[derive(Debug, XmlRead)]
|
||||
#[xml(tag = "amqp")]
|
||||
|
|
@ -91,6 +93,7 @@ fn codegen(amqp: &Amqp) {
|
|||
codegen_class_defs(amqp);
|
||||
codegen_parser(amqp);
|
||||
codegen_write(amqp);
|
||||
codegen_random(amqp);
|
||||
}
|
||||
|
||||
fn codegen_domain_defs(amqp: &Amqp) {
|
||||
|
|
|
|||
61
amqp_codegen/src/random.rs
Normal file
61
amqp_codegen/src/random.rs
Normal file
|
|
@ -0,0 +1,61 @@
|
|||
use crate::{snake_case, Amqp};
|
||||
use heck::ToUpperCamelCase;
|
||||
|
||||
pub(crate) fn codegen_random(amqp: &Amqp) {
|
||||
println!(
|
||||
"#[cfg(test)]
|
||||
mod random {{
|
||||
use rand::Rng;
|
||||
use crate::classes::tests::RandomMethod;
|
||||
use super::*;
|
||||
"
|
||||
);
|
||||
|
||||
impl_random("Class", || {
|
||||
let class_lens = amqp.classes.len();
|
||||
println!(" match rand::thread_rng().gen_range(0u32..{class_lens}) {{");
|
||||
for (i, class) in amqp.classes.iter().enumerate() {
|
||||
let class_name = class.name.to_upper_camel_case();
|
||||
println!(" {i} => Class::{class_name}({class_name}::random(rng)),");
|
||||
}
|
||||
println!(
|
||||
" _ => unreachable!(),
|
||||
}}"
|
||||
);
|
||||
});
|
||||
|
||||
for class in &amqp.classes {
|
||||
let class_name = class.name.to_upper_camel_case();
|
||||
impl_random(&class_name, || {
|
||||
let method_len = class.methods.len();
|
||||
println!(" match rand::thread_rng().gen_range(0u32..{method_len}) {{");
|
||||
|
||||
for (i, method) in class.methods.iter().enumerate() {
|
||||
let method_name = method.name.to_upper_camel_case();
|
||||
println!(" {i} => {class_name}::{method_name} {{");
|
||||
for field in &method.fields {
|
||||
let field_name = snake_case(&field.name);
|
||||
println!(" {field_name}: RandomMethod::random(rng),");
|
||||
}
|
||||
println!(" }},");
|
||||
}
|
||||
println!(
|
||||
" _ => unreachable!(),
|
||||
}}"
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
println!("}}");
|
||||
}
|
||||
|
||||
fn impl_random(name: &str, body: impl FnOnce()) {
|
||||
println!(
|
||||
"impl<R: Rng> RandomMethod<R> for {name} {{
|
||||
fn random(rng: &mut R) -> Self {{"
|
||||
);
|
||||
|
||||
body();
|
||||
|
||||
println!(" }}\n}}");
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue