emlabcpp
modern opinionated embedded C++ library
gtest.h
Go to the documentation of this file.
1 
24 #pragma once
25 
26 #include "../../pmr/new_delete_resource.h"
27 #include "./base.h"
28 #include "./controller.h"
29 #include "./json.h"
30 
31 #ifdef EMLABCPP_USE_GTEST
32 
33 #include <gtest/gtest.h>
34 
35 namespace emlabcpp::testing
36 {
37 
38 inline ::testing::AssertionResult gtest_predicate( char const*, test_result const& tres )
39 {
40 
41  ::testing::AssertionResult res = ::testing::AssertionSuccess();
42  switch ( tres.status ) {
45  return res;
47  return ::testing::AssertionFailure() << "Test produced a failure, stopping";
49  return ::testing::AssertionFailure() << "Test errored";
50  }
51 
52  return res;
53 }
54 
55 class gtest : public ::testing::Test
56 {
57  test_id tid_;
58  controller& cont_;
59 
60 public:
61  gtest( controller& cont, test_id tid )
62  : tid_( tid )
63  , cont_( cont )
64  {
65  }
66 
67  void TestBody() final
68  {
69  cont_.start_test( tid_ );
70  while ( cont_.is_test_running() )
71  cont_.tick();
72  }
73 };
74 
75 inline void register_gtests( controller& cont )
76 {
77  std::string const suite_name = std::string{ cont.suite_name() };
78  for ( auto [tid, name] : cont.get_tests() ) {
79  std::string const sname{ name.begin(), name.end() };
80  test_id const test_id = tid;
81  ::testing::RegisterTest(
82  suite_name.c_str(),
83  sname.c_str(),
84  nullptr,
85  nullptr,
86  __FILE__,
87  __LINE__,
88  [&cont, test_id] {
89  return new gtest( cont, test_id );
90  } );
91  }
92 }
93 
94 } // namespace emlabcpp::testing
95 
96 #endif
MIT License.
Definition: base.h:37
uint16_t test_id
Definition: base.h:54
T res
Definition: algorithm.h:505