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

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

%requires ../../../../qlib/QUnit.qm

%exec-class TypeTest

public class TypeTest inherits QUnit::Test {
    constructor() : Test("TypeTest", "1.1") {
        addTestCase("Test type() function", \typeTestMethod());
        addTestCase("conversions", \conversions());

        # Return for compatibility with test harness that checks return value.
        set_return_value(main());
    }

    typeTestMethod() {
        assertEq("bool", type(True));
        assertEq("integer", type(1));
        assertEq("float", type(3.14));
        assertEq("number", type(13.37n));
        assertEq("date", type(2012-02-17T19:05:54+01:00));
        assertEq("NULL", type(NULL));
        assertEq("call reference", type(\type()));
        assertEq("closure", type(sub(){int x = 5; ++x;}));
        assertEq("hash", type({}));
        assertEq("string", type("str"));

        assertEq("bool", True.type());
        assertEq("integer", 1.type());
        assertEq("float", 3.14.type());
        assertEq("number", 13.37n.type());
        assertEq("date", 2012-02-17T19:05:54+01:00.type());
        assertEq("NULL", NULL.type());
        assertEq("call reference", (\type()).type());
        assertEq("closure", (sub(){int x = 5; ++x;}).type());
        assertEq("hash", {}.type());
        assertEq("string", "str".type());

        assertEq(NT_BOOLEAN, True.typeCode());
        assertEq(NT_INT, 1.typeCode());
        assertEq(NT_FLOAT, 3.14.typeCode());
        assertEq(NT_NUMBER, 13.37n.typeCode());
        assertEq(NT_DATE, 2012-02-17T19:05:54+01:00.typeCode());
        assertEq(NT_NULL, NULL.typeCode());
        assertEq(NT_CALLREF, (\type()).typeCode());
        assertEq(NT_CLOSURE, (sub(){int x = 5; ++x;}).typeCode());
        assertEq(NT_HASH, {}.typeCode());
        assertEq(NT_STRING, "str".typeCode());
    }

    conversions() {
        assertEq(2016-08-11, date("2016-08-11"));
        assertEq(2016-08-11, date("20160811"));
        assertEq(1s, date(1));
        assertEq(PT1S100000u, date(1.1));
        assertEq(PT1S100000u, date(1.1n));
    }
}
