use serde::{Deserialize, Serialize};
use toml_input::TomlInput;
#[test]
fn test_schema() {
#[derive(Debug, TomlInput, Serialize, Deserialize, Default)]
struct Test {
a: i32,
b: Option<usize>,
}
let res = Test::schema_to_string().unwrap();
let text = "# comment `Test`
# comment `a`
a = 0
# comment `b`
b = 0"
.to_string();
assert_eq!(res, text);
let _: Test = toml::from_str(&text).unwrap();
}
#[test]
fn test_value() {
#[derive(Debug, Clone, TomlInput, Serialize, Deserialize, PartialEq, Default)]
struct Test {
a: i32,
b: Option<usize>,
}
let test = Test { a: 2, b: None };
let res = test.clone().into_string().unwrap();
let text = "# comment `Test`
# comment `a`
a = 2
# comment `b`
#!b = 0"
.to_string();
assert_eq!(res, text);
let test1: Test = toml::from_str(&text).unwrap();
assert_eq!(test, test1);
}
#[test]
fn test_value_array() {
#[derive(Debug, Clone, TomlInput, Serialize, Deserialize, PartialEq, Default)]
struct Test {
a: i32,
b: Vec<usize>,
}
let test = Test {
a: 1,
b: vec![2, 3, 4],
};
let res = test.clone().into_string().unwrap();
println!("{res}");
let text = "# comment `Test`
# comment `a`
a = 1
# comment `b`
b = [2, 3, 4]"
.to_string();
assert_eq!(res, text);
let test1: Test = toml::from_str(&text).unwrap();
assert_eq!(test, test1);
}
#[test]
fn test_array() {
#[derive(Debug, Clone, TomlInput, Serialize, Deserialize, PartialEq, Default)]
struct Test {
a: i32,
b: Vec<usize>,
}
#[derive(Debug, Clone, TomlInput, Serialize, Default, Deserialize, PartialEq)]
struct Test1 {
c: i32,
d: Vec<Test>,
}
let test2 = Test {
a: 1,
b: vec![2, 3, 4],
};
let test3 = Test {
a: 5,
b: vec![6, 7, 8],
};
let test1 = Test1 {
c: 1,
d: vec![test2, test3],
};
let res = test1.clone().into_string().unwrap();
println!("{res}");
let text = "# comment `Test1`
# comment `c`
c = 1
# comment `d`
[[d]]
# comment `a`
a = 1
# comment `b`
b = [2, 3, 4]
# comment `d`
[[d]]
# comment `a`
a = 5
# comment `b`
b = [6, 7, 8]"
.to_string();
assert_eq!(res, text);
let test2: Test1 = toml::from_str(&text).unwrap();
assert_eq!(test1, test2);
}
#[test]
fn test_array_empty() {
#[derive(Debug, Clone, TomlInput, Serialize, Deserialize, PartialEq, Default)]
struct Test {
a: i32,
b: Vec<usize>,
}
let test = Test { a: 1, b: vec![] };
let res = test.clone().into_string().unwrap();
println!("{res}");
let text = "# comment `Test`
# comment `a`
a = 1
# comment `b`
b = []"
.to_string();
assert_eq!(res, text);
let test1: Test = toml::from_str(&text).unwrap();
assert_eq!(test, test1);
}
#[test]
fn test_nested() {
#[derive(Debug, Clone, TomlInput, Serialize, Deserialize, PartialEq, Default)]
struct Test {
a: i32,
b: Vec<usize>,
}
#[derive(Debug, Clone, TomlInput, Serialize, Default, Deserialize, PartialEq)]
struct Test1 {
c: i32,
d: Test,
e: Option<String>,
}
let test2 = Test {
a: 1,
b: vec![2, 3, 4],
};
let test1 = Test1 {
c: 1,
d: test2,
e: None,
};
let res = test1.clone().into_string().unwrap();
let text = r#"# comment `Test1`
# comment `c`
c = 1
# comment `e`
#!e = ""
# comment `d`
[d]
# comment `a`
a = 1
# comment `b`
b = [2, 3, 4]"#
.to_string();
assert_eq!(res, text);
let test2: Test1 = toml::from_str(&text).unwrap();
assert_eq!(test1, test2);
}
#[test]
fn test_section_comment() {
#[derive(Debug, Clone, TomlInput, Serialize, Deserialize, PartialEq, Default)]
struct Test {
a: i32,
b: Option<Test1>,
}
#[derive(Debug, Clone, TomlInput, Serialize, Default, Deserialize, PartialEq)]
struct Test1 {
c: i32,
}
let test = Test { a: 1, b: None };
let res = test.clone().into_string().unwrap();
println!("{res}");
let text = r#"# comment `Test`
# comment `a`
a = 1
# comment `b`
#![b]
# comment `c`
#!c = 0"#
.to_string();
assert_eq!(res, text);
let test1: Test = toml::from_str(&text).unwrap();
assert_eq!(test, test1);
}
#[test]
fn test_skip() {
#[derive(Debug, Clone, TomlInput, Serialize, Deserialize, PartialEq)]
#[serde(default)]
struct Test {
a: i32,
b: Option<usize>,
#[serde(skip)]
c: i32,
}
impl Default for Test {
fn default() -> Self {
Test {
a: 0,
b: Some(1),
c: 2,
}
}
}
let test = Test {
a: 2,
b: Some(3),
c: 2,
};
let res = test.clone().into_string().unwrap();
let text = "# comment `Test`
# comment `a`
a = 2
# comment `b`
b = 3"
.to_string();
assert_eq!(res, text);
let test1: Test = toml::from_str(&text).unwrap();
assert_eq!(test, test1);
}