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

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

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

%exec-class FiletypesTest

class FiletypesTest inherits QUnit::Test {
    constructor() : QUnit::Test("Filetypes test", "1.0") {
        addTestCase("Test filetype functions", \testFiletypes());
        set_return_value(main());
    }

    testFiletypes() {
%ifdef Windows
        testSkip("skipping because the test is being run on Windows");
%endif
        string nonExistantPath = "/abcdefgh/XYZ/X987654321/asde7g1b23m6u7e9s2c0h4g5sd6sf";

        # in case /dev/null is a symlink (such as on Solaris)
        string dev = realpath("/dev/null");

        assertEq(True, is_file(get_script_path()), "is_file()");
        assertEq(False, is_file("/"), "is_file() false 1");
        assertEq(False, is_file(nonExistantPath), "is_file() false 2");
        assertEq(False, is_file(""), "is_file() false 3");

        assertEq(True, is_executable("/bin/sh"), "is_executable()");
        assertEq(False, is_executable(dev), "is_executable() false 1");
        assertEq(False, is_executable(nonExistantPath), "is_executable() false 2");
        assertEq(False, is_executable(""), "is_executable() false 3");

        assertEq(True, is_dir("/"), "is_dir()");
        assertEq(False, is_dir(nonExistantPath), "is_dir() false 1");
        assertEq(False, is_dir(""), "is_dir() false 2");

        assertEq(True, is_writeable(ENV.HOME), "is_writable()");
        assertEq(False, is_writeable(nonExistantPath), "is_writable() false 1");
        assertEq(False, is_writeable(""), "is_writable() false 2");

        assertEq(True, is_readable(ENV.HOME), "is_readable()");
        assertEq(False, is_readable(nonExistantPath), "is_readable() false 1");
        assertEq(False, is_readable(""), "is_readable() false 2");

        assertEq(True, is_dev(dev), "is_dev()");
        assertEq(False, is_dev("/"), "is_dev() false 1");
        assertEq(False, is_dev(nonExistantPath), "is_dev() false 2");
        assertEq(False, is_dev(""), "is_dev() false 3");

        assertEq(True, is_cdev(dev), "is_cdev()");
        assertEq(False, is_cdev("/"), "is_cdev() false 1");
        assertEq(False, is_cdev(nonExistantPath), "is_cdev() false 2");
        assertEq(False, is_cdev(""), "is_cdev() false 3");

        assertEq(False, is_bdev(dev), "is_bdev() 1");
        assertEq(False, is_bdev(nonExistantPath), "is_bdev() 2");
        assertEq(False, is_bdev(""), "is_bdev() 3");

        assertEq(False, is_link("/"), "is_link() 1");
        assertEq(False, is_link(nonExistantPath), "is_link() 2");
        assertEq(False, is_link(""), "is_link() 3");

        assertEq(False, is_socket("/"), "is_socket() 1");
        assertEq(False, is_socket(nonExistantPath), "is_socket() 2");
        assertEq(False, is_socket(""), "is_socket() 3");

        assertEq(False, is_pipe("/"), "is_pipe() 1");
        assertEq(False, is_pipe(nonExistantPath), "is_pipe() 2");
        assertEq(False, is_pipe(""), "is_pipe() 3");
    }
}
