1 extern crate yaml_rust;
2
3 use yaml_rust::{Yaml, YamlEmitter, YamlLoader};
4
roundtrip(original: &Yaml)5 fn roundtrip(original: &Yaml) {
6 let mut emitted = String::new();
7 YamlEmitter::new(&mut emitted).dump(original).unwrap();
8
9 let documents = YamlLoader::load_from_str(&emitted).unwrap();
10 println!("emitted {}", emitted);
11
12 assert_eq!(documents.len(), 1);
13 assert_eq!(documents[0], *original);
14 }
15
double_roundtrip(original: &str)16 fn double_roundtrip(original: &str) {
17 let parsed = YamlLoader::load_from_str(&original).unwrap();
18
19 let mut serialized = String::new();
20 YamlEmitter::new(&mut serialized).dump(&parsed[0]).unwrap();
21
22 let reparsed = YamlLoader::load_from_str(&serialized).unwrap();
23
24 assert_eq!(parsed, reparsed);
25 }
26
27 #[test]
test_escape_character()28 fn test_escape_character() {
29 let y = Yaml::String("\x1b".to_owned());
30 roundtrip(&y);
31 }
32
33 #[test]
test_colon_in_string()34 fn test_colon_in_string() {
35 let y = Yaml::String("x: %".to_owned());
36 roundtrip(&y);
37 }
38
39 #[test]
test_numberlike_strings()40 fn test_numberlike_strings() {
41 let docs = [
42 r#"x: "1234""#, r#"x: "01234""#, r#""1234""#,
43 r#""01234""#, r#"" 01234""#, r#""0x1234""#,
44 r#"" 0x1234""#,
45 ];
46
47 for doc in &docs {
48 roundtrip(&Yaml::String(doc.to_string()));
49 double_roundtrip(&doc);
50 }
51 }
52
53 /// Example from https://github.com/chyh1990/yaml-rust/issues/133
54 #[test]
test_issue133()55 fn test_issue133() {
56
57 let doc = YamlLoader::load_from_str("\"0x123\"").unwrap().pop().unwrap();
58 assert_eq!(doc, Yaml::String("0x123".to_string()));
59
60 let mut out_str = String::new();
61 YamlEmitter::new(&mut out_str).dump(&doc).unwrap();
62 let doc2 = YamlLoader::load_from_str(&out_str).unwrap().pop().unwrap();
63 assert_eq!(doc, doc2); // This failed because the type has changed to a number now
64
65 }
66