Skip to content

Core\Test\RunResult::stdout

$runResult->stdout(): string

stdout() returns the text that one run of your program wrote to standard output. In a test, Core\Test::run() runs your own program as a command. The result is a Core\Test\RunResult, and stdout() reads the output from it as a string.

The result contains everything the program printed with echo or Core\Cli::write, in order. It also contains the text printed before exit, or before an error the program did not catch. If the program printed nothing, the result is an empty string. Messages written to standard error are not part of it. stderr() returns those.

A control character, such as a carriage return, is replaced by a visible character before it is written. The program prints the same text when you start it with nvs run.

stdout() throws a RuntimeError when the output is not valid UTF-8. bytes() returns the same output as bytes, and never throws. Each call to stdout() returns the same text.

The examples below read one line a program printed and give a program some input. The last one is a test that compares the whole report a command prints.

Return value

Returns string. Everything the program wrote with echo, in order. If the program wrote nothing, the result is an empty string.

Errors

  • RuntimeErrorThe output is not valid UTF-8, so it is not a string. Read it with bytes().

Changelog

VersionChange
0.0.5Core\Test::run runs your program as a command, with the arguments you give and an optional stdin. It returns a Core\Test\RunResult with the exit code, the standard output and the standard error. The program runs inside the test's own process.

Examples

Read what the program printed

<?nvs
// Shows how to read the text that a run of your own program printed.
use Core\Test;
if ((Core\Cli::arguments()[0] ?? '') == 'greet') {
// This part is the program. It prints one line.
echo 'Hello, ', Core\Cli::arguments()[1] ?? '', "\n";
} else {
var $run = Test::run(['greet', 'Ada']);
// `stdout()` returns everything the program printed, with the newline at the end.
// This prints `The program printed: Hello, Ada`.
echo 'The program printed: ', $run->stdout();
}
Output
The program printed: Hello, Ada

Send input to the program

<?nvs
// Shows how to give a run some input and read the output it prints.
use Core\Test;
if ((Core\Cli::arguments()[0] ?? '') == 'shout') {
// This part is the program. It reads its input and prints it in capital letters.
echo Core\Str::upper(Core\IO::stdin());
} else {
// `stdin` is the input of the program.
var $run = Test::run(['shout'], { stdin: "milk\nbread\n" });
// This prints `MILK` and `BREAD`, each on its own line.
echo $run->stdout();
}
Output
MILK
BREAD

Test the report a command prints

<?nvs
// Shows how to test the report that a command-line tool prints.
use Core\Test;
class Tool
{
#[Core\Command(name: 'report', about: 'Print the orders of one day')]
public static function report(string $day): uint
{
// A real tool would read the orders from a database here.
echo 'orders on ', $day, ": 3\n";
echo "total: 42.50 EUR\n";
return 0;
}
}
if ((Core\Cli::arguments()[0] ?? '') != '') {
// This part is the tool.
exit(Core\Command::run() as int);
} else {
// This part is the test. It compares the whole output with the report it expects.
var $run = Test::run(['report', '2024-05-01']);
Test::assertEquals($run->stdout(), "orders on 2024-05-01: 3\ntotal: 42.50 EUR\n");
// The check passed, so this prints `the report is correct`.
echo "the report is correct\n";
}
Output
the report is correct