Skip to content

Commit

Permalink
test: testify suites
Browse files Browse the repository at this point in the history
  • Loading branch information
fredrikaverpil committed Jul 11, 2024
1 parent c947f7e commit 92a0f4d
Show file tree
Hide file tree
Showing 6 changed files with 267 additions and 109 deletions.
23 changes: 23 additions & 0 deletions tests/go/testify/othersuite_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package testify

// Basic imports
import (
"testing"

"github.com/stretchr/testify/suite"
)

type OtherTestSuite struct {
suite.Suite
VariableThatShouldStartAtFive int
}

// A second suite setup method.
func (suite *OtherTestSuite) SetupTest() {
suite.VariableThatShouldStartAtFive = 5
}

func TestOtherTestSuite(t *testing.T) {
s := &OtherTestSuite{}
suite.Run(t, s)
}
172 changes: 172 additions & 0 deletions tests/go/testify/positions_spec.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,172 @@
local nio = require("nio")
local _ = require("plenary")

local adapter = require("neotest-golang")
local options = require("neotest-golang.options")
local testify = require("neotest-golang.features.testify")

local function compareIgnoringKeys(t1, t2, ignoreKeys)
local function copyTable(t, ignoreKeys)
local copy = {}
for k, v in pairs(t) do
if not ignoreKeys[k] then
if type(v) == "table" then
copy[k] = copyTable(v, ignoreKeys)
else
copy[k] = v
end
end
end
return copy
end
return copyTable(t1, ignoreKeys), copyTable(t2, ignoreKeys)
end

describe("With testify_enabled=false", function()
it("Discover test functions", function()
-- Arrange
local test_filepath = vim.loop.cwd()
.. "/tests/go/testify/positions_test.go"
local expected = {
{
id = test_filepath,
name = "positions_test.go",
path = test_filepath,
type = "file",
},
{
{
id = test_filepath .. "::TestExampleTestSuite",
name = "TestExampleTestSuite",
path = test_filepath,
type = "test",
},
},
{
{
id = test_filepath .. "::TestTrivial",
name = "TestTrivial",
path = test_filepath,
type = "test",
},
},
}

-- Act
---@type neotest.Tree
local tree =
nio.tests.with_async_context(adapter.discover_positions, test_filepath)

-- Assert
local result = tree:to_list()
local ignoreKeys = { range = true }
local expectedCopy, resultCopy =
compareIgnoringKeys(expected, result, ignoreKeys)
assert.are.same(vim.inspect(expectedCopy), vim.inspect(resultCopy))
assert.are.same(expectedCopy, resultCopy)
end)
end)

describe("With testify_enabled=true", function()
it("Discover namespaces, test methods and test function", function()
-- Arrange
local test_filepath = vim.loop.cwd()
.. "/tests/go/testify/positions_test.go"
options.set({ testify = true }) -- enable testify
testify.lookup.generate() -- generate lookup

local expected = {
{
id = test_filepath,
name = "positions_test.go",
path = test_filepath,
type = "file",
},
{
{
id = test_filepath .. "::TestExampleTestSuite",
name = "TestExampleTestSuite",
path = test_filepath,
type = "namespace",
},
{
{
id = test_filepath .. "::TestExampleTestSuite::TestExample",
name = "TestExample",
path = test_filepath,
type = "test",
},
},
{
{
id = test_filepath .. "::TestExampleTestSuite::TestExample2",
name = "TestExample2",
path = test_filepath,
type = "test",
},
},
},
{
{
id = test_filepath .. "::ExampleTestSuite2",
name = "ExampleTestSuite2",
path = test_filepath,
type = "namespace",
},
{
{
id = test_filepath .. "::ExampleTestSuite2::TestExample",
name = "TestExample",
path = test_filepath,
type = "test",
},
},
{
{
id = test_filepath .. "::ExampleTestSuite2::TestExample2",
name = "TestExample2",
path = test_filepath,
type = "test",
},
},
},
{
{
id = test_filepath .. "::TestTrivial",
name = "TestTrivial",
path = test_filepath,
type = "test",
},
},
{
{
id = test_filepath .. "::TestOtherTestSuite",
name = "TestOtherTestSuite",
path = test_filepath,
type = "namespace",
},
{
{
id = test_filepath .. "::TestOtherTestSuite::TestOther",
name = "TestOther",
path = test_filepath,
type = "test",
},
},
},
}

-- Act
---@type neotest.Tree
local tree =
nio.tests.with_async_context(adapter.discover_positions, test_filepath)

-- Assert
local result = tree:to_list()
local ignoreKeys = { range = true }
local expectedCopy, resultCopy =
compareIgnoringKeys(expected, result, ignoreKeys)
assert.are.same(vim.inspect(expectedCopy), vim.inspect(resultCopy))
assert.are.same(expectedCopy, resultCopy)
end)
end)
72 changes: 72 additions & 0 deletions tests/go/testify/positions_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package testify

// Basic imports
import (
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/suite"
)

// Define the suite, and absorb the built-in basic suite
// functionality from testify - including a T() method which
// returns the current testing context
type ExampleTestSuite struct {
suite.Suite
VariableThatShouldStartAtFive int
}

// Make sure that VariableThatShouldStartAtFive is set to five
// before each test
func (suite *ExampleTestSuite) SetupTest() {
suite.VariableThatShouldStartAtFive = 5
}

// All methods that begin with "Test" are run as tests within a
// suite.
func (suite *ExampleTestSuite) TestExample() {
assert.Equal(suite.T(), 5, suite.VariableThatShouldStartAtFive)
}

func (suite *ExampleTestSuite) TestExample2() {
assert.Equal(suite.T(), 5, suite.VariableThatShouldStartAtFive)
}

// In order for 'go test' to run this suite, we need to create
// a normal test function and pass our suite to suite.Run
func TestExampleTestSuite(t *testing.T) {
suite.Run(t, new(ExampleTestSuite))
}

// --------------------------------------------------------------------

type ExampleTestSuite2 struct {
suite.Suite
VariableThatShouldStartAtFive int
}

func (suite *ExampleTestSuite2) SetupTest() {
suite.VariableThatShouldStartAtFive = 5
}

func (suite *ExampleTestSuite2) TestExample() {
assert.Equal(suite.T(), 5, suite.VariableThatShouldStartAtFive)
}

func (suite *ExampleTestSuite2) TestExample2() {
assert.Equal(suite.T(), 5, suite.VariableThatShouldStartAtFive)
}

// --------------------------------------------------------------------

// Just a regular test.
func TestTrivial(t *testing.T) {
assert.Equal(t, 1, 1)
}

// --------------------------------------------------------------------

// A test method which uses a receiver type defined by struct in another file.
func (suite *OtherTestSuite) TestOther() {
assert.Equal(suite.T(), 5, suite.VariableThatShouldStartAtFive)
}
41 changes: 0 additions & 41 deletions tests/go/testify1_test.go

This file was deleted.

32 changes: 0 additions & 32 deletions tests/go/testify2_test.go

This file was deleted.

36 changes: 0 additions & 36 deletions tests/go/testify_test.go

This file was deleted.

0 comments on commit 92a0f4d

Please sign in to comment.