extern crate redirectionio;
#[rustfmt::skip]
mod generated_tests {
use redirectionio::action::RunExample;
use redirectionio::api::{TestExamplesInput, Rule};
use redirectionio::router::Router;
use serde_json::{from_str as json_decode, to_string_pretty as json_encode};
use std::env;
{%- for name in names %}
#[test]
fn test_examples_{{ name }}() {
do_test("{{ name }}");
}
{%- endfor %}
fn do_test(name: &str) {
let json_in = std::fs::read_to_string(format!("tests/test_examples/{}.in.json", name)).unwrap();
let test_examples_input: TestExamplesInput = json_decode(&json_in).unwrap();
let mut router = Router::<Rule>::from_config(test_examples_input.router_config.clone());
for rule in test_examples_input.rules.iter() {
router.insert(rule.clone());
}
let mut results = Vec::new();
for rule in &test_examples_input.rules {
if let Some(examples) = rule.examples.as_ref() {
for example in examples {
let mut run_example = RunExample::new(&router, example).unwrap();
run_example.request.created_at = None;
results.push(run_example);
}
}
}
let json_out_expected = std::fs::read_to_string(format!("tests/test_examples/{}.out.json", name)).unwrap();
let json_out = json_encode(&results).unwrap();
if env::var("RIO_UPDATE_FIXTURES").is_ok() {
std::fs::write(format!("tests/test_examples/{}.out.json", name), &json_out).unwrap();
return;
}
if json_out != json_out_expected {
std::fs::write(format!("tests/test_examples/{}.out.current.json", name), &json_out).unwrap();
}
assert_eq!(
json_out.parse::<serde_json::Value>().unwrap(),
json_out_expected.parse::<serde_json::Value>().unwrap(),
"check for tests/test_examples/{}.out.current.json",
name
);
}
}