Skip to content

Commit fb3174b

Browse files
committed
Run WPT tests for base64
1 parent 63493ba commit fb3174b

File tree

1 file changed

+65
-7
lines changed

1 file changed

+65
-7
lines changed

tests/wpt.rs

Lines changed: 65 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,7 @@ fn collect_data_url<F>(add_test: &mut F)
3535
Some(expected_mime.as_str().unwrap().to_owned())
3636
};
3737

38-
let expected_body = test.get(2).map(|j| {
39-
j.as_array().unwrap().iter().map(|byte| {
40-
let byte = byte.as_u64().unwrap();
41-
assert!(byte <= 0xFF);
42-
byte as u8
43-
}).collect::<Vec<u8>>()
44-
});
38+
let expected_body = test.get(2).map(json_byte_array);
4539

4640
let should_panic = [
4741
"data://test:test/,X",
@@ -69,6 +63,69 @@ fn collect_data_url<F>(add_test: &mut F)
6963
}
7064
}
7165

66+
fn run_base64(input: String, expected: Option<Vec<u8>>) {
67+
let result = data_url::forgiving_base64::decode_to_vec(input.as_bytes());
68+
match (result, expected) {
69+
(Ok(bytes), Some(expected)) => assert_eq!(bytes, expected),
70+
(Ok(bytes), None) => panic!("Expected error, got {:?}", bytes),
71+
(Err(_), Some(expected)) => panic!("Expected {:?}, got error", expected),
72+
(Err(_), None) => {}
73+
}
74+
}
75+
76+
77+
fn collect_base64<F>(add_test: &mut F)
78+
where F: FnMut(String, bool, rustc_test::TestFn)
79+
{
80+
let json = include_str!("base64.json");
81+
let v: serde_json::Value = serde_json::from_str(json).unwrap();
82+
for test in v.as_array().unwrap() {
83+
let input = test.get(0).unwrap().as_str().unwrap().to_owned();
84+
let expected = test.get(1).unwrap();
85+
let expected = if expected.is_null() {
86+
None
87+
} else {
88+
Some(json_byte_array(expected))
89+
};
90+
91+
let should_panic = [
92+
" \t\n\u{c}\r ab\t\n\u{c}\r cd\t\n\u{c}\r ",
93+
" abcd",
94+
"////A",
95+
"///A",
96+
"AAA/",
97+
"AAAA/",
98+
"ab cd",
99+
"ab==",
100+
"ab\ncd",
101+
"ab\rcd",
102+
"ab\t\n\u{c}\r =\t\n\u{c}\r =\t\n\u{c}\r ",
103+
"ab\t\n\u{c}\r cd",
104+
"ab\tcd",
105+
"ab\u{c}cd",
106+
"abc=",
107+
"abcd ",
108+
"abcd",
109+
"abcde",
110+
].contains(&&*input);
111+
add_test(
112+
format!("base64 {:?}", input),
113+
should_panic,
114+
rustc_test::TestFn::dyn_test_fn(move || {
115+
run_base64(input, expected)
116+
})
117+
);
118+
}
119+
}
120+
121+
fn json_byte_array(j: &serde_json::Value) -> Vec<u8> {
122+
j.as_array().unwrap().iter().map(|byte| {
123+
let byte = byte.as_u64().unwrap();
124+
assert!(byte <= 0xFF);
125+
byte as u8
126+
}).collect()
127+
}
128+
72129
fn main() {
73130
let mut tests = Vec::new();
74131
{
@@ -80,6 +137,7 @@ fn main() {
80137
tests.push(rustc_test::TestDescAndFn { desc, testfn: run })
81138
};
82139
collect_data_url(&mut add_one);
140+
collect_base64(&mut add_one);
83141
}
84142
rustc_test::test_main(&std::env::args().collect::<Vec<_>>(), tests)
85143
}

0 commit comments

Comments
 (0)