repositories / dotfiles

dotfiles

bugabingas dorkfiles

owned by bugabinga

neovim/tests/std/dbg/init_spec.lua

Raw
describe( 'std.dbg.init', function ()
	local dbg
	local dbg_file
	local mod

	before_each( function ()
		package.loaded['std.dbg'] = nil
		package.loaded['std.dbg.init'] = nil
		mod = require 'std.dbg.file'
		dbg_file = mod.get_path()
		vim.fn.delete( dbg_file )
		dbg = require 'std.dbg'
		vim.g.bugabinga_debug_mode = false
	end )

	after_each( function ()
		vim.g.bugabinga_debug_mode = false
		vim.fn.delete( dbg_file )
	end )

	describe( 'dbg(...) __call', function ()
		it( 'should be callable (no error when called)', function ()
			vim.g.bugabinga_debug_mode = false
			local ok, err = pcall( function () dbg  'test'  end )
			assert.is_true( ok )
		end )

		it( 'should be no-op when debug mode is off', function ()
			vim.g.bugabinga_debug_mode = false
			dbg  'test message'
			local file = io.open( dbg_file, 'r' )
			assert.is_nil( file )
		end )

		it( 'should write event when debug mode is on', function ()
			vim.g.bugabinga_debug_mode = true
			dbg( 'test {1}', 'hello' )
			local file = io.open( dbg_file, 'r' )
			assert.is_not_nil( file )
			local content = file:read '*all'
			file:close()
			assert.is_not_nil( content:match '"kind"' )
			assert.is_not_nil( content:match '"message"' )
			assert.is_not_nil( content:match '"data"' )
		end )

		it( 'should write event with date as os.time() integer', function ()
			vim.g.bugabinga_debug_mode = true
			dbg  'test'
			local file = io.open( dbg_file, 'r' )
			local content = file:read '*all'
			file:close()
			local json = vim.json.decode( content )
			assert.is_number( json.date )
		end )

		it( 'should have all required fields in written event', function ()
			vim.g.bugabinga_debug_mode = true
			dbg( 'test {1}', 'y' )
			local file = io.open( dbg_file, 'r' )
			local content = file:read '*all'
			file:close()
			local json = vim.json.decode( content )
			assert.equals( 'DEBUG',    json.kind )
			assert.equals( 'test y', json.message )
			assert.is_table( json.data )
			assert.is_string( json.filename )
			assert.is_string( json.fn )
			assert.is_number( json.line )
		end )
	end )

	describe( 'dbg.kind()', function ()
		it( 'should be a function', function ()
			assert.is_function( dbg.kind )
		end )

		it( 'should log with specified kind', function ()
			vim.g.bugabinga_debug_mode = true
			dbg.kind( 'ERROR', 'error message' )
			local file = io.open( dbg_file, 'r' )
			local content = file:read '*all'
			file:close()
			local json = vim.json.decode( content )
			assert.equals( 'ERROR', json.kind )
		end )

		it( 'should log TRACE kind', function ()
			vim.g.bugabinga_debug_mode = true
			dbg.kind( 'TRACE', 'trace message' )
			local file = io.open( dbg_file, 'r' )
			local content = file:read '*all'
			file:close()
			local json = vim.json.decode( content )
			assert.equals( 'TRACE', json.kind )
		end )
	end )

	describe( 'dbg.toggle()', function ()
		it( 'should toggle debug mode from off to on', function ()
			vim.g.bugabinga_debug_mode = false
			assert.is_false( dbg.get() )
			dbg.toggle()
			assert.is_true( dbg.get() )
		end )

		it( 'should toggle debug mode from on to off', function ()
			vim.g.bugabinga_debug_mode = true
			assert.is_true( dbg.get() )
			dbg.toggle()
			assert.is_false( dbg.get() )
		end )
	end )

	describe( 'dbg.get()', function ()
		it( 'should return current debug mode state', function ()
			vim.g.bugabinga_debug_mode = false
			assert.is_false( dbg.get() )
			vim.g.bugabinga_debug_mode = true
			assert.is_true( dbg.get() )
		end )
	end )

	describe( '__newindex guard', function ()
		it( 'should error on assignment attempt', function ()
			local ok, err = pcall( function ()
				dbg.newfield = 'value'
			end )
			assert.is_false( ok )
			assert.is_string( err )
		end )
	end )

	describe( 'user commands', function ()
		it( 'should register :Debug command', function ()
			local ok = pcall( vim.api.nvim_get_command, 'Debug' )
			assert.is_true( ok ~= nil )
		end )

		it( 'should register :DebugOpen command', function ()
			local ok = pcall( vim.api.nvim_get_command, 'DebugOpen' )
			assert.is_true( ok ~= nil )
		end )

		it( 'should register :DebugOpenRaw command', function ()
			local ok = pcall( vim.api.nvim_get_command, 'DebugOpenRaw' )
			assert.is_true( ok ~= nil )
		end )
	end )
end )