Skip to content

Commit 1b8284e

Browse files
committed
feat: make is_test_file function configurable
1 parent c7daf28 commit 1b8284e

4 files changed

Lines changed: 30 additions & 4 deletions

File tree

README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ The adapter comes with the following defaults:
6767

6868
--- @class neotest-cpp.Config
6969
--- @field executables neotest-cpp.Config.Executable
70+
--- @field is_test_file fun(file_path: string): boolean
7071
--- @field log_level vim.log.levels
7172
M.defaults = {
7273
executables = {
@@ -129,6 +130,12 @@ M.defaults = {
129130
end,
130131
},
131132
},
133+
is_test_file = function(file_path)
134+
return vim.endswith(file_path, ".cpp")
135+
or vim.endswith(file_path, ".cxx")
136+
or vim.endswith(file_path, ".cc")
137+
or vim.endswith(file_path, ".c++")
138+
end,
132139
log_level = vim.log.levels.INFO,
133140
}
134141
}

lua/neotest-cpp/adapter.lua

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,8 @@ end
3232
---@param file_path string
3333
---@return boolean
3434
M.is_test_file = function(file_path)
35-
return vim.endswith(file_path, ".cpp")
36-
or vim.endswith(file_path, ".cxx")
37-
or vim.endswith(file_path, ".cc")
38-
or vim.endswith(file_path, ".c++")
35+
local config = require("neotest-cpp.config")
36+
return config.get().is_test_file(file_path)
3937
end
4038

4139
---Given a file path, parse all the tests within it.

lua/neotest-cpp/config.lua

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ local M = {}
1212

1313
--- @class neotest-cpp.Config
1414
--- @field executables neotest-cpp.Config.Executable
15+
--- @field is_test_file fun(file_path: string): boolean
1516
--- @field log_level vim.log.levels
1617
M.defaults = {
1718
executables = {
@@ -74,6 +75,12 @@ M.defaults = {
7475
end,
7576
},
7677
},
78+
is_test_file = function(file_path)
79+
return vim.endswith(file_path, ".cpp")
80+
or vim.endswith(file_path, ".cxx")
81+
or vim.endswith(file_path, ".cc")
82+
or vim.endswith(file_path, ".c++")
83+
end,
7784
log_level = vim.log.levels.INFO,
7885
}
7986

tests/unit/test_is_test_file.lua

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,4 +36,18 @@ T["should return false for non-C++ files"] = function()
3636
eq(is_test_file("/path/to/file.txt"), false)
3737
end
3838

39+
T["should respect custom is_test_file configuration"] = function()
40+
child.lua_func(function()
41+
require("neotest-cpp").setup({
42+
is_test_file = function(file_path)
43+
return vim.endswith(file_path, "_test.cpp")
44+
end,
45+
})
46+
end)
47+
48+
eq(is_test_file("/path/to/my_test.cpp"), true)
49+
eq(is_test_file("/path/to/regular.cpp"), false)
50+
eq(is_test_file("/path/to/test.cpp"), false)
51+
end
52+
3953
return T

0 commit comments

Comments
 (0)