Programming Language

Nocter

A self-contained systems language built around simplicity, encapsulation, and foolproof design.

/development/std/process/capture_session_tests_darwin.nct

capture_session_tests_darwin.nct

//! Native qualification for owning capture-session buffers and stream separation.

see ./capture_session_darwin.nct
see ./pipe_darwin.nct

use /internal/os/darwin
use /ptr

test capture_session_owns_and_separates_both_complete_streams {
    match create_capture_session() {
        CapturePreparationAttempt.failed(_) {
            return error.new("std.process.test", "capture session creation failed")
        }
        CapturePreparationAttempt.ready(session) {
            var capture = move session
            let stdout_bytes: [u8; 3] = [11, 12, 13]
            let stderr_bytes: [u8; 2] = [21, 22]
            let stdout_write = darwin.syscall3(
                darwin.SYS_WRITE,
                pipe_write_fd(&capture.stdout_pipe),
                ptr.addr(ptr.from_ref(&stdout_bytes[0])),
                3,
            )
            let stderr_write = darwin.syscall3(
                darwin.SYS_WRITE,
                pipe_write_fd(&capture.stderr_pipe),
                ptr.addr(ptr.from_ref(&stderr_bytes[0])),
                2,
            )
            if stdout_write.errno != 0 || stdout_write.value != 3
                || stderr_write.errno != 0 || stderr_write.value != 2 {
                return error.new("std.process.test", "capture session fixture write failed")
            }

            match drain_captured_streams(move capture) {
                CaptureSessionAttempt.failed(_) {
                    return error.new("std.process.test", "capture session drain failed")
                }
                CaptureSessionAttempt.malformed {
                    return error.new("std.process.test", "capture session drain was malformed")
                }
                CaptureSessionAttempt.complete(stdout, stderr) {
                    if stdout.len() != 3 || stdout[0] != 11 || stdout[1] != 12
                        || stdout[2] != 13 {
                        return error.new("std.process.test", "stdout capture changed")
                    }
                    if stderr.len() != 2 || stderr[0] != 21 || stderr[1] != 22 {
                        return error.new("std.process.test", "stderr capture changed")
                    }
                }
            }
        }
    }
    return
}