Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove restrictions #51

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
name = "rspec"
description = "Write Rspec-like tests with stable rust"
version = "1.0.0"
edition = "2018"

readme = "README.md"
repository = "https://github.com/rust-rspec/rspec"
Expand Down Expand Up @@ -32,7 +33,6 @@ version = "0.0.153"
colored = "2.0"
derive-new = "0.5"
derive_builder = "0.9"
rayon = "1.5"
time = "0.2"

[dependencies.expectest]
Expand Down
19 changes: 4 additions & 15 deletions src/block/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@
//! Running these tests and doing asserts is not the job of the Context, but the Runner.
//!

use block::{Block, Example};
use header::{ContextHeader, ContextLabel, ExampleHeader, ExampleLabel};
use report::ExampleResult;
use crate::block::{Block, Example};
use crate::header::{ContextHeader, ContextLabel, ExampleHeader, ExampleLabel};
use crate::report::ExampleResult;

/// Test contexts are a convenient tool for adding structure and code sharing to a test suite.
pub struct Context<T> {
Expand Down Expand Up @@ -47,12 +47,6 @@ impl<T> Context<T> {
}
}

// Both `Send` and `Sync` are necessary for parallel threaded execution.
unsafe impl<T> Send for Context<T> where T: Send {}

// Both `Send` and `Sync` are necessary for parallel threaded execution.
unsafe impl<T> Sync for Context<T> where T: Sync {}

