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

test view function with struct arguments and generic type parameters #7761

Merged
merged 7 commits into from
Apr 14, 2023
Merged
Show file tree
Hide file tree
Changes from 5 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
Original file line number Diff line number Diff line change
Expand Up @@ -105,4 +105,10 @@ module 0xCAFE::test {
std::vector::reverse(&mut s);
std::string::utf8(s)
}

#[view]
public fun get_state<T: key>(o: Object<T>): String acquires ModuleData {
let addr = aptos_std::object::object_address(&o);
borrow_global<ModuleData>(addr).state
}
}
81 changes: 74 additions & 7 deletions aptos-move/e2e-move-tests/src/tests/constructor_args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,12 @@ use aptos_types::{
on_chain_config::FeatureFlag,
transaction::{ExecutionStatus, TransactionStatus},
};
use move_core_types::{language_storage::TypeTag, parser::parse_struct_tag, vm_status::StatusCode};
use move_core_types::{
identifier::Identifier,
language_storage::{StructTag, TypeTag},
parser::parse_struct_tag,
vm_status::StatusCode,
};
use serde::{Deserialize, Serialize};

/// Mimics `0xcafe::test::ModuleData`
Expand All @@ -21,13 +26,15 @@ const OBJECT_ADDRESS: AccountAddress = AccountAddress::new([
0x90, 0x71, 0xAA, 0x3F, 0xBD, 0x2A, 0xB9, 0x51, 0x37, 0xF7, 0xCB, 0xAD, 0x13, 0x6F, 0x09, 0x2B,
]);

fn success(tests: Vec<(&str, Vec<Vec<u8>>, &str)>) {
success_generic(vec![], tests)
fn success(h: &mut MoveHarness, tests: Vec<(&str, Vec<Vec<u8>>, &str)>) {
success_generic(h, vec![], tests)
}

fn success_generic(ty_args: Vec<TypeTag>, tests: Vec<(&str, Vec<Vec<u8>>, &str)>) {
let mut h = MoveHarness::new_with_features(vec![FeatureFlag::STRUCT_CONSTRUCTORS], vec![]);

fn success_generic(
h: &mut MoveHarness,
ty_args: Vec<TypeTag>,
tests: Vec<(&str, Vec<Vec<u8>>, &str)>,
) {
// Load the code
let acc = h.new_account_at(AccountAddress::from_hex_literal("0xcafe").unwrap());
assert_success!(h.publish_package(&acc, &common::test_dir_path("constructor_args.data/pack")));
Expand Down Expand Up @@ -56,6 +63,29 @@ fn success_generic(ty_args: Vec<TypeTag>, tests: Vec<(&str, Vec<Vec<u8>>, &str)>
}
}

fn success_generic_view(
h: &mut MoveHarness,
ty_args: Vec<TypeTag>,
tests: Vec<(&str, Vec<Vec<u8>>, &str)>,
) {
// Load the code
let acc = h.new_account_at(AccountAddress::from_hex_literal("0xcafe").unwrap());
assert_success!(h.publish_package(&acc, &common::test_dir_path("constructor_args.data/pack")));

let module_data = parse_struct_tag("0xCAFE::test::ModuleData").unwrap();

// Check in initial state, resource does not exist.
assert!(!h.exists_resource(acc.address(), module_data.clone()));

for (entry, args, expected) in tests {
let res = h.execute_view_function(str::parse(entry).unwrap(), ty_args.clone(), args);
assert!(res.is_ok(), "{}", res.err().unwrap().to_string());
let bcs = res.unwrap().pop().unwrap();
let res = bcs::from_bytes::<String>(&bcs).unwrap();
assert_eq!(res, expected);
}
}

type Closure = Box<dyn FnOnce(TransactionStatus) -> bool>;

fn fail(tests: Vec<(&str, Vec<Vec<u8>>, Closure)>) {
Expand Down Expand Up @@ -130,7 +160,44 @@ fn constructor_args_good() {
),
];

success(tests);
let mut h = MoveHarness::new_with_features(vec![FeatureFlag::STRUCT_CONSTRUCTORS], vec![]);

success(&mut h, tests);
}

#[test]
fn view_constructor_args() {
let tests = vec![
// ensure object exist
("0xcafe::test::initialize", vec![], ""),
// make state equal hi
(
"0xcafe::test::object_arg",
vec![
bcs::to_bytes("hi").unwrap(),
bcs::to_bytes(&OBJECT_ADDRESS).unwrap(),
],
"hi",
),
];

let mut h = MoveHarness::new_with_features(vec![FeatureFlag::STRUCT_CONSTRUCTORS], vec![]);

success(&mut h, tests);

let view = vec![(
"0xcafe::test::get_state",
vec![bcs::to_bytes(&OBJECT_ADDRESS).unwrap()],
"hi",
)];
let module_data_struct = StructTag {
address: AccountAddress::from_hex_literal("0xcafe").expect("valid address"),
module: Identifier::new("test").expect("valid identifier"),
name: Identifier::new("ModuleData").expect("valid identifier"),
type_params: vec![],
};
let module_data_type = TypeTag::Struct(Box::new(module_data_struct));
success_generic_view(&mut h, vec![module_data_type], view);
}

#[test]
Expand Down