Skip to content

Commit

Permalink
Allow tests to be run when alloc is enabled without std.
Browse files Browse the repository at this point in the history
  • Loading branch information
briansmith committed Jul 9, 2019
1 parent 085d23b commit 4ecd09d
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 22 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -333,7 +333,7 @@ dev_urandom_fallback = ["std", "lazy_static"]
internal_benches = []
slow_tests = []
std = ["alloc"]
test_logging = []
test_logging = ["std"]

# XXX: debug = false because of https://github.com/rust-lang/rust/issues/34122

Expand Down
50 changes: 30 additions & 20 deletions src/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,12 +116,11 @@
//! stack trace to the line in the test code that panicked: entry 9 in the
//! stack trace pointing to line 652 of the file `example.rs`.

use crate::{bits, digest, error};

#[cfg(feature = "alloc")]
use alloc::{format, string::String, vec::Vec};

#[cfg(feature = "std")]
use crate::{bits, digest, error};

#[cfg(feature = "std")]
use std::println;

Expand Down Expand Up @@ -155,13 +154,11 @@ pub fn compile_time_assert_std_error_error<T: std::error::Error>() {}
/// typos and omissions.
///
/// Requires the `std` default feature to be enabled.
#[cfg(feature = "std")]
#[derive(Debug)]
pub struct TestCase {
attributes: Vec<(String, String, bool)>,
}

#[cfg(feature = "std")]
impl TestCase {
/// Maps the string "true" to true and the string "false" to false.
pub fn consume_bool(&mut self, key: &str) -> bool {
Expand Down Expand Up @@ -311,7 +308,6 @@ pub struct File<'a> {
/// failure either by returning `Err()` or by panicking.
///
/// Requires the `std` default feature to be enabled
#[cfg(feature = "std")]
pub fn run<F>(test_file: File, mut f: F)
where
F: FnMut(&str, &mut TestCase) -> Result<(), error::Unspecified>,
Expand All @@ -322,9 +318,14 @@ where
let mut failed = false;

while let Some(mut test_case) = parse_test_case(&mut current_section, lines) {
#[cfg(feature = "std")]
let result = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| {
f(&current_section, &mut test_case)
}));

#[cfg(not(feature = "std"))]
let result: Result<_, error::Unspecified> = Ok(f(&current_section, &mut test_case));

let result = match result {
Ok(Ok(())) => {
if !test_case
Expand All @@ -342,15 +343,21 @@ where
Err(_) => Err("Test panicked."),
};

if let Err(msg) = result {
if result.is_err() {
failed = true;
}

println!("{}: {}", test_file.file_name, msg);
for (name, value, consumed) in test_case.attributes {
let consumed_str = if consumed { "" } else { " (unconsumed)" };
println!("{}{} = {}", name, consumed_str, value);
}
};
#[cfg(feature = "std")]
{
if let Err(msg) = result {
println!("{}: {}", test_file.file_name, msg);

for (name, value, consumed) in test_case.attributes {
let consumed_str = if consumed { "" } else { " (unconsumed)" };
println!("{}{} = {}", name, consumed_str, value);
}
};
}
}

if failed {
Expand Down Expand Up @@ -390,8 +397,6 @@ fn from_hex_digit(d: u8) -> Result<u8, String> {
}
}


#[cfg(feature = "std")]
fn parse_test_case(
current_section: &mut String,
lines: &mut dyn Iterator<Item = &str>,
Expand All @@ -402,7 +407,8 @@ fn parse_test_case(
loop {
let line = lines.next();

if cfg!(feature = "test_logging") {
#[cfg(feature = "test_logging")]
{
if let Some(text) = &line {
println!("Line: {}", text);
}
Expand Down Expand Up @@ -560,7 +566,8 @@ mod tests {
}

#[test]
#[should_panic(expected = "Test failed.")]
#[cfg_attr(feature = "std", should_panic(expected = "Test failed."))]
#[cfg_attr(not(feature = "std"), should_panic)]
fn one_panics() {
test::run(test_file!("test_1_tests.txt"), |_, test_case| {
let _ = test_case.consume_string("Key");
Expand Down Expand Up @@ -601,19 +608,22 @@ mod tests {
}

#[test]
#[should_panic(expected = "Test failed.")]
#[cfg_attr(feature = "std", should_panic(expected = "Test failed."))]
#[cfg_attr(not(feature = "std"), should_panic)]
fn first_panic() {
panic_one(0)
}

#[test]
#[should_panic(expected = "Test failed.")]
#[cfg_attr(feature = "std", should_panic(expected = "Test failed."))]
#[cfg_attr(not(feature = "std"), should_panic)]
fn middle_panic() {
panic_one(1)
}

#[test]
#[should_panic(expected = "Test failed.")]
#[cfg_attr(feature = "std", should_panic(expected = "Test failed."))]
#[cfg_attr(not(feature = "std"), should_panic)]
fn last_panic() {
panic_one(2)
}
Expand Down
2 changes: 1 addition & 1 deletion tests/pbkdf2_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@
warnings
)]

use ring::{digest, error, pbkdf2, test, test_file};
use core::num::NonZeroU32;
use ring::{digest, error, pbkdf2, test, test_file};

#[test]
pub fn pbkdf2_tests() {
Expand Down

0 comments on commit 4ecd09d

Please sign in to comment.