Skip to content

Commit

Permalink
cargo fmt
Browse files Browse the repository at this point in the history
  • Loading branch information
utkarshkukreti committed Jan 11, 2019
1 parent c7359dc commit 3fb7f81
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 46 deletions.
19 changes: 8 additions & 11 deletions src/block.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use proc_macro2::Span;
use syn;
use syn::synom::Synom;
use unicode_xid::UnicodeXID;
use proc_macro2::Span;

pub struct Root(pub(crate) Describe);

Expand Down Expand Up @@ -35,7 +35,7 @@ pub enum Block {
Describe(Describe),
It(It),
Bench(Bench),
Item(syn::Item)
Item(syn::Item),
}

impl Synom for Block {
Expand All @@ -50,11 +50,10 @@ impl Synom for Block {
));
}


enum DescribeBlock {
Regular(Block),
Before(syn::Block),
After(syn::Block)
After(syn::Block),
}

impl Synom for DescribeBlock {
Expand All @@ -77,8 +76,8 @@ impl Synom for DescribeBlock {
pub struct Describe {
pub name: syn::Ident,
pub before: Vec<syn::Block>,
pub after: Vec<syn::Block>,
pub blocks: Vec<Block>
pub after: Vec<syn::Block>,
pub blocks: Vec<Block>,
}

impl Synom for Describe {
Expand All @@ -96,12 +95,11 @@ impl Synom for Describe {
));
}


#[derive(Clone)]
pub struct It {
pub name: syn::Ident,
pub attributes: Vec<syn::Attribute>,
pub block: syn::Block
pub block: syn::Block,
}

impl Synom for It {
Expand All @@ -121,12 +119,11 @@ impl Synom for It {
));
}


#[derive(Clone)]
pub struct Bench {
pub name: syn::Ident,
pub ident: syn::Ident,
pub block: syn::Block
pub block: syn::Block,
}

