use pretty_assertions::assert_eq;
use regex::Regex;
use rustdns::Message;
use serde::Deserialize;
use std::fs;
const TEST_DATA_FILENAME: &str = "tests/test_data.yaml";
#[derive(Deserialize)]
struct TestCase {
name: String,
binary: String,
string: String,
}
#[test]
fn tests() {
let s = fs::read(TEST_DATA_FILENAME).expect("failed read test input");
let tests: Vec<TestCase> =
serde_yaml::from_slice(&s).expect("failed to deserialise test input");
for case in tests {
test_from_slice(case);
}
}
fn normalise_whitespace(s: &str) -> String {
let re = Regex::new(r"[ ]+").unwrap();
return re.replace_all(s, " ").to_string();
}
fn test_from_slice(case: TestCase) {
let input = match hex::decode(case.binary) {
Err(e) => panic!("{}: Invalid test case input: {}", case.name, e),
Ok(i) => i,
};
let m = match Message::from_slice(&input) {
Err(e) => panic!("{}: Unable to parse: {}", case.name, e),
Ok(p) => p,
};
let got = normalise_whitespace(&format!("{}", m));
let want = normalise_whitespace(&case.string);
assert_eq!(got, want, "{}: Formatted string doesn't match", case.name);
}