Luigit
repositories / dotfiles

dotfiles

bugabingas dorkfiles

owned by admin

neovim/tests/bugabinga/pivi/editor/follow_spec.lua

Raw
---@diagnostic disable: duplicate-set-field, need-check-nil, param-type-mismatch, assign-type-mismatch
local follow = require 'bugabinga.pivi.follow'
local fake = dofile 'tests/helpers/pivi_fake.lua'

local temp_file = function( lines )
  local path = vim.fs.normalize( vim.fn.tempname() .. '.txt' )
  vim.fn.writefile( lines, path )
  return path
end

describe( 'pivi follow', function()
  after_each( function()
    follow.reset()
    fake.close_extra_windows()
    vim.cmd 'silent! %bwipeout!'
  end )

  it( 'is disabled until the user selects a mode', function()
    assert.is_nil( follow.mode() )
    assert.is_nil( follow.to { path = temp_file { 'one', }, line = 1, } )
  end )

  it( 'toggles on and off', function()
    assert.equal( 'window', follow.toggle() )
    assert.is_nil( follow.toggle() )
    assert.equal( 'tab', follow.toggle 'tab' )
  end )

  it( 'refuses an unknown mode', function()
    follow.set 'window'
    assert.equal( 'window', follow.set 'sideways' )
  end )

  it( 'follows into a dedicated window without taking the working window', function()
    local working = vim.api.nvim_get_current_win()
    local path = temp_file { 'one', 'two', 'three', }
    follow.set 'window'

    local target = follow.to { path = path, line = 3, }

    assert.is_number( target )
    assert.is_not.equal( working, target )
    assert.equal( working, vim.api.nvim_get_current_win() )
    assert.equal( 3, vim.api.nvim_win_get_cursor( target )[1] )
  end )

  it( 'reuses its dedicated window for later locations', function()
    follow.set 'window'
    local first = follow.to { path = temp_file { 'a', }, line = 1, }
    local second = follow.to { path = temp_file { 'b', 'c', }, line = 2, }
    assert.equal( first, second )
  end )

  it( 'clamps a location beyond the end of the file', function()
    follow.set 'window'
    local target = follow.to { path = temp_file { 'only', }, line = 99, }
    assert.equal( 1, vim.api.nvim_win_get_cursor( target )[1] )
  end )

  it( 'follows in the current window when asked', function()
    follow.set 'current'
    local working = vim.api.nvim_get_current_win()
    local target = follow.to { path = temp_file { 'one', 'two', }, line = 2, }
    assert.equal( working, target )
  end )

  it( 'returns to the location held before following began', function()
    local path = temp_file { 'alpha', 'beta', 'gamma', }
    vim.cmd( 'edit! ' .. vim.fn.fnameescape( path ) )
    local working = vim.api.nvim_get_current_win()
    local buffer = vim.api.nvim_get_current_buf()
    vim.api.nvim_win_set_cursor( working, { 2, 0, } )

    follow.set 'window'
    follow.to { path = temp_file { 'elsewhere', }, line = 1, }

    assert.is_true( follow.back() )
    assert.equal( working, vim.api.nvim_get_current_win() )
    assert.equal( buffer, vim.api.nvim_get_current_buf() )
    assert.equal( 2, vim.api.nvim_win_get_cursor( working )[1] )
  end )

  it( 'reports nothing to return to when following never happened', function()
    assert.is_false( follow.back() )
  end )
end )