Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: extend table tests #69

Merged
merged 1 commit into from
Jun 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
146 changes: 124 additions & 22 deletions tests/go/positions_spec.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,23 @@
local nio = require("nio")
local adapter = require("neotest-golang")
local _ = require("plenary")

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("Discovery of test positions", function()
it("Discover OK", function()
Expand All @@ -8,17 +26,15 @@ describe("Discovery of test positions", function()
local expected = {
{
id = test_filepath,
name = vim.fn.fnamemodify(test_filepath, ":t"),
name = "positions_test.go",
path = test_filepath,
range = { 0, 0, 59, 0 }, -- NOTE: this always gets changed when tests are added or removed
type = "file",
},
{
{
id = test_filepath .. "::TestTopLevel",
name = "TestTopLevel",
path = test_filepath,
range = { 4, 0, 8, 1 },
type = "test",
},
},
Expand All @@ -27,85 +43,166 @@ describe("Discovery of test positions", function()
id = test_filepath .. "::TestTopLevelWithSubTest",
name = "TestTopLevelWithSubTest",
path = test_filepath,
range = { 10, 0, 16, 1 },
type = "test",
},
{
{
id = test_filepath .. '::TestTopLevelWithSubTest::"SubTest"',
name = '"SubTest"',
path = test_filepath,
range = { 11, 1, 15, 3 },
type = "test",
},
},
},
{
{
id = test_filepath .. "::TestTopLevelWithTableTests",
name = "TestTopLevelWithTableTests",
id = test_filepath .. "::TestTableTestStruct",
name = "TestTableTestStruct",
path = test_filepath,
range = { 18, 0, 36, 1 },
type = "test",
},
{
{
id = test_filepath .. '::TestTopLevelWithTableTests::"TableTest1"',
id = test_filepath .. '::TestTableTestStruct::"TableTest1"',
name = '"TableTest1"',
path = test_filepath,
range = { 25, 2, 25, 47 },
type = "test",
},
},
{
{
id = test_filepath .. '::TestTopLevelWithTableTests::"TableTest2"',
id = test_filepath .. '::TestTableTestStruct::"TableTest2"',
name = '"TableTest2"',
path = test_filepath,
range = { 26, 2, 26, 47 },
type = "test",
},
},
},
{
{
id = test_filepath .. "::TestTopLevelWithSubTestWithTableTests",
name = "TestTopLevelWithSubTestWithTableTests",
id = test_filepath .. "::TestSubTestTableTestStruct",
name = "TestSubTestTableTestStruct",
path = test_filepath,
type = "test",
},
{
{
id = test_filepath .. '::TestSubTestTableTestStruct::"SubTest"',
name = '"SubTest"',
path = test_filepath,
type = "test",
},
{
{
id = test_filepath
.. '::TestSubTestTableTestStruct::"SubTest"::"TableTest1"',
name = '"TableTest1"',
path = test_filepath,
type = "test",
},
},
{
{
id = test_filepath
.. '::TestSubTestTableTestStruct::"SubTest"::"TableTest2"',
name = '"TableTest2"',
path = test_filepath,
type = "test",
},
},
},
},
{
{
id = test_filepath .. "::TestTableTestInlineStruct",
name = "TestTableTestInlineStruct",
path = test_filepath,
type = "test",
},
{
{
id = test_filepath .. '::TestTableTestInlineStruct::"TableTest1"',
name = '"TableTest1"',
path = test_filepath,
type = "test",
},
},
{
{
id = test_filepath .. '::TestTableTestInlineStruct::"TableTest2"',
name = '"TableTest2"',
path = test_filepath,
type = "test",
},
},
},
{
{
id = test_filepath .. "::TestSubTestTableTestInlineStruct",
name = "TestSubTestTableTestInlineStruct",
path = test_filepath,
range = { 38, 0, 58, 1 },
type = "test",
},
{
{
id = test_filepath
.. '::TestTopLevelWithSubTestWithTableTests::"SubTest"',
.. '::TestSubTestTableTestInlineStruct::"SubTest"',
name = '"SubTest"',
path = test_filepath,
range = { 39, 1, 57, 3 },
type = "test",
},
{
{
id = test_filepath
.. '::TestTopLevelWithSubTestWithTableTests::"SubTest"::"TableTest1"',
.. '::TestSubTestTableTestInlineStruct::"SubTest"::"TableTest1"',
name = '"TableTest1"',
path = test_filepath,
range = { 46, 3, 46, 48 },
type = "test",
},
},
{
{
id = test_filepath
.. '::TestTopLevelWithSubTestWithTableTests::"SubTest"::"TableTest2"',
.. '::TestSubTestTableTestInlineStruct::"SubTest"::"TableTest2"',
name = '"TableTest2"',
path = test_filepath,
range = { 47, 3, 47, 48 },
type = "test",
},
},
},
},
{
{
id = test_filepath .. "::TestTableTestMap",
name = "TestTableTestMap",
path = test_filepath,
type = "test",
},
{
{
id = test_filepath .. '::TestTableTestMap::"add 1+1"',
name = '"add 1+1"',
path = test_filepath,
type = "test",
},
},
{
{
id = test_filepath .. '::TestTableTestMap::"add 2+2"',
name = '"add 2+2"',
path = test_filepath,
type = "test",
},
},
{
{
id = test_filepath .. '::TestTableTestMap::"add 5+5"',
name = '"add 5+5"',
path = test_filepath,
type = "test",
},
},
},
}

-- Act
Expand All @@ -115,6 +212,11 @@ describe("Discovery of test positions", function()

-- Assert
local result = tree:to_list()
assert.are.same(vim.inspect(expected), vim.inspect(result))

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)
109 changes: 92 additions & 17 deletions tests/go/positions_test.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
package main

import "testing"
import (
"testing"
)

// Vanilla top-level test.
func TestTopLevel(t *testing.T) {
if Add(1, 2) != 3 {
t.Fail()
}
}

// Top-level test with sub-test.
func TestTopLevelWithSubTest(t *testing.T) {
t.Run("SubTest", func(t *testing.T) {
if Add(1, 2) != 3 {
Expand All @@ -16,44 +20,115 @@ func TestTopLevelWithSubTest(t *testing.T) {
})
}

func TestTopLevelWithTableTests(t *testing.T) {
// Table test defined as struct.
func TestTableTestStruct(t *testing.T) {
type table struct {
name string
x int
y int
want int
}

tt := []table{
{name: "TableTest1", x: 1, y: 2, want: 3},
{name: "TableTest2", x: 3, y: 4, want: 7},
}

for _, tc := range tt {
t.Run(tc.name, func(t *testing.T) {
if Add(tc.x, tc.y) != tc.want {
t.Fail()
}
})
}
}

// Table test defined as struct (in sub-test).
func TestSubTestTableTestStruct(t *testing.T) {
t.Run("SubTest", func(t *testing.T) {
type table struct {
name string
x int
y int
want int
}

tt := []table{
{name: "TableTest1", x: 1, y: 2, want: 3},
{name: "TableTest2", x: 3, y: 4, want: 7},
}

for _, tc := range tt {
t.Run(tc.name, func(t *testing.T) {
if Add(tc.x, tc.y) != tc.want {
t.Fail()
}
})
}
})
}

// Table test defined as anonymous struct.
func TestTableTestInlineStruct(t *testing.T) {
tt := []struct {
name string
x int
y int
expected int
name string
x int
y int
want int
}{
{name: "TableTest1", x: 1, y: 2, expected: 3},
{name: "TableTest2", x: 3, y: 4, expected: 7},
{name: "TableTest1", x: 1, y: 2, want: 3},
{name: "TableTest2", x: 3, y: 4, want: 7},
}

for _, tc := range tt {
t.Run(tc.name, func(t *testing.T) {
if Add(tc.x, tc.y) != tc.expected {
if Add(tc.x, tc.y) != tc.want {
t.Fail()
}
})
}
}

func TestTopLevelWithSubTestWithTableTests(t *testing.T) {
// Table test defined as anonymous struct (in sub-test).
func TestSubTestTableTestInlineStruct(t *testing.T) {
t.Run("SubTest", func(t *testing.T) {
tt := []struct {
name string
x int
y int
expected int
name string
x int
y int
want int
}{
{name: "TableTest1", x: 1, y: 2, expected: 3},
{name: "TableTest2", x: 3, y: 4, expected: 7},
{name: "TableTest1", x: 1, y: 2, want: 3},
{name: "TableTest2", x: 3, y: 4, want: 7},
}

for _, tc := range tt {
t.Run(tc.name, func(t *testing.T) {
if Add(tc.x, tc.y) != tc.expected {
if Add(tc.x, tc.y) != tc.want {
t.Fail()
}
})
}
})
}

// Table test defined as map.
func TestTableTestMap(t *testing.T) {
tt := map[string]struct {
a int
b int
want int
}{
"add 1+1": {a: 1, b: 1, want: 2},
"add 2+2": {a: 2, b: 2, want: 4},
"add 5+5": {a: 5, b: 5, want: 10},
}
for name, tc := range tt {
t.Run(name, func(t *testing.T) {
got := Add(tc.a, tc.b)
if got != tc.want {
t.Errorf("got %d, want %d", got, tc.want)
}
})
}
}
Loading