simplification
Download
61 lines 2.4 KiBNomsuModified 2026-11-08executable raw
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
#!/usr/bin/env nomsu -V8.0.0
###
    Tool to run all tests in a file (i.e. the code block inside a call to 'test $'). Usage:
    nomsu tools/test.nom file1 file2 directory1 ...
    
use "filesystem"
use "consolecolor"
use "commandline"

command line program with $args:
    for ($i = $filename) in $args.extras:
        ### Precompiled files don't have tests, so tests come from the source file
        $filename = ($filename, with "%.lua$" -> ".nom")
        $file = (read file $filename)
        unless $file:
            fail "Couldn't find \$filename"
        $env = (new environment)
        $env.COMPILING_TESTS = (yes)
        $env, run ($NomsuCode, from (Source $filename 1 #$file) $file)
        $file_tests = {}
        for ($src = $test) in $env.TESTS:
            add {.test = $test, .source = $src} to $file_tests
        
        unless ($file_tests is empty):
            sort $file_tests by $ -> $.source
            say "[ .. ] \$filename" inline
            call $io.flush
            if $args.v: say ""
            $failures = {}
            for $ in $file_tests:
                $src = ($Source, from string $.source)
                $l1 = ($file, line number at $src.start)
                $l2 = ($file, line number at $src.stop)
                if $args.v:
                    $code = ($file, from $src.start to $src.stop, matching ":[ ]*(.*)")
                    $indent = ($code, matching "\n([ ]*)")
                    if $indent: $code = ($code, with "\n\$indent" -> "\n")
                    say "    \(yellow ($code, with "\n" -> "\n    "))"
                
                if $args.e:
                    call $.test
                ..else:
                    try:
                        call $.test
                    ..if it fails with $msg:
                        $failure = ("
                            \(yellow "\($src.filename):\($l1)-\$l2:")
                            \(bright (red ($msg, indented)))
                        ")
                        add $failure to $failures
            
            if ($failures is empty):
                if $args.v:
                    say (green "PASS")
                ..else:
                    say "\r[\(green "PASS")"
            ..else:
                if $args.v:
                    say (red (bright "FAIL"))
                ..else:
                    say "\r[\(red (bright "FAIL"))"
                say "\(($failures joined with "\n"), indented)"