Skip to content

Commit 231884e

Browse files
committed
Add a simple hello world test
1 parent 36134b5 commit 231884e

6 files changed

Lines changed: 86 additions & 2 deletions

File tree

CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ set(VERSION_MINOR "1")
1010
set(VERSION_PATCH "0")
1111
set(VERSION "${VERSION_MAJOR}.${VERSION_MINOR}.${VERSION_PATCH}")
1212

13+
enable_testing()
14+
1315
# Default to release C++ flags if CMAKE_BUILD_TYPE not set
1416
if(NOT CMAKE_BUILD_TYPE)
1517
set(CMAKE_BUILD_TYPE Release CACHE STRING

Jenkinsfile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ pipeline {
1111
steps {
1212
sh "mkdir -p build-debug"
1313
sh "cd build-debug && cmake -DCMAKE_BUILD_TYPE=Debug .."
14-
sh "cd build-debug && cmake --build . --target all -- -j3"
14+
sh "cd build-debug && cmake --build . --target all -- -j3 && ctest"
1515
}
1616
}
1717
stage('CMake Release build') {
@@ -24,7 +24,7 @@ pipeline {
2424
steps {
2525
sh "mkdir -p build-release"
2626
sh "cd build-release && cmake -DCMAKE_BUILD_TYPE=Release .."
27-
sh "cd build-release && cmake --build . --target all -- -j3"
27+
sh "cd build-release && cmake --build . --target all -- -j3 && ctest"
2828
}
2929
}
3030
}

src/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,4 @@ install(TARGETS ${LIBRARY_NAME}
2424
PUBLIC_HEADER DESTINATION include
2525
)
2626

27+
add_subdirectory(tests)

src/tests/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
add_subdirectory(hello_world)
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
set(SIMPLE_LOGGER_DIR ${CMAKE_SOURCE_DIR}/src)
2+
include_directories(${SIMPLE_LOGGER_DIR} ${CMAKE_CURRENT_SOURCE_DIR})
3+
4+
set(NAME hello_world)
5+
set(SRC ${NAME}.cpp)
6+
7+
set(EXECUTABLE_OUTPUT_PATH ${CMAKE_BINARY_DIR}/tests)
8+
add_executable(${NAME} ${SRC})
9+
add_test(${NAME} ${CMAKE_BINARY_DIR}/tests/${NAME})
10+
target_link_libraries(${NAME} ${LIBRARY_NAME})
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
// MIT License
2+
//
3+
// Copyright (c) 2019 Jussi Lind <jussi.lind@iki.fi>
4+
//
5+
// https://github.com/juzzlin/SimpleLogger
6+
//
7+
// Permission is hereby granted, free of charge, to any person obtaining a copy
8+
// of this software and associated documentation files (the "Software"), to deal
9+
// in the Software without restriction, including without limitation the rights
10+
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11+
// copies of the Software, and to permit persons to whom the Software is
12+
// furnished to do so, subject to the following conditions:
13+
//
14+
// The above copyright notice and this permission notice shall be included in all
15+
// copies or substantial portions of the Software.
16+
//
17+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18+
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19+
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20+
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21+
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22+
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23+
// SOFTWARE.
24+
25+
#include "simple_logger.hpp"
26+
27+
// Don't compile asserts away
28+
#ifdef NDEBUG
29+
#undef NDEBUG
30+
#endif
31+
32+
#include <cassert>
33+
#include <cstdlib>
34+
#include <fstream>
35+
36+
int main(int, char **)
37+
{
38+
using juzzlin::L;
39+
40+
const auto logFile = "hello_world.log";
41+
42+
L::init(logFile);
43+
L::enableEchoMode(true);
44+
L::enableDateTime(true);
45+
L::setLoggingLevel(L::Level::Trace);
46+
47+
std::string message = "Hello, world!";
48+
49+
L().trace() << message;
50+
L().debug() << message;
51+
L().info() << message;
52+
L().warning() << message;
53+
L().error() << message;
54+
L().fatal() << message;
55+
56+
std::ifstream fin{logFile};
57+
assert(fin.is_open());
58+
59+
int lines = 0;
60+
std::string line;
61+
while (std::getline(fin, line))
62+
{
63+
assert(line.find(message) != std::string::npos);
64+
lines++;
65+
}
66+
67+
assert(lines == 6);
68+
69+
return EXIT_SUCCESS;
70+
}

0 commit comments

Comments
 (0)