view tests/run.d @ 206:d3c148ca429b

Major moving of files. all src now goes into src, all docs in docs.
author Anders Johnsen <skabet@gmail.com>
date Tue, 12 Aug 2008 18:14:56 +0200
parents
children 41ccd50e7cbc
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;


enum 
{
    SuccessSuccess,
    SuccessFailure,
    FailureSuccess,
    FailureFailure,
}

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 SuccessSuccess:
                    success_success++;
                    break;
                case SuccessFailure:
                    success_failure++;
                    break;
                case FailureFailure:
                    failure_failure++;
                    break;
                case FailureSuccess:
                    failure_success++;
                    break;
            }

        }
    }

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

class Test
{
    enum TestValue
    {
        Success = 0,
        Lexer = 2,
        Parser = 3,
        Gen = 4,

        Fail = 100
    }

    FilePath target;

    TestValue[int] testValues;
    
    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);

        TestValue mode;

        char[] data = file.read;
        char[][] commands = split(splitLines(data)[0], " ");
        if(commands[0] == "//fail")
        {
            mode = TestValue.Fail;
            if(commands.length > 1)
            {
                try
                {
                    int i = Integer.toInt(commands[1]);
                    if(i in testValues)
                        mode = testValues[i];
                }
                catch{}
            }
        }
/*        if(data.length > 6 && data[0..6] == "//fail")
        {
            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, TestValue 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 == TestValue.Success)
            {
                Stdout(good("SUCCESS")).newline;
                return SuccessSuccess;
            }
            if(mode == TestValue.Fail)
            {
                Stdout(bad("SUCCESS - Unexpected")).newline;
                return FailureSuccess;
            }
        }
        else
        {
            if(mode == TestValue.Fail)
            {
                Stdout(good("FAILURE")).newline;
                return FailureFailure;
            }
            if(mode == TestValue.Success)
            {
                Stdout(bad("FAILURE - Unexpected")).newline;
                return SuccessFailure;
            }
        }
    }
}