macroconf/tests/enum.rs

63 lines
1.7 KiB
Rust

use macroconf::ConfigEnum;
use miniconf::{Error::Traversal, JsonCoreSlash, JsonPath, Traversal::Access, TreeKey};
use rstest::*;
use serde::{Deserialize, Serialize};
use std::str::from_utf8;
/// Description
#[derive(Debug, Default, PartialEq, Eq, ConfigEnum, Serialize, Deserialize)]
enum Test {
#[default]
Variant1,
Variant2,
Variant3,
}
#[test]
fn nodes() {
let nodes = Test::nodes::<JsonPath<String>>()
.map(|p| p.unwrap().0.into_inner())
.collect::<Vec<_>>();
assert_eq!(
nodes,
vec![".value", ".variants", ".description", ".default"]
);
}
#[fixture]
#[once]
fn test() -> Test {
Test::Variant2
}
#[rstest]
#[case("/", "\"Variant2\"")]
#[case("/value", "\"Variant2\"")]
#[case("/description", "\"Description\"")]
#[case("/default", "\"Variant1\"")]
#[case("/variants", "[\"Variant1\",\"Variant2\",\"Variant3\"]")]
fn serialize(test: &Test, #[case] path: &str, #[case] expected: &str) {
let mut buffer = [0u8; 64];
let len = test.get_json(path, &mut buffer).unwrap();
assert_eq!(from_utf8(&buffer[..len]), Ok(expected));
}
#[rstest]
#[case("/", Ok(10))]
#[case("/value", Ok(10))]
#[case("/variants", Err(Traversal(Access(0, "Cannot write limits"))))]
#[case("/default", Err(Traversal(Access(0, "Cannot write limits"))))]
#[case("/description", Err(Traversal(Access(0, "Cannot write limits"))))]
fn deserialize(
#[case] path: &str,
#[case] expected: Result<usize, miniconf::Error<serde_json_core::de::Error>>,
) {
let mut config = Test::Variant2;
let res = config.set_json(path, b"\"Variant3\"");
assert_eq!(res, expected);
if res.is_ok() {
assert_eq!(config, Test::Variant3);
}
}