Skip to content

Commit

Permalink
Merge pull request #4359 from xmake-io/doctest
Browse files Browse the repository at this point in the history
Add doctest example
  • Loading branch information
waruqi authored Nov 3, 2023
2 parents 264f224 + c081aec commit 7a34812
Show file tree
Hide file tree
Showing 8 changed files with 121 additions and 26 deletions.
8 changes: 8 additions & 0 deletions tests/projects/c++/doctest/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# Xmake cache
.xmake/
build/

# MacOS Cache
.DS_Store


2 changes: 2 additions & 0 deletions tests/projects/c++/doctest/src/foo.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
void foo() {
}
9 changes: 9 additions & 0 deletions tests/projects/c++/doctest/src/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#include <iostream>

using namespace std;

int main(int argc, char** argv)
{
cout << "hello world!" << endl;
return 0;
}
12 changes: 12 additions & 0 deletions tests/projects/c++/doctest/tests/test_1.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#include "doctest/doctest.h"

static int factorial(int number) {
return number <= 1 ? number : factorial(number - 1) * number;
}

TEST_CASE("testing the factorial function") {
CHECK(factorial(1) == 10);
CHECK(factorial(2) == 2);
CHECK(factorial(3) == 6);
CHECK(factorial(10) == 3628800);
}
12 changes: 12 additions & 0 deletions tests/projects/c++/doctest/tests/test_2.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#include "doctest/doctest.h"

static int factorial(int number) {
return number <= 1 ? number : factorial(number - 1) * number;
}

TEST_CASE("testing the factorial function") {
CHECK(factorial(1) == 1);
CHECK(factorial(2) == 2);
CHECK(factorial(3) == 6);
CHECK(factorial(10) == 3628800);
}
28 changes: 28 additions & 0 deletions tests/projects/c++/doctest/xmake.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
add_rules("mode.debug", "mode.release")

add_requires("doctest")

target("doctest")
set_kind("binary")
add_files("src/*.cpp")
for _, testfile in ipairs(os.files("tests/*.cpp")) do
add_tests(path.basename(testfile), {
files = testfile,
remove_files = "src/main.cpp",
languages = "c++11",
packages = "doctest",
defines = "DOCTEST_CONFIG_IMPLEMENT_WITH_MAIN"})
end

target("doctest_shared")
set_kind("shared")
add_files("src/foo.cpp")
for _, testfile in ipairs(os.files("tests/*.cpp")) do
add_tests(path.basename(testfile), {
kind = "binary",
files = testfile,
languages = "c++11",
packages = "doctest",
defines = "DOCTEST_CONFIG_IMPLEMENT_WITH_MAIN"})
end

75 changes: 49 additions & 26 deletions xmake/actions/test/main.lua
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ function _do_test_target(target, opt)
errors = errdata or errors
if not errors or #errors == 0 then
if ok ~= nil then
errors = string.format("run failed, exit code: %d", ok)
errors = string.format("%s\nrun failed, exit code: %d", outdata or "", ok)
else
errors = string.format("run failed, exit error: %s", syserrors and syserrors or "unknown reason")
end
Expand Down Expand Up @@ -344,34 +344,57 @@ function main()
group_pattern = "^" .. path.pattern(group_pattern) .. "$"
end
for _, target in ipairs(project.ordertargets()) do
if target:is_binary() or target:script("run") then
for _, name in ipairs(target:get("tests")) do
local extra = target:extraconf("tests", name)
local testname = target:name() .. "/" .. name
local testinfo = {name = testname, target = target}
if extra then
table.join2(testinfo, extra)
if extra.files then
local target_new = target:clone()
local scriptdir = target:scriptdir()
target_new:name_set(target:name() .. "_" .. name)
for _, file in ipairs(extra.files) do
file = path.absolute(file, scriptdir)
file = path.relative(file, os.projectdir())
target_new:add("files", file, {defines = extra.defines})
project.target_add(target_new)
end
testinfo.target = target_new
for _, name in ipairs(target:get("tests")) do
local extra = target:extraconf("tests", name)
local testname = target:name() .. "/" .. name
local testinfo = {name = testname, target = target}
if extra then
table.join2(testinfo, extra)
if extra.files then
local target_new = target:clone()
local scriptdir = target:scriptdir()
target_new:name_set(target:name() .. "_" .. name)
for _, file in ipairs(extra.files) do
file = path.absolute(file, scriptdir)
file = path.relative(file, os.projectdir())
target_new:add("files", file, {defines = extra.defines,
cflags = extra.cflags,
cxflags = extra.cxflags,
cxxflags = extra.cxxflags,
undefines = extra.undefines,
languages = extra.languages})
project.target_add(target_new)
end
for _, file in ipairs(extra.remove_files) do
file = path.absolute(file, scriptdir)
file = path.relative(file, os.projectdir())
target_new:remove("files", file)
end
if extra.kind then
target_new:set("kind", kind)
end
if extra.frameworks then
target_new:add("frameworks", extra.frameworks)
end
if extra.links then
target_new:add("links", extra.links)
end
if extra.syslinks then
target_new:add("syslinks", extra.syslinks)
end
if extra.packages then
target_new:add("packages", extra.packages)
end
testinfo.target = target_new
end
if not testinfo.group then
testinfo.group = target:get("group")
end
end
if not testinfo.group then
testinfo.group = target:get("group")
end

local group = testinfo.group
if (not group_pattern) or (group_pattern and group and group:match(group_pattern)) then
tests[testname] = testinfo
end
local group = testinfo.group
if (not group_pattern) or (group_pattern and group and group:match(group_pattern)) then
tests[testname] = testinfo
end
end
end
Expand Down
1 change: 1 addition & 0 deletions xmake/core/project/target.lua
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,7 @@ function _instance:_invalidate(name)
-- we need to flush the source files cache if target/files are modified, e.g. `target:add("files", "xxx.c")`
if name == "files" then
self._SOURCEFILES = nil
self._FILESCONFIG = nil
elseif name == "deps" then
self._DEPS = nil
self._ORDERDEPS = nil
Expand Down

0 comments on commit 7a34812

Please sign in to comment.