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
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
108 changes: 108 additions & 0 deletions src/ast.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
/// A test context is used to pass state between different steps of a test case.
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) -> Option<String> {
match self {
TestCase::Background(s) => s.name.clone(),
TestCase::Scenario(s) => s.name.clone()
}
}


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

pub struct TestResult<C: TestContext> {
pub test_case_name: String,
pub pass: bool,
pub context: C
}

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

impl<C: TestContext> Feature<C> {
pub fn eval(&self) -> Vec<TestResult<C>> {

let mut results = vec![];
for tc in self.test_cases.iter() {
let mut context = C::new();

if let Some(TestCase::Background(ref bg)) = self.background {
match bg.eval(context) {
mut r @ TestResult { pass: false, ..} => {
r.test_case_name = "<Background>".to_string();
results.push(r);
continue;
},
TestResult { pass: true, context: c, ..} => {
context = c;
}
}
}

results.push(tc.eval(context));
}

results
}
}

pub struct Scenario<TC: TestContext> {
pub name: Option<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, mut context: C) -> TestResult<C> {
for s in self.steps.iter() {
if !s.eval(&mut context) {
return TestResult {
test_case_name: match self.name.as_ref() {
Some(s) => s.clone(),
None => "".to_string()

Choose a reason for hiding this comment

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

I'm not sure what is more idiomatic, but in cases like this I usually use String::new()

},
pass: false,
context: context
};
}
}

TestResult {
test_case_name: match self.name.as_ref() {
Some(s) => s.clone(),
None => "".to_string()
},
pass: true,
context: context
}
}
}

/// 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
Expand Up @@ -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;


48 changes: 39 additions & 9 deletions src/parse_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use combine::Stream;
use combine::ParseError;

use combine::char::newline;
use combine::{Parser, many, many1, none_of, try};
use combine::{Parser, many, many1, none_of, try, eof};

/// Match a single non-newline character
///
Expand All @@ -31,9 +31,36 @@ where I: Stream<Item = char>,
none_of("\r\n".chars())
}

/// Parse one or more characters up to the next newline character, consuming it.
/// Return the characters consumed on the way to the newline, but not the
/// newline.

/// Parse either a newline() or an end-of-file marker, consuming any parsed
/// characters.
/// # Examples
//
/// ```
/// # extern crate combine;
/// # extern crate rherkin;
/// # use combine::*;
/// # use rherkin::parse_utils::eol;
/// # fn main() {
/// let mut parser1 = eol();
/// let result1 = parser1.parse("\n");
/// assert_eq!(result1, Ok(((), "")));
///
/// let mut parser2 = eol();
/// let result2 = parser2.parse("");
/// assert_eq!(result2, Ok(((), "")));
/// # }
/// ```
pub fn eol<I>() -> impl Parser<Input = I, Output = ()>
where I: Stream<Item = char>,
I::Error: ParseError<I::Item, I::Range, I::Position>
{
choice! { newline().map(|_| ()), eof() }
}

/// Parse one or more characters up to the end of line, using `eol()`. Return
/// the characters consumed on the way to the line end, but not any newline
/// character.
///
/// # Examples
//
Expand All @@ -43,17 +70,20 @@ where I: Stream<Item = char>,
/// # use combine::*;
/// # use rherkin::parse_utils::until_eol;
/// # fn main() {
/// let mut parser = until_eol();
/// let result = parser.parse("abc\ndef");
/// assert_eq!(result, Ok(("abc".to_string(), "def")));
/// let mut parser1 = until_eol();
/// let result1 = parser1.parse("abc\ndef");
/// assert_eq!(result1, Ok(("abc".to_string(), "def")));
///
/// let mut parser2 = until_eol();
/// let result2 = parser2.parse("def");
/// assert_eq!(result2, Ok(("def".to_string(), "")));
/// # }
/// ```
pub fn until_eol<I>() -> impl Parser<Input = I, Output = String>
where I: Stream<Item = char>,
I::Error: ParseError<I::Item, I::Range, I::Position>
{
( many1(non_newline()), newline() )
.map(|(s, _)| s)
( many1(non_newline()), eol()).map(|(s, _)| s)
}


Expand Down
Loading