#![allow(clippy::expect_used)]
use std::{net::SocketAddr, path::PathBuf};
use sample_config::SampleConfig;
use serde::Deserialize;
#[derive(Debug, PartialEq, Eq, SampleConfig, Deserialize)]
struct TestConfig {
#[doc = "Example field 1."]
example_sub_struct: ExampleSubConfig,
enum_test: ExampleEnum,
path: Box<PathBuf>,
vec_of_structs: Vec<ExampleSubSubConfig>,
}
impl Default for TestConfig {
fn default() -> Self {
Self {
example_sub_struct: ExampleSubConfig {
string: Some("Cats are cute.".to_owned()),
numbers: vec![1, 2, 3],
sub_struct: Some(ExampleSubSubConfig {
some_field: vec!["127.0.0.1:8080"
.parse()
.expect("parsing constant socket address")],
}),
},
enum_test: ExampleEnum::VariantA,
path: Box::new(PathBuf::from("file.txt")),
vec_of_structs: vec![ExampleSubSubConfig { some_field: vec![] }],
}
}
}
#[derive(Debug, PartialEq, Eq, SampleConfig, Deserialize)]
struct ExampleSubConfig {
string: Option<String>,
numbers: Vec<usize>,
sub_struct: Option<ExampleSubSubConfig>,
}
#[derive(Debug, PartialEq, Eq, SampleConfig, Deserialize)]
struct ExampleSubSubConfig {
some_field: Vec<SocketAddr>,
}
#[derive(Debug, PartialEq, Eq, SampleConfig, Deserialize)]
enum ExampleEnum {
VariantA,
}
#[test]
fn generated_yaml_config() {
let config = TestConfig::default();
let generated = config.generate_sample_yaml();
let expected = include_str!("./expected/config_generation.yaml");
assert_eq!(&generated, expected);
let deserialized: TestConfig =
serde_yaml::from_str(expected).expect("deserialize expected YAML");
assert_eq!(deserialized, config);
}
#[test]
fn generated_json_config() {
let config = TestConfig::default();
let generated = config.generate_sample_json();
let expected = include_str!("./expected/config_generation.json5");
assert_eq!(&generated, expected);
let deserialized: TestConfig = json5::from_str(expected).expect("deserialize expected JSON");
assert_eq!(deserialized, config);
}