add some tests for the enum macro
This commit is contained in:
parent
a5bf501ebd
commit
82256007e8
59
tests/enum.rs
Normal file
59
tests/enum.rs
Normal file
@ -0,0 +1,59 @@
|
|||||||
|
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(Default, 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);
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user