impl<T> Context<T>
where
T: Clone,
Expand Down Expand Up @@ -98,7 +92,6 @@ where
pub fn context<F>(&mut self, name: &'static str, body: F)
where
F: FnOnce(&mut Context<T>),
T: ::std::fmt::Debug,
{
let header = ContextHeader {
label: ContextLabel::Context,
Expand All @@ -115,7 +108,6 @@ where
pub fn specify<F>(&mut self, name: &'static str, body: F)
where
F: FnOnce(&mut Context<T>),
T: ::std::fmt::Debug,
{
let header = ContextHeader {
label: ContextLabel::Specify,
Expand All @@ -132,7 +124,6 @@ where
pub fn when<F>(&mut self, name: &'static str, body: F)
where
F: FnOnce(&mut Context<T>),
T: ::std::fmt::Debug,
{
let header = ContextHeader {
label: ContextLabel::When,
Expand Down Expand Up @@ -187,15 +178,13 @@ where
pub fn scope<F>(&mut self, body: F)
where
F: FnOnce(&mut Context<T>),
T: ::std::fmt::Debug,
{
self.context_internal(None, body)
}

fn context_internal<F>(&mut self, header: Option<ContextHeader>, body: F)
where
F: FnOnce(&mut Context<T>),
T: ::std::fmt::Debug,
{
let mut child = Context::new(header);
body(&mut child);
Expand Down Expand Up @@ -542,7 +531,7 @@ impl<T> Default for Context<T> {

#[cfg(test)]
mod tests {
use block::{describe, given, suite};
use crate::block::{describe, given, suite};

macro_rules! test_suite_alias {
($suite: ident) => {
Expand Down
7 changes: 2 additions & 5 deletions src/block/example.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use header::ExampleHeader;
use report::ExampleResult;
use crate::header::ExampleHeader;
use crate::report::ExampleResult;

/// Test examples are the smallest unit of a testing framework, wrapping one or more assertions.
pub struct Example<T> {
Expand Down Expand Up @@ -36,6 +36,3 @@ impl<T> Example<T> {
Example::new(ExampleHeader::default(), |_| ExampleResult::Failure(None))
}
}

unsafe impl<T> Send for Example<T> where T: Send {}
unsafe impl<T> Sync for Example<T> where T: Sync {}
9 changes: 3 additions & 6 deletions src/block/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ pub mod context;
pub mod example;
pub mod suite;

pub use block::context::*;
pub use block::example::*;
pub use block::suite::*;
pub use crate::block::context::*;
pub use crate::block::example::*;
pub use crate::block::suite::*;

/// Blocks are used to build a tree structure of named tests and contextes.
pub enum Block<T> {
Expand All @@ -22,6 +22,3 @@ impl<T> Block<T> {
}
}
}

unsafe impl<T> Send for Block<T> where T: Send {}
unsafe impl<T> Sync for Block<T> where T: Sync {}
15 changes: 6 additions & 9 deletions src/block/suite.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use block::Context;
use header::{SuiteHeader, SuiteLabel};
use crate::block::Context;
use crate::header::{SuiteHeader, SuiteLabel};

/// Test suites bundle a set of closely related test examples into a logical execution group.
#[derive(new)]
Expand All @@ -23,9 +23,6 @@ impl<T> Suite<T> {
}
}

unsafe impl<T> Send for Suite<T> where T: Send {}
unsafe impl<T> Sync for Suite<T> where T: Sync {}

/// Creates a test suite from a given root context.
///
/// # Examples
Expand Down Expand Up @@ -62,7 +59,7 @@ unsafe impl<T> Sync for Suite<T> where T: Sync {}
pub fn suite<F, T>(name: &'static str, environment: T, body: F) -> Suite<T>
where
F: FnOnce(&mut Context<T>),
T: Clone + ::std::fmt::Debug,
T: Clone,
{
let header = SuiteHeader {
label: SuiteLabel::Suite,
Expand All @@ -79,7 +76,7 @@ where
pub fn describe<F, T>(name: &'static str, environment: T, body: F) -> Suite<T>
where
F: FnOnce(&mut Context<T>),
T: Clone + ::std::fmt::Debug,
T: Clone,
{
let header = SuiteHeader {
label: SuiteLabel::Describe,
Expand All @@ -96,7 +93,7 @@ where
pub fn given<F, T>(name: &'static str, environment: T, body: F) -> Suite<T>
where
F: FnOnce(&mut Context<T>),
T: Clone + ::std::fmt::Debug,
T: Clone,
{
let header = SuiteHeader {
label: SuiteLabel::Given,
Expand All @@ -108,7 +105,7 @@ where
fn suite_internal<F, T>(header: SuiteHeader, environment: T, body: F) -> Suite<T>
where
F: FnOnce(&mut Context<T>),
T: Clone + ::std::fmt::Debug,
T: Clone,
{
let mut ctx = Context::new(None);
body(&mut ctx);
Expand Down
4 changes: 2 additions & 2 deletions src/header/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ mod tests {
fn label_fmt() {
fn subject(label: ContextLabel) -> String {
format!("{}", label)
};
}
assert_eq!(subject(ContextLabel::Context), "Context".to_owned());
assert_eq!(subject(ContextLabel::Specify), "Specify".to_owned());
assert_eq!(subject(ContextLabel::When), "When".to_owned());
Expand All @@ -49,7 +49,7 @@ mod tests {
fn header_fmt() {
fn subject(label: ContextLabel) -> String {
format!("{}", ContextHeader::new(label, "Test"))
};
}
assert_eq!(
subject(ContextLabel::Context),
"Context \"Test\"".to_owned()
Expand Down
4 changes: 2 additions & 2 deletions src/header/example.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ mod tests {
fn label_fmt() {
fn subject(label: ExampleLabel) -> String {
format!("{}", label)
};
}
assert_eq!(subject(ExampleLabel::Example), "Example".to_owned());
assert_eq!(subject(ExampleLabel::It), "It".to_owned());
assert_eq!(subject(ExampleLabel::Then), "Then".to_owned());
Expand All @@ -57,7 +57,7 @@ mod tests {
fn header_fmt() {
fn subject(label: ExampleLabel) -> String {
format!("{}", ExampleHeader::new(label, "Test"))
};
}
assert_eq!(
subject(ExampleLabel::Example),
"Example \"Test\"".to_owned()
Expand Down
6 changes: 3 additions & 3 deletions src/header/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ pub mod context;
pub mod example;
pub mod suite;

pub use header::context::*;
pub use header::example::*;
pub use header::suite::*;
pub use crate::header::context::*;
pub use crate::header::example::*;
pub use crate::header::suite::*;
4 changes: 2 additions & 2 deletions src/header/suite.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ mod tests {
fn label_fmt() {
fn subject(label: SuiteLabel) -> String {
format!("{}", label)
};
}
assert_eq!(subject(SuiteLabel::Suite), "Suite".to_owned());
assert_eq!(subject(SuiteLabel::Describe), "Describe".to_owned());
assert_eq!(subject(SuiteLabel::Given), "Given".to_owned());
Expand All @@ -49,7 +49,7 @@ mod tests {
fn header_fmt() {
fn subject(label: SuiteLabel) -> String {
format!("{}", SuiteHeader::new(label, "Test"))
};
}
assert_eq!(subject(SuiteLabel::Suite), "Suite \"Test\"".to_owned());
assert_eq!(
subject(SuiteLabel::Describe),
Expand Down
13 changes: 6 additions & 7 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ extern crate derive_new;
extern crate colored;
#[cfg(feature = "expectest_compat")]
extern crate expectest;
extern crate rayon;
extern crate time;

pub mod block;
Expand All @@ -23,11 +22,11 @@ pub mod runner;

mod visitor;

pub use block::{describe, given, suite};
pub use logger::Logger;
pub use runner::{Configuration, ConfigurationBuilder, Runner};
pub use crate::block::{describe, given, suite};
pub use crate::logger::Logger;
pub use crate::runner::{Configuration, ConfigurationBuilder, Runner};

use block::Suite;
use crate::block::Suite;

/// A wrapper for conveniently running a test suite with
/// the default configuration with considerebly less glue-code.
Expand All @@ -49,7 +48,7 @@ use block::Suite;
/// ```
pub fn run<T>(suite: &Suite<T>)
where
T: Clone + Send + Sync + ::std::fmt::Debug,
T: Clone,
{
use std::io;
use std::sync::Arc;
Expand All @@ -65,7 +64,7 @@ where
mod tests {

pub use super::*;
pub use block::*;
pub use crate::block::*;

// Test list:
// x check that tests can call `assert_eq!`
Expand Down
46 changes: 10 additions & 36 deletions src/logger/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ mod serial;

use std::io;

use header::{ContextHeader, ExampleHeader, SuiteHeader};
use logger::serial::SerialLogger;
use report::{BlockReport, ContextReport, ExampleReport, SuiteReport};
use runner::{Runner, RunnerObserver};
use crate::header::{ContextHeader, ExampleHeader, SuiteHeader};
use crate::logger::serial::SerialLogger;
use crate::report::{BlockReport, ContextReport, ExampleReport, SuiteReport};
use crate::runner::{Runner, RunnerObserver};

/// Preferred logger for test suite execution.
pub struct Logger<T: io::Write> {
Expand Down Expand Up @@ -84,52 +84,26 @@ where
T: Send + Sync,
{
fn enter_suite(&self, runner: &Runner, header: &SuiteHeader) {
if runner.configuration.parallel {
// If the suite is being evaluated in parallel we basically wait for `exit_suite`.
} else {
self.serial.enter_suite(runner, header);
}
self.serial.enter_suite(runner, header);
}

fn exit_suite(&self, runner: &Runner, header: &SuiteHeader, report: &SuiteReport) {
if runner.configuration.parallel {
// If the suite is being evaluated in parallel and we have reached the end of it,
// then it is time to forward a replay of the events to the inner serial logger:
self.replay_suite(runner, header, report);
} else {
self.serial.exit_suite(runner, header, report);
}
self.serial.exit_suite(runner, header, report);
}

fn enter_context(&self, runner: &Runner, header: &ContextHeader) {
if runner.configuration.parallel {
// If the suite is being evaluated in parallel we basically wait for `exit_suite`.
} else {
self.serial.enter_context(runner, header);
}
self.serial.enter_context(runner, header);
}

fn exit_context(&self, runner: &Runner, header: &ContextHeader, report: &ContextReport) {
if runner.configuration.parallel {
// If the suite is being evaluated in parallel we basically wait for `exit_suite`.
} else {
self.serial.exit_context(runner, header, report);
}
self.serial.exit_context(runner, header, report);
}

fn enter_example(&self, runner: &Runner, header: &ExampleHeader) {
if runner.configuration.parallel {
// If the suite is being evaluated in parallel we basically wait for `exit_suite`.
} else {
self.serial.enter_example(runner, header);
}
self.serial.enter_example(runner, header);
}

fn exit_example(&self, runner: &Runner, header: &ExampleHeader, report: &ExampleReport) {
if runner.configuration.parallel {
// If the suite is being evaluated in parallel we basically wait for `exit_suite`.
} else {
self.serial.exit_example(runner, header, report);
}
self.serial.exit_example(runner, header, report);
}
}
8 changes: 5 additions & 3 deletions src/logger/serial.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@ use time::Duration;

use colored::*;

use header::{ContextHeader, ExampleHeader, SuiteHeader};
use report::{BlockReport, ContextReport, ExampleReport, ExampleResult, Report, SuiteReport};
use runner::{Runner, RunnerObserver};
use crate::header::{ContextHeader, ExampleHeader, SuiteHeader};
use crate::report::{
BlockReport, ContextReport, ExampleReport, ExampleResult, Report, SuiteReport,
};
use crate::runner::{Runner, RunnerObserver};

#[derive(new)]
struct SerialLoggerState<T: io::Write = io::Stdout> {
Expand Down
2 changes: 1 addition & 1 deletion src/report/context.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use report::{BlockReport, Report};
use crate::report::{BlockReport, Report};
use time::Duration;

/// `ContextReport` holds the results of a context's test execution.
Expand Down
2 changes: 1 addition & 1 deletion src/report/example.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::convert::From;

use time::Duration;

use report::Report;
use crate::report::Report;

#[cfg(feature = "expectest_compat")]
use expectest::core::TestResult as ExpectestResult;
Expand Down
Loading