✓
Passing This code compiles and runs correctly.
Code
// Test: Complex flow with chained events and mocks
//
// This is the REAL test of the testing framework:
// - Real program events defined outside tests
// - Subflow chains multiple events together
// - Tests mock impure events, call real flows
// - Pure events called directly (no mock needed)
~import std/testing
// ============================================
// THE ACTUAL PROGRAM
// ============================================
// Database layer (impure - external DB)
~event db.get-user { id: u32 }
| found { id: u32, balance: u32 }
| not-found
// Payment service (impure - external API)
~event payment.charge { user_id: u32, amount: u32 }
| success []const u8
| failed []const u8
// Pure validation (no side effects)
~event validate.amount { amount: u32, balance: u32 }
| valid
| insufficient
~[pure] proc validate.amount|zig {
if (amount <= balance) {
return .{ .valid = .{} };
} else {
return .{ .insufficient = .{} };
}
}
// The main flow - chains everything together
~event process-payment { user_id: u32, amount: u32 }
| success []const u8
| user-not-found
| insufficient-funds
~process-payment = db.get-user(id: user_id)
| found u |> validate.amount(amount, u.balance)
| valid |> payment.charge(user_id: u.id, amount)
| success tx => success tx
| failed _ => insufficient-funds
| insufficient => insufficient-funds
| not-found => user-not-found
// ============================================
// THE TESTS
// ============================================
~test(Successful payment) {
~db.get-user => found { id: 42, balance: 100 }
~payment.charge => success "tx123"
~process-payment(user_id: 42, amount: 50)
| success s |> assert(s.len == 5) // "tx123" has 5 chars
}
~test(Insufficient funds rejected) {
~db.get-user => found { id: 42, balance: 30 }
~process-payment(user_id: 42, amount: 50)
| insufficient-funds |> assert.ok()
}
~test(User not found) {
~db.get-user => not-found
~process-payment(user_id: 999, amount: 50)
| user-not-found |> assert.ok()
}
Test Configuration
Post-validation Script:
#!/bin/bash
# Run the actual Zig tests to verify mocks work correctly
cd "$(dirname "$0")"
# Run zig test on the generated output
zig test output_emitted.zig 2>&1
exit $?