Luigit
repositories / dotfiles

dotfiles

bugabingas dorkfiles

owned by admin

neovim/tests/std/dbg/init_spec.lua

Raw
describe( 'std.dbg', function ()
	local dbg
	local TEST_FILE = vim.fn.stdpath 'state' .. '/test_dbg_spec.jsonl'

	local function read_events()
		local handle = io.open( TEST_FILE, 'r' )
		if not handle then return {} end
		local events = {}
		for line in handle:lines() do
			if line ~= '' then table.insert( events, vim.json.decode( line ) ) end
		end
		handle:close()
		return events
	end

	before_each( function ()
		package.loaded['std.dbg'] = nil
		vim.g.bugabinga_debug_file_override = TEST_FILE
		vim.fn.delete( TEST_FILE )
		dbg = require 'std.dbg'
		vim.g.bugabinga_debug_mode = false
	end )

	after_each( function ()
		vim.g.bugabinga_debug_mode = false
		vim.g.bugabinga_debug_file_override = nil
		vim.fn.delete( TEST_FILE )
	end )

	it( 'is a no-op when debug mode is off', function ()
		dbg 'nothing'
		assert.are.same( {}, read_events() )
	end )

	it( 'writes a DEBUG event when enabled', function ()
		vim.g.bugabinga_debug_mode = true
		dbg 'hello'
		local events = read_events()
		assert.equal( 1, #events )
		assert.equal( 'DEBUG', events[1].kind )
		assert.equal( 'hello', events[1].message )
	end )

	it( 'formats printf-style with string.format', function ()
		vim.g.bugabinga_debug_mode = true
		dbg( '%s:%d', 'file', 42 )
		assert.equal( 'file:42', read_events()[1].message )
	end )

	it( 'falls back to the raw message on format errors', function ()
		vim.g.bugabinga_debug_mode = true
		dbg '100% complete'
		assert.equal( '100% complete', read_events()[1].message )
	end )

	it( 'records the given kind via dbg.kind', function ()
		vim.g.bugabinga_debug_mode = true
		dbg.kind( 'TRACE', 'tracing %s', 'x' )
		local event = read_events()[1]
		assert.equal( 'TRACE', event.kind )
		assert.equal( 'tracing x', event.message )
	end )

	it( 'logs via dbg.print', function ()
		vim.g.bugabinga_debug_mode = true
		dbg.print 'printed'
		assert.equal( 'printed', read_events()[1].message )
	end )

	it( 'captures caller context', function ()
		-- note: this spec lives under tests/std/dbg, which context() deliberately
		-- skips, so filename points at the test runner rather than this file.
		-- we can still assert that structured context is recorded.
		vim.g.bugabinga_debug_mode = true
		dbg 'ctx'
		local event = read_events()[1]
		assert.is_true( type( event.filename ) == 'string' and event.filename ~= '' )
		assert.is_true( type( event.line ) == 'number' )
	end )

	it( 'toggle flips debug mode and get reports it', function ()
		assert.is_false( dbg.get() )
		dbg.toggle()
		assert.is_true( dbg.get() )
	end )

	it( 'refuses assignment', function ()
		assert.has_error( function () dbg.foo = 1 end )
	end )
end )