Skip to content

Commit

Permalink
Merge pull request #30 from stlab/sean-parent/macro-utilities
Browse files Browse the repository at this point in the history
Sean parent/macro utilities
  • Loading branch information
sean-parent authored Oct 22, 2024
2 parents e7a2be8 + de55402 commit dc64b3c
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 21 deletions.
1 change: 0 additions & 1 deletion test/custom_lightweight_configuration_tests.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#include "adobe/contract_checks.hpp"
#include "portable_death_tests.hpp"
#include <gtest/gtest.h>
#include <stdexcept>

Expand Down
15 changes: 5 additions & 10 deletions test/custom_verbose_configuration_tests.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#include "adobe/contract_checks.hpp"

#include "portable_death_tests.hpp"
#include "macro_utilities.hpp"

#include <gmock/gmock.h>
#include <gtest/gtest.h>
Expand Down Expand Up @@ -42,27 +42,22 @@ template<class F> void expect_throw(F fun, const char *match)

}// namespace

// LINE_STRING turns __LINE__ into a string literal.
#define STRINGIZE(x) STRINGIZE2(x)// NOLINT(cppcoreguidelines-macro-usage)
#define STRINGIZE2(x) #x// NOLINT(cppcoreguidelines-macro-usage)
#define LINE_STRING STRINGIZE(__LINE__)// NOLINT(cppcoreguidelines-macro-usage)

