Luigit
repositories / dotfiles

dotfiles

bugabingas dorkfiles

owned by admin

neovim/tests/std/const_spec.lua

Raw
local const = require('std.const')

describe('std.const', function()
  local cache_path

  before_each(function()
    cache_path = vim.fn.stdpath('cache') .. '/os_background'
    vim.env.NVIM_BACKGROUND = nil
  end)

  after_each(function()
    vim.env.NVIM_BACKGROUND = nil
    local f = io.open(cache_path, 'r')
    if f then
      f:close()
    end
    local ok = os.remove(cache_path)
    if not ok then
      os.remove(cache_path .. '.bak')
    end
  end)

  describe('os_background', function()
    it('should return either dark or light', function()
      local result = const.os_background
      assert.is_true(result == 'dark' or result == 'light')
    end)

    it('should respect NVIM_BACKGROUND env var', function()
      vim.env.NVIM_BACKGROUND = 'dark'
      package.loaded['std.const'] = nil
      local fresh = require('std.const')
      assert.equal('dark', fresh.os_background)
    end)

    it('should create a cache file after query', function()
      os.remove(cache_path)
      local f = io.open(cache_path, 'r')
      assert.is_nil(f)
      package.loaded['std.const'] = nil
      require('std.const')
      f = assert(io.open(cache_path, 'r'))
      local cached = f:read('*l')
      f:close()
      assert.is_true(cached == 'dark' or cached == 'light')
    end)

    it('should return cached value on subsequent calls', function()
      local f = assert(io.open(cache_path, 'w'))
      f:write('light\n')
      f:close()
      package.loaded['std.const'] = nil
      local fresh = require('std.const')
      assert.equal('light', fresh.os_background)
    end)
  end)

  describe('invalidate_os_background', function()
    it('should be a function', function()
      assert.is_function(const.invalidate_os_background)
    end)

    it('should clear the cache file', function()
      local f = assert(io.open(cache_path, 'w'))
      f:write('dark\n')
      f:close()
      const.invalidate_os_background()
      local f2 = io.open(cache_path, 'r')
      assert.is_nil(f2)
    end)
  end)
end)