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

Add merge test #248

Merged
merged 1 commit into from
Aug 28, 2017
Merged
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
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ shred-derive = "0.3"
tuple_utils = "0.2"
rayon = "0.8.2"

futures = { version = "0.1.14", optional = true }
futures = { version = "0.1", optional = true }
serde = { version = "1.0", optional = true }
serde_derive = { version = "1.0", optional = true }

Expand Down
91 changes: 90 additions & 1 deletion src/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -198,4 +198,93 @@ fn retain_mut<T, F>(vec: &mut Vec<T>, mut f: F)
if del > 0 {
vec.truncate(len - del);
}
}
}

#[cfg(test)]
mod test {

use std::error::Error;
use std::fmt::{Display, Formatter, Result as FmtResult};

use futures::task;
use futures::Poll;
use futures::future::{Future, FutureResult, result};

use common::{BoxedErr, Errors, Merge};
use Component;
use DispatcherBuilder;
use storage::{NullStorage, VecStorage};
use world::World;
#[test]
fn test_merge() {
#[derive(Default)]
struct TestComponent;

impl Component for TestComponent {
type Storage = NullStorage<Self>;
}

struct TestFuture {
result: FutureResult<TestComponent, BoxedErr>,
}

impl Future for TestFuture {
type Item = TestComponent;
type Error = BoxedErr;

fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
task::current(); // This function called purely to see if we can.
// Futures will expect to be able to call this function without panicking.
self.result.poll()
}
}

impl Component for TestFuture {
type Storage = VecStorage<Self>;
}

#[derive(Debug)]
struct TestError;

impl Display for TestError {
fn fmt(&self, fmt: &mut Formatter) -> FmtResult {
fmt.write_str("TestError")
}
}

impl Error for TestError {
fn description(&self) -> &str {
"An error used for testing"
}

fn cause(&self) -> Option<&Error> {
None
}
}

let mut world = World::new();
world.add_resource(Errors::new());
world.register::<TestComponent>();
world.register::<TestFuture>();
let success = world.create_entity()
.with(TestFuture {
result: result(Ok(TestComponent))
}).build();
let error = world.create_entity()
.with(TestFuture {
result: result(Err(BoxedErr::new(TestError)))
}).build();

let system: Merge<TestFuture> = Merge::new();

let mut dispatcher = DispatcherBuilder::new().add(system, "merge", &[]).build();

// Sequential dispatch used in order to avoid missing panics due to them happening in
// another thread.
dispatcher.dispatch_seq(&mut world.res);
let components = world.read::<TestComponent>();
assert!(components.get(success).is_some());
assert!(components.get(error).is_none());
assert_eq!(world.read_resource::<Errors>().errors.len(), 1);
}
}