Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Move decode test to an integration test
  • Loading branch information
ascjones committed Apr 12, 2022
commit 6069a70e4139b5133c664b6be96c54ab2ff8af45
112 changes: 0 additions & 112 deletions src/cmd/decode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,115 +65,3 @@ impl DecodeCommand {
Ok(())
}
}

#[cfg(test)]
mod tests {
use crate::util::tests::with_new_contract_project;
use std::path::Path;

/// Create a `cargo contract` command
fn cargo_contract(path: &Path) -> assert_cmd::Command {
let mut cmd = assert_cmd::Command::cargo_bin(env!("CARGO_PKG_NAME")).unwrap();
cmd.current_dir(path).arg("contract");
cmd
}

#[test]
fn decode_works() {
// given
let contract = r#"
#![cfg_attr(not(feature = "std"), no_std)]

use ink_lang as ink;

#[ink::contract]
mod switcher {
#[ink(event)]
pub struct Switched {
new_value: bool,
}

#[ink(storage)]
pub struct Switcher {
value: bool,
}

impl Switcher {
#[ink(constructor, selector = 0xBABEBABE)]
pub fn new(init_value: bool) -> Self {
Self { value: init_value }
}

#[ink(message, selector = 0xBABEBABE)]
pub fn switch(&mut self, value: bool) {
self.value = value;
}
}
}"#;

// when
// contract is built
with_new_contract_project(|manifest_path| {
let project_dir = manifest_path.directory().expect("directory must exist");
let lib = project_dir.join("lib.rs");
std::fs::write(&lib, contract)?;

assert_cmd::Command::new("rustup")
.arg("override")
.arg("set")
.arg("nightly")
.assert()
.success();

log::info!("Building contract in {}", project_dir.to_string_lossy());
cargo_contract(project_dir).arg("build").assert().success();

let msg_data: &str = "babebabe01";
let msg_decoded: &str = r#"switch { value: true }"#;

// then
// message data is being decoded properly
cargo_contract(project_dir)
.arg("decode")
.arg("--data")
.arg(msg_data)
.arg("-t")
.arg("message")
.assert()
.success()
.stdout(predicates::str::contains(msg_decoded));

let event_data: &str = "080001";
let event_decoded: &str = r#"Switched { new_value: true }"#;

// and
// event data is being decoded properly
cargo_contract(project_dir)
.arg("decode")
.arg("--data")
.arg(event_data)
.arg("-t")
.arg("event")
.assert()
.success()
.stdout(predicates::str::contains(event_decoded));

let constructor_data: &str = "babebabe00";
let constructor_decoded: &str = r#"new { init_value: false }"#;

// and
// event data is being decoded properly
cargo_contract(project_dir)
.arg("decode")
.arg("-d")
.arg(constructor_data)
.arg("-t")
.arg("constructor")
.assert()
.success()
.stdout(predicates::str::contains(constructor_decoded));

Ok(())
})
}
}
134 changes: 134 additions & 0 deletions tests/decode.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
// Copyright 2018-2020 Parity Technologies (UK) Ltd.
// This file is part of cargo-contract.
//
// cargo-contract is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// cargo-contract is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with cargo-contract. If not, see <http://www.gnu.org/licenses/>.

use std::path::Path;

/// Create a `cargo contract` command
fn cargo_contract<P: AsRef<Path>>(path: P) -> assert_cmd::Command {
let mut cmd = assert_cmd::Command::cargo_bin(env!("CARGO_PKG_NAME")).unwrap();
cmd.current_dir(path).arg("contract");
cmd
}

#[test]
fn decode_works() {
// given
let contract = r#"
#![cfg_attr(not(feature = "std"), no_std)]

use ink_lang as ink;

#[ink::contract]
mod switcher {
#[ink(event)]
pub struct Switched {
new_value: bool,
}

#[ink(storage)]
pub struct Switcher {
value: bool,
}

impl Switcher {
#[ink(constructor, selector = 0xBABEBABE)]
pub fn new(init_value: bool) -> Self {
Self { value: init_value }
}

#[ink(message, selector = 0xBABEBABE)]
pub fn switch(&mut self, value: bool) {
self.value = value;
}
}
}"#;

let tmp_dir = tempfile::Builder::new()
.prefix("cargo-contract.cli.test.")
.tempdir()
.expect("temporary directory creation failed");

// cargo contract new decode_test
cargo_contract(tmp_dir.path())
.arg("new")
.arg("switcher")
.assert()
.success();

let project_dir = tmp_dir.path().to_path_buf().join("switcher");

let lib = project_dir.join("lib.rs");
std::fs::write(&lib, contract).expect("Failed to write contract lib.rs");

assert_cmd::Command::new("rustup")
.arg("override")
.arg("set")
.arg("nightly")
.assert()
.success();

log::info!("Building contract in {}", project_dir.to_string_lossy());
cargo_contract(&project_dir)
.arg("build")
.arg("--skip-linting")
.assert()
.success();

let msg_data: &str = "babebabe01";
let msg_decoded: &str = r#"switch { value: true }"#;

// then
// message data is being decoded properly
cargo_contract(&project_dir)
.arg("decode")
.arg("--data")
.arg(msg_data)
.arg("-t")
.arg("message")
.assert()
.success()
.stdout(predicates::str::contains(msg_decoded));

let event_data: &str = "080001";
let event_decoded: &str = r#"Switched { new_value: true }"#;

// and
// event data is being decoded properly
cargo_contract(&project_dir)
.arg("decode")
.arg("--data")
.arg(event_data)
.arg("-t")
.arg("event")
.assert()
.success()
.stdout(predicates::str::contains(event_decoded));

let constructor_data: &str = "babebabe00";
let constructor_decoded: &str = r#"new { init_value: false }"#;

// and
// event data is being decoded properly
cargo_contract(&project_dir)
.arg("decode")
.arg("-d")
.arg(constructor_data)
.arg("-t")
.arg("constructor")
.assert()
.success()
.stdout(predicates::str::contains(constructor_decoded));
}