1
use std::f64;
2

            
3
use ron::{
4
    error::Error,
5
    value::{Map, Number, Value},
6
};
7
use serde_derive::{Deserialize, Serialize};
8

            
9
#[test]
10
4
fn bool() {
11
4
    assert_eq!("true".parse(), Ok(Value::Bool(true)));
12
4
    assert_eq!("false".parse(), Ok(Value::Bool(false)));
13

            
14
4
    assert_eq!(ron::to_string(&Value::Bool(true)).unwrap(), "true");
15
4
    assert_eq!(ron::to_string(&Value::Bool(false)).unwrap(), "false");
16
4
}
17

            
18
#[test]
19
4
fn char() {
20
4
    assert_eq!("'a'".parse(), Ok(Value::Char('a')));
21

            
22
4
    assert_eq!(ron::to_string(&Value::Char('a')).unwrap(), "'a'");
23
4
}
24

            
25
#[test]
26
4
fn map() {
27
4
    let mut map = Map::new();
28
4
    map.insert(Value::Char('a'), Value::Number(Number::U8(1)));
29
4
    map.insert(Value::Char('b'), Value::Number(Number::new(2f32)));
30
4
    let map = Value::Map(map);
31
4

            
32
4
    assert_eq!(ron::to_string(&map).unwrap(), "{'a':1,'b':2.0}");
33

            
34
4
    assert_eq!("{ 'a': 1, 'b': 2.0 }".parse(), Ok(map));
35
4
}
36

            
37
#[test]
38
4
fn number() {
39
4
    assert_eq!("42".parse(), Ok(Value::Number(Number::U8(42))));
40
4
    assert_eq!(
41
4
        "3.141592653589793".parse(),
42
4
        Ok(Value::Number(Number::new(f64::consts::PI)))
43
4
    );
44

            
45
4
    assert_eq!(
46
4
        ron::to_string(&Value::Number(Number::U8(42))).unwrap(),
47
4
        "42"
48
4
    );
49
4
    assert_eq!(
50
4
        ron::to_string(&Value::Number(Number::F64(f64::consts::PI.into()))).unwrap(),
51
4
        "3.141592653589793"
52
4
    );
53
4
}
54

            
55
#[test]
56
4
fn option() {
57
4
    let opt = Some(Box::new(Value::Char('c')));
58
4
    assert_eq!("Some('c')".parse(), Ok(Value::Option(opt)));
59
4
    assert_eq!("None".parse(), Ok(Value::Option(None)));
60

            
61
4
    assert_eq!(
62
4
        ron::to_string(&Value::Option(Some(Box::new(Value::Char('c'))))).unwrap(),
63
4
        "Some('c')"
64
4
    );
65
4
    assert_eq!(ron::to_string(&Value::Option(None)).unwrap(), "None");
66
4
}
67

            
68
#[test]
69
4
fn string() {
70
4
    let normal = "\"String\"";
71
4
    assert_eq!(normal.parse(), Ok(Value::String("String".into())));
72
4
    assert_eq!(
73
4
        ron::to_string(&Value::String("String".into())).unwrap(),
74
4
        "\"String\""
75
4
    );
76

            
77
4
    let raw = "r\"Raw String\"";
78
4
    assert_eq!(raw.parse(), Ok(Value::String("Raw String".into())));
79

            
80
4
    let raw_hashes = "r#\"Raw String\"#";
81
4
    assert_eq!(raw_hashes.parse(), Ok(Value::String("Raw String".into())));
82

            
83
4
    let raw_escaped = "r##\"Contains \"#\"##";
84
4
    assert_eq!(
85
4
        raw_escaped.parse(),
86
4
        Ok(Value::String("Contains \"#".into()))
87
4
    );
88

            
89
4
    let raw_multi_line = "r\"Multi\nLine\"";
90
4
    assert_eq!(
91
4
        raw_multi_line.parse(),
92
4
        Ok(Value::String("Multi\nLine".into()))
93
4
    );
94
4
    assert_eq!(
95
4
        ron::to_string(&Value::String("Multi\nLine".into())).unwrap(),
96
4
        "\"Multi\\nLine\""
97
4
    );
98
4
}
99

            
100
#[test]
101
4
fn byte_string() {
102
4
    assert_eq!(
103
4
        "b\"\\x01\\u{2}\\0\\x04\"".parse(),
104
4
        Ok(Value::Bytes(vec![1, 2, 0, 4]))
105
4
    );
106
4
    assert_eq!(
107
4
        ron::to_string(&Value::Bytes(vec![1, 2, 0, 4])).unwrap(),
108
4
        "b\"\\x01\\x02\\x00\\x04\""
109
4
    );
110
4
}
111

            
112
#[test]
113
4
fn seq() {
114
4
    let seq = Value::Seq(vec![
115
4
        Value::Number(Number::U8(1)),
116
4
        Value::Number(Number::new(2f32)),
117
4
    ]);
118
4
    assert_eq!(ron::to_string(&seq).unwrap(), "[1,2.0]");
119
4
    assert_eq!("[1, 2.0]".parse(), Ok(seq));
120

            
121
4
    let err = Value::Seq(vec![Value::Number(Number::new(1))])
122
4
        .into_rust::<[i32; 2]>()
123
4
        .unwrap_err();
124
4

            
125
4
    assert_eq!(
126
4
        err,
127
4
        Error::ExpectedDifferentLength {
128
4
            expected: String::from("an array of length 2"),
129
4
            found: 1,
130
4
        }
131
4
    );
132

            
133
4
    let err = Value::Seq(vec![
134
4
        Value::Number(Number::new(1)),
135
4
        Value::Number(Number::new(2)),
136
4
        Value::Number(Number::new(3)),
137
4
    ])
138
4
    .into_rust::<[i32; 2]>()
139
4
    .unwrap_err();
140
4

            
141
4
    assert_eq!(
142
4
        err,
143
4
        Error::ExpectedDifferentLength {
144
4
            expected: String::from("a sequence of length 2"),
145
4
            found: 3,
146
4
        }
147
4
    );
148
4
}
149

            
150
#[test]
151
4
fn unit() {
152
    use ron::error::{Error, Position, SpannedError};
153

            
154
4
    assert_eq!("()".parse(), Ok(Value::Unit));
155
4
    assert_eq!("Foo".parse(), Ok(Value::Unit));
156

            
157
4
    assert_eq!(
158
4
        "".parse::<Value>(),
159
4
        Err(SpannedError {
160
4
            code: Error::Eof,
161
4
            position: Position { col: 1, line: 1 }
162
4
        })
163
4
    );
164

            
165
4
    assert_eq!(ron::to_string(&Value::Unit).unwrap(), "()");
166
4
}
167

            
168
4
#[derive(Serialize, Deserialize, PartialEq, Eq, Debug)]
169
struct Scene(Option<(u32, u32)>);
170

            
171
8
#[derive(Serialize, Deserialize, PartialEq, Eq, Debug)]
172
struct Scene2 {
173
    foo: Option<(u32, u32)>,
174
}
175

            
176
#[test]
177
4
fn roundtrip() {
178
    use ron::{de::from_str, ser::to_string};
179

            
180
    {
181
4
        let v = Scene2 {
182
4
            foo: Some((122, 13)),
183
4
        };
184
4
        let s = to_string(&v).unwrap();
185
4
        println!("{}", s);
186
4
        let val: Value = from_str(&s).unwrap();
187
4
        println!("{:?}", val);
188
4
        let v2 = val.into_rust::<Scene2>();
189
4
        assert_eq!(v2, Ok(v));
190
    }
191
    {
192
4
        let v = Scene(Some((13, 122)));
193
4
        let s = to_string(&v).unwrap();
194
4
        println!("{}", s);
195
4
        let val: Value = from_str(&s).unwrap();
196
4
        println!("{:?}", val);
197
4
        let v2 = val.into_rust::<Scene>();
198
4
        assert_eq!(v2, Ok(v));
199
    }
200
    {
201
4
        let v = (42,);
202
4
        let s = to_string(&v).unwrap();
203
4
        println!("{}", s);
204
4
        let val: Value = from_str(&s).unwrap();
205
4
        println!("{:?}", val);
206
4
        let v2 = val.into_rust::<(i32,)>();
207
4
        assert_eq!(v2, Ok(v));
208
    }
209
4
}
210

            
211
#[test]
212
4
fn map_roundtrip_338() {
213
4
    // https://github.com/ron-rs/ron/issues/338
214
4

            
215
4
    let v: Value = ron::from_str("{}").unwrap();
216
4
    println!("{:?}", v);
217
4

            
218
4
    let ser = ron::to_string(&v).unwrap();
219
4
    println!("{:?}", ser);
220
4

            
221
4
    let roundtrip = ron::from_str(&ser).unwrap();
222
4
    println!("{:?}", roundtrip);
223
4

            
224
4
    assert_eq!(v, roundtrip);
225
4
}