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

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

%allow-injection
%no-child-restrictions

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

%exec-class ModuleProgramTest

class ModuleProgramTest inherits QUnit::Test {
    constructor() : QUnit::Test("Module Program Test", "1.0") {
        addTestCase("Test", \testModuleProgram());
        set_return_value(main());
    }

    testModuleProgram() {
        {
            # set up the Program container
            Program mp(PO_NO_CHILD_PO_RESTRICTIONS|PO_NEW_STYLE);
            mp.loadApplyToUserModule(get_script_dir() + "../../../../../qlib/RestClient.qm", True);
        }
        hash h = get_module_hash().RestClient;
        assertEq(True, h.injected);
        assertEq(True, h.reinjected);

        {
            # set up the Program container
            Program mp(PO_NO_CHILD_PO_RESTRICTIONS|PO_NEW_STYLE);
            mp.loadApplyToUserModule(get_script_dir() + "../../../../../qlib/RestClient.qm", True);
        }
        h = get_module_hash().RestClient;
        assertEq(True, h.injected);
        assertEq(True, h.reinjected);

        {
            # set up the Program container
            Program mp(PO_NO_CHILD_PO_RESTRICTIONS|PO_NEW_STYLE);
            # issue 1805: this last line causes a crash when qore exits
            mp.loadApplyToUserModule(get_script_dir() + "../../../../../qlib/RestClient.qm", True);
        }
        h = get_module_hash().RestClient;
        assertEq(True, h.injected);
        assertEq(True, h.reinjected);
    }
}
