Core\Test\RunResult::bytes
$runResult->bytes(): bytesbytes() returns what one run of your program wrote to standard output, as bytes. In a test, Core\Test::run() runs your own program as a command. The result is a Core\Test\RunResult, and bytes() reads the output from it. The type bytes is raw binary data.
The result contains every byte of the standard output, in order. It is the same output that stdout() returns as a string. If the program printed nothing, the result is empty bytes.
bytes() never throws. stdout() throws a RuntimeError when the output is not valid UTF-8, so use bytes() for such an output. bytes() is also useful when you need to count bytes, or compare the output byte for byte with ==.
You can call bytes() as often as you like. Each call returns the same bytes.
The examples below count the bytes of an output and show the bytes as hex digits. The last one is a test that compares an export with the bytes it expects.
Return value
Returns bytes. Every byte of the standard output, in order. The bytes do not have to be valid UTF-8.
Changelog
| Version | Change |
|---|---|
0.0.5 | Core\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
Count the bytes of the output
<?nvs// Shows that `bytes()` returns the output of a run as bytes, so you can count every byte.
use Core\Test;
if ((Core\Cli::arguments()[0] ?? '') == 'price') { // This part is the program. It prints a price in euros. echo "12 €\n";} else { var $run = Test::run(['price']);
// The output has 5 characters. The `€` sign is 3 bytes in UTF-8, so it has 7 bytes. // This prints `7 bytes, 5 characters`. echo Core\Bytes::length($run->bytes()), ' bytes, ', Core\Str::length($run->stdout()), " characters\n";}7 bytes, 5 charactersSee invisible characters as hex
<?nvs// Shows how to find characters you cannot see in the output, such as a tab.
use Core\Test;
if ((Core\Cli::arguments()[0] ?? '') == 'header') { // This part is the program. A tab separates the two column names. echo "name\tprice\n";} else { var $run = Test::run(['header']);
// `Core\Encoding::toHex` shows each byte as two hex digits. // The tab is `09` and the newline is `0a`. This prints `6e616d65097072696365 0a`. var $hex = Core\Encoding::toHex($run->bytes()); echo Core\Str::slice($hex, 0, 20), ' ', Core\Str::slice($hex, 20), "\n";}6e616d65097072696365 0aTest an export byte for byte
<?nvs// Shows how to test that an export command prints exactly the bytes you expect.
use Core\Test;
class Tool{ #[Core\Command(name: 'export', about: 'Print the customers as CSV')] public static function export(): uint { // A real tool would read the customers from a database here. echo "id,name\n"; echo "1,Zoë\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. `==` compares two `bytes` values byte for byte. var $expected = "id,name\n1,Zoë\n" as bytes; Test::assertTrue(Test::run(['export'])->bytes() == $expected);
// The check passed, so this prints `the export has the expected bytes`. echo "the export has the expected bytes\n";}the export has the expected bytes