-
-
Save durka/cc1c4f0d1853a0a7885f to your computer and use it in GitHub Desktop.
Fixed version of Nashenas88's test-case macro
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
macro_rules! as_item { ($i:item) => ($i) } // <-- empirically, this is needed to placate the compiler | |
macro_rules! test_cases { | |
( | |
$test_name:ident | |
$param_names:tt, // <-- just treat the param names as a tuple (type 'tt' to delay parsing) | |
[$($case_name:ident : $test_params:tt)+] // <-- same for the param values | |
$test_code:block | |
) => (as_item!( | |
#[cfg(test)] | |
pub mod $test_name { | |
$( | |
#[test] | |
#[allow(non_snake_case)] | |
fn $case_name () { | |
let $param_names = $test_params; | |
$test_code | |
} | |
)+} | |
);); | |
} | |
test_cases!( | |
test_hex_to_base_64 | |
(hex_str, expected_base64_str), | |
[ | |
A: ("49276d206b696c6c696e6720796f757220627261696e206c696b65206120706f69736f6e6f7573206d757368726f6f6d", "SSdtIGtpbGxpbmcgeW91ciBicmFpbiBsaWtlIGEgcG9pc29ub3VzIG11c2hyb29t") | |
B: ("49276d206b696c6c696e6720796f757220627261696e206c696b65206120706f69736f6e6f7573206d757368726f6f6", "SSdtIGtpbGxpbmcgeW91ciBicmFpbiBsaWtlIGEgcG9pc29ub3VzIG11c2hyb29g") | |
C: ("49276d206b696c6c696e6720796f757220627261696e206c696b65206120706f69736f6e6f7573206d757368726f6f", "SSdtIGtpbGxpbmcgeW91ciBicmFpbiBsaWtlIGEgcG9pc29ub3VzIG11c2hyb28=") | |
D: ("49276d206b696c6c696e6720796f757220627261696e206c696b65206120706f69736f6e6f7573206d757368726f6", "SSdtIGtpbGxpbmcgeW91ciBicmFpbiBsaWtlIGEgcG9pc29ub3VzIG11c2hyb2A=") | |
E: ("49276d206b696c6c696e6720796f757220627261696e206c696b65206120706f69736f6e6f7573206d757368726f", "SSdtIGtpbGxpbmcgeW91ciBicmFpbiBsaWtlIGEgcG9pc29ub3VzIG11c2hybw==") | |
F: ("49276d206b696c6c696e6720796f75722x627261696e206c696b65206120706f69736f6e6f7573206d757368726", "49276d206b696c6c696e6720796f75722x627261696e206c696b65206120706f69736f6e6f7573206d757368726") | |
] // NB: I changed test case F so that it will fail | |
{ | |
assert!(hex_str != expected_base64_str); | |
} | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment