| 1 | // Allow unstable items until Rust hits 1.0 |
| 2 | |
| 3 | extern crate yaml; |
| 4 | |
| 5 | use yaml::constructor::YamlStandardData; |
| 6 | use yaml::ffi::YamlEncoding; |
| 7 | use std::io::Cursor; |
| 8 | |
| 9 | macro_rules! test_utf8{ |
| 10 | ($filename: expr, $expected: expr) => ( |
| 11 | test_file!(yaml::ffi::YamlEncoding::YamlUtf8Encoding, $filename, $expected) |
| 12 | ) |
| 13 | } |
| 14 | |
| 15 | macro_rules! test_file{ |
| 16 | ($encoding: expr, $filename: expr, $expected: expr) => ( |
| 17 | { |
| 18 | let data: &[u8] = include_bytes!($filename); |
| 19 | let mut reader = Cursor::new(data); |
| 20 | match yaml::parse_io(&mut reader, $encoding) { |
| 21 | Ok(docs) => if docs.len() == 1 { |
| 22 | assert_eq!(docs[..].first().unwrap(), &$expected) |
| 23 | } else { |
| 24 | panic!("too many number of documents: {:?}", docs) |
| 25 | }, |
| 26 | Err(e) => panic!("parse failure: {:?}", e) |
| 27 | } |
| 28 | } |
| 29 | ) |
| 30 | } |
| 31 | |
| 32 | macro_rules! ystr{ |
| 33 | ($e: expr) => ( |
| 34 | YamlStandardData::YamlString($e.to_string()) |
| 35 | ) |
| 36 | } |
| 37 | |
| 38 | macro_rules! yint{ |
| 39 | ($e: expr) => ( |
| 40 | YamlStandardData::YamlInteger($e) |
| 41 | ) |
| 42 | } |
| 43 | |
| 44 | macro_rules! yfloat{ |
| 45 | ($e: expr) => ( |
| 46 | YamlStandardData::YamlFloat($e) |
| 47 | ) |
| 48 | } |
| 49 | |
| 50 | macro_rules! yseq{ |
| 51 | ($($e:expr),*) => ( |
| 52 | YamlStandardData::YamlSequence(vec![$(($e),)*]) |
| 53 | ) |
| 54 | } |
| 55 | |
| 56 | macro_rules! ymap{ |
| 57 | ($($k:expr => $v:expr),*) => ( |
| 58 | YamlStandardData::YamlMapping(vec![$((ystr!($k), $v),)*]) |
| 59 | ) |
| 60 | } |
| 61 | |
| 62 | macro_rules! y_cmp_map{ |
| 63 | ($($k:expr => $v:expr),*) => ( |
| 64 | YamlStandardData::YamlMapping(vec![$(($k, $v),)*]) |
| 65 | ) |
| 66 | } |
| 67 | |
| 68 | #[test] |
| 69 | fn sequence_of_scalars() { |
| 70 | test_utf8!("source/ball_players.yml", yseq![ystr!("Mark McGwire"), ystr!("Sammy Sosa"), ystr!("Ken Griffey")]); |
| 71 | } |
| 72 | |
| 73 | #[test] |
| 74 | fn scalar_mappings() { |
| 75 | test_utf8!("source/player_stat.yml", ymap!{ |
| 76 | "hr" => yint!(65), |
| 77 | "avg" => yfloat!(0.278), |
| 78 | "rbi" => yint!(147) |
| 79 | }) |
| 80 | } |
| 81 | |
| 82 | #[test] |
| 83 | fn maps_of_sequences() { |
| 84 | test_utf8!("source/ball_clubs.yml", ymap!{ |
| 85 | "american" => yseq![ystr!("Boston Red Sox"), ystr!("Detroit Tigers"), ystr!("New York Yankees")], |
| 86 | "national" => yseq![ystr!("New York Mets"), ystr!("Chicago Cubs"), ystr!("Atlanta Braves")] |
| 87 | }) |
| 88 | } |
| 89 | |
| 90 | #[test] |
| 91 | fn sequence_of_maps() { |
| 92 | test_utf8!("source/multiple_player_stat.yml", |
| 93 | yseq![ |
| 94 | ymap!{ |
| 95 | "name" => ystr!("Mark McGwire"), |
| 96 | "hr" => yint!(65) |
| 97 | }, |
| 98 | ymap!{ |
| 99 | "name" => ystr!("Sammy Sosa"), |
| 100 | "hr" => yint!(63) |
| 101 | } |
| 102 | ]) |
| 103 | } |
| 104 | |
| 105 | #[test] |
| 106 | fn sequence_of_sequences() { |
| 107 | test_utf8!("source/csv.yml", |
| 108 | yseq![ |
| 109 | yseq![ystr!("name"), ystr!("hr")], |
| 110 | yseq![ystr!("Mark McGwire"), yint!(65)], |
| 111 | yseq![ystr!("Sammy Sosa"), yint!(63)] |
| 112 | ]) |
| 113 | } |
| 114 | |
| 115 | #[test] |
| 116 | fn mapping_of_mappings() { |
| 117 | test_utf8!("source/map_map.yml", |
| 118 | ymap!{ |
| 119 | "Mark McGwire" => ymap!{ "hr" => yint!(65) }, |
| 120 | "Sammy Sosa" => ymap!{ "hr" => yint!(63) } |
| 121 | }) |
| 122 | } |
| 123 | |
| 124 | #[test] |
| 125 | fn alias() { |
| 126 | test_utf8!("source/alias.yml", |
| 127 | ymap!{ |
| 128 | "hr" => yseq![ystr!("Mark McGwire"), ystr!("Sammy Sosa")], |
| 129 | "rbi" => yseq![ystr!("Sammy Sosa"), ystr!("Ken Griffey")] |
| 130 | }) |
| 131 | } |
| 132 | |
| 133 | #[test] |
| 134 | fn complex_keys() { |
| 135 | test_utf8!("source/complex_key.yml", |
| 136 | y_cmp_map!{ |
| 137 | yseq![ystr!("Detroit Tigers"), ystr!("Chicago Cubs")] => yseq![ystr!("2001-07-23")], |
| 138 | yseq![ystr!("New York Yankees"), ystr!("Atlanta Braves")] => yseq![ystr!("2001-07-02"), ystr!("2001-08-12"), ystr!("2001-08-14")] |
| 139 | }) |
| 140 | } |
| 141 | |
| 142 | #[test] |
| 143 | fn block_literal() { |
| 144 | test_utf8!("source/block_literal.yml", ystr!("\\//||\\/||\n// || ||__\n")) |
| 145 | } |
| 146 | |
| 147 | #[test] |
| 148 | fn plain_scalar() { |
| 149 | test_utf8!("source/plain_scalar.yml", ystr!("Mark McGwire's year was crippled by a knee injury.")) |
| 150 | } |
| 151 | |
| 152 | #[test] |
| 153 | fn quoted_scalar() { |
| 154 | test_utf8!("source/quoted_scalar.yml", |
| 155 | ymap!{ |
| 156 | "unicode" => ystr!("Sosa did fine.\u{263A}"), |
| 157 | "control" => ystr!("\x081998\t1999\t2000\n"), |
| 158 | "hexesc" => ystr!("\x13\x10 is \r\n"), |
| 159 | "single" => ystr!(r#""Howdy!" he cried."#), |
| 160 | "quoted" => ystr!(" # not a 'comment'."), |
| 161 | "tie-fighter" => ystr!(r"|\-*-/|") |
| 162 | }) |
| 163 | } |
| 164 | |
| 165 | #[test] |
| 166 | fn multi_line_scalar() { |
| 167 | test_utf8!("source/multi_line_scalar.yml", |
| 168 | ymap!{ |
| 169 | "plain" => ystr!("This unquoted scalar spans many lines."), |
| 170 | "quoted" => ystr!("So does this quoted scalar.\n") |
| 171 | }) |
| 172 | } |
| 173 | |
| 174 | #[test] |
| 175 | fn utf16le() { |
| 176 | test_file!(YamlEncoding::YamlUtf16LeEncoding, "source/utf16le.yml", |
| 177 | yseq![ystr!("Hello"), ystr!("世界")] |
| 178 | ) |
| 179 | } |
| 180 | |
| 181 | #[test] |
| 182 | fn utf16be() { |
| 183 | test_file!(YamlEncoding::YamlUtf16BeEncoding, "source/utf16be.yml", |
| 184 | yseq![ystr!("Hello"), ystr!("世界")] |
| 185 | ) |
| 186 | } |