Skip to content

Commit

Permalink
satisfies #27
Browse files Browse the repository at this point in the history
  • Loading branch information
Alkass committed Dec 25, 2017
1 parent a46e40e commit 8d88ba9
Show file tree
Hide file tree
Showing 6 changed files with 96 additions and 18 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
name = "polish"
description = "Polish is a test frammework designed to allow the construction of test-driven development processes written in Rust"
version = "0.9.6"
version = "0.9.8"
authors = ["Fadi Hanna Al-Kass"]
documentation = "https://docs.rs/polish"
homepage = "https://github.com/AlKass/polish"
Expand Down
1 change: 0 additions & 1 deletion examples/module_path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ use polish::logger::Logger;
fn main() {
TestRunner::new()
.set_attribute(attributes.minimize_output)
.set_attribute(attributes.disable_final_stats)
.set_attribute(attributes.bail_out_after_first_failure)
.set_module_path(module_path!())
.run_test(TestCase::new("title", "criteria", Box::new(|_: &mut Logger| -> TestCaseStatus {TestCaseStatus::UNKNOWN})));
Expand Down
29 changes: 29 additions & 0 deletions examples/time_unit.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
extern crate polish;

use polish::test_case::{TestRunner, TestCase, TestCaseStatus, TEST_RUNNER_TIME_UNITS as timeunits};
use polish::logger::Logger;

fn main() {
TestRunner::new() // nanoseconds by default
.run_test(TestCase::new("title", "criteria", Box::new(|_: &mut Logger| -> TestCaseStatus {TestCaseStatus::UNKNOWN})));

TestRunner::new()
.set_time_unit(timeunits.minutes)
.run_test(TestCase::new("title", "criteria", Box::new(|_: &mut Logger| -> TestCaseStatus {TestCaseStatus::UNKNOWN})));

TestRunner::new()
.set_time_unit(timeunits.seconds)
.run_test(TestCase::new("title", "criteria", Box::new(|_: &mut Logger| -> TestCaseStatus {TestCaseStatus::UNKNOWN})));

TestRunner::new()
.set_time_unit(timeunits.milliseconds)
.run_test(TestCase::new("title", "criteria", Box::new(|_: &mut Logger| -> TestCaseStatus {TestCaseStatus::UNKNOWN})));

TestRunner::new()
.set_time_unit(timeunits.microseconds)
.run_test(TestCase::new("title", "criteria", Box::new(|_: &mut Logger| -> TestCaseStatus {TestCaseStatus::UNKNOWN})));

TestRunner::new()
.set_time_unit(timeunits.nanoseconds)
.run_test(TestCase::new("title", "criteria", Box::new(|_: &mut Logger| -> TestCaseStatus {TestCaseStatus::UNKNOWN})));
}
2 changes: 1 addition & 1 deletion makefile
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
test:
bash scripts/test.sh
. scripts/test.sh
5 changes: 5 additions & 0 deletions scripts/test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,10 @@ for example in examples/*.rs; do
echo $example
cp $example src/main.rs
cargo run
rv=$?
if [ $rv -ne 0 ]; then
echo returning $rv
return $rv
fi
done
rm src/main.rs
75 changes: 60 additions & 15 deletions src/test_case.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,12 @@ impl TestCase {
}
}

type DurationType = f64;

pub struct TestCaseResults {
title: &'static str,
criteria: &'static str,
duration: i32,
duration: DurationType,
status: TestCaseStatus,
}

Expand All @@ -56,16 +58,37 @@ pub static TEST_RUNNER_ATTRIBUTES: TestRunnerAttributes = TestRunnerAttributes {
minimize_output: 0x4,
};

type TimeUnit = (i16, &'static str);

#[allow(dead_code)]
pub struct TestRunnerTimeUnits {
pub minutes: TimeUnit,
pub seconds: TimeUnit,
pub milliseconds: TimeUnit,
pub microseconds: TimeUnit,
pub nanoseconds: TimeUnit,
}

pub static TEST_RUNNER_TIME_UNITS: TestRunnerTimeUnits = TestRunnerTimeUnits {
minutes: (0x1, "min"),
seconds: (0x2, "sec"),
milliseconds: (0x4, "ms"),
microseconds: (0x8, "μs"),
nanoseconds: (0x10, "ns"),
};

#[allow(dead_code)]
pub struct TestRunner {
attributes: i64,
time_unit: TimeUnit,
results: Vec<TestCaseResults>,
module_path: &'static str,
}
impl TestRunner {
pub fn new() -> TestRunner {
TestRunner {
attributes: 0,
time_unit: TEST_RUNNER_TIME_UNITS.nanoseconds,
results: vec![],
module_path: "",
}
Expand All @@ -82,6 +105,10 @@ impl TestRunner {
self.attributes = attributes;
self
}
pub fn set_time_unit(&mut self, time_unit: TimeUnit) -> &mut Self {
self.time_unit = time_unit;
self
}
pub fn has_attribute(&self, attribute: i64) -> bool {
self.attributes & attribute == attribute
}
Expand Down Expand Up @@ -122,17 +149,31 @@ impl TestRunner {
TestCaseStatus::SKIPPED => Yellow.paint(test.criteria),
TestCaseStatus::UNKNOWN => Yellow.paint(test.criteria),
};
let mut duration: DurationType = (ending_time - starting_time) as DurationType;
if self.time_unit == TEST_RUNNER_TIME_UNITS.minutes {
duration /= 1000000000 as DurationType;
duration /= 60 as DurationType;
}
else if self.time_unit == TEST_RUNNER_TIME_UNITS.seconds {
duration /= 1000000000 as DurationType;
}
else if self.time_unit == TEST_RUNNER_TIME_UNITS.milliseconds {
duration /= 1000000 as DurationType;
}
else if self.time_unit == TEST_RUNNER_TIME_UNITS.microseconds {
duration /= 1000 as DurationType;
}
let test_info = TestCaseResults {
title: test.title,
criteria: test.criteria,
duration: ending_time - starting_time,
duration: duration,
status: status.clone(),
};
if self.module_path.len() > 0 {
println!("{} {}::{}: {} ({}ns)", mark, self.module_path, test.title, formatted_criteria, test_info.duration);
println!("{} {}::{}: {} ({}{})", mark, self.module_path, test.title, formatted_criteria, test_info.duration, self.time_unit.1);
}
else {
println!("{} {}: {} ({}ns)", mark, test.title, formatted_criteria, test_info.duration);
println!("{} {}: {} ({}{})", mark, test.title, formatted_criteria, test_info.duration, self.time_unit.1);
}
self.results.push(test_info);
return status == TestCaseStatus::PASSED;
Expand All @@ -152,7 +193,7 @@ impl TestRunner {
impl Drop for TestRunner {
fn drop(&mut self) {
if !self.has_attribute(TEST_RUNNER_ATTRIBUTES.disable_final_stats) {
let (mut total_count, mut total_duration): (i32, i32) = (0, 0);
let (mut total_count, mut total_duration): (i32, DurationType) = (0, 0 as DurationType);
let (mut pass, mut fail, mut skip): (i32, i32, i32) = (0, 0, 0);
print!("\n");
for stat in self.results.iter() {
Expand All @@ -176,20 +217,24 @@ impl Drop for TestRunner {
};
total_count += 1;
total_duration += stat.duration;
let formatted_text = color.paint(format!("{} ({}) ... {}ns",
stat.title,
stat.criteria,
stat.duration));
println!("{}", formatted_text);
if self.module_path.len() > 0 {
println!("{}", color.paint(format!("{}::{}: {} ({}{})", self.module_path, stat.title, stat.criteria, stat.duration, self.time_unit.1)));
}
else {
println!("{}", color.paint(format!("{}: {} ({}{})", stat.title, stat.criteria, stat.duration, self.time_unit.1)));
}

}
if total_count == 1 {
println!("\nRan 1 test in {}{}", total_duration, self.time_unit.1);
}
else {
println!("\nRan {} tests in {}{}", total_count, total_duration, self.time_unit.1);
}
println!("\nRan {} test(s) in {}ns", total_count, total_duration);
let formatted_pass = Green.paint(format!("{} Passed", pass));
let formatted_failed = Red.paint(format!("{} Failed", fail));
let formatted_skipped = Yellow.paint(format!("{} Skipped", skip));
println!("{} {} {}",
formatted_pass,
formatted_failed,
formatted_skipped);
println!("{} {} {}", formatted_pass, formatted_failed, formatted_skipped);
}
}
}

0 comments on commit 8d88ba9

Please sign in to comment.