Skip to content

Commit

Permalink
Resolve merge conflict.
Browse files Browse the repository at this point in the history
  • Loading branch information
71 committed Jul 10, 2018
2 parents 04c2ff0 + 46d9640 commit 0dbbfd1
Show file tree
Hide file tree
Showing 7 changed files with 76 additions and 25 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "speculate"
version = "0.0.26"
version = "0.0.27"
authors = ["Utkarsh Kukreti <utkarshkukreti@gmail.com>"]
description = "An RSpec inspired minimal testing framework for Rust."
repository = "https://github.com/utkarshkukreti/speculate.rs"
Expand Down
19 changes: 19 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,25 @@ Inside `speculate! { ... }`, you can have any "Item", like `static`, `const`,
}
```

You can optionally add attributes to this block:

```rust
#[ignore]
test "ignore" {
assert_eq!(1, 2);
}

#[should_panic]
test "should panic" {
assert_eq!(1, 2);
}

#[should_panic(expected = "foo")]
test "should panic with foo" {
panic!("foo");
}
```

* `bench` - contains benchmarks.

For example:
Expand Down
1 change: 1 addition & 0 deletions src/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ pub struct Describe {
#[derive(Clone, Debug)]
pub struct It {
pub name: String,
pub attributes: Vec<ast::Attribute>,
pub block: P<ast::Block>,
}

Expand Down
30 changes: 16 additions & 14 deletions src/generator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use syntax::ast;
use syntax::ext::base::ExtCtxt;
use syntax::ptr::P;

use block::{Block, Describe, It, Bench};
use block::{Bench, Block, Describe, It};

pub trait Generate {
fn generate(self, cx: &mut ExtCtxt, up: Option<&Describe>) -> P<ast::Item>;
Expand All @@ -24,19 +24,17 @@ impl Generate for Describe {
let name = cx.ident_of(&self.name);

if let Some(ref up) = up {
self.before = up.before
self.before = up
.before
.iter()
.chain(self.before.iter())
.cloned()
.collect();
self.after = self.after
.iter()
.chain(up.after.iter())
.cloned()
.collect();
self.after = self.after.iter().chain(up.after.iter()).cloned().collect();
}

let items = self.blocks
let items = self
.blocks
.iter()
.map(|block| block.clone().generate(cx, Some(&self)))
.collect::<Vec<_>>();
Expand All @@ -45,8 +43,7 @@ impl Generate for Describe {
#[allow(unused_imports)]
use super::*;
$items
})
.unwrap()
}).unwrap()
}
}

Expand All @@ -65,11 +62,12 @@ impl Generate for It {
vec![self.block]
};

let attributes = self.attributes;
let mut blocks = blocks.into_iter();
let head = blocks.next().unwrap();
let block = blocks.fold(head, merge_blocks);

quote_item!(cx, #[test] fn $name() { $block }).unwrap()
quote_item!(cx, #[test] $attributes fn $name() { $block }).unwrap()
}
}

Expand All @@ -95,14 +93,18 @@ impl Generate for Bench {
let ident = self.ident;
quote_item!(cx, #[bench] fn $name($ident: &mut ::test::Bencher) {
$block
})
.unwrap()
}).unwrap()
}
}

fn merge_blocks(left: P<ast::Block>, right: P<ast::Block>) -> P<ast::Block> {
P(ast::Block {
stmts: left.stmts.iter().chain(right.stmts.iter()).cloned().collect(),
stmts: left
.stmts
.iter()
.chain(right.stmts.iter())
.cloned()
.collect(),
..(*left).clone()
})
}
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ use syntax::util::small_vector::SmallVector;
use generator::Generate;

mod block;
mod parser;
mod generator;
mod parser;

#[plugin_registrar]
pub fn plugin_registrar(reg: &mut Registry) {
Expand Down
28 changes: 19 additions & 9 deletions src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use syntax::parse::parser::Parser;
use syntax::parse::token;
use syntax::symbol::Symbol;

use block::{Block, Describe, It, Bench};
use block::{Bench, Block, Describe, It};

pub fn parse(root_name: &str, parser: &mut Parser) -> Describe {
parse_describe(Symbol::intern(root_name), parser)
Expand All @@ -18,6 +18,11 @@ fn parse_describe(name: Symbol, parser: &mut Parser) -> Describe {
break;
}

let mut attributes = vec![];
while let token::Pound = parser.token {
attributes.push(parser.parse_attribute(false).unwrap());
}

let span = parser.span;
if let token::Ident(ident, _ident_style) = parser.token {
match &*ident.name.as_str() {
Expand All @@ -39,6 +44,7 @@ fn parse_describe(name: Symbol, parser: &mut Parser) -> Describe {

blocks.push(Block::It(It {
name: name.to_string(),
attributes: attributes,
block: block,
}))
}
Expand Down Expand Up @@ -78,19 +84,23 @@ fn parse_describe(name: Symbol, parser: &mut Parser) -> Describe {
None => {}
}
} else {
let message = format!("Expected an item, `describe`, `context`, \
`before`, `after`, `it`, `test`, or `bench`, \
found `{}`",
otherwise);
let message = format!(
"Expected an item, `describe`, `context`, \
`before`, `after`, `it`, `test`, or `bench`, \
found `{}`",
otherwise
);
panic!("{:?}", parser.span_fatal(span, &message))
}
}
}
} else {
let message = format!("Expected an item, `describe`, `context`, \
`before`, `after`, `it`, `test`, or `bench`, \
found `{:?}`",
parser.token);
let message = format!(
"Expected an item, `describe`, `context`, \
`before`, `after`, `it`, `test`, or `bench`, \
found `{:?}`",
parser.token
);
panic!("{:?}", parser.span_fatal(span, &message))
}
}
Expand Down
19 changes: 19 additions & 0 deletions tests/basic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,3 +106,22 @@ mod ec5 {
after {}
}
}

mod attributes {
speculate! {
#[ignore]
test "ignore" {
assert_eq!(1, 2);
}

#[should_panic]
test "should panic" {
assert_eq!(1, 2);
}

#[should_panic(expected = "foo")]
test "should panic with foo" {
panic!("foo");
}
}
}

0 comments on commit 0dbbfd1

Please sign in to comment.