Skip to content

Commit 92d67ba

Browse files
committed
feat: improve tests
1 parent 9b2750b commit 92d67ba

File tree

3 files changed

+806
-22
lines changed

3 files changed

+806
-22
lines changed

crates/iota-framework/packages/iota-framework/sources/account.move

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,3 +45,13 @@ native fun create_auth_info_v1_impl(
4545
public fun authenticator_df_name(): vector<u8> {
4646
AUTHENTICATOR_DF_NAME
4747
}
48+
49+
/// Creates an `AuthenticatorInfoV1` instance for testing, skipping validation.
50+
#[test_only]
51+
public fun create_auth_info_v1_for_testing(
52+
package: address,
53+
module_name: ascii::String,
54+
function_name: ascii::String,
55+
): AuthenticatorInfoV1 {
56+
AuthenticatorInfoV1{ package: package.to_id(), module_name, function_name }
57+
}

examples/move/iota_account/sources/iota_account.move

Lines changed: 15 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ public fun add_field<Name: copy + drop + store, Value: store>(
7676
ensure_tx_sender_is_account(self, ctx);
7777

7878
// Check if `name` is allowed to be used.
79-
check_df_name(&name);
79+
check_reserved_df_name(&name);
8080

8181
// Add a new field.
8282
dynamic_field::add(&mut self.id, name, value);
@@ -93,7 +93,7 @@ public fun remove_field<Name: copy + drop + store, Value: store>(
9393
ensure_tx_sender_is_account(self, ctx);
9494

9595
// Check if `name` is allowed to be used.
96-
check_df_name(&name);
96+
check_reserved_df_name(&name);
9797

9898
// Remove a new field and return it.
9999
dynamic_field::remove(&mut self.id, name)
@@ -120,12 +120,17 @@ public fun borrow_field_mut<Name: copy + drop + store, Value: store>(
120120
ensure_tx_sender_is_account(self, ctx);
121121

122122
// Check if `name` is allowed to be used.
123-
check_df_name(&name);
123+
check_reserved_df_name(&name);
124124

125125
// Borrow the related dynamic field.
126126
dynamic_field::borrow_mut(&mut self.id, name)
127127
}
128128

129+
/// Returns `true` if and only if `self` has a dynamic field with the specified `name`.
130+
public fun has_field<Name: copy + drop + store>(self: &IOTAccount, name: Name): bool {
131+
dynamic_field::exists_(&self.id, name)
132+
}
133+
129134
// --------------------------------------- Authentication ---------------------------------------
130135

131136
/// Rotates the account owner public key to a new one as well as the authenticator.
@@ -221,7 +226,7 @@ fun ensure_tx_sender_is_account(self: &IOTAccount, ctx: &TxContext) {
221226
}
222227

223228
/// Checks if `name` is allowed to be used for a user-defined dynamic field.
224-
fun check_df_name<Name: copy + drop + store>(name: &Name) {
229+
fun check_reserved_df_name<Name: copy + drop + store>(name: &Name) {
225230
// Check that `Name` is not `OwnerPublicKey`.
226231
assert!(std::type_name::get<Name>() != std::type_name::get<OwnerPublicKey>(), EOwnerPublicKeyCannotBeUsed);
227232

@@ -233,26 +238,14 @@ fun check_df_name<Name: copy + drop + store>(name: &Name) {
233238
);
234239
}
235240

236-
// --------------------------------------- Tests ---------------------------------------
241+
// --------------------------------------- Test Utilities ---------------------------------------
237242

238243
#[test_only]
239-
use std::string;
240-
241-
#[test]
242-
fun test_valid_dynamic_field_name() {
243-
check_df_name(&42);
244-
check_df_name(&b"vector");
245-
check_df_name(&string::utf8(b"std::string"));
244+
public fun create_owner_public_key_for_testing(): OwnerPublicKey {
245+
OwnerPublicKey{}
246246
}
247247

248-
#[test]
249-
#[expected_failure(abort_code = EOwnerPublicKeyCannotBeUsed)]
250-
fun test_owner_public_key_dynamic_field_name() {
251-
check_df_name(&OwnerPublicKey{});
252-
}
253-
254-
#[test]
255-
#[expected_failure(abort_code = EAuthenticatorDynamicFieldNameCannotBeUsed)]
256-
fun test_authenticator_dynamic_field_name() {
257-
check_df_name(&account::authenticator_df_name());
248+
#[test_only]
249+
public fun get_address(self: &IOTAccount): address {
250+
self.id.to_address()
258251
}

0 commit comments

Comments
 (0)