Skip to content

Commit

Permalink
Added test for tests and examples.
Browse files Browse the repository at this point in the history
  • Loading branch information
GPMueller committed Aug 17, 2019
1 parent b66a071 commit 2dd91b8
Show file tree
Hide file tree
Showing 7 changed files with 83 additions and 0 deletions.
17 changes: 17 additions & 0 deletions test/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,23 @@ def test_openmp(self):

# self.assertEqual(output, 'Hello!')

def test_tests_examples(self):
clang_build_try_except(['-d', 'test/tests_examples', '-V', '--test', '--examples'])

try:
output = subprocess.check_output(['./build/mylib/default/tests/bin/test'], stderr=subprocess.STDOUT).decode('utf-8').strip()
except subprocess.CalledProcessError as e:
self.fail(f'Could not run compiled program. Message:\n{e.output}')

self.assertRegex(output, r'.*All tests passed.*')

try:
output = subprocess.check_output(['./build/mylib/default/examples/bin/example_example'], stderr=subprocess.STDOUT).decode('utf-8').strip()
except subprocess.CalledProcessError as e:
self.fail(f'Could not run compiled program. Message:\n{e.output}')

self.assertRegex(output, r'.*Calling `mylib::magic_function\(1, 2\)` returns `5`')

def setUp(self):
logger = logging.getLogger('clang_build')
logger.setLevel(logging.INFO)
Expand Down
13 changes: 13 additions & 0 deletions test/tests_examples/clang-build.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
[mylib]
[mylib.tests]
single_executable = true
dependencies = ["catch"]

[catch]
url = "https://github.com/catchorg/Catch2"
include_directories_public = ["include", "single_include"]
[catch.tests]
sources_exclude = ["*"]
[catch.examples]
sources = ["010-TestCase.cpp", "231-Cfg-OutputStreams.cpp"] # Note: Cannot do multiple-file examples
# sources_exclude = ["*"]
9 changes: 9 additions & 0 deletions test/tests_examples/examples/example.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#include <mylib/lib.hpp>

#include <iostream>

int main()
{
std::cerr << "This is an example of using mylib:\n";
std::cerr << "Calling `mylib::magic_function(1, 2)` returns `" << mylib::magic_function(1, 2) << "`\n";
}
10 changes: 10 additions & 0 deletions test/tests_examples/include/mylib/lib.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#pragma once

namespace mylib
{
template <typename T>
T magic_function(const T& t1, const T& t2)
{
return t1 + 2 * t2;
}
}
2 changes: 2 additions & 0 deletions test/tests_examples/tests/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#define CATCH_CONFIG_MAIN
#include <catch2/catch.hpp>
16 changes: 16 additions & 0 deletions test/tests_examples/tests/test_1_2.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#include <mylib/lib.hpp>

#include <catch2/catch.hpp>

TEST_CASE( "first", "[1]" )
{
SECTION( "2" )
{
REQUIRE( mylib::magic_function(1, 2) == 5 );
}

SECTION( "3" )
{
REQUIRE( mylib::magic_function(1, 3) == 7 );
}
}
16 changes: 16 additions & 0 deletions test/tests_examples/tests/test_1_3.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#include <mylib/lib.hpp>

#include <catch2/catch.hpp>

TEST_CASE( "second", "[2]" )
{
SECTION( "5" )
{
REQUIRE( mylib::magic_function(2, 5) == 12 );
}

SECTION( "6" )
{
REQUIRE( mylib::magic_function(2, 6) == 14 );
}
}

0 comments on commit 2dd91b8

Please sign in to comment.