-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathcommon.rs
84 lines (72 loc) · 2.52 KB
/
common.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
use std::{
io::BufRead,
path::{Path, PathBuf},
};
use lazy_static::lazy_static;
use regex::Regex;
pub fn fixture_path<P: AsRef<Path>>(path: P) -> PathBuf {
Path::new(env!("CARGO_MANIFEST_DIR")).join(path)
}
/// Read a test fixture from a path relative to CARGO_MANIFEST_DIR
pub fn read_fixture<P: AsRef<Path>>(path: P) -> Vec<u8> {
std::fs::read(&fixture_path(path)).expect("error reading file contents")
}
#[cfg(not(feature = "prince"))]
#[allow(unused)]
pub fn read_fixture_font<P: AsRef<Path>>(path: P) -> Vec<u8> {
read_fixture(Path::new("tests/fonts").join(path))
}
#[cfg(feature = "prince")]
#[allow(unused)]
pub fn read_fixture_font<P: AsRef<Path>>(path: P) -> Vec<u8> {
[
Path::new("tests/fonts").join(path.as_ref()),
Path::new("../../../tests/data/fonts").join(path.as_ref()),
]
.iter()
.find(|path| path.is_file())
.map(read_fixture)
.unwrap_or_else(|| panic!("unable to find fixture font {}", path.as_ref().display()))
}
#[allow(unused)]
fn parse_expected_output(expected_output: &str, ignore: &[u16]) -> (Vec<u16>, Option<String>) {
fn parse(s: &str, ignore: &[u16]) -> Vec<u16> {
s.split('|')
.map(|s| s.parse::<u16>().expect("error parsing glyph index"))
.filter(|i| ignore.is_empty() || !ignore.contains(i))
.collect()
}
lazy_static! {
static ref REGEX: Regex = Regex::new(r"^\[(\d+(?:\|\d+)*)\](?:\s*:\s*(.*))?$").unwrap();
}
if let Some(captures) = REGEX.captures(expected_output) {
let indices = parse(&captures[1], ignore);
let reason = captures.get(2).map(|s| String::from(s.as_str()));
(indices, reason)
} else {
panic!("invalid expected output format: {:?}", expected_output);
}
}
#[allow(unused)]
pub fn read_inputs<P: AsRef<Path>, B: AsRef<Path>>(base: B, inputs_path: P) -> Vec<String> {
read_fixture_inputs(base, inputs_path)
.lines()
.collect::<Result<_, _>>()
.expect("error reading inputs")
}
#[allow(unused)]
fn read_fixture_inputs<P: AsRef<Path>, B: AsRef<Path>>(base: B, path: P) -> Vec<u8> {
read_fixture(base.as_ref().join(path))
}
#[allow(unused)]
pub fn parse_expected_outputs<B: AsRef<Path>, P: AsRef<Path>>(
base: B,
expected_outputs_path: P,
ignore: &[u16],
) -> Vec<(Vec<u16>, Option<String>)> {
read_fixture_inputs(base, expected_outputs_path)
.lines()
.map(|line| line.expect("error reading expected output"))
.map(|line| parse_expected_output(&line, ignore))
.collect()
}