Luigit
repositories / dotfiles

dotfiles

bugabingas dorkfiles

owned by admin

neovim/tests/bugabinga/cmdline/state_spec.lua

Raw
local state = require 'bugabinga.cmdline.state'

describe('state.new', function()
  before_each(function()
    package.loaded['bugabinga.cmdline.state'] = nil
  end)

  it('concatenates content chunks into text', function()
    local s = state.new({
      { {}, 'hello ' },
      { {}, 'world' },
    }, 11, ':', '', 0, 1)
    assert.equal('hello world', s:text())
  end)

  it('stores cursor position', function()
    local s = state.new({}, 5, ':', '', 0, 1)
    assert.equal(5, s:cursor_byte())
  end)

  it('stores firstc, prompt, level', function()
    local s = state.new({}, 0, '/', 'search: ', 2, 3)
    assert.equal('/', s.firstc)
    assert.equal('search: ', s.prompt)
    assert.equal(3, s.level)
  end)

  it('handles empty content', function()
    local s = state.new({}, 0, ':', '', 0, 1)
    assert.equal('', s:text())
  end)

  it('uses non-deprecated validation signature', function()
    local validate = vim.validate
    rawset(vim, 'validate', function(name, value, validator, optional_or_msg)
      assert.equal('string', type(name))
      return validate(name, value, validator, optional_or_msg)
    end)

    local ok, err = pcall(function()
      state.new({}, 0, ':', '', 0, 1)
    end)

    rawset(vim, 'validate', validate)
    assert(ok, err)
  end)

  it('handles single chunk content', function()
    local s = state.new({
      { {}, 'only' },
    }, 4, ':', '', 0, 1)
    assert.equal('only', s:text())
  end)
end)

describe('state methods', function()
  before_each(function()
    package.loaded['bugabinga.cmdline.state'] = nil
  end)

  it('text() returns concatenated string', function()
    local s = state.new({
      { {}, 'foo' },
      { {}, 'bar' },
    }, 6, ':', '', 0, 1)
    assert.equal('foobar', s:text())
  end)

  it('cursor_byte() returns byte offset', function()
    local s = state.new({}, 7, ':', '', 0, 1)
    assert.equal(7, s:cursor_byte())
  end)

  it('is_search() returns true for /', function()
    local s = state.new({}, 0, '/', '', 0, 1)
    assert.is_true(s:is_search())
  end)

  it('is_search() returns true for ?', function()
    local s = state.new({}, 0, '?', '', 0, 1)
    assert.is_true(s:is_search())
  end)

  it('is_search() returns false for :', function()
    local s = state.new({}, 0, ':', '', 0, 1)
    assert.is_false(s:is_search())
  end)

  it('is_input() returns true when prompt non-empty', function()
    local s = state.new({}, 0, ':', 'Name: ', 0, 1)
    assert.is_true(s:is_input())
  end)

  it('is_input() returns false when prompt empty', function()
    local s = state.new({}, 0, ':', '', 0, 1)
    assert.is_false(s:is_input())
  end)
end)