Skip to content

Commit

Permalink
test: add initial tests for the State module
Browse files Browse the repository at this point in the history
  • Loading branch information
PriceHiller committed Nov 2, 2023
1 parent 281925c commit 4ebb84b
Showing 1 changed file with 59 additions and 0 deletions.
59 changes: 59 additions & 0 deletions tests/plenary/state/state_spec.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
local utils = require('orgmode.utils')
local state_mod = require('orgmode.state.state')
local cache_path = vim.fs.normalize(vim.fn.stdpath('cache') .. '/org-cache.json', { expand_env = false })

describe('State', function()
before_each(function()
-- Ensure the cache file is removed before each run
vim.fn.delete(cache_path, 'rf')
end)
it("should create a state file if it doesn't exist", function()
-- Ensure the file doesn't exist
local err, stat = pcall(vim.loop.fs_stat, cache_path)
if not err then
error('Cache file existed before it should! Ensure it is deleted before each test run!')
end

-- This creates the cache file on new instances of `State`
local state = state_mod()
state:load():finally(function()
---@diagnostic disable-next-line: redefined-local
local err, stat = pcall(vim.loop.fs_stat, cache_path)
if err then
if type(stat) == 'string' and stat:match([[^ENOENT.*]]) then
error('Cache file did not exist')
end
end
end)
end)

it('should save the cache file as valid json', function()
local state = state_mod()
state:save():finally(function()
utils.readfile(cache_path, { raw = true }):next(function(cache_content)
local err, err_msg = vim.json.decode(cache_content, {
luanil = { object = true, array = true },
})

if err then
error('Cache file did not contain valid json after saving! Error: ' .. vim.inspect(err_msg))
end
end)
end)
end)

it('should be able to save and load state data', function()
local state = state_mod()

-- Set a variable into the state object
state.my_var = 'hello world'
state:save():finally(function()
-- "Wipe" the variable
state.my_var = nil
state:load():finally(function()
-- These should be the same after the wipe. We just loaded it back in from the state cache.
assert.are.equal(state.my_var, 'hello world')
end)
end)
end)
end)

0 comments on commit 4ebb84b

Please sign in to comment.