view tests/run.d @ 20:95e3940d91d4

Small changes to the text program. * Sort test-cases by name * Color output on linux
author Anders Halager <halager@gmail.com>
date Fri, 18 Apr 2008 20:28:49 +0200
parents 2a7b05d2e4f9
children 1a7a308f75b2
line wrap: on
line source

module run.d;

import tango.io.Stdout,
       tango.core.Array,
       tango.io.FilePath,
       tango.io.GrowBuffer,
       tango.io.UnicodeFile,
       tango.io.stream.BufferStream,
       tango.text.Util,
       tango.io.protocol.Reader,
       tango.io.protocol.Writer,
       tango.text.Unicode,
       tango.sys.Process;


char[] prog = "./Dang";

void main(char[][] args)
{
    auto cPath = FilePath("tests");

    ubyte success_success, success_failure, failure_failure, failure_success;

    foreach( path ; cPath.toList((FilePath path, bool isFolder){return isFolder;}))
    {
        Stdout(path.name)(":").newline;
        auto paths = path.toList(
                (FilePath path, bool isFolder)
                {
                    if(path.ext == "d" && path.name[0] != '.')
                        return true;
                    return false;
                });
        sort(paths, (FilePath a, FilePath b) { return a.name < b.name; });
        foreach (p ; paths)
        {
            auto test = new Test(p);    
            ubyte result = test.run();

            switch(result)
            {
                case 0:
                    success_success++;
                    break;
                case 1:
                    success_failure++;
                    break;
                case 2:
                    failure_failure++;
                    break;
                case 3:
                    failure_success++;
                    break;
            }

        }
    }

    Stdout().newline.newline()
        ("Result:").newline()
        ("  - Succes/Success:   ")(success_success).newline()
        ("  - Succes/Failure:   ")(success_failure).newline()
        ("  - Failure/Failure:  ")(failure_failure).newline()
        ("  - Failure/Success:  ")(failure_success).newline;
}

class Test
{
    FilePath target;
    public this(FilePath target)
    {
        this.target = target;
    }

    public ubyte run()
    {
        auto process = new Process(prog,"--gen-llvm",target.path~target.file);

        auto file = new UnicodeFile!(char)(target.path~target.file, Encoding.UTF_8);

        int mode;

        char[] data = file.read;
        if(data.length > 6 && data[0..6] == "//test")
        {
            char[] str = data.splitLines()[0][6..$];

            switch(toLower(trim(str)))
            {
                case "fail":
                case "failure":
                    mode = 1;
                    break;
                default:
                    mode = 0;
            }
        }

        Stdout.format("  {,-25}", target.file);

        process.execute;
        auto result = process.wait;

        if(result.status == 0)
        {
            auto llvm_process = new Process("llvm-as");
            llvm_process.execute;
            llvm_process.stdin.copy(process.stdout);
            llvm_process.stdin.close();
            result = llvm_process.wait;
        }

        return resultOf(result.status, mode);
    }

    private int resultOf(int status, int mode)
    {
        char[] good(char[] s)
        {
            version (Posix)
                return "\033[1;32m" ~ s ~ "\033[m";
            else
                return s;
        }

        char[] bad(char[] s)
        {
            version (Posix)
                return "\033[1;31m" ~ s ~ "\033[m";
            else
                return s;
        }

        if(status == 0)
        {
            if(mode == 0)
            {
                Stdout(good("SUCCES")).newline;
                return 0;
            }
            if(mode == 1)
            {
                Stdout(bad("SUCCES - Unexpected")).newline;
                return 3;
            }
        }
        else
        {
            if(mode == 1)
            {
                Stdout(good("FAILURE")).newline;
                return 2;
            }
            if(mode == 0)
            {
                Stdout(bad("FAILURE - Unexpected")).newline;
                return 1;
            }
        }
    }
}