Skip to content

Commit

Permalink
test(gas_service): additional coverage (#59)
Browse files Browse the repository at this point in the history
* test(gas-service): additional coverage

* test(gas-service): using random value
  • Loading branch information
re1ro authored Jun 23, 2024
1 parent 767ba19 commit 34a30e2
Showing 1 changed file with 63 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,9 @@ module gas_service::gas_service {
fun test_pay_gas() {
let ctx = &mut sui::tx_context::dummy();
let (mut service, cap) = new(ctx);
let value = 10;
// 2 bytes of the digest for a pseudo-random 1..65,536
let digest = ctx.digest();
let value = (((digest[0] as u16) << 8) | (digest[1] as u16) as u64) + 1;
let c: Coin<SUI> = coin::mint_for_testing(value, ctx);

service.pay_gas(
Expand All @@ -209,7 +211,8 @@ module gas_service::gas_service {
fun test_add_gas() {
let ctx = &mut sui::tx_context::dummy();
let (mut service, cap) = new(ctx);
let value = 10;
let digest = ctx.digest();
let value = (((digest[0] as u16) << 8) | (digest[1] as u16) as u64) + 1; // 1..65,536
let c: Coin<SUI> = coin::mint_for_testing(value, ctx);

service.add_gas(
Expand All @@ -229,7 +232,8 @@ module gas_service::gas_service {
fun test_collect_gas() {
let ctx = &mut sui::tx_context::dummy();
let (mut service, cap) = new(ctx);
let value = 10;
let digest = ctx.digest();
let value = (((digest[0] as u16) << 8) | (digest[1] as u16) as u64) + 1; // 1..65,536
let c: Coin<SUI> = coin::mint_for_testing(value, ctx);

service.add_gas(
Expand All @@ -256,7 +260,8 @@ module gas_service::gas_service {
fun test_refund() {
let ctx = &mut sui::tx_context::dummy();
let (mut service, cap) = new(ctx);
let value = 10;
let digest = ctx.digest();
let value = (((digest[0] as u16) << 8) | (digest[1] as u16) as u64) + 1; // 1..65,536
let c: Coin<SUI> = coin::mint_for_testing(value, ctx);

service.add_gas(
Expand All @@ -279,4 +284,58 @@ module gas_service::gas_service {
cap.destroy_cap();
service.destroy();
}

#[test]
#[expected_failure(abort_code = sui::balance::ENotEnough)]
fun test_collect_gas_insufficient_balance() {
let ctx = &mut sui::tx_context::dummy();
let (mut service, cap) = new(ctx);
let digest = ctx.digest();
let value = (((digest[0] as u16) << 8) | (digest[1] as u16) as u64) + 1; // 1..65,536
let c: Coin<SUI> = coin::mint_for_testing(value, ctx);

service.add_gas(
c,
std::ascii::string(b"message id"),
@0x0,
vector[],
);

service.collect_gas(
&cap,
ctx.sender(),
value + 1,
ctx,
);

cap.destroy_cap();
service.destroy();
}

#[test]
#[expected_failure(abort_code = sui::balance::ENotEnough)]
fun test_refund_insufficient_balance() {
let ctx = &mut sui::tx_context::dummy();
let (mut service, cap) = new(ctx);
let value = 10;
let c: Coin<SUI> = coin::mint_for_testing(value, ctx);

service.add_gas(
c,
std::ascii::string(b"message id"),
@0x0,
vector[],
);

service.refund(
&cap,
std::ascii::string(b"message id"),
ctx.sender(),
value + 1,
ctx,
);

cap.destroy_cap();
service.destroy();
}
}

0 comments on commit 34a30e2

Please sign in to comment.