Luigit
repositories / dotfiles

dotfiles

bugabingas dorkfiles

owned by admin

neovim/tests/std/table/join_spec.lua

Raw
local join = require('std.table.join')

describe('std.table.join', function()
  describe('basic joins', function()
    it('should join two empty tables', function()
      local result = join({}, {})
      assert.equal('{}', vim.inspect(result))
    end)

    it('should join empty table with non-empty table (left empty)', function()
      local result = join({}, {1})
      assert.equal('{ 1 }', vim.inspect(result))
    end)

    it('should join empty table with non-empty table (right empty)', function()
      local result = join({1}, {})
      assert.equal('{ 1 }', vim.inspect(result))
    end)

    it('should join two tables of equal size', function()
      local result = join({1, 2}, {3, 4})
      assert.equal('{ 1, 2, 3, 4 }', vim.inspect(result))
    end)

    it('should join tables with mismatched sizes (left smaller)', function()
      local result = join({1}, {2, 3})
      assert.equal('{ 1, 2, 3 }', vim.inspect(result))
    end)

    it('should join tables with mismatched sizes (right smaller)', function()
      local result = join({1, 2}, {3})
      assert.equal('{ 1, 2, 3 }', vim.inspect(result))
    end)
  end)

  describe('non-mutation', function()
    it('should not modify the left table', function()
      local tableA = {1, 2}
      local tableB = {3, 4}
      local originalA = vim.deepcopy(tableA)
      join(tableA, tableB)
      assert.equal(vim.inspect(originalA), vim.inspect(tableA))
    end)

    it('should not modify the right table', function()
      local tableA = {1, 2}
      local tableB = {3, 4}
      local originalB = vim.deepcopy(tableB)
      join(tableA, tableB)
      assert.equal(vim.inspect(originalB), vim.inspect(tableB))
    end)
  end)

  describe('new table', function()
    it('should return a new table, not either input', function()
      local tableA = {1, 2}
      local tableB = {3, 4}
      local result = join(tableA, tableB)
      assert.is_not_nil(result)
      assert.equal('table', type(result))
      assert.is_true(result ~= tableA)
      assert.is_true(result ~= tableB)
    end)
  end)
end)