From 27c26d3bd695b22abfb715b592f85ef1df18bd5b Mon Sep 17 00:00:00 2001 From: Dmitry Lavrenov Date: Thu, 4 Jan 2024 13:17:33 +0300 Subject: [PATCH] Add decrease_balance related tests --- frame/evm-balances/src/tests/fungible.rs | 74 +++++++++++++++++++++++- 1 file changed, 73 insertions(+), 1 deletion(-) diff --git a/frame/evm-balances/src/tests/fungible.rs b/frame/evm-balances/src/tests/fungible.rs index 96f295acf2..022e3b7470 100644 --- a/frame/evm-balances/src/tests/fungible.rs +++ b/frame/evm-balances/src/tests/fungible.rs @@ -2,7 +2,10 @@ use frame_support::{ assert_noop, assert_ok, - traits::fungible::{Inspect, Unbalanced}, + traits::{ + fungible::{Inspect, Unbalanced}, + tokens::Precision, + }, }; use sp_core::H160; use sp_runtime::TokenError; @@ -167,3 +170,72 @@ fn set_total_issuance_works() { assert_eq!(EvmBalances::total_issuance(), set_total_issuance_balance); }); } + +#[test] +fn decrease_balance_works_exact_expendable() { + new_test_ext().execute_with_ext(|_| { + // Check test preconditions. + assert_eq!(EvmBalances::total_balance(&alice()), INIT_BALANCE); + + let decreased_balance = 100; + + // Invoke the function under test. + assert_ok!(EvmBalances::decrease_balance( + &alice(), + decreased_balance, + Precision::Exact, + Preservation::Expendable, + Fortitude::Polite + )); + + // Assert state changes. + assert_eq!( + EvmBalances::total_balance(&alice()), + INIT_BALANCE - decreased_balance + ); + }); +} + +#[test] +fn decrease_balance_works_best_effort_preserve() { + new_test_ext().execute_with_ext(|_| { + // Check test preconditions. + assert_eq!(EvmBalances::total_balance(&alice()), INIT_BALANCE); + + let decreased_balance = INIT_BALANCE + 1; + + // Invoke the function under test. + assert_ok!(EvmBalances::decrease_balance( + &alice(), + decreased_balance, + Precision::BestEffort, + Preservation::Preserve, + Fortitude::Polite + )); + + // Assert state changes. + assert_eq!(EvmBalances::total_balance(&alice()), 1); + }); +} + +#[test] +fn decrease_balance_fails_funds_unavailable() { + new_test_ext().execute_with_ext(|_| { + // Check test preconditions. + assert_eq!(EvmBalances::total_balance(&alice()), INIT_BALANCE); + + let decreased_balance = INIT_BALANCE + 1; + + // Invoke the function under test. + assert_noop!( + EvmBalances::decrease_balance( + &alice(), + decreased_balance, + Precision::Exact, + Preservation::Preserve, + Fortitude::Polite + ), + TokenError::FundsUnavailable + ); + }); +}