-
Notifications
You must be signed in to change notification settings - Fork 3
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
mullr
wants to merge
3
commits into
devel
Choose a base branch
from
background_support
base: devel
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+427
−375
Open
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. | ||
pub trait Step<C: TestContext> { | ||
fn eval(&self, &mut C) -> bool; | ||
} | ||
|
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,8 +6,9 @@ use combine::stream::state::State; | |
use combine::char::{string, digit}; | ||
|
||
extern crate rherkin; | ||
use rherkin::feature::{self, TestContext}; | ||
use rherkin::scenario::{self, Step, BoxedStep}; | ||
//use rherkin::feature; | ||
//use rherkin::scenario::{self, Step, BoxedStep, TestContext}; | ||
use rherkin::{ast, parser}; | ||
|
||
// An rpn calculator, something we can write tests for. | ||
#[derive(Debug)] | ||
|
@@ -80,7 +81,7 @@ impl Calculator { | |
} | ||
} | ||
|
||
impl TestContext for Calculator { | ||
impl ast::TestContext for Calculator { | ||
fn new() -> Calculator { | ||
Calculator { | ||
current: vec!(), | ||
|
@@ -93,7 +94,7 @@ mod steps { | |
use super::*; | ||
|
||
pub struct Clear { } | ||
impl Step<Calculator> for Clear { | ||
impl ast::Step<Calculator> for Clear { | ||
fn eval(&self, calc: &mut Calculator) -> bool { | ||
println!("Clear"); | ||
calc.current = vec!(); | ||
|
@@ -103,15 +104,15 @@ mod steps { | |
} | ||
|
||
pub struct Press { pub button: Button } | ||
impl Step<Calculator> for Press { | ||
impl ast::Step<Calculator> for Press { | ||
fn eval(&self, calc: &mut Calculator) -> bool { | ||
println!("Press {:?}", self.button); | ||
calc.press(&self.button) | ||
} | ||
} | ||
|
||
pub struct CheckDisplay { pub expected: String } | ||
impl Step<Calculator> for CheckDisplay { | ||
impl ast::Step<Calculator> for CheckDisplay { | ||
fn eval(&self, calc: &mut Calculator) -> bool { | ||
let actual = calc.stack.last(); | ||
println!("Check display: expected {:?}, actual {:#?}", self.expected, actual); | ||
|
@@ -177,15 +178,33 @@ Then the display should read 2 | |
let then = choice! { check_display }; | ||
|
||
let mut p = | ||
feature::parser( | ||
scenario::parser( | ||
given.map(|x| BoxedStep { val: Box::new(x) }), | ||
when.map(|x| BoxedStep { val: Box::new(x) }), | ||
then.map(|x| BoxedStep { val: Box::new(x) }))); | ||
parser::feature( | ||
parser::scenario( | ||
given.map(|x| parser::BoxedStep { val: Box::new(x) }), | ||
when.map (|x| parser::BoxedStep { val: Box::new(x) }), | ||
then.map (|x| parser::BoxedStep { val: Box::new(x) }))); | ||
|
||
let (f, remaining) = p.easy_parse(State::new(spec)).unwrap(); | ||
|
||
let (success, _) = f.eval(); | ||
assert!(success); | ||
} | ||
|
||
|
||
// fn proptests() { | ||
// let spec = r#" | ||
// Feature: RPN Calculator Property Specs | ||
|
||
// PropSpec: arbitrary addition | ||
// Given a fresh calculator | ||
// And a number A less than 10000 | ||
// And a number B less than 10000 | ||
// When I enter the number A | ||
// And I press enter | ||
// And I enter the number B | ||
// And I press plus | ||
// Then the displayed value should be less than 20000 | ||
// "#; | ||
|
||
// assert!(true) | ||
// } | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Dead code? |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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?