Import toml 0.7.3
[platform/upstream/rust-toml.git] / tests / testsuite / tables_last.rs
1 use std::collections::HashMap;
2
3 use serde::Deserialize;
4 use serde::Serialize;
5
6 #[test]
7 fn always_works() {
8     // Ensure this works without the removed "toml::ser::tables_last"
9     #[derive(Serialize)]
10     struct A {
11         vals: HashMap<&'static str, Value>,
12     }
13
14     #[derive(Serialize)]
15     #[serde(untagged)]
16     enum Value {
17         Map(HashMap<&'static str, &'static str>),
18         Int(i32),
19     }
20
21     let mut a = A {
22         vals: HashMap::new(),
23     };
24     a.vals.insert("foo", Value::Int(0));
25
26     let mut sub = HashMap::new();
27     sub.insert("foo", "bar");
28     a.vals.insert("bar", Value::Map(sub));
29
30     toml::to_string(&a).unwrap();
31 }
32
33 #[test]
34 fn vec_of_vec_issue_387() {
35     #[derive(Deserialize, Serialize, Debug)]
36     struct Glyph {
37         components: Vec<Component>,
38         contours: Vec<Contour>,
39     }
40
41     #[derive(Deserialize, Serialize, Debug)]
42     struct Point {
43         x: f64,
44         y: f64,
45         pt_type: String,
46     }
47
48     type Contour = Vec<Point>;
49
50     #[derive(Deserialize, Serialize, Debug)]
51     struct Component {
52         base: String,
53         transform: (f64, f64, f64, f64, f64, f64),
54     }
55
56     let comp1 = Component {
57         base: "b".to_string(),
58         transform: (1.0, 0.0, 0.0, 1.0, 0.0, 0.0),
59     };
60     let comp2 = Component {
61         base: "c".to_string(),
62         transform: (1.0, 0.0, 0.0, 1.0, 0.0, 0.0),
63     };
64     let components = vec![comp1, comp2];
65
66     let contours = vec![
67         vec![
68             Point {
69                 x: 3.0,
70                 y: 4.0,
71                 pt_type: "line".to_string(),
72             },
73             Point {
74                 x: 5.0,
75                 y: 6.0,
76                 pt_type: "line".to_string(),
77             },
78         ],
79         vec![
80             Point {
81                 x: 0.0,
82                 y: 0.0,
83                 pt_type: "move".to_string(),
84             },
85             Point {
86                 x: 7.0,
87                 y: 9.0,
88                 pt_type: "offcurve".to_string(),
89             },
90             Point {
91                 x: 8.0,
92                 y: 10.0,
93                 pt_type: "offcurve".to_string(),
94             },
95             Point {
96                 x: 11.0,
97                 y: 12.0,
98                 pt_type: "curve".to_string(),
99             },
100         ],
101     ];
102     let g1 = Glyph {
103         contours,
104         components,
105     };
106
107     let s = toml::to_string_pretty(&g1).unwrap();
108     let _g2: Glyph = toml::from_str(&s).unwrap();
109 }
110
111 #[test]
112 fn vec_order_issue_356() {
113     #[derive(Serialize, Deserialize)]
114     struct Outer {
115         v1: Vec<Inner>,
116         v2: Vec<Inner>,
117     }
118
119     #[derive(Serialize, Deserialize)]
120     struct Inner {}
121
122     let outer = Outer {
123         v1: vec![Inner {}],
124         v2: vec![],
125     };
126     let s = toml::to_string_pretty(&outer).unwrap();
127     let _o: Outer = toml::from_str(&s).unwrap();
128 }
129
130 #[test]
131 fn values_before_tables_issue_403() {
132     #[derive(Serialize, Deserialize)]
133     struct A {
134         a: String,
135         b: String,
136     }
137
138     #[derive(Serialize, Deserialize)]
139     struct B {
140         a: String,
141         b: Vec<String>,
142     }
143
144     #[derive(Serialize, Deserialize)]
145     struct C {
146         a: A,
147         b: Vec<String>,
148         c: Vec<B>,
149     }
150     toml::to_string(&C {
151         a: A {
152             a: "aa".to_string(),
153             b: "ab".to_string(),
154         },
155         b: vec!["b".to_string()],
156         c: vec![B {
157             a: "cba".to_string(),
158             b: vec!["cbb".to_string()],
159         }],
160     })
161     .unwrap();
162 }