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

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

%disable-warning invalid-operation

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

%exec-class Test

class Test inherits QUnit::Test {
    constructor() : QUnit::Test("hash version of the map operator", "1.0") {
        addTestCase("basic tests", \basicTests());
        addTestCase("tests with selector", \selectTests());
        set_return_value(main());
    }

    basicTests() {
        assertEq({"2" : 4, "5" : 25, "9" : 81}, map {$1 : $1 * $1}, (2, 5, 9));
        assertEq({"aa" : True, "rrrr" : True}, map {$1 + $1 : True}, ("a", "rr"));
        assertEq({}, map {$1 + $1 : True}, ());
        assertEq({"key" : "b"}, map {"key" : $1}, ("a", "a", "b"));
        assertEq({"2" : (2, 3), "1" : (1, 2, 3), "3" : (3, 4)}, map {$1[0] : $1}, ((2, 3), (1, 2, 3), (3, 4)));
        assertEq({"aa" : "ab"}, map {$1 + $1 : $1 + 'b'}, "a");
        assertEq(NOTHING, map {$1 + $1 : $1 + 'b'}, NOTHING);
        assertEq({"2" : "1b", "44" : "4b", "12" : "6b"}, map {$1 + $1 : $1 + 'b'}, (1, "4", 6).iterator());
        assertEq({}, map {}, (1, 2));
    }

    selectTests() {
        assertEq({"1" : 2, "6" : 12}, map {$1 : $1 * 2}, (1, 4, 6), $1 != 4);
        assertEq({}, map {$1 : $1 * 2}, (1, 4, 6), False);
        assertEq({}, map {$1 : $1 * 2}, (1, 4, 6), NOTHING);
        assertEq({"1" : 2, "4" : 8, "6" : 12}, map {$1 : $1 * 2}, (1, 4, 6), True);
    }
}
