simplify encoding test macro (#4385)

pull/4299/head
Kirawi 2 years ago committed by GitHub
parent fbf8078611
commit ce399471f0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -1354,8 +1354,10 @@ mod test {
); );
} }
macro_rules! test_decode { macro_rules! decode {
($label:expr, $label_override:expr) => { ($name:ident, $label:expr, $label_override:expr) => {
#[test]
fn $name() {
let encoding = encoding::Encoding::for_label($label_override.as_bytes()).unwrap(); let encoding = encoding::Encoding::for_label($label_override.as_bytes()).unwrap();
let base_path = PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("tests/encoding"); let base_path = PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("tests/encoding");
let path = base_path.join(format!("{}_in.txt", $label)); let path = base_path.join(format!("{}_in.txt", $label));
@ -1370,11 +1372,17 @@ mod test {
.to_string(); .to_string();
let expectation = std::fs::read_to_string(ref_path).unwrap(); let expectation = std::fs::read_to_string(ref_path).unwrap();
assert_eq!(text[..], expectation[..]); assert_eq!(text[..], expectation[..]);
}
};
($name:ident, $label:expr) => {
decode!($name, $label, $label);
}; };
} }
macro_rules! test_encode { macro_rules! encode {
($label:expr, $label_override:expr) => { ($name:ident, $label:expr, $label_override:expr) => {
#[test]
fn $name() {
let encoding = encoding::Encoding::for_label($label_override.as_bytes()).unwrap(); let encoding = encoding::Encoding::for_label($label_override.as_bytes()).unwrap();
let base_path = PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("tests/encoding"); let base_path = PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("tests/encoding");
let path = base_path.join(format!("{}_out.txt", $label)); let path = base_path.join(format!("{}_out.txt", $label));
@ -1388,50 +1396,24 @@ mod test {
let expectation = std::fs::read(ref_path).unwrap(); let expectation = std::fs::read(ref_path).unwrap();
assert_eq!(buf, expectation); assert_eq!(buf, expectation);
};
}
macro_rules! test_decode_fn {
($name:ident, $label:expr, $label_override:expr) => {
#[test]
fn $name() {
test_decode!($label, $label_override);
} }
}; };
($name:ident, $label:expr) => { ($name:ident, $label:expr) => {
#[test] encode!($name, $label, $label);
fn $name() {
test_decode!($label, $label);
}
};
}
macro_rules! test_encode_fn {
($name:ident, $label:expr, $label_override:expr) => {
#[test]
fn $name() {
test_encode!($label, $label_override);
}
};
($name:ident, $label:expr) => {
#[test]
fn $name() {
test_encode!($label, $label);
}
}; };
} }
test_decode_fn!(test_big5_decode, "big5"); decode!(big5_decode, "big5");
test_encode_fn!(test_big5_encode, "big5"); encode!(big5_encode, "big5");
test_decode_fn!(test_euc_kr_decode, "euc_kr", "EUC-KR"); decode!(euc_kr_decode, "euc_kr", "EUC-KR");
test_encode_fn!(test_euc_kr_encode, "euc_kr", "EUC-KR"); encode!(euc_kr_encode, "euc_kr", "EUC-KR");
test_decode_fn!(test_gb18030_decode, "gb18030"); decode!(gb18030_decode, "gb18030");
test_encode_fn!(test_gb18030_encode, "gb18030"); encode!(gb18030_encode, "gb18030");
test_decode_fn!(test_iso_2022_jp_decode, "iso_2022_jp", "ISO-2022-JP"); decode!(iso_2022_jp_decode, "iso_2022_jp", "ISO-2022-JP");
test_encode_fn!(test_iso_2022_jp_encode, "iso_2022_jp", "ISO-2022-JP"); encode!(iso_2022_jp_encode, "iso_2022_jp", "ISO-2022-JP");
test_decode_fn!(test_jis0208_decode, "jis0208", "EUC-JP"); decode!(jis0208_decode, "jis0208", "EUC-JP");
test_encode_fn!(test_jis0208_encode, "jis0208", "EUC-JP"); encode!(jis0208_encode, "jis0208", "EUC-JP");
test_decode_fn!(test_jis0212_decode, "jis0212", "EUC-JP"); decode!(jis0212_decode, "jis0212", "EUC-JP");
test_decode_fn!(test_shift_jis_decode, "shift_jis"); decode!(shift_jis_decode, "shift_jis");
test_encode_fn!(test_shift_jis_encode, "shift_jis"); encode!(shift_jis_encode, "shift_jis");
} }

Loading…
Cancel
Save