• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 use snapbox::assert_data_eq;
2 use snapbox::prelude::*;
3 use snapbox::str;
4 
5 use toml_edit::{DocumentMut, Item, Value};
6 
7 #[test]
table_into_inline()8 fn table_into_inline() {
9     let toml = r#"
10 [table]
11 string = "value"
12 array = [1, 2, 3]
13 inline = { "1" = 1, "2" = 2 }
14 
15 [table.child]
16 other = "world"
17 "#;
18     let mut doc = toml.parse::<DocumentMut>().unwrap();
19 
20     doc.get_mut("table").unwrap().make_value();
21 
22     let actual = doc.to_string();
23     // `table=` is because we didn't re-format the table key, only the value
24     assert_data_eq!(actual, str![[r#"
25 table= { string = "value", array = [1, 2, 3], inline = { "1" = 1, "2" = 2 }, child = { other = "world" } }
26 
27 "#]].raw());
28 }
29 
30 #[test]
inline_table_to_table()31 fn inline_table_to_table() {
32     let toml = r#"table = { string = "value", array = [1, 2, 3], inline = { "1" = 1, "2" = 2 }, child = { other = "world" } }
33 "#;
34     let mut doc = toml.parse::<DocumentMut>().unwrap();
35 
36     let t = doc.remove("table").unwrap();
37     let t = match t {
38         Item::Value(Value::InlineTable(t)) => t,
39         _ => unreachable!("Unexpected {:?}", t),
40     };
41     let t = t.into_table();
42     doc.insert("table", Item::Table(t));
43 
44     let actual = doc.to_string();
45     assert_data_eq!(
46         actual,
47         str![[r#"
48 [table]
49 string = "value"
50 array = [1, 2, 3]
51 inline = { "1" = 1, "2" = 2 }
52 child = { other = "world" }
53 
54 "#]]
55         .raw()
56     );
57 }
58 
59 #[test]
array_of_tables_to_array()60 fn array_of_tables_to_array() {
61     let toml = r#"
62 [[table]]
63 string = "value"
64 array = [1, 2, 3]
65 inline = { "1" = 1, "2" = 2 }
66 
67 [table.child]
68 other = "world"
69 
70 [[table]]
71 string = "value"
72 array = [1, 2, 3]
73 inline = { "1" = 1, "2" = 2 }
74 
75 [table.child]
76 other = "world"
77 "#;
78     let mut doc = toml.parse::<DocumentMut>().unwrap();
79 
80     doc.get_mut("table").unwrap().make_value();
81 
82     let actual = doc.to_string();
83     // `table=` is because we didn't re-format the table key, only the value
84     assert_data_eq!(actual, str![[r#"
85 table= [{ string = "value", array = [1, 2, 3], inline = { "1" = 1, "2" = 2 }, child = { other = "world" } }, { string = "value", array = [1, 2, 3], inline = { "1" = 1, "2" = 2 }, child = { other = "world" } }]
86 
87 "#]].raw());
88 }
89