Skip to content

Commit 8435a54

Browse files
committed
Fix unit test includes and test data directory location
1 parent 2163c0b commit 8435a54

7 files changed

+28
-26
lines changed

CMakeLists.txt

+4-1
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,8 @@ execute_process(COMMAND scripts/update_single_include.sh WORKING_DIRECTORY ${PRO
8181
if(BUILD_TESTING AND INJA_BUILD_TESTS)
8282
enable_testing()
8383

84+
add_definitions(-D__TEST_DIR__=${CMAKE_CURRENT_SOURCE_DIR}/test)
85+
8486
add_executable(inja_test test/test.cpp)
8587
target_link_libraries(inja_test PRIVATE inja)
8688
target_include_directories(inja_test PRIVATE include third_party/include)
@@ -104,7 +106,8 @@ if(BUILD_TESTING AND INJA_BUILD_TESTS)
104106
add_test(single_inja_test ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/single_inja_test)
105107

106108

107-
add_executable(inja_benchmark test/benchmark.cpp)
109+
add_executable(inja_benchmark test/benchmark.cpp
110+
test/test-common.hpp)
108111
target_link_libraries(inja_benchmark PRIVATE inja)
109112
target_include_directories(inja_benchmark PRIVATE third_party/include)
110113
endif()

test/test-common.hpp

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#ifndef INCLUDE_TEST_COMMON_HPP_
2+
#define INCLUDE_TEST_COMMON_HPP_
3+
4+
#include <string>
5+
#include <doctest/doctest.h>
6+
7+
extern const std::string test_file_directory;
8+
9+
#endif // INCLUDE_TEST_COMMON_HPP_

test/test-files.cpp

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
// Copyright (c) 2020 Pantor. All rights reserved.
22

3-
#include <doctest/doctest.h>
4-
53
#include "inja/environment.hpp"
6-
#include "inja/inja.hpp"
4+
5+
#include "test-common.hpp"
76

87
TEST_CASE("loading") {
98
inja::Environment env;
@@ -99,7 +98,8 @@ TEST_CASE("include-in-memory-and-file-template") {
9998
inja::json data;
10099
data["name"] = "Jeff";
101100

102-
CHECK_THROWS_WITH(env.render_file("include-both.txt", data), "[inja.exception.file_error] failed accessing file at '../test/data/body'");
101+
std::string error_message = "[inja.exception.file_error] failed accessing file at '" + test_file_directory + "body'";
102+
CHECK_THROWS_WITH(env.render_file("include-both.txt", data), error_message.c_str());
103103

104104
const auto parsed_body_template = env.parse("Bye {{ name }}.");
105105
env.include_template("body", parsed_body_template);

test/test-functions.cpp

+2-4
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
// Copyright (c) 2020 Pantor. All rights reserved.
22

3-
#include <doctest/doctest.h>
4-
#include <string>
5-
63
#include "inja/environment.hpp"
7-
#include "inja/inja.hpp"
4+
5+
#include "test-common.hpp"
86

97
TEST_CASE("functions") {
108
inja::Environment env;

test/test-renderer.cpp

+2-5
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,8 @@
11
// Copyright (c) 2020 Pantor. All rights reserved.
22

3-
#include <doctest/doctest.h>
4-
#include <string>
5-
63
#include "inja/environment.hpp"
7-
#include "inja/inja.hpp"
8-
#include "inja/template.hpp"
4+
5+
#include "test-common.hpp"
96

107
TEST_CASE("types") {
118
inja::Environment env;

test/test-units.cpp

+2-6
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,8 @@
11
// Copyright (c) 2020 Pantor. All rights reserved.
22

3-
#include <doctest/doctest.h>
4-
#include <string>
5-
63
#include "inja/environment.hpp"
7-
#include "inja/function_storage.hpp"
8-
#include "inja/inja.hpp"
9-
#include "inja/utils.hpp"
4+
5+
#include "test-common.hpp"
106

117
TEST_CASE("source location") {
128
std::string content = R""""(Lorem Ipsum

test/test.cpp

+5-6
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,15 @@
11
// Copyright (c) 2020 Pantor. All rights reserved.
22

33
#define DOCTEST_CONFIG_IMPLEMENT_WITH_MAIN
4-
5-
#include <doctest/doctest.h>
6-
74
#define JSON_USE_IMPLICIT_CONVERSIONS 0
85
#define JSON_NO_IO 1
9-
#include "inja/inja.hpp"
10-
11-
const std::string test_file_directory {"../test/data/"};
126

137
#include "test-files.cpp"
148
#include "test-functions.cpp"
159
#include "test-renderer.cpp"
1610
#include "test-units.cpp"
11+
12+
#define xstr(s) str(s)
13+
#define str(s) #s
14+
15+
const std::string test_file_directory { xstr(__TEST_DIR__)"/data/" };

0 commit comments

Comments
 (0)