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