Giter VIP home page Giter VIP logo

Comments (2)

HiPhish avatar HiPhish commented on July 19, 2024

Injecting it

Here is a hack that works: we explicitly inject it into nio.tests.it:

nio.tests.it = function(it, name, async_func)
  it(name, with_timeout(async_func, tonumber(vim.env.PLENARY_TEST_TIMEOUT)))
end

Now we can write the test:

	nio.tests.it(it, 'Discovers nothing in an empty file', function()
		vim.fn.writefile({''}, tempfile, 's')
		local result = adapter.discover_positions(tempfile)
	end)

It is kind of pointless to have separate it, before_each and after_each functions inside nio.tests if we have to pass the globals explicitly anyway. So we can add a metatable to nio.tests that lets us call the module and pass the global busted function:

local mt = {
	__call = function(_table, hook, name, async_func)
		hook(name, with_timeout(async_func, tonumber(vim.env.PLENARY_TEST_TIMEOUT)))
	end
}

setmetatable(nio.tests, mt)

With this the module remains backwards compatible and the test is less noisy:

	nio.tests(it, 'Discovers nothing in an empty file', function()
		vim.fn.writefile({''}, tempfile, 's')
		local result = adapter.discover_positions(tempfile)
	end)

What do you think? Is this an acceptable solution?

from neotest.

HiPhish avatar HiPhish commented on July 19, 2024

Dynamically resolve it and friends

A variant of the previous solution which preserves the existing API without making the module callable. Here we use the __index metamethod to automatically resolve it, before_each and after_each when they are requested.

local mt = {
	__index = function(_table, key)
		local env = getfenv(2)
		local hook = env[key]
		return function(name, async_func)
			hook(name, with_timeout(async_func, tonumber(vim.env.PLENARY_TEST_TIMEOUT)))
		end
	end
}

setmetatable(nio.tests, mt)

Now the test can be written as usual:

	nio.tests.it('Discovers nothing in an empty file', function()
		vim.fn.writefile({''}, tempfile, 's')
		local result = adapter.discover_positions(tempfile)
	end)

from neotest.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.