[][src]Crate casperlabs_engine_test_support

A library to support testing of Wasm smart contracts for use on the CasperLabs Platform.

Example

Consider a contract held in "contract.wasm" which stores an arbitrary String under a Key named "special_value":

use casperlabs_contract::{contract_api::{runtime, storage}, unwrap_or_revert::UnwrapOrRevert};
use casperlabs_types::{Key, ApiError};
const KEY: &str = "special_value";

#[no_mangle]
pub extern "C" fn call() {
    let value: String = runtime::get_arg(0)
        .unwrap_or_revert_with(ApiError::MissingArgument)
        .unwrap_or_revert_with(ApiError::InvalidArgument);

    let value_ref = storage::new_turef(value);
    let value_key: Key = value_ref.into();
    runtime::put_key(KEY, value_key);
}

The test could be written as follows:

use casperlabs_engine_test_support::{TestContextBuilder, SessionBuilder, Value, Error, Code};

const MY_ACCOUNT: [u8; 32] = [7u8; 32];
const KEY: &str = "special_value";
const VALUE: &str = "hello world";

let mut context = TestContextBuilder::new()
    .with_account(MY_ACCOUNT, 128_000_000.into())
    .build();

// The test framework checks for compiled Wasm files in '<current working dir>/wasm'.  Paths
// relative to the current working dir (e.g. 'wasm/contract.wasm') can also be used, as can
// absolute paths.
let session_code = Code::from("contract.wasm");
let session_args = (VALUE,);
let session = SessionBuilder::new(session_code, session_args)
    .with_address(MY_ACCOUNT)
    .with_authorization_keys(&[MY_ACCOUNT])
    .build();

let result_of_query: Result<Value, Error> = context
    .run(session)
    .query(MY_ACCOUNT, &[KEY]);

let returned_value = result_of_query.expect("should be a value");

let expected_value = Value::from_t(VALUE.to_string()).expect("should construct Value");
assert_eq!(expected_value, returned_value);

Structs

Error

The error type returned by any casperlabs-engine-test-support operation.

Session

A single session, i.e. a single request to execute a single deploy within the test context.

SessionBuilder

Builder for a Session.

TestContext

Context in which to run a test of a Wasm smart contract.

TestContextBuilder

Builder for a TestContext.

Value

A value stored under a given key on the network.

Enums

Code

Represents the types of session or payment code.

Constants

DEFAULT_ACCOUNT_ADDR

Default test account address.

DEFAULT_ACCOUNT_INITIAL_BALANCE

Default initial balance of a test account in motes.

Type Definitions

Address

An address of an entity (e.g. an account or key) on the network.

Hash

The hash of a smart contract stored on the network, which can be used to reference the contract.

Result

A specialized std::result::Result for this crate.

URefAddr

The address of a URef (unforgeable reference) on the network.