From ddd6f8f4ad7c78c8b5e2cd71b2c7fd6b9317ee46 Mon Sep 17 00:00:00 2001 From: Fredrik Averpil Date: Sat, 29 Jun 2024 14:34:14 +0200 Subject: [PATCH] feat: extend table tests --- tests/go/positions_spec.lua | 146 ++++++++++++++++++++++++++++++------ tests/go/positions_test.go | 109 ++++++++++++++++++++++----- 2 files changed, 216 insertions(+), 39 deletions(-) diff --git a/tests/go/positions_spec.lua b/tests/go/positions_spec.lua index e5e2c28f..939cf053 100644 --- a/tests/go/positions_spec.lua +++ b/tests/go/positions_spec.lua @@ -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() @@ -8,9 +26,8 @@ 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", }, { @@ -18,7 +35,6 @@ describe("Discovery of test positions", function() id = test_filepath .. "::TestTopLevel", name = "TestTopLevel", path = test_filepath, - range = { 4, 0, 8, 1 }, type = "test", }, }, @@ -27,7 +43,6 @@ describe("Discovery of test positions", function() id = test_filepath .. "::TestTopLevelWithSubTest", name = "TestTopLevelWithSubTest", path = test_filepath, - range = { 10, 0, 16, 1 }, type = "test", }, { @@ -35,77 +50,159 @@ describe("Discovery of test positions", function() 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 @@ -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) diff --git a/tests/go/positions_test.go b/tests/go/positions_test.go index 60329c31..f4bbeb0a 100644 --- a/tests/go/positions_test.go +++ b/tests/go/positions_test.go @@ -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 { @@ -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) + } + }) + } +}