local path = require('fundo.fs.path')

describe('path module.', function()
    local sep
    setup(function()
        sep = path.sep
    end)
    teardown(function()
        path.sep = sep
    end)
    describe('dirname', function()
        describe('on Unix,', function()
            setup(function()
                path.sep = '/'
            end)
            it('normal path.', function()
                assert.equal('/foo/bar/baz/asdf', path.dirname('/foo/bar/baz/asdf/quux.html'))
            end)
            it([[path is equal to '/'.]], function()
                assert.equal('/', path.dirname('/'))
            end)
            it([[path without any'/'.]], function()
                assert.equal('.', path.dirname('foo'))
            end)
        end)
    end)
    describe('basename', function()
        describe('on Unix,', function()
            local p
            setup(function()
                p = '/foo/bar/baz/asdf/quux.html'
                path.sep = '/'
            end)
            it('return a basename.', function()
                assert.equal('quux.html', path.basename(p))
            end)
            it('return a basename after remove existed suffix.', function()
                assert.equal('quux', path.basename(p, '.html'))
            end)
            it('return a basename after remove non-existed suffix.', function()
                assert.equal('quux.html', path.basename(p, '.h'))
            end)
            it('return a basename after remove self as suffix.', function()
                local b = path.basename(p)
                b = path.basename(p, b)
                assert.equal('quux.html', b)
            end)
        end)
        describe('on Windows,', function()
            local p
            setup(function()
                p = [[C:\foo.html]]
                path.sep = [[\]]
            end)
            it('return a basename.', function()
                assert.equal('foo.html', path.basename(p))
            end)
            it('return a basename after remove existed suffix.', function()
                assert.equal('foo', path.basename(p, '.html'))
            end)
        end)
    end)
    describe('normalize', function()
        describe('on Unix,', function()
            setup(function()
                path.sep = '/'
            end)
            it([[return itself if normalized.]], function()
                assert.equal('/foo/bar/baz/asdf/quux', path.normalize('/foo/bar/baz/asdf/quux'))
            end)
            it([[a path with '//'.]], function()
                assert.equal('/foo/bar/baz/asdf/quux', path.normalize('/foo/bar//baz/asdf/quux'))
            end)
            it([[a path with '.'.]], function()
                assert.equal('/foo/bar/baz/asdf/quux', path.normalize('/foo/bar/baz/asdf/quux/.'))
            end)
            it([[a path with '..'.]], function()
                assert.equal('/foo/bar/baz/asdf', path.normalize('/foo/bar/baz/asdf/quux/..'))
            end)
            it([[a path with '//', '.' and '..'.]], function()
                assert.equal('/foo/bar/baz/asdf', path.normalize('/foo/bar//baz/asdf/quux/./..'))
            end)
        end)
        describe('on Windows', function()
            setup(function()
                path.sep = [[\]]
            end)
            it([[return itself if normalized.]], function()
                assert.equal([[C:\temp\foo\]], path.normalize([[C:\temp\foo\]]))
            end)
            it([[a path with '\\'.]], function()
                assert.equal([[C:\temp\foo\bar\]], path.normalize([[C:\temp\\foo\bar\]]))
            end)
            it([[a path with '..'.]], function()
                assert.equal([[C:\temp\foo\]], path.normalize([[C:\temp\foo\bar\..\]]))
            end)
            it([[a path with '\\' and '..'.]], function()
                assert.equal([[C:\temp\foo\]], path.normalize([[C:\temp\\foo\bar\..\]]))
            end)
        end)
    end)
    describe('join', function()
        describe('on Unix,', function()
            setup(function()
                path.sep = '/'
            end)
            it('join a sequence of path segment.', function()
                assert.equal('/foo/bar/baz/asdf', path.join('/foo', 'bar', 'baz/asdf', 'quux', '..'))
            end)
        end)
        describe('on Windows,', function()
            setup(function()
                path.sep = [[\]]
            end)
            it('join a sequence of path segment.', function()
                assert.equal([[C:\temp\bar]], path.join('C:', 'temp', 'foo', '..', 'bar'))
            end)
        end)
    end)
end)
