ludic/packages/ludic.wallet/tests/wallet_test.ludic

60 lines
2 KiB
Text

# wallet_test.ludic - earn, spend only what is there, lose down to zero, a fact per change, and
# its own save section
import "ludic.wallet"
import "ludic.base"
program WalletTest {
numbers float
function changes(wallet_st: WalletState) -> []MoneyChanged { return q_drain(wallet_changes(wallet_st)) }
function waiting(wallet_st: WalletState) -> int { return q_len(wallet_changes(wallet_st)) }
test "earning and spending move the purse, and each says so" (wallet_st: mut WalletState) {
wallet_reset(wallet_st)
wallet_earn(wallet_st, 40)
expect(wallet_spend(wallet_st, 15))
expect_eq(wallet_money(wallet_st), 25)
let cs = changes(wallet_st)
expect_eq(len(cs), 2)
expect_eq(cs[0].after, 40)
expect_eq(cs[1].before, 40)
expect_eq(cs[1].after, 25)
}
test "a spend past what is there is refused and changes nothing" (wallet_st: mut WalletState) {
wallet_reset(wallet_st)
wallet_set(wallet_st, 10)
changes(wallet_st)
expect(not wallet_spend(wallet_st, 11))
expect_eq(wallet_money(wallet_st), 10)
expect_eq(waiting(wallet_st), 0)
expect(wallet_can(wallet_st, 10))
expect(not wallet_can(wallet_st, 11))
}
test "a loss happens anyway, but never below zero" (wallet_st: mut WalletState) {
wallet_reset(wallet_st)
wallet_set(wallet_st, 7)
wallet_lose(wallet_st, 20)
expect_eq(wallet_money(wallet_st), 0)
}
test "setting what is already there is no change" (wallet_st: mut WalletState) {
wallet_reset(wallet_st)
wallet_set(wallet_st, 5)
changes(wallet_st)
wallet_set(wallet_st, 5)
expect_eq(waiting(wallet_st), 0)
}
test "a save section reads back the same purse" (wallet_st: mut WalletState) {
wallet_reset(wallet_st)
wallet_set(wallet_st, 123)
let root = save_tree()
save_section(root, "wallet", 1, wallet_save(wallet_st))
let text = save_encode(root)
wallet_reset(wallet_st)
let n = load_section(save_decode(text), "wallet")
wallet_load(wallet_st, n.data, n.version)
expect_eq(wallet_money(wallet_st), 123)
}
}