[−][src]Struct lab_grader::criterion::Criterion
A criterion
Fields
name: String
worth: i16
messages: (&'static str, &'static str)
test: Box<dyn Fn(&HashMap<String, String>) -> bool>
status: Option<bool>
hide: bool
Methods
impl Criterion
[src]
pub fn new<S: AsRef<str>>(
name: S,
worth: i16,
messages: (&'static str, &'static str),
test: Box<dyn Fn(&HashMap<String, String>) -> bool>
) -> Self
[src]
name: S,
worth: i16,
messages: (&'static str, &'static str),
test: Box<dyn Fn(&HashMap<String, String>) -> bool>
) -> Self
Creates a new Criterion with the given parameters.
The messages
parameter should be a tuple of
&str
containing a success then failure message, respectively.
These messages will be printed when printing the criterion.
The test
parameter is a Box
around a closure accepting
a HashMap returning a bool. This can get a bit confusing.
The test
closure should return true if the criterion passes, otherwise false.
The &HashMap
parameter allows data from outside the closure to be passed in. The signature
of the &HashMap
is &HashMap<String, String>
, so all keys and values must be String
s.
This is done to generalize the test
field, as all criteria must have the same signature.
Example
A basic criterion
use std::collections::HashMap; use lab_grader::criterion::Criterion; let mut c = Criterion::new( "A test criterion", 10, ("Success!", "Failure!"), Box::new(|_: &HashMap<String, String>| { // Code to test criterion goes here, // and should return false or... true }) ); assert!(c.test());
A criterion with data
let mut c = Criterion::new( "A test criterion with data!", 10, ("Success!", "Failure!"), Box::new(|data: &HashMap<String, String>| { return data["my_key"] == "my_value"; }) ); // The above criterion takes a HashMap into it's closure, // so we must establish the data to send into the closure let my_data = data! { "my_key" => "my_value" }; assert!(c.test_with_data(&my_data));
pub fn success_message(&self) -> &'static str
[src]
Returns the success message, ie. the first message in the messages
tuple
pub fn failure_message(&self) -> &'static str
[src]
Returns the failure message, ie. the second message in the messages
tuple
pub fn hide(&mut self)
[src]
Toggles the hide
field on a criterion
If hide is true, printing the criterion with the default formatter will print nothing. Good if you want a secret criterion that the students don't know about
pub fn test_with_data(&mut self, data: &HashMap<String, String>) -> bool
[src]
Runs the criterion's test function with the data provided.
This is almost equivilent to calling (criterion.test)(data)
, but this
method also sets the status of the criterion to the result of the test.
You should avoid calling the test directly, and call this or the
test
method instead.
The criterion must be mutable to call this method, as the status is changed to the result of the test.
Example
let mut c = Criterion::new( "A test criterion with data!", 10, ("Success!", "Failure!"), Box::new(|data: &HashMap<String, String>| { return data["my_key"] == "my_value"; }) ); let my_data = data! { "my_key" => "my_value" }; c.test_with_data(&my_data); // It's either Some(true) or Some(false) since we've tested assert!(c.status.is_some());
pub fn test(&mut self) -> bool
[src]
Runs the criterions test and assigns the result to criterion.status
.
This is equivilent to running test_with_data
with
an empty HashMap
.
Criterion must be mutable.
Example
let mut c = Criterion::new( "A test criterion with data!", 10, ("Success!", "Failure!"), Box::new(|_: &HashMap<String, String>| { true }) ); assert!(c.test()); assert!(c.status.is_some());
Trait Implementations
impl Display for Criterion
[src]
Displays the results of the criterion. You should test the criterion before printing it.
Output will be aligned, as you'll normally be printing a lot of these at once.
Given a configuration with the name Test criterion
,
success message passed!
, and failure message failed!
,
this is what would print:
Printed before testing
My first criterion +** not tested
Printed after a successful test
My first criterion +10 passed!
Printed after a failed test
My first criterion + 0 failed!
Auto Trait Implementations
impl !RefUnwindSafe for Criterion
impl !Send for Criterion
impl !Sync for Criterion
impl Unpin for Criterion
impl !UnwindSafe for Criterion
Blanket Implementations
impl<T> Any for T where
T: 'static + ?Sized,
[src]
T: 'static + ?Sized,
impl<T> Borrow<T> for T where
T: ?Sized,
[src]
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
[src]
T: ?Sized,
fn borrow_mut(&mut self) -> &mut T
[src]
impl<T> From<T> for T
[src]
impl<T, U> Into<U> for T where
U: From<T>,
[src]
U: From<T>,
impl<T> ToString for T where
T: Display + ?Sized,
[src]
T: Display + ?Sized,
impl<T, U> TryFrom<U> for T where
U: Into<T>,
[src]
U: Into<T>,
type Error = Infallible
The type returned in the event of a conversion error.
fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>
[src]
impl<T, U> TryInto<U> for T where
U: TryFrom<T>,
[src]
U: TryFrom<T>,