Newer
Older
Import / applications / HighwayDash / ports / Framework / UnitTest.h
//  BlockyFroggy
//  Created by John Ryland on 17/3/19.
//  Copyright © 2019 John Ryland. All rights reserved.
#pragma once

#ifndef UnitTest_h
#define UnitTest_h


#include "Factory.h"


#define ENABLE_UNIT_TESTS   1


//! Unit testing framework
namespace UnitTest
{
  typedef void(*UnitTestFunc)();
  using UnitTestFactoryItem = GenericFactoryItem<UnitTestFunc>;
  void UnitTestAssert(bool a_expression, const char* a_expressionStr, const char* a_file, int a_line);
} // namespace UnitTest


#if ENABLE_UNIT_TESTS
// Note: This macro doesn't evaluate the expression wrapped with a try/catch block. On failure will exit with -1.
#  define CHECK(expression)              UnitTestAssert(expression, #expression, __FILE__, __LINE__)
#  define DECLARE_UNIT_TEST(testName)    DECLARE_UNIT_TEST_HELPER(NAME_AND_LINE(testName, __LINE__))
#else
#  define CHECK(expression)              (void)(expression)
#  define DECLARE_UNIT_TEST(testName)    static void unitTestFunction_##testName()
#endif // ENABLE_UNIT_TESTS


#define NAME_AND_LINE(x, y)            NAME_AND_LINE_INTERNAL(x, y)
#define NAME_AND_LINE_INTERNAL(x, y)   x##y

#define DECLARE_UNIT_TEST_HELPER(testName) \
  DECLARE_UNIT_TEST_HELPER_INTERNAL(testName)
#define DECLARE_UNIT_TEST_HELPER_INTERNAL(testName) \
  namespace UnitTest \
  { \
    void unitTestFunction_##testName(); \
    UnitTestFactoryItem g_register_##testName(#testName, &unitTestFunction_##testName); \
  } \
  void UnitTest::unitTestFunction_##testName()


#endif /* UnitTest_h */