Improve test running

Move test running to a method on the Test class and improve output
printing logic.
This commit is contained in:
Nathan McCarty 2024-12-31 19:08:42 -05:00
parent 09ac7506ee
commit 4b2c2eb4dd
3 changed files with 40 additions and 24 deletions

View file

@ -41,26 +41,7 @@ multi MAIN(
say "{colored '**', 'magenta bold'} Testing $colored-module"
.indent(2);
for $module.tests -> $test {
try {
# FIXME this doesn't actually capture the exit code
idris-exec $test.expr, $module.source.relative, $test.output-type;
}
my $testf = colored $test.name, 'underline';
if $! ~~ ExpressionError {
# TODO: Don't show stdout if its empty
$module-failures += 1;
say "{colored '+', 'red'} $testf: {colored 'FAIL', 'red bold'}"
.indent(4);
say "stdout:".indent(6);
say $!.err.lines.map(*.indent(8)).join("\n");
say (colored 'exit code', 'red').indent(6),
": {$!.exit-code}";
} elsif $! {
die $!;
} else {
say "{colored '+', 'green'} $testf: {colored 'pass', 'green'}"
.indent(4);
}
$module-failures += 1 if $test.run: $module.source;
}
if $module-failures == 0 {
say "All $colored-module ".indent(4),

View file

@ -3,6 +3,8 @@ unit module IUtils;
need IUtils::IDEMode;
use Terminal::ANSIColor;
use IUtils::Regexes;
use IUtils::Compiler;
@ -16,6 +18,38 @@ class Test {
has Str:D $.expr is required;
#| The output type of the test
has ExprOutput:D $.output-type is required;
#| Run this test, and return true if it failed, false if it passed
method run(IO::Path:D $source, Int:D $indent-level? = 4 --> Bool) {
CATCH {
when ExpressionError {
say "{colored '+', 'red'} $.name: {colored 'FAIL', 'red bold'}"
.indent($indent-level);
say "{colored('exit code', 'red')}: {$_.exit-code}"
.indent($indent-level + 2);
if $_.out.trim {
say colored('stdout:', 'underline')
.indent($indent-level + 2);
say $_.out.trim.lines.map(*.indent($indent-level + 2))
.join("\n");
}
if $_.err.trim {
say colored('stderr:', 'underline')
.indent($indent-level + 2);
say $_.err.trim.lines.map(*.indent($indent-level + 2))
.join("\n");
}
return True;
}
}
idris-exec $.expr, $source.relative, $.output-type;
# The exception handler graps flow if the test failed, here the test passed
my $output =
"{colored '+', 'green'} $.name: {colored 'pass', 'green'}";
say $output.indent($indent-level);
return False;
}
}
#| Structure representing the tests in a module
@ -60,7 +94,6 @@ class PackageInfo {
when * eq 'Bool' {succeed Boolean};
when * eq 'Either' {succeed Either};
};
say $output-type;
my $test =
Test.new(name => $match<test-name>.Str,
expr => $match<expression-name>.Str,

View file

@ -88,9 +88,11 @@ my constant $bool-lambda =
'(\x => if x then exitSuccess else exitFailure)';
# TODO: Implemenent support for the Either case
# TODO: Use the ide protocol to drive this so we can avoid the user needing to
# import anything
# Exec the expression with the given name in the given file
# TODO: Use the ide protocol to drive this so we can avoid the user needing to import anything
#| Exec the expression with the given name in the given file
#|
#| Uses the provided $output-type to hook up an adaptor for tests returning a
#| non () value
sub idris-exec($expr, $file, $output-type? = Unit) is export {
# Have idris compile an executable for the expression,
given $output-type {