nx_metadata_sdk 1.0 Metadata SDK

Classes | Macros | Typedefs | Functions
test.h File Reference
`#include <functional>`
`#include <iostream>`
`#include <sstream>`
`#include <stdint.h>`
`#include <string>`
`#include <nx/kit/utils.h>`

Go to the source code of this file.

## Classes
struct | nx::kit::test::detail::Test
struct | nx::kit::test::detail::UniversalString

## Macros
#define | NX_KIT_API
#define | TEST(TEST_CASE, TEST_NAME) ENABLED_TEST(TEST_CASE, TEST_NAME)
#define | ENABLED_TEST(TEST_CASE, TEST_NAME)
#define | DISABLED_TEST(TEST_CASE, TEST_NAME) static void disabled_test_##TEST_CASE##_##TEST_NAME() /* The function will be unused. */
#define | ASSERT_TRUE(CONDITION) ::nx::kit::test::detail::assertBool(true, !!(CONDITION), #CONDITION, __FILE__, __LINE__)
#define | ASSERT_TRUE_AT_LINE(LINE, CONDITION) ::nx::kit::test::detail::assertBool(true, !!(CONDITION), #CONDITION, __FILE__, LINE, __LINE__)
#define | ASSERT_FALSE(CONDITION) ::nx::kit::test::detail::assertBool(false, !!(CONDITION), #CONDITION, __FILE__, __LINE__)
#define | ASSERT_FALSE_AT_LINE(LINE, CONDITION) ::nx::kit::test::detail::assertBool(false, !!(CONDITION), #CONDITION, __FILE__, LINE, __LINE__)
#define | ASSERT_EQ(EXPECTED, ACTUAL)
#define | ASSERT_EQ_AT_LINE(LINE, EXPECTED, ACTUAL)
#define | ASSERT_STREQ(EXPECTED, ACTUAL)
#define | ASSERT_STREQ_AT_LINE(LINE, EXPECTED, ACTUAL)

## Typedefs
typedef std::function< void()> | nx::kit::test::detail::TestFunc

## Functions
void | nx::kit::test::assertMultilineTextEquals (const char *file, int line, const std::string &testCaseTag, const std::string &expected, const std::string &actual, const std::string actualSubstrToReplace, const std::string &actualSubstrReplacement)
const char * | nx::kit::test::tempDir ()
const char * | nx::kit::test::staticTempDir ()
int | nx::kit::test::runAllTests (const char *testSuiteName, const char *specificArgsHelp)
void | nx::kit::test::createFile (const std::string &filename, const std::string &content)
int | nx::kit::test::detail::regTest (const Test &test)
void | nx::kit::test::detail::failEq (const std::string &expectedValue, const char *const expectedExpr, const std::string &actualValue, const char *const actualExpr, const char *const file, int line, int actualLine)
void | nx::kit::test::detail::assertBool (bool expected, bool condition, const char *conditionStr, const char *file, int line, int actualLine)
template<typename Expected , typename Actual >
void | nx::kit::test::detail::assertEq (const Expected &expected, const char *expectedExpr, const Actual &actual, const char *actualExpr, const char *file, int line, int actualLine=-1)
std::ostream & | nx::kit::test::detail::operator<< (std::ostream &stream, const UniversalString &str)
void | nx::kit::test::detail::assertStrEq (const UniversalString &expectedValue, const char *expectedExpr, const UniversalString &actualValue, const char *actualExpr, const char *file, int line, int actualLine)

## Detailed Description

Rudimentary standalone unit testing framework designed to mimic Google Test to a certain degree.

## Macro Definition Documentation

## ◆ ASSERT_EQ

#define ASSERT_EQ | ( |  | EXPECTED,
|  |  | ACTUAL
| ) |  |

Value:
::nx::kit::test::detail::assertEq( \
(EXPECTED), #EXPECTED, (ACTUAL), #ACTUAL, __FILE__, __LINE__)

## ◆ ASSERT_EQ_AT_LINE

#define ASSERT_EQ_AT_LINE | ( |  | LINE,
|  |  | EXPECTED,
|  |  | ACTUAL
| ) |  |

Value:
::nx::kit::test::detail::assertEq( \
(EXPECTED), #EXPECTED, (ACTUAL), #ACTUAL, __FILE__, LINE, __LINE__)

## ◆ ASSERT_STREQ

#define ASSERT_STREQ | ( |  | EXPECTED,
|  |  | ACTUAL
| ) |  |

