Skip to content

Commit

Permalink
Migrate flipper waterfall test to e2e (#1573)
Browse files Browse the repository at this point in the history
* Add basic flip test

* Add default constructor test

* Rename e2e tests to match unit tests
  • Loading branch information
ascjones authored Jan 4, 2023
1 parent dda44c6 commit 8c5ee37
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 0 deletions.
4 changes: 4 additions & 0 deletions examples/flipper/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ ink = { path = "../../crates/ink", default-features = false }
scale = { package = "parity-scale-codec", version = "3", default-features = false, features = ["derive"] }
scale-info = { version = "2.3", default-features = false, features = ["derive"], optional = true }

[dev-dependencies]
ink_e2e = { path = "../../crates/e2e" }

[lib]
name = "flipper"
path = "lib.rs"
Expand All @@ -24,3 +27,4 @@ std = [
"scale-info/std",
]
ink-as-dependency = []
e2e-tests = []
70 changes: 70 additions & 0 deletions examples/flipper/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,74 @@ pub mod flipper {
assert!(flipper.get());
}
}

#[cfg(all(test, feature = "e2e-tests"))]
mod e2e_tests {
use super::*;
use ink_e2e::build_message;

type E2EResult<T> = std::result::Result<T, Box<dyn std::error::Error>>;

#[ink_e2e::test]
async fn it_works(mut client: ink_e2e::Client<C, E>) -> E2EResult<()> {
// given
let constructor = FlipperRef::new(false);
let contract_acc_id = client
.instantiate("flipper", &ink_e2e::alice(), constructor, 0, None)
.await
.expect("instantiate failed")
.account_id;

let get = build_message::<FlipperRef>(contract_acc_id.clone())
.call(|flipper| flipper.get());
let get_res = client
.call(&ink_e2e::bob(), get, 0, None)
.await
.expect("get failed");
assert!(matches!(get_res.return_value(), Ok(false)));

// when
let flip = build_message::<FlipperRef>(contract_acc_id.clone())
.call(|flipper| flipper.flip());
let _flip_res = client
.call(&ink_e2e::bob(), flip, 0, None)
.await
.expect("flip failed");

// then
let get = build_message::<FlipperRef>(contract_acc_id.clone())
.call(|flipper| flipper.get());
let get_res = client
.call(&ink_e2e::bob(), get, 0, None)
.await
.expect("get failed");
assert!(matches!(get_res.return_value(), Ok(true)));

Ok(())
}

#[ink_e2e::test]
async fn default_works(mut client: ink_e2e::Client<C, E>) -> E2EResult<()> {
// given
let constructor = FlipperRef::new_default();

// when
let contract_acc_id = client
.instantiate("flipper", &ink_e2e::bob(), constructor, 0, None)
.await
.expect("instantiate failed")
.account_id;

// then
let get = build_message::<FlipperRef>(contract_acc_id.clone())
.call(|flipper| flipper.get());
let get_res = client
.call(&ink_e2e::bob(), get, 0, None)
.await
.expect("get failed");
assert!(matches!(get_res.return_value(), Ok(false)));

Ok(())
}
}
}

0 comments on commit 8c5ee37

Please sign in to comment.