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

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

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

%exec-class ParseToQoreValueTest

public class ParseToQoreValueTest inherits QUnit::Test {
    constructor() : Test ("ParseToQoreValueTest", "1.0") {
        addTestCase("issue 3483", \issue3483());
        addTestCase("issue 3138", \issue3138());
        addTestCase("Tests for parse_to_qore_value()", \parseToQoreValueTest());
        set_return_value(main());
    }

    issue3483() {
        auto l = parse_to_qore_value("(1,2,(3,4))");
        assertEq((1, 2, (3, 4)), l);
        assertEq("list<int>", l[2].fullType());
    }

    issue3138() {
        assertEq((), parse_to_qore_value("()"));
        assertEq({"a": ()}, parse_to_qore_value("a=()"));
        assertEq({}, parse_to_qore_value("{}"));
        assertEq({"a": {}}, parse_to_qore_value("a={}"));
        assertEq((1,), parse_to_qore_value("(1,)"));
        assertEq({"a": NOTHING}, parse_to_qore_value("a="));
        assertEq({"a": ("x",)}, parse_to_qore_value("a=(x,)"));
        assertThrows("PARSE-ERROR", \parse_to_qore_value(), "a,=x");
    }

    parseToQoreValueTest() {
        assertEq(-3.409, parse_to_qore_value("-3.4090"));
        assertEq(-3.0, parse_to_qore_value("-3.00000"));
        assertEq(3.0, parse_to_qore_value("3.0"));

        assertEq(0, parse_to_qore_value("%EVAL=int i = 0"));
        assertEq(3.3, parse_to_qore_value("%EVAL=auto f = 3.3"));
        assertEq(1, parse_to_qore_value("1"));
        assertEq("two", parse_to_qore_value("two"));
        assertEq("three", parse_to_qore_value("\"three\""));
        assertEq(2016-05-09, parse_to_qore_value("2016-05-09"));

        # test %STR=
        assertEq("hello, there, a=b", parse_to_qore_value("%STR=hello, there, a=b"));

        # test %EVAL=
        assertEq(("a": 1, "b": (2, 3)), parse_to_qore_value("%EVAL=('a':1, 'b': (2, 3))"));

        # test support of parentheses
        assertEq((1, 2, 3, ("a": "num", "b": "txt", "c": (1, 2, "three")), 5),
            parse_to_qore_value("1,2,3,(a=num, b=txt, c=(1, 2, \"three\")),5"));
        assertEq((("a": (1, 2, 3, "num"), "b": "txt", "c": (1, 2, "three")), 5),
            parse_to_qore_value("(a=(1,2,3,num), b= txt , c=(1, 2, \"three\")),5"));
        assertEq(("o": ("v": 10, "s": 11)), parse_to_qore_value("o=(v=10,s=11)"));
        assertEq((("a": ("b": (("c": "x"), 2016-05-18, 3))), "x"),
            parse_to_qore_value("a=(b=(c=x, 2016-05-18, 3)),x"));
        assertEq((("a": ("b": (("c": "x"), 2016-05-18T20:47, 3))), False),
            parse_to_qore_value(" a = ( b = ( c = x , 2016-05-18T20:47, 3) ) , False "));
        assertEq((("a": ("b ": (("c": "x"), 2016-05-18T20:47:16.356788, 3))), True),
            parse_to_qore_value(" a = ( \"b \" = ( c = x , 2016-05-18T20:47:16.356788 , 3) ) , true "));
        assertEq((("a": 500), ("a": 1.234)), parse_to_qore_value("(a = 500),a=1.234"));

        assertEq({"a": 1, "b": (1, 2)}, parse_to_qore_value("{a=1,b=(1,2)}"));
    }
}
