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

Background support #2

Open
wants to merge 3 commits into
base: devel
Choose a base branch
from
Open
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Change TestCase from trait objects to enum
Previously, TestCase was a trait, and features were composed of boxed TestCase
trait objects. This changes it to an enum, since we expect to have only a small
number of TestCase types, and users are not expected to provide new ones.

This also changes the module layout; previously, the scenario and feature
parsers and AST structs were each in their own modules. This makes less sense
when using the enum at the TestCase level. Now all the AST structs are in the
AST module, and the parsers are in the parser module.
Russell Mull committed Jul 10, 2018

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
commit ceb0c6c07d0cca489c211be7cc85f192f3f1745d
77 changes: 77 additions & 0 deletions src/ast.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/// A test context is used to pass state between different steps of a test case.
/// It may also be initialized at the feature level via a Background (TODO)
pub trait TestContext {
fn new() -> Self;
}


pub enum TestCase<C: TestContext> {
Background(Scenario<C>),
Scenario(Scenario<C>),
}

impl<C: TestContext> TestCase<C> {
pub fn name(&self) -> String {
match self {
TestCase::Background(s) => s.name.clone(),
TestCase::Scenario(s) => s.name.clone()
}
}


pub fn eval(&self, context: &mut C) -> bool {
match self {
TestCase::Background(s) => s.eval(context),
TestCase::Scenario(s) => s.eval(context)
}
}
}

/// A feature is a collection of test cases.
pub struct Feature<C: TestContext> {
pub name: String,
pub comment: String,
pub test_cases: Vec<TestCase<C>>,
}

impl<C: TestContext> Feature<C> {
pub fn eval(&self) -> (bool, C) {
let mut context = C::new();

for tc in self.test_cases.iter() {
let pass = tc.eval(&mut context);

if !pass {
return (false, context);
}
}

(true, context)
}
}

pub struct Scenario<TC: TestContext> {
pub name: String,
pub steps: Vec<Box<Step<TC>>>,
}

impl<C: TestContext> Scenario<C> {
/// Execute a scenario by running each step in order, with mutable access to
/// the context.
pub fn eval(&self, context: &mut C) -> bool {
for s in self.steps.iter() {
if !s.eval(context) {
return false;
}
}

true
}
}

/// A specific step which makes up a scenario. Users should create their own
/// implementations of this trait, which are returned by their step parsers.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For rustdoc, I think the first line is supposed to be a summary of the functionality and then more details get added after a blank line. I think so it renders nicer?

pub trait Step<C: TestContext> {
fn eval(&self, &mut C) -> bool;
}

163 changes: 0 additions & 163 deletions src/feature.rs

This file was deleted.

4 changes: 2 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -3,8 +3,8 @@ extern crate combine;

extern crate itertools;

pub mod feature;
pub mod scenario;
pub mod parser;
pub mod ast;
pub mod parse_utils;


Loading