-
Notifications
You must be signed in to change notification settings - Fork 5
/
test.rs
93 lines (81 loc) · 2.36 KB
/
test.rs
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
83
84
85
86
87
88
89
90
91
92
93
macro_rules! unescape_assert_eq {
($l:expr, $r:expr) => {
assert_eq!(crate::unescape($l).unwrap(), $r);
};
}
macro_rules! unescape_assert_err {
($l:expr, $r:expr) => {
assert_eq!(crate::unescape($l).unwrap_err(), $r);
};
}
macro_rules! unescape_assert_err_str {
($s:expr, $e:expr, $e_src:expr) => {{
let e = crate::unescape($s).unwrap_err();
assert_eq!(e.to_string(), $e);
assert_eq!(e.source().unwrap().to_string(), $e_src);
}};
}
#[test]
fn error() {
// std
use std::error::Error;
// hack-ink
use crate::Error::*;
unescape_assert_err!(r"\", IncompleteStr(0));
unescape_assert_err!(r"\0\", IncompleteStr(2));
unescape_assert_err!(r"\{}", InvalidChar { char: '{', pos: 1 });
unescape_assert_err!(r"\0\{}", InvalidChar { char: '{', pos: 3 });
unescape_assert_err_str!(
r"\u{g}",
"parse int error, break at 4",
"invalid digit found in string"
);
unescape_assert_err_str!(
r"\0\u{g}",
"parse int error, break at 6",
"invalid digit found in string"
);
}
#[test]
fn unescape_unicode() {
unescape_assert_eq!(r"\u0000", "\0");
unescape_assert_eq!(r"\u0009", "\t");
unescape_assert_eq!(r"\u000a", "\n");
unescape_assert_eq!(r"\uffff", "\u{ffff}");
unescape_assert_eq!(r"\u0000XavierJane", "\0XavierJane");
unescape_assert_eq!(r"\u{0}", "\0");
unescape_assert_eq!(r"\u{9}", "\t");
unescape_assert_eq!(r"\u{a}", "\n");
unescape_assert_eq!(r"\u{ffff}", "\u{ffff}");
unescape_assert_eq!(r"\u{1F600}", "\u{1F600}");
unescape_assert_eq!(r"\u{1F600}", "😀");
unescape_assert_eq!(r"\u{0}XavierJane", "\0XavierJane");
}
#[test]
fn unescape_byte() {
unescape_assert_eq!(r"\x00", "\x00");
unescape_assert_eq!(r"\x09", "\t");
unescape_assert_eq!(r"\x0a", "\n");
unescape_assert_eq!(r"\x7f", "\x7f");
unescape_assert_eq!(r"\x00XavierJane", "\x00XavierJane");
}
#[test]
fn unescape_octal() {
unescape_assert_eq!(r"\0", "\0");
unescape_assert_eq!(r"\11", "\t");
unescape_assert_eq!(r"\12", "\n");
unescape_assert_eq!(r"\377", "\u{00ff}");
unescape_assert_eq!(r"\0XavierJane", "\0XavierJane");
}
#[test]
fn unescape_special_symbols() {
unescape_assert_eq!(r"\b", "\u{0008}");
unescape_assert_eq!(r"\f", "\u{000c}");
unescape_assert_eq!(r"\n", "\n");
unescape_assert_eq!(r"\r", "\r");
unescape_assert_eq!(r"\t", "\t");
unescape_assert_eq!(r"\'", "\'");
unescape_assert_eq!(r#"\""#, "\"");
unescape_assert_eq!(r"\\", "\\");
unescape_assert_eq!(r"//", "//");
}