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

%new-style
%enable-all-warnings
%require-types
%strict-args
%no-child-restrictions

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

%exec-class CatchTypeTest

class CatchTypeTest inherits QUnit::Test {
    constructor() : QUnit::Test("Catch type test", "1.0") {
        addTestCase("Test catch type", \testCatch());
        set_return_value(main());
    }

    testCatch() {
        {
            Program p(PO_NEW_STYLE);
            p.parse("try{}catch(ex){ ex = 5; }", "");
            assertTrue(True);
        }
        {
            Program p(PO_NEW_STYLE);
            p.parse("try{}catch(my ex){ ex = 5; }", "");
            assertTrue(True);
        }
        {
            Program p(PO_NEW_STYLE);
            p.parse("try{}catch(my hash ex){ ex = {}; }", "");
            assertTrue(True);
        }
        {
            Program p(PO_NEW_STYLE);
            p.parse("try{}catch(*hash ex){ ex = NOTHING; }", "");
            assertTrue(True);
        }
        {
            Program p(PO_NEW_STYLE);
            p.parse("try{}catch(any ex){ ex = 5; }", "");
            assertTrue(True);
        }
        {
            Program p(PO_NEW_STYLE);
            assertThrows("PARSE-EXCEPTION", "catch block",
                \p.parse(), ("try{}catch(my int ex){}", ""));
        }
        {
            Program p(PO_NEW_STYLE);
            assertThrows("PARSE-TYPE-ERROR", "lvalue for assignment operator.*expects type 'hash', but right-hand side is type 'int'",
                \p.parse(), ("try{}catch(hash ex){ ex = 5; }", ""));
        }
    }
}
