1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
#[cfg(test)]
mod tests {
use serde_yml::value::{Mapping, Value};
use std::borrow::Cow;
// Conversion tests for non-numeric types
#[test]
fn test_from_bool() {
// Verify conversion from bool to Value
// Given a bool value,
let b = false;
// When converting it to Value,
let x: Value = b.into();
// Then it should be converted correctly.
assert_eq!(x, Value::Bool(false));
}
#[test]
fn test_from_string() {
// Verify conversion from String to Value
// Given a String,
let s: String = "lorem".to_string();
// When converting it to Value,
let x: Value = s.into();
// Then it should be converted correctly.
assert_eq!(x, Value::String("lorem".to_string()));
}
#[test]
fn test_from_str() {
// Verify conversion from &str to Value
// Given a string slice,
let s: &str = "lorem";
// When converting it to Value,
let x: Value = s.into();
// Then it should be converted correctly.
assert_eq!(x, Value::String("lorem".to_string()));
}
#[test]
fn test_from_cow_borrowed() {
// Verify conversion from Cow<str> (borrowed) to Value
// Given a borrowed Cow<str>,
let s: Cow<'_, str> = Cow::Borrowed("lorem");
// When converting it to Value,
let x: Value = s.into();
// Then it should be converted correctly.
assert_eq!(x, Value::String("lorem".to_string()));
}
#[test]
fn test_from_cow_owned() {
// Verify conversion from Cow<str> (owned) to Value
// Given an owned Cow<str>,
let s: Cow<'_, str> = Cow::Owned("lorem".to_string());
// When converting it to Value,
let x: Value = s.into();
// Then it should be converted correctly.
assert_eq!(x, Value::String("lorem".to_string()));
}
#[test]
fn test_from_mapping() {
// Verify conversion from Mapping to Value
// Given a Mapping,
let mut m = Mapping::new();
m.insert("Lorem".into(), "ipsum".into());
// When converting it to Value,
let x: Value = m.into();
// Then it should be converted correctly.
assert_eq!(
x,
Value::Mapping(Mapping::from_iter(vec![(
"Lorem".into(),
"ipsum".into()
)]))
);
}
#[test]
fn test_from_vec() {
// Verify conversion from Vec to Value
// Given a Vec,
let v = vec!["lorem", "ipsum", "dolor"];
// When converting it to Value,
let x: Value = v.into();
// Then it should be converted correctly.
assert_eq!(
x,
Value::Sequence(vec![
"lorem".into(),
"ipsum".into(),
"dolor".into()
])
);
}
#[test]
fn test_from_slice() {
// Verify conversion from slice to Value
// Given a slice,
let v: &[&str] = &["lorem", "ipsum", "dolor"];
// When converting it to Value,
let x: Value = v.into();
// Then it should be converted correctly.
assert_eq!(
x,
Value::Sequence(vec![
"lorem".into(),
"ipsum".into(),
"dolor".into()
])
);
}
#[test]
fn test_from_iterator() {
// Verify conversion from iterator to Value
// Given an iterator that repeats a value,
let v = std::iter::repeat(42).take(5);
// When collecting it into Value,
let x: Value = v.collect();
// Then it should be converted correctly.
assert_eq!(
x,
Value::Sequence(vec![
42.into(),
42.into(),
42.into(),
42.into(),
42.into()
])
);
// Given a Vec,
let v: Vec<_> = vec!["lorem", "ipsum", "dolor"];
// When converting it to Value,
let x: Value = v.into_iter().collect();
// Then it should be converted correctly.
assert_eq!(
x,
Value::Sequence(vec![
"lorem".into(),
"ipsum".into(),
"dolor".into()
])
);
// Given values to collect,
let x: Value =
Value::from_iter(vec!["lorem", "ipsum", "dolor"]);
// Then they should be converted correctly.
assert_eq!(
x,
Value::Sequence(vec![
"lorem".into(),
"ipsum".into(),
"dolor".into()
])
);
}
// Conversion tests for numeric types
#[test]
fn test_from_number_i8() {
// Verify conversion from i8 to Value
// Given an i8 value,
let n: i8 = 42;
// When converting it to Value,
let x: Value = n.into();
// Then it should be converted correctly.
assert_eq!(x, Value::Number(42.into()));
}
#[test]
fn test_from_number_i16() {
// Verify conversion from i16 to Value
// Given an i16 value,
let n: i16 = 42;
// When converting it to Value,
let x: Value = n.into();
// Then it should be converted correctly.
assert_eq!(x, Value::Number(42.into()));
}
#[test]
fn test_from_number_i32() {
// Verify conversion from i32 to Value
// Given an i32 value,
let n: i32 = 42;
// When converting it to Value,
let x: Value = n.into();
// Then it should be converted correctly.
assert_eq!(x, Value::Number(42.into()));
}
#[test]
fn test_from_number_i64() {
// Verify conversion from i64 to Value
// Given an i64 value,
let n: i64 = 42;
// When converting it to Value,
let x: Value = n.into();
// Then it should be converted correctly.
assert_eq!(x, Value::Number(42.into()));
}
#[test]
fn test_from_number_isize() {
// Verify conversion from isize to Value
// Given an isize value,
let n: isize = 42;
// When converting it to Value,
let x: Value = n.into();
// Then it should be converted correctly.
assert_eq!(x, Value::Number(42.into()));
}
#[test]
fn test_from_number_u8() {
// Verify conversion from u8 to Value
// Given a u8 value,
let n: u8 = 42;
// When converting it to Value,
let x: Value = n.into();
// Then it should be converted correctly.
assert_eq!(x, Value::Number(42.into()));
}
#[test]
fn test_from_number_u16() {
// Verify conversion from u16 to Value
// Given a u16 value,
let n: u16 = 42;
// When converting it to Value,
let x: Value = n.into();
// Then it should be converted correctly.
assert_eq!(x, Value::Number(42.into()));
}
#[test]
fn test_from_number_u32() {
// Verify conversion from u32 to Value
// Given a u32 value,
let n: u32 = 42;
// When converting it to Value,
let x: Value = n.into();
// Then it should be converted correctly.
assert_eq!(x, Value::Number(42.into()));
}
#[test]
fn test_from_number_u64() {
// Verify conversion from u64 to Value
// Given a u64 value,
let n: u64 = 42;
// When converting it to Value,
let x: Value = n.into();
// Then it should be converted correctly.
assert_eq!(x, Value::Number(42.into()));
}
#[test]
fn test_from_number_usize() {
// Verify conversion from usize to Value
// Given a usize value,
let n: usize = 42;
// When converting it to Value,
let x: Value = n.into();
// Then it should be converted correctly.
assert_eq!(x, Value::Number(42.into()));
}
#[test]
fn test_from_number_f32() {
// Verify conversion from f32 to Value
// Given an f32 value,
let n: f32 = 42.5;
// When converting it to Value,
let x: Value = n.into();
// Then it should be converted correctly.
assert_eq!(x, Value::Number(42.5.into()));
}
#[test]
fn test_from_number_f64() {
// Verify conversion from f64 to Value
// Given an f64 value,
let n: f64 = 42.5;
// When converting it to Value,
let x: Value = n.into();
// Then it should be converted correctly.
assert_eq!(x, Value::Number(42.5.into()));
}
}