Luigit
repositories / dotfiles

dotfiles

bugabingas dorkfiles

owned by admin

neovim/tests/bugabinga/cmdline/format_spec.lua

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

describe('format.detect', function()
  it('detects : as cmdline', function()
    local result = format.detect(':', 'hello', '')
    assert.equal('cmdline', result.type)
    assert.equal('', result.icon)
  end)

  it('detects / as search_down', function()
    local result = format.detect('/', 'hello', '')
    assert.equal('search_down', result.type)
    assert.equal('', result.icon)
  end)

  it('detects ? as search_up', function()
    local result = format.detect('?', 'hello', '')
    assert.equal('search_up', result.type)
    assert.equal('', result.icon)
  end)

  it('detects ! as shell', function()
    local result = format.detect('!', 'ls -la', '')
    assert.equal('shell', result.type)
    assert.equal('', result.icon)
  end)

  it('detects :help as help', function()
    local result = format.detect(':', 'help mytopic', '')
    assert.equal('help', result.type)
    assert.equal('󰋖', result.icon)
  end)

  it('detects :h as help', function()
    local result = format.detect(':', 'h', '')
    assert.equal('help', result.type)
    assert.equal('󰋖', result.icon)
  end)

  it('detects :he as help', function()
    local result = format.detect(':', 'he', '')
    assert.equal('help', result.type)
    assert.equal('󰋖', result.icon)
  end)

  it('detects :hel as help', function()
    local result = format.detect(':', 'hel', '')
    assert.equal('help', result.type)
    assert.equal('󰋖', result.icon)
  end)

  it('detects :help alone as help', function()
    local result = format.detect(':', 'help', '')
    assert.equal('help', result.type)
    assert.equal('󰋖', result.icon)
  end)

  it('detects :h with topic as help', function()
    local result = format.detect(':', 'h usr_01', '')
    assert.equal('help', result.type)
    assert.equal('󰋖', result.icon)
  end)

  it('detects :!cmd as shell', function()
    local result = format.detect(':', '!ls', '')
    assert.equal('shell', result.type)
    assert.equal('', result.icon)
  end)

  it('detects :lua as lua', function()
    local result = format.detect(':', 'lua print("hi")', '')
    assert.equal('lua', result.type)
    assert.equal('', result.icon)
  end)

  it('detects :lua alone as lua', function()
    local result = format.detect(':', 'lua', '')
    assert.equal('lua', result.type)
    assert.equal('', result.icon)
  end)

  it('detects := as lua', function()
    local result = format.detect(':', '= { foo = 1 }', '')
    assert.equal('lua', result.type)
    assert.equal('', result.icon)
  end)

  it('does not detect :luafile as lua', function()
    local result = format.detect(':', 'luafile', '')
    assert.equal('cmdline', result.type)
  end)

  it('does not detect :luado as lua', function()
    local result = format.detect(':', 'luado', '')
    assert.equal('cmdline', result.type)
  end)

  it('detects input when prompt is non-empty', function()
    local result = format.detect(':', 'some input', 'Enter name: ')
    assert.equal('input', result.type)
    assert.equal('', result.icon)
  end)

  it('returns cmdline for unknown firstc', function()
    local result = format.detect('-', 'some text', '')
    assert.equal('cmdline', result.type)
  end)

  it('returns input for empty firstc and prompt', function()
    local result = format.detect('', 'some text', '')
    assert.equal('input', result.type)
  end)

  it('handles nil content gracefully', function()
    local result = format.detect(':', nil, '')
    assert.equal('cmdline', result.type)
  end)
end)

describe('format.prompt_lines', function()
  it('returns empty table for nil prompt', function()
    local result = format.prompt_lines(nil, 80)
    assert.equal(0, #result)
  end)

  it('returns empty table for empty prompt', function()
    local result = format.prompt_lines('', 80)
    assert.equal(0, #result)
  end)

  it('returns single entry for short prompt', function()
    local result = format.prompt_lines('Enter name: ', 80)
    assert.equal(1, #result)
    assert.equal('Enter name: ', result[1])
  end)

  it('returns single entry when prompt fits exactly at max_width', function()
    local prompt = 'abc'
    local max_w = 3
    local result = format.prompt_lines(prompt, max_w)
    assert.equal(1, #result)
    assert.equal('abc', result[1])
  end)

  it('splits on newline', function()
    local result = format.prompt_lines('line 1\nline 2', 80)
    assert.equal(2, #result)
    assert.equal('line 1', result[1])
    assert.equal('line 2', result[2])
  end)

  it('double newline produces blank line', function()
    local result = format.prompt_lines('a\n\nb', 80)
    assert.equal(3, #result)
    assert.equal('a', result[1])
    assert.equal('', result[2])
    assert.equal('b', result[3])
  end)

  it('trailing newline is stripped', function()
    local result = format.prompt_lines('hello\n', 80)
    assert.equal(1, #result)
    assert.equal('hello', result[1])
  end)

  it('wraps long line at word boundary', function()
    local long = 'the quick brown fox jumps over the lazy dog'
    local max_w = 20
    local result = format.prompt_lines(long, max_w)
    for _, line in ipairs(result) do
      assert(vim.fn.strdisplaywidth(line) <= max_w, 'line exceeds max_width: ' .. line)
    end
    local concatenated = table.concat(result, ' ')
    assert.equal(long, concatenated)
  end)

  it('hard-breaks single word exceeding max_width', function()
    local long = 'supercalifragilisticexpialidocious'
    local max_w = 10
    local result = format.prompt_lines(long, max_w)
    assert(#result > 1, 'single long word should be broken into multiple pieces')
    for _, line in ipairs(result) do
      assert(vim.fn.strdisplaywidth(line) <= max_w, 'line exceeds max_width: ' .. line)
    end
  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()
      format.prompt_lines('hello', 80)
    end)

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

  it('no wrapping when prompt shorter than max_width', function()
    local result = format.prompt_lines('short', 80)
    assert.equal(1, #result)
    assert.equal('short', result[1])
  end)
end)

describe('format.PROMPT_MAX_RATIO', function()
  it('is 0.85', function()
    assert.equal(0.85, format.PROMPT_MAX_RATIO)
  end)
end)