009 cross module mock

✓ Passing This code compiles and runs correctly.

Code

// Test: Cross-module mocking
//
// This test verifies mocking events from IMPORTED modules.
//
// The syntax `~std.fs:read_lines = failed "mock error"` means:
// "For this test, provide this implementation for the std.fs:read_lines event"
//
// This is NOT just for testing - Source blocks with impl declarations enable:
// - Compile-time code synthesis
// - AI-generated implementations
// - Remote code fetching
// - Interactive compilation
//
// The compiler becomes a development partner, not just a batch processor.

~import "$std/testing"
~import "$std/fs"

// A flow that depends on an external module event
~event process_config { path: []const u8 }
| ok { line_count: usize }
| failed { reason: []const u8 }

~process_config = std.fs:read_lines(path: path)
| lines l |> ok { line_count: l.len }
| failed msg |> failed { reason: msg }

// ============================================
// THE TEST - Cross-module mock
// ============================================

~test(Cross-module mock with failed branch) {
    // Mock the external module's event with the 'failed' branch
    // This returns a simple string ([]const u8), which works without array literals
    ~std.fs:read_lines = failed "mocked file read error"

    ~process_config(path: "/this/file/does/not/exist.txt")
    | ok |> assert.fail()
    | failed result |> assert(result.reason.len > 0)
}

// ============================================
// Array literal mock
// ============================================

~test(Cross-module mock should return 3 lines) {
    ~std.fs:read_lines = lines ["line1", "line2", "line3"]

    ~process_config(path: "/fake/path")
    | ok result |> assert(result.line_count == 3)
    | failed |> assert.fail()
}
input.kz

Test Configuration

Post-validation Script:

#!/bin/bash
# Run the actual Zig tests to verify cross-module mocking works at runtime
cd "$(dirname "$0")"
zig test output_emitted.zig 2>&1
exit $?