#!/usr/bin/env qore
# -*- mode: qore; indent-tabs-mode: nil -*-

%new-style
%enable-all-warnings
%require-types
%strict-args

%requires ../../../../qlib/Util.qm
%requires ../../../../qlib/QUnit.qm
%requires ../../../../qlib/DataProvider
%requires ../../../../qlib/FixedLengthUtil

%exec-class Test

class Test inherits QUnit::Test {
    public {
        const Data = (
            ("type": "type1", "record": ("col1" : 11111, "col2" : "bb")),
            ("type": "type2", "record": ("col3" : "c", "col4" : "ddd", "col5" : "31122014")),
            ("type": "type1", "record": ("col1" : 22222, "col2" : "gg")),
            );

        const DataText = "11111bb\ncddd31122014\n22222gg\n";

        const Specs = (
            "type1" : (
                "col1" : (
                    "length" : 5,
                    "type"   : "int"
                ),
                "col2" : (
                    "length" : 2,
                    "type"   : "string",
                ),
            ),
            "type2" : (
                "col3" : (
                    "length" : 1,
                    "type"   : "string",
                ),
                "col4" : (
                    "length" : 3,
                    "type"   : "string",
                ),
                "col5" : (
                    "length"   : 8,
                    "type"     : "date",
                    "format"   : "DDMMYYYY",
                    "timezone" : "Europe/Prague",
                ),
            ),
            );

        const GlobalOptions = (
            "eol" : "\n",
            "file_flags": O_CREAT | O_WRONLY | O_TRUNC,
            );
    }

    constructor() : QUnit::Test("transition_check_writer", "1.0", \ARGV) {
        addTestCase("transition check writer", \check());
        set_return_value(main());
    }

    check() {
        string file = tmp_location() + "/file.fll";

        MyFixedLengthDataWriter1 w1(Specs, GlobalOptions);
        on_exit unlink(file);
        testAssertion("file content check", \w1.write(), (Data,), new TestResultValue(DataText));

        MyFixedLengthDataWriter2 w2(Specs, GlobalOptions);
        code wr = sub (list d) {
            w2.write(d);
        };
        testAssertion("record transition negative test", wr, (Data,), new TestResultExceptionType("RECORD-TRANSITION-ERROR"));
    }
}

public class MyFixedLengthDataWriter1 inherits FixedLengthUtil::FixedLengthDataWriter {
    constructor(hash h, *hash g) : FixedLengthUtil::FixedLengthDataWriter(h, g) {
    }

    bool checkTransition(*string from, *string to) {
        if ((from == "type1" && to == "type2")
            || (from == NOTHING && to == "type1")
            || (from == "type2" && to == "type1")) {
            return True;
        }

        return False;
    }
}

public class MyFixedLengthDataWriter2 inherits FixedLengthUtil::FixedLengthDataWriter {
    constructor(hash h, *hash g) : FixedLengthUtil::FixedLengthDataWriter(h, g) {
    }

    bool checkTransition(*string from, *string to) {
        if ((from == "type1" && to == "type2")
            || (from == "type2" && to == "type1")) {
            return True;
        }

        return False;
    }
}
