1 use serde::ser::Serialize;
2 use snapbox::assert_eq;
3
4 const NO_PRETTY: &str = "\
5 [example]
6 array = [\"item 1\", \"item 2\"]
7 empty = []
8 oneline = \"this has no newlines.\"
9 text = '''
10
11 this is the first line\\nthis is the second line
12 '''
13 ";
14
15 #[test]
no_pretty()16 fn no_pretty() {
17 let toml = NO_PRETTY;
18 let value: toml::Value = toml::from_str(toml).unwrap();
19 let mut result = String::with_capacity(128);
20 value.serialize(toml::Serializer::new(&mut result)).unwrap();
21 assert_eq(toml, &result);
22 }
23
24 const PRETTY_STD: &str = "\
25 [example]
26 array = [
27 \"item 1\",
28 \"item 2\",
29 ]
30 empty = []
31 one = [\"one\"]
32 oneline = \"this has no newlines.\"
33 text = \"\"\"
34 this is the first line
35 this is the second line
36 \"\"\"
37 ";
38
39 #[test]
pretty_std()40 fn pretty_std() {
41 let toml = PRETTY_STD;
42 let value: toml::Value = toml::from_str(toml).unwrap();
43 let mut result = String::with_capacity(128);
44 value
45 .serialize(toml::Serializer::pretty(&mut result))
46 .unwrap();
47 assert_eq(toml, &result);
48 }
49
50 const PRETTY_TRICKY: &str = r#"[example]
51 f = "\f"
52 glass = """
53 Nothing too unusual, except that I can eat glass in:
54 - Greek: Μπορώ να φάω σπασμένα γυαλιά χωρίς να πάθω τίποτα.
55 - Polish: Mogę jeść szkło, i mi nie szkodzi.
56 - Hindi: मैं काँच खा सकता हूँ, मुझे उस से कोई पीडा नहीं होती.
57 - Japanese: 私はガラスを食べられます。それは私を傷つけません。
58 """
59 r = "\r"
60 r_newline = """
61 \r
62 """
63 single = "this is a single line but has '' cuz it's tricky"
64 single_tricky = "single line with ''' in it"
65 tabs = """
66 this is pretty standard
67 \texcept for some \ttabs right here
68 """
69 text = """
70 this is the first line.
71 This has a ''' in it and \"\"\" cuz it's tricky yo
72 Also ' and \" because why not
73 this is the fourth line
74 """
75 "#;
76
77 #[test]
pretty_tricky()78 fn pretty_tricky() {
79 let toml = PRETTY_TRICKY;
80 let value: toml::Value = toml::from_str(toml).unwrap();
81 let mut result = String::with_capacity(128);
82 value
83 .serialize(toml::Serializer::pretty(&mut result))
84 .unwrap();
85 assert_eq(toml, &result);
86 }
87
88 const PRETTY_TABLE_ARRAY: &str = r#"[[array]]
89 key = "foo"
90
91 [[array]]
92 key = "bar"
93
94 [abc]
95 doc = "this is a table"
96
97 [example]
98 single = "this is a single line string"
99 "#;
100
101 #[test]
pretty_table_array()102 fn pretty_table_array() {
103 let toml = PRETTY_TABLE_ARRAY;
104 let value: toml::Value = toml::from_str(toml).unwrap();
105 let mut result = String::with_capacity(128);
106 value
107 .serialize(toml::Serializer::pretty(&mut result))
108 .unwrap();
109 assert_eq(toml, &result);
110 }
111
112 const TABLE_ARRAY: &str = r#"[[array]]
113 key = "foo"
114
115 [[array]]
116 key = "bar"
117
118 [abc]
119 doc = "this is a table"
120
121 [example]
122 single = "this is a single line string"
123 "#;
124
125 #[test]
table_array()126 fn table_array() {
127 let toml = TABLE_ARRAY;
128 let value: toml::Value = toml::from_str(toml).unwrap();
129 let mut result = String::with_capacity(128);
130 value.serialize(toml::Serializer::new(&mut result)).unwrap();
131 assert_eq(toml, &result);
132 }
133
134 const PRETTY_EMPTY_TABLE: &str = r#"[example]
135 "#;
136
137 #[test]
pretty_empty_table()138 fn pretty_empty_table() {
139 let toml = PRETTY_EMPTY_TABLE;
140 let value: toml::Value = toml::from_str(toml).unwrap();
141 let mut result = String::with_capacity(128);
142 value.serialize(toml::Serializer::new(&mut result)).unwrap();
143 assert_eq(toml, &result);
144 }
145
146 #[test]
error_includes_key()147 fn error_includes_key() {
148 #[derive(Debug, serde::Serialize, serde::Deserialize)]
149 struct Package {
150 name: String,
151 version: String,
152 authors: Vec<String>,
153 profile: Profile,
154 }
155
156 #[derive(Debug, serde::Serialize, serde::Deserialize)]
157 struct Profile {
158 dev: Dev,
159 }
160
161 #[derive(Debug, serde::Serialize, serde::Deserialize)]
162 struct Dev {
163 debug: U32OrBool,
164 }
165
166 #[derive(Clone, Debug, serde::Deserialize, serde::Serialize, Eq, PartialEq)]
167 #[serde(untagged, expecting = "expected a boolean or an integer")]
168 pub enum U32OrBool {
169 U32(u32),
170 Bool(bool),
171 }
172
173 let raw = r#"name = "foo"
174 version = "0.0.0"
175 authors = []
176
177 [profile.dev]
178 debug = true
179 "#;
180
181 let pkg: Package = toml::from_str(raw).unwrap();
182 let pretty = toml::to_string_pretty(&pkg).unwrap();
183 assert_eq(raw, pretty);
184 }
185