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
|
use serde::Deserialize;
use std::collections::HashMap;
use crate::de::from_str;
#[test]
fn deserialize_none() {
let result: Option<usize> = from_str("null").expect("should deserialize");
assert_eq!(None, result);
}
#[test]
fn deserialize_some() {
let result: Option<usize> = from_str("40 + 2").expect("should deserialize");
assert_eq!(Some(42), result);
}
#[test]
fn deserialize_string() {
let result: String = from_str(
r#"
let greeter = name: "Hello ${name}!";
in greeter "Slartibartfast"
"#,
)
.expect("should deserialize");
assert_eq!(result, "Hello Slartibartfast!");
}
#[test]
fn deserialize_empty_list() {
let result: Vec<usize> = from_str("[ ]").expect("should deserialize");
assert!(result.is_empty())
}
#[test]
fn deserialize_integer_list() {
let result: Vec<usize> =
from_str("builtins.map (n: n + 2) [ 21 40 67 ]").expect("should deserialize");
assert_eq!(result, vec![23, 42, 69]);
}
#[test]
fn deserialize_empty_map() {
let result: HashMap<String, usize> = from_str("{ }").expect("should deserialize");
assert!(result.is_empty());
}
#[test]
fn deserialize_integer_map() {
let result: HashMap<String, usize> = from_str("{ age = 40 + 2; }").expect("should deserialize");
assert_eq!(result.len(), 1);
assert_eq!(*result.get("age").unwrap(), 42);
}
#[test]
fn deserialize_struct() {
#[derive(Debug, Deserialize, PartialEq)]
struct Person {
name: String,
age: usize,
}
let result: Person = from_str(
r#"
{
name = "Slartibartfast";
age = 42;
}
"#,
)
.expect("should deserialize");
assert_eq!(
result,
Person {
name: "Slartibartfast".into(),
age: 42,
}
);
}
|