impl Synom for Bench {
Expand All @@ -138,7 +135,7 @@ impl Synom for Bench {
punct!(|) >>
ident: syn!(syn::Ident) >>
punct!(|) >>

block: syn!(syn::Block) >>

(Bench {
Expand Down
6 changes: 3 additions & 3 deletions src/generator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ impl Generate for Block {
fn generate(self, up: Option<&Describe>) -> TokenStream {
match self {
Block::Describe(describe) => describe.generate(up),
Block::It(it) => it.generate(up),
Block::Bench(bench) => bench.generate(up),
Block::Item(item) => item.into_token_stream()
Block::It(it) => it.generate(up),
Block::Bench(bench) => bench.generate(up),
Block::Item(item) => item.into_token_stream(),
}
}
}
Expand Down
28 changes: 14 additions & 14 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
//! `speculate` is a crate that provides a very simple macro
//! that is used to easily and elegantly define unit tests in Rust.
//!
//!
//! Please see the documentation for the [`speculate`](./fn.speculate.html) macro
//! for more information and examples.

#![cfg_attr(feature="nightly", feature(proc_macro_span))]
#![cfg_attr(feature = "nightly", feature(proc_macro_span))]

extern crate proc_macro;
extern crate proc_macro2;
Expand All @@ -23,40 +23,40 @@ use generator::Generate;

use proc_macro::TokenStream;

#[cfg(not(feature="nightly"))]
#[cfg(not(feature = "nightly"))]
use std::sync::atomic::{AtomicUsize, Ordering, ATOMIC_USIZE_INIT};

#[cfg(feature="nightly")]
#[cfg(feature = "nightly")]
fn get_root_name() -> proc_macro2::Ident {
let start_line = proc_macro::Span::call_site().start().line;
let module_name = format!("speculate_{}", start_line);
return syn::Ident::new(&module_name, proc_macro2::Span::call_site());
}

// TODO: Get rid of this once proc_macro_span stabilises
#[cfg(not(feature="nightly"))]
#[cfg(not(feature = "nightly"))]
static GLOBAL_SPECULATE_COUNT: AtomicUsize = ATOMIC_USIZE_INIT;

#[cfg(not(feature="nightly"))]
#[cfg(not(feature = "nightly"))]
fn get_root_name() -> proc_macro2::Ident {
let count = GLOBAL_SPECULATE_COUNT.fetch_add(1, Ordering::SeqCst);
let module_name = format!("speculate_{}", count);
return syn::Ident::new(&module_name, proc_macro2::Span::call_site());
}

/// Creates a `test` module using a friendly syntax.
///
///
/// Inside this block, the following elements can be used:
///
/// * `describe` (or its alias `context`) - to group tests in a hierarchy, for
/// readability. Can be arbitrarily nested.
///
///
/// * `before` - contains setup code that's inserted before every sibling and nested
/// `it` and `bench` blocks.
///
///
/// * `after` - contains teardown code that's inserted after every sibling and
/// nested `it` and `bench` blocks.
///
///
/// * `it` (or its alias `test`) - contains tests.
///
/// For example:
Expand Down Expand Up @@ -108,11 +108,11 @@ fn get_root_name() -> proc_macro2::Ident {
/// }
/// # }
/// ```
///
///
/// * Any other Rust "Item", such as `static`, `const`, `fn`, etc.
///
///
/// # Example
///
///
/// ```rust
/// #[macro_use] extern crate speculate as other_speculate;
/// # fn main() {}
Expand Down Expand Up @@ -150,7 +150,7 @@ fn get_root_name() -> proc_macro2::Ident {
pub fn speculate(input: TokenStream) -> TokenStream {
let input: proc_macro2::TokenStream = input.into();
let mut root = syn::parse2::<Root>(input).unwrap();

root.0.name = get_root_name();

let mut prefix = quote!( #[allow(non_snake_case)] );
Expand Down
30 changes: 15 additions & 15 deletions tests/basic.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
#[cfg(not(feature="nightly"))]
#[cfg(not(feature = "nightly"))]
#[macro_use]
extern crate speculate as other_speculate;

#[cfg(feature="nightly")]
#[cfg(feature = "nightly")]
extern crate speculate as other_speculate;

#[cfg(feature="nightly")]
#[cfg(feature = "nightly")]
use other_speculate::speculate;

pub fn zero() -> u32 {
Expand Down Expand Up @@ -81,16 +81,16 @@ speculate! {

// Parsing edge cases
mod ec1 {
#[cfg(feature="nightly")]
#[cfg(feature = "nightly")]
use other_speculate::speculate;

speculate!{}
speculate! {}
}

mod ec2 {
#[cfg(feature="nightly")]
#[cfg(feature = "nightly")]
use other_speculate::speculate;

speculate! {
before {}
it "works" {}
Expand All @@ -103,27 +103,27 @@ mod ec2 {
}

mod ec3 {
#[cfg(feature="nightly")]
#[cfg(feature = "nightly")]
use other_speculate::speculate;

speculate! {
it "foo" {}
}
}

mod ec4 {
#[cfg(feature="nightly")]
#[cfg(feature = "nightly")]
use other_speculate::speculate;

speculate! {
after {}
}
}

mod ec5 {
#[cfg(feature="nightly")]
#[cfg(feature = "nightly")]
use other_speculate::speculate;

speculate! {
before {}
it "foo" {}
Expand All @@ -132,9 +132,9 @@ mod ec5 {
}

mod attributes {
#[cfg(feature="nightly")]
#[cfg(feature = "nightly")]
use other_speculate::speculate;

speculate! {
#[ignore]
test "ignore" {
Expand Down
6 changes: 3 additions & 3 deletions tests/example.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
#[cfg(not(feature="nightly"))]
#[cfg(not(feature = "nightly"))]
#[macro_use]
extern crate speculate as other_speculate;

#[cfg(feature="nightly")]
#[cfg(feature = "nightly")]
extern crate speculate as other_speculate;

#[cfg(feature="nightly")]
#[cfg(feature = "nightly")]
use other_speculate::speculate;

speculate! {
Expand Down

0 comments on commit 3fb7f81

Please sign in to comment.