#!/usr/bin/env qore

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

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

%exec-class ShellGlobToRegexTest

public class ShellGlobToRegexTest inherits QUnit::Test {
    constructor() : Test ("ShellGlobToRegexTest", "1.0") {
        addTestCase ("Tests for glob_to_regex() function", \test_glob_to_regex());
        set_return_value(main());
    }

    test_glob_to_regex() {
        list cases = (
                ('p': '',
                 'r': '',
                 'f': ''),
                ('p': 'a?c',
                 'r': 'a.c',
                 'f': 'abc'),
                ('p': 'a*d',
                 'r': 'a.*d',
                 'f': 'abcd'),
                ('p': '*.*',
                 'r': '.*\..*',
                 'f': 'ab.cd'),
                ('p': '[CB]at!',
                 'r': '[CB]at\!',
                 'f': 'Bat!'),
                ('p': 'Book-[0-9]',
                 'r': 'Book\-[0-9]',
                 'f': 'Book-1'),
                ('p': 'Book-[!3-5]',
                 'r': 'Book\-[^3-5]',
                 'f': 'Book-1'),
                ('p': '^test$',
                 'r': '\^test\$',
                 'f': '^test$'),
                );

        foreach hash t in (cases) {
            assertEq ('\A' + t.r + '\Z', glob_to_regex (t.p), sprintf ("checking glob to regex for '%s'", t.p));
            assertTrue (regex (t.f, glob_to_regex (t.p)), sprintf ("checking regex match for '%s'", t.p));
        }
    }
}