Value:
::nx::kit::test::detail::assertStrEq( \
EXPECTED, #EXPECTED, ACTUAL, #ACTUAL, __FILE__, __LINE__)

## ◆ ASSERT_STREQ_AT_LINE

#define ASSERT_STREQ_AT_LINE | ( |  | LINE,
|  |  | EXPECTED,
|  |  | ACTUAL
| ) |  |

Value:
::nx::kit::test::detail::assertStrEq( \
EXPECTED, #EXPECTED, ACTUAL, #ACTUAL, __FILE__, LINE, __LINE__)

## ◆ ENABLED_TEST

#define ENABLED_TEST | ( |  | TEST_CASE,
|  |  | TEST_NAME
| ) |  |

Value:
static void test_##TEST_CASE##_##TEST_NAME(); \
int unused_##TEST_CASE##_##TEST_NAME /* Not `static const` to suppress "unused" warning. */ = \
::nx::kit::test::detail::regTest( \
{#TEST_CASE, #TEST_NAME, #TEST_CASE "." #TEST_NAME, test_##TEST_CASE##_##TEST_NAME, \
/*tempDir*/ ""}); \
static void test_##TEST_CASE##_##TEST_NAME()

## ◆ TEST

#define TEST | ( |  | TEST_CASE,
|  |  | TEST_NAME
| ) |  | ENABLED_TEST(TEST_CASE, TEST_NAME)

Main macro for defining a test function. Calls a helper macro - such trick allows to comment out all tests except one by redefining TEST to DISABLED_TEST, and changing that one test to use ENABLED_TEST instead of TEST.

Usage:

TEST(MySuite, myTest)
{
int var = 42;
ASSERT_EQ(42, var);
}

## Function Documentation

## ◆ assertMultilineTextEquals()

NX_KIT_API void nx::kit::test::assertMultilineTextEquals | ( | const char * | file,
|  | int | line,
|  | const std::string & | testCaseTag,
|  | const std::string & | expected,
|  | const std::string & | actual,
|  | const std::string | actualSubstrToReplace = `""`,
|  | const std::string & | actualSubstrReplacement = `""`
| ) |  |

Tests that the given string has the expected value, having replaced all occurrences of the given substring with the given replacement (placeholder) - useful when the string being tested contains some substring (e.g. a file name) known only at runtime. Zero bytes inside the strings are supported.

If the strings are not equal, they are treated as multiline via '
' - line-by-line exact comparison is performed, and the different lines are printed. Also for convenience the actual string is printed as a multiline text with line numbers, non-printable chars as '?', and trailing spaces as '#'.

Parameters

  actualSubstrToReplace | Must be empty if the replacement is not needed.
  actualSubstrReplacement | Must be empty if the replacement is not needed.

## ◆ createFile()

NX_KIT_API void nx::kit::test::createFile | ( | const std::string & | filename,
|  | const std::string & | content
| ) |  |

Allows zero bytes in the content.

## ◆ runAllTests()

NX_KIT_API int nx::kit::test::runAllTests | ( | const char * | testSuiteName,
|  | const char * | specificArgsHelp = `nullptr`
| ) |  |

Usage: call from main():

        int main()
        {
            return nx::kit::test::runAllTests("myTests");
        }

Returns
  Number of failed tests.

## ◆ staticTempDir()

NX_KIT_API const char * nx::kit::test::staticTempDir | ( |  | ) |

Should be called for tests that require temp dir outside the TEST() body, e.g. from a static initialization code.

Returns
  Path to the directory to create temp files in, including the trailing path separator: "base-temp-dir/static/", where "base-temp-dir" is the same as for tempDir(). The directory is created (if already exists - a fatal error is produced).

## ◆ tempDir()

NX_KIT_API const char * nx::kit::test::tempDir | ( |  | ) |

Should be called for regular tests, from the TEST() body.

Returns
  Path to the directory to create temp files in, including the trailing path separator: "base-temp-dir/case.test/", where "base-temp-dir" can be assigned with "--tmp" command line option and by default is "system-temp-dir/nx_kit_test_#", where # is a random number. The directory is created (if already exists - a fatal error is produced).