TEST(CustomVerboseConfiguration, OneArgumentFormsCallHandlerWithCorrectArguments)
{
// clang-format off
expect_throw([] { ADOBE_PRECONDITION(false); }, "custom_verbose_configuration_tests\\.cpp:" LINE_STRING
expect_throw([] { ADOBE_PRECONDITION(false); }, "custom_verbose_configuration_tests\\.cpp:" ADOBE_INTERNAL_LINE_STRING()
": Precondition violated \\(false\\)\\. \n");
expect_throw([] { ADOBE_INVARIANT(false); }, "custom_verbose_configuration_tests\\.cpp:" LINE_STRING
expect_throw([] { ADOBE_INVARIANT(false); }, "custom_verbose_configuration_tests\\.cpp:" ADOBE_INTERNAL_LINE_STRING()
": Invariant not upheld \\(false\\)\\. \n");
// clang-format on
}

TEST(CustomVerboseConfiguration, TwoArgumentFormsCallHandlerWithCorrectArguments)
{
// clang-format off
expect_throw([] { ADOBE_PRECONDITION(false, "% Message %"); }, "custom_verbose_configuration_tests\\.cpp:" LINE_STRING
expect_throw([] { ADOBE_PRECONDITION(false, "% Message %"); }, "custom_verbose_configuration_tests\\.cpp:" ADOBE_INTERNAL_LINE_STRING()
": Precondition violated \\(false\\)\\. % Message %\n");
expect_throw([] { ADOBE_INVARIANT(false, "% Message %"); }, "custom_verbose_configuration_tests\\.cpp:" LINE_STRING
expect_throw([] { ADOBE_INVARIANT(false, "% Message %"); }, "custom_verbose_configuration_tests\\.cpp:" ADOBE_INTERNAL_LINE_STRING()
": Invariant not upheld \\(false\\)\\. % Message %\n");
// clang-format on
}
Expand Down
13 changes: 13 additions & 0 deletions test/macro_utilities.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#ifndef ADOBE_INTERNAL_TEST_MACRO_UTILITIES_HPP

/// turns `x` into a string literal.
#define ADOBE_INTERNAL_STRINGIZE(x) \
ADOBE_INTERNAL_STRINGIZE2(x)// NOLINTNEXTLINE(cppcoreguidelines-macro-usage)
#define ADOBE_INTERNAL_STRINGIZE2(x) #x// NOLINT(cppcoreguidelines-macro-usage)


/// a string literal for __LINE__.
#define ADOBE_INTERNAL_LINE_STRING() \
ADOBE_INTERNAL_STRINGIZE(__LINE__)// NOLINT(cppcoreguidelines-macro-usage)

#endif
4 changes: 4 additions & 0 deletions test/portable_death_tests.hpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#ifndef ADOBE_INTERNAL_PORTABLE_DEATH_TESTS_HPP

#include <gtest/gtest.h>
#if !defined(GTEST_OS_WINDOWS) && !defined(__EMSCRIPTEN__)
#include <csignal>
Expand Down Expand Up @@ -45,3 +47,5 @@
#define EXPECT_PORTABLE_DEATH(code, expected_output_regex) EXPECT_DEATH(code, expected_output_regex)

#endif

#endif// ADOBE_INTERNAL_PORTABLE_DEATH_TESTS_HPP
1 change: 0 additions & 1 deletion test/unsafe_configuration_tests.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#include "adobe/contract_checks.hpp"
#include "portable_death_tests.hpp"
#include <gtest/gtest.h>

TEST(UnsafeConfiguration, AllChecksAreNoOps)
Expand Down
15 changes: 6 additions & 9 deletions test/verbose_configuration_tests.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#include "adobe/contract_checks.hpp"
#include "macro_utilities.hpp"
#include "portable_death_tests.hpp"

#include <gtest/gtest.h>

// ****** Death tests should precede non-death tests. *********
Expand All @@ -13,27 +15,22 @@ TEST(VerboseConfigurationDeathTest, ContractViolationsCauseAbort)
EXPECT_ABORT(ADOBE_INVARIANT(false), "");
}

// LINE_STRING turns __LINE__ into a string literal.
#define STRINGIZE(x) STRINGIZE2(x)// NOLINT(cppcoreguidelines-macro-usage)
#define STRINGIZE2(x) #x// NOLINT(cppcoreguidelines-macro-usage)
#define LINE_STRING STRINGIZE(__LINE__)// NOLINT(cppcoreguidelines-macro-usage)

TEST(VerboseConfigurationDeathTest, OneArgumentFormsAbortWithCorrectOutput)
{
// clang-format off
EXPECT_ABORT(ADOBE_PRECONDITION(false), "verbose_configuration_tests\\.cpp:" LINE_STRING
EXPECT_ABORT(ADOBE_PRECONDITION(false), "verbose_configuration_tests\\.cpp:" ADOBE_INTERNAL_LINE_STRING()
": Precondition violated \\(false\\)\\. \n");
EXPECT_ABORT(ADOBE_INVARIANT(false), "verbose_configuration_tests\\.cpp:" LINE_STRING
EXPECT_ABORT(ADOBE_INVARIANT(false), "verbose_configuration_tests\\.cpp:" ADOBE_INTERNAL_LINE_STRING()
": Invariant not upheld \\(false\\)\\. \n");
// clang-format on
}

TEST(VerboseConfigurationDeathTest, TwoArgumentFormsAbortWithCorrectOutput)
{
// clang-format off
EXPECT_ABORT(ADOBE_PRECONDITION(false, "% Message %"), "verbose_configuration_tests\\.cpp:" LINE_STRING
EXPECT_ABORT(ADOBE_PRECONDITION(false, "% Message %"), "verbose_configuration_tests\\.cpp:" ADOBE_INTERNAL_LINE_STRING()
": Precondition violated \\(false\\)\\. % Message %\n");
EXPECT_ABORT(ADOBE_INVARIANT(false, "% Message %"), "verbose_configuration_tests\\.cpp:" LINE_STRING
EXPECT_ABORT(ADOBE_INVARIANT(false, "% Message %"), "verbose_configuration_tests\\.cpp:" ADOBE_INTERNAL_LINE_STRING()
": Invariant not upheld \\(false\\)\\. % Message %\n");
// clang-format on
}
Expand Down

0 comments on commit dc64b3c

Please sign in to comment.