describe('lift.path', function()

  local path = require 'lift.path'
  local fs = require 'lift.fs'

  it('offers is_root() to test if path is lexically a root dir', function()
    assert.False(path.is_root'/foo', path.is_root'C:/foo', path.is_root'C:')
    assert.True(path.is_root'/', path.is_root'C:/')
  end)

  it('offers volume() to get the volume name from Windows paths', function()
    assert.equal('', path.volume'/foo/file.ext')
    assert.equal('C:', path.volume'C:/foo')
    assert.equal('X:', path.volume'X:/foo/file.ext')
  end)

  it('offers base() to get the last element of a path', function()
    assert.equal('file.ext', path.base'/foo/dir/file.ext')
    assert.equal('file.ext', path.base'C:/foo/dir/file.ext')
    assert.equal('dir', path.base'/foo/dir/')
    assert.equal('/', path.base'/')
    assert.equal('.', path.base'')
  end)

  it('offers dir() to get the directory of a path', function()
    assert.equal('/foo/dir', path.dir'/foo/dir/file.ext')
    assert.equal('C:/foo/dir', path.dir'C:/foo/dir/file.ext')
    assert.equal('/foo/dir', path.dir'/foo/dir/')
    assert.equal('/', path.dir'/')
    assert.equal('C:/', path.dir'C:/', path.dir'C:/foo')
    assert.equal('.', path.dir'', path.dir'C:', path.dir'file.ext')
  end)

  it('offers ext() to get the filename extension of a path', function()
    assert.equal('ext', path.ext'/dir/file.ext', path.ext'C:/dir/file.x.ext')
    assert.equal('', path.ext'/dir/file', path.ext'/dir/file.')
  end)

  it('offers clean() to normalize a path', function()
    assert.equal('..', path.clean'..')
    assert.equal('/', path.clean'/', path.clean'/..', path.clean'/../')
    assert.equal('C:/', path.clean'C:/', path.clean'C:/..')
    assert.equal('.', path.clean'', path.clean'.', path.clean'./')
    assert.equal('/foo/dir', path.clean'/foo/dir/')
    assert.equal('/foo/dir', path.clean'/foo/dir')
    assert.equal('/foo/dir', path.clean('/foo/dir', true))
    assert.equal('/foo/dir/', path.clean('/foo/dir/', true))
    assert.equal('/foo/dir/file.ext', path.clean'/foo/dir/file.ext')
  end)

  it('offers is_abs() to test if a path is absolute', function()
    assert.True(path.is_abs'/foo/file.ext')
    assert.True(path.is_abs'C:/foo/file.ext')
    assert.False(path.is_abs'./foo/dir/')
    assert.False(path.is_abs'file.ext')
  end)

  it('offers abs() to make a path absolute', function()
    assert.equal('/foo/file.ext', path.abs'/foo/file.ext')
    assert.equal('/foo/dir/', path.abs'/foo/dir/')
    assert.equal('/', path.abs'/')
    assert.equal(fs.cwd(), path.abs'')
    assert.equal(fs.cwd()..'/file', path.abs'file')
    assert.equal('/usr/local/*/file', path.abs('../*/file', '/usr/local/bin'))
    assert.equal('C:/usr/local/file', path.abs('../file', 'C:/usr/local/bin'))
    assert.equal('/usr/dir', path.abs('../dir', '/usr/local', true))
    assert.equal('/usr/dir/', path.abs('../dir/', '/usr/local', true))
  end)

  it('offers rel() to make a path relative to some other path', function()
    assert.equal('b/c', path.rel('/a', '/a/b/c'))
    assert.equal('b/c', path.rel('C:/a', 'C:/a/b/c'))
    assert.equal('../b/c', path.rel('/a', '/b/c'))
    assert.equal('../b/c', path.rel('C:/a', 'C:/b/c'))
    assert.equal('c', path.rel('a/b', 'a/b/c'))
    assert.equal('c', path.rel('./a/b', './a/b/c'))
    assert.equal('..', path.rel('./a/b/c', './a/b/'))
    assert.error(function() path.rel('/a', './b/c') end,
      "expected two relative paths or two absolute paths")
    assert.equal('X:/b/c', path.rel('C:/a', 'X:/b/c'))
  end)

  it('offers join() to join path elements', function()
    assert.equal('/usr/local', path.join('/usr', '', '', 'local'))
    assert.equal('/usr/local/bin', path.join('/./usr/', 'local', 'bin/'))
  end)

  it('offers split() to get the dir and file components of a path', function()
    assert.same({'/usr/local/',''}, {path.split('/usr/local/')})
    assert.same({'C:/usr/local/',''}, {path.split('C:/usr/local/')})
    assert.same({'/usr/local/','bin'}, {path.split('/usr/local/bin')})
    assert.same({'C:/usr/local/','bin'}, {path.split('C:/usr/local/bin')})
    assert.same({'C:/','usr'}, {path.split('C:/usr')})
    assert.same({'C:/',''}, {path.split('C:/')})
    assert.same({'','file.ext'}, {path.split('file.ext')})
  end)

end)
