| 1 |
|
| 2 |
|
| 3 |
use one_ini::{parse, parse_to_vec, TokenTypes}; |
| 4 |
use std::str; |
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
#[test] |
| 9 |
fn no_whitespace() { |
| 10 |
compare("a=b", "a=b\n"); |
| 11 |
} |
| 12 |
|
| 13 |
#[test] |
| 14 |
fn trims_spaces_around_equals() { |
| 15 |
compare("a = b", "a=b\n"); |
| 16 |
} |
| 17 |
|
| 18 |
#[test] |
| 19 |
fn trims_multiple_spaces_around_equals() { |
| 20 |
compare("a = b", "a=b\n"); |
| 21 |
} |
| 22 |
|
| 23 |
#[test] |
| 24 |
fn trims_spaces_before_pair_key() { |
| 25 |
compare(" a=b", "a=b\n"); |
| 26 |
} |
| 27 |
|
| 28 |
#[test] |
| 29 |
fn trims_spaces_after_pair_value() { |
| 30 |
compare("a=b ", "a=b\n"); |
| 31 |
} |
| 32 |
|
| 33 |
#[test] |
| 34 |
fn removes_blank_lines_between_properties() { |
| 35 |
compare("\na=b\n\nc=d", "a=b\nc=d\n"); |
| 36 |
} |
| 37 |
|
| 38 |
#[test] |
| 39 |
fn includes_spaces_in_section_name() { |
| 40 |
compare("[ a b ]", "[ a b ]\n") |
| 41 |
} |
| 42 |
|
| 43 |
#[test] |
| 44 |
fn trims_spaces_before_section_name() { |
| 45 |
compare(" [a]", "[a]\n"); |
| 46 |
} |
| 47 |
|
| 48 |
#[test] |
| 49 |
fn trims_spaces_after_section_name() { |
| 50 |
compare("[a] ", "[a]\n"); |
| 51 |
} |
| 52 |
|
| 53 |
#[test] |
| 54 |
fn handles_nested_section_braces() { |
| 55 |
compare("[[a]]", "[[a]]\n"); |
| 56 |
} |
| 57 |
|
| 58 |
#[test] |
| 59 |
fn trims_spaces_before_middle_pair() { |
| 60 |
compare("a=b\n c=d\ne=f", "a=b\nc=d\ne=f\n"); |
| 61 |
} |
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
#[test] |
| 66 |
fn comment_indicator_in_section_before_pair() { |
| 67 |
compare("[a]\n;b\nc=d", "[a]\n;b\nc=d\n"); |
| 68 |
compare("[a]\n#b\nc=d", "[a]\n#b\nc=d\n"); |
| 69 |
} |
| 70 |
|
| 71 |
#[test] |
| 72 |
fn comment_indicator_in_section_between_pairs() { |
| 73 |
compare("[a]\nb=c\n;d\ne=f", "[a]\nb=c\n;d\ne=f\n"); |
| 74 |
compare("[a]\nb=c\n#d\ne=f", "[a]\nb=c\n#d\ne=f\n"); |
| 75 |
} |
| 76 |
|
| 77 |
#[test] |
| 78 |
fn comment_indicator_included_in_value() { |
| 79 |
compare("a=b; c", "a=b; c\n"); |
| 80 |
compare("a=b# c", "a=b# c\n"); |
| 81 |
} |
| 82 |
|
| 83 |
#[test] |
| 84 |
fn escaped_comment_indicator_in_value() { |
| 85 |
compare("a=b\\;c", "a=b\\;c\n"); |
| 86 |
compare("a=b\\#c", "a=b\\#c\n"); |
| 87 |
} |
| 88 |
|
| 89 |
#[test] |
| 90 |
fn escaped_comment_indicator_in_section_name() { |
| 91 |
compare("[a\\;b]", "[a\\;b]\n"); |
| 92 |
compare("[a\\#b]", "[a\\#b]\n"); |
| 93 |
} |
| 94 |
|
| 95 |
#[test] |
| 96 |
fn removes_bom() { |
| 97 |
let utf8_bom: [u8; 3] = [0xef, 0xbb, 0xbf]; |
| 98 |
let contents = format!("{}a=b", str::from_utf8(&utf8_bom).unwrap()); |
| 99 |
compare(contents, "a=b\n"); |
| 100 |
} |
| 101 |
|
| 102 |
#[test] |
| 103 |
fn crlf_line_separators() { |
| 104 |
compare("[a]\r\nb=c", "[a]\nb=c\n"); |
| 105 |
} |
| 106 |
|
| 107 |
|
| 108 |
fn compare<S: Into<String>>(contents: S, expected: &str) { |
| 109 |
let ast = parse(&contents.into()).unwrap(); |
| 110 |
assert_eq!(ast.to_string(), expected); |
| 111 |
} |
| 112 |
|
| 113 |
#[test] |
| 114 |
fn no_whitespace_vec() { |
| 115 |
compare_vec( |
| 116 |
"a=b", |
| 117 |
&vec![(TokenTypes::Key, "a"), (TokenTypes::Value, "b")], |
| 118 |
); |
| 119 |
} |
| 120 |
|
| 121 |
#[test] |
| 122 |
fn trims_spaces_around_equals_vec() { |
| 123 |
compare_vec( |
| 124 |
"a = b", |
| 125 |
&vec![(TokenTypes::Key, "a"), (TokenTypes::Value, "b")], |
| 126 |
); |
| 127 |
} |
| 128 |
|
| 129 |
#[test] |
| 130 |
fn trims_multiple_spaces_around_equals_vec() { |
| 131 |
compare_vec( |
| 132 |
"a = b", |
| 133 |
&vec![(TokenTypes::Key, "a"), (TokenTypes::Value, "b")], |
| 134 |
); |
| 135 |
} |
| 136 |
|
| 137 |
#[test] |
| 138 |
fn trims_spaces_before_pair_key_vec() { |
| 139 |
compare_vec( |
| 140 |
" a=b", |
| 141 |
&vec![(TokenTypes::Key, "a"), (TokenTypes::Value, "b")], |
| 142 |
); |
| 143 |
} |
| 144 |
|
| 145 |
#[test] |
| 146 |
fn trims_spaces_after_pair_value_vec() { |
| 147 |
compare_vec( |
| 148 |
"a=b ", |
| 149 |
&vec![(TokenTypes::Key, "a"), (TokenTypes::Value, "b")], |
| 150 |
); |
| 151 |
} |
| 152 |
|
| 153 |
#[test] |
| 154 |
fn removes_blank_lines_between_properties_vec() { |
| 155 |
compare_vec( |
| 156 |
"\na=b\n\nc=d", |
| 157 |
&vec![ |
| 158 |
(TokenTypes::Key, "a"), |
| 159 |
(TokenTypes::Value, "b"), |
| 160 |
(TokenTypes::Key, "c"), |
| 161 |
(TokenTypes::Value, "d"), |
| 162 |
], |
| 163 |
); |
| 164 |
} |
| 165 |
|
| 166 |
#[test] |
| 167 |
fn includes_spaces_in_section_name_vec() { |
| 168 |
compare_vec("[ a b ]", &vec![(TokenTypes::Section, " a b ")]) |
| 169 |
} |
| 170 |
|
| 171 |
#[test] |
| 172 |
fn trims_spaces_before_section_name_vec() { |
| 173 |
compare_vec(" [a]", &vec![(TokenTypes::Section, "a")]); |
| 174 |
} |
| 175 |
|
| 176 |
#[test] |
| 177 |
fn trims_spaces_after_section_name_vec() { |
| 178 |
compare_vec("[a] ", &vec![(TokenTypes::Section, "a")]); |
| 179 |
} |
| 180 |
|
| 181 |
#[test] |
| 182 |
fn handles_nested_section_braces_vec() { |
| 183 |
compare_vec("[[a]]", &vec![(TokenTypes::Section, "[a]")]); |
| 184 |
} |
| 185 |
|
| 186 |
#[test] |
| 187 |
fn trims_spaces_before_middle_pair_vec() { |
| 188 |
compare_vec( |
| 189 |
"a=b\n c=d\ne=f", |
| 190 |
&vec![ |
| 191 |
(TokenTypes::Key, "a"), |
| 192 |
(TokenTypes::Value, "b"), |
| 193 |
(TokenTypes::Key, "c"), |
| 194 |
(TokenTypes::Value, "d"), |
| 195 |
(TokenTypes::Key, "e"), |
| 196 |
(TokenTypes::Value, "f"), |
| 197 |
], |
| 198 |
); |
| 199 |
} |
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
#[test] |
| 204 |
fn comment_indicator_in_section_before_pair_vec() { |
| 205 |
compare_vec( |
| 206 |
"[a]\n;b\nc=d", |
| 207 |
&vec![ |
| 208 |
(TokenTypes::Section, "a"), |
| 209 |
(TokenTypes::CommentIndicator, ";"), |
| 210 |
(TokenTypes::CommentValue, "b"), |
| 211 |
(TokenTypes::Key, "c"), |
| 212 |
(TokenTypes::Value, "d"), |
| 213 |
], |
| 214 |
); |
| 215 |
compare_vec( |
| 216 |
"[a]\n#b\nc=d", |
| 217 |
&vec![ |
| 218 |
(TokenTypes::Section, "a"), |
| 219 |
(TokenTypes::CommentIndicator, "#"), |
| 220 |
(TokenTypes::CommentValue, "b"), |
| 221 |
(TokenTypes::Key, "c"), |
| 222 |
(TokenTypes::Value, "d"), |
| 223 |
], |
| 224 |
); |
| 225 |
} |
| 226 |
|
| 227 |
#[test] |
| 228 |
fn comment_indicator_in_section_between_pairs_vec() { |
| 229 |
compare_vec( |
| 230 |
"[a]\nb=c\n;d\ne=f", |
| 231 |
&vec![ |
| 232 |
(TokenTypes::Section, "a"), |
| 233 |
(TokenTypes::Key, "b"), |
| 234 |
(TokenTypes::Value, "c"), |
| 235 |
(TokenTypes::CommentIndicator, ";"), |
| 236 |
(TokenTypes::CommentValue, "d"), |
| 237 |
(TokenTypes::Key, "e"), |
| 238 |
(TokenTypes::Value, "f"), |
| 239 |
], |
| 240 |
); |
| 241 |
compare_vec( |
| 242 |
"[a]\nb=c\n#d\ne=f", |
| 243 |
&vec![ |
| 244 |
(TokenTypes::Section, "a"), |
| 245 |
(TokenTypes::Key, "b"), |
| 246 |
(TokenTypes::Value, "c"), |
| 247 |
(TokenTypes::CommentIndicator, "#"), |
| 248 |
(TokenTypes::CommentValue, "d"), |
| 249 |
(TokenTypes::Key, "e"), |
| 250 |
(TokenTypes::Value, "f"), |
| 251 |
], |
| 252 |
); |
| 253 |
} |
| 254 |
|
| 255 |
#[test] |
| 256 |
fn comment_indicator_included_in_value_vec() { |
| 257 |
compare_vec( |
| 258 |
"a=b; c", |
| 259 |
&vec![(TokenTypes::Key, "a"), (TokenTypes::Value, "b; c")], |
| 260 |
); |
| 261 |
compare_vec( |
| 262 |
"a=b# c", |
| 263 |
&vec![(TokenTypes::Key, "a"), (TokenTypes::Value, "b# c")], |
| 264 |
); |
| 265 |
} |
| 266 |
|
| 267 |
#[test] |
| 268 |
fn escaped_comment_indicator_in_value_vec() { |
| 269 |
|
| 270 |
|
| 271 |
compare_vec( |
| 272 |
"a=b\\;c", |
| 273 |
&vec![(TokenTypes::Key, "a"), (TokenTypes::Value, "b\\;c")], |
| 274 |
); |
| 275 |
compare_vec( |
| 276 |
"a=b\\#c", |
| 277 |
&vec![(TokenTypes::Key, "a"), (TokenTypes::Value, "b\\#c")], |
| 278 |
); |
| 279 |
} |
| 280 |
|
| 281 |
#[test] |
| 282 |
fn escaped_comment_indicator_in_section_name_vec() { |
| 283 |
|
| 284 |
|
| 285 |
compare_vec("[a\\;b]", &vec![(TokenTypes::Section, "a\\;b")]); |
| 286 |
compare_vec("[a\\#b]", &vec![(TokenTypes::Section, "a\\#b")]); |
| 287 |
} |
| 288 |
|
| 289 |
#[test] |
| 290 |
fn removes_bom_vec() { |
| 291 |
compare_vec( |
| 292 |
"\u{feff}a=b", |
| 293 |
&vec![(TokenTypes::Key, "a"), (TokenTypes::Value, "b")], |
| 294 |
); |
| 295 |
} |
| 296 |
|
| 297 |
#[test] |
| 298 |
fn crlf_line_separators_vec() { |
| 299 |
compare_vec( |
| 300 |
"[a]\r\nb=c", |
| 301 |
&vec![ |
| 302 |
(TokenTypes::Section, "a"), |
| 303 |
(TokenTypes::Key, "b"), |
| 304 |
(TokenTypes::Value, "c"), |
| 305 |
], |
| 306 |
); |
| 307 |
} |
| 308 |
|
| 309 |
#[test] |
| 310 |
fn partial_section_vec() { |
| 311 |
compare_vec("[foo", &vec![]); |
| 312 |
} |
| 313 |
|
| 314 |
|
| 315 |
#[test] |
| 316 |
#[should_panic] |
| 317 |
fn compare_vec_not_enough() { |
| 318 |
compare_vec("", &vec![(TokenTypes::Key, "a")]); |
| 319 |
} |
| 320 |
|
| 321 |
#[test] |
| 322 |
#[should_panic] |
| 323 |
fn compare_vec_too_many() { |
| 324 |
compare_vec("a=b", &vec![(TokenTypes::Key, "a")]); |
| 325 |
} |
| 326 |
|
| 327 |
#[test] |
| 328 |
#[should_panic] |
| 329 |
fn compare_vec_wrong_token() { |
| 330 |
compare_vec("a=b", &vec![(TokenTypes::Key, "a"), (TokenTypes::Key, "b")]); |
| 331 |
} |
| 332 |
|
| 333 |
#[test] |
| 334 |
#[should_panic] |
| 335 |
fn compare_vec_wrong_value() { |
| 336 |
compare_vec( |
| 337 |
"a=b", |
| 338 |
&vec![(TokenTypes::Key, "a"), (TokenTypes::Value, "c")], |
| 339 |
); |
| 340 |
} |
| 341 |
|
| 342 |
fn compare_vec<S: Into<String>>(contents: S, expected: &Vec<(TokenTypes, &str)>) { |
| 343 |
let s: String = contents.into(); |
| 344 |
let v = parse_to_vec(&s).unwrap(); |
| 345 |
let buf = s.as_bytes(); |
| 346 |
|
| 347 |
let mut i = 0; |
| 348 |
for chunk in v.chunks(3) { |
| 349 |
let (etyp, estr) = expected[i]; |
| 350 |
assert_eq!(chunk[0], etyp as u32); |
| 351 |
assert_eq!( |
| 352 |
str::from_utf8(&buf[chunk[1] as usize..chunk[2] as usize]).unwrap(), |
| 353 |
estr |
| 354 |
); |
| 355 |
i += 1; |
| 356 |
} |
| 357 |
assert_eq!(i, expected.len()); |
| 358 |
} |