From ec0c0efec6d7eed868b424434748b97651e02bcc Mon Sep 17 00:00:00 2001 From: John Ericson Date: Sun, 29 Jan 2023 12:37:32 -0500 Subject: Allow unit test infra to be reused across libs' tests This allows using Arbitrary "instances" defined in libstore-tests in libexpr-tests, something we will leverage in a moment. --- .gitignore | 6 +- src/libexpr/tests/error_traces.cc | 2 +- src/libexpr/tests/json.cc | 2 +- src/libexpr/tests/libexpr.hh | 137 ++++++++++++++++++++++++++++++++++++ src/libexpr/tests/libexprtests.hh | 137 ------------------------------------ src/libexpr/tests/local.mk | 4 +- src/libexpr/tests/primops.cc | 2 +- src/libexpr/tests/trivial.cc | 2 +- src/libexpr/tests/value/context.cc | 2 +- src/libstore/tests/libstore.hh | 23 ++++++ src/libstore/tests/libstoretests.hh | 23 ------ src/libstore/tests/local.mk | 18 ++++- src/libstore/tests/path.cc | 8 +-- src/libstore/tests/path.hh | 15 ++++ src/libutil/tests/local.mk | 2 + 15 files changed, 206 insertions(+), 177 deletions(-) create mode 100644 src/libexpr/tests/libexpr.hh delete mode 100644 src/libexpr/tests/libexprtests.hh create mode 100644 src/libstore/tests/libstore.hh delete mode 100644 src/libstore/tests/libstoretests.hh create mode 100644 src/libstore/tests/path.hh diff --git a/.gitignore b/.gitignore index 8e0db013f..47815fde6 100644 --- a/.gitignore +++ b/.gitignore @@ -37,14 +37,14 @@ perl/Makefile.config /src/libexpr/parser-tab.hh /src/libexpr/parser-tab.output /src/libexpr/nix.tbl -/src/libexpr/tests/libexpr-tests +/src/libexpr/tests/libnixexpr-tests # /src/libstore/ *.gen.* -/src/libstore/tests/libstore-tests +/src/libstore/tests/libnixstore-tests # /src/libutil/ -/src/libutil/tests/libutil-tests +/src/libutil/tests/libnixutil-tests /src/nix/nix diff --git a/src/libexpr/tests/error_traces.cc b/src/libexpr/tests/error_traces.cc index 5e2213f69..24e95ac39 100644 --- a/src/libexpr/tests/error_traces.cc +++ b/src/libexpr/tests/error_traces.cc @@ -1,7 +1,7 @@ #include #include -#include "libexprtests.hh" +#include "tests/libexpr.hh" namespace nix { diff --git a/src/libexpr/tests/json.cc b/src/libexpr/tests/json.cc index f1ea1b197..411bc0ac3 100644 --- a/src/libexpr/tests/json.cc +++ b/src/libexpr/tests/json.cc @@ -1,4 +1,4 @@ -#include "libexprtests.hh" +#include "tests/libexpr.hh" #include "value-to-json.hh" namespace nix { diff --git a/src/libexpr/tests/libexpr.hh b/src/libexpr/tests/libexpr.hh new file mode 100644 index 000000000..8534d9567 --- /dev/null +++ b/src/libexpr/tests/libexpr.hh @@ -0,0 +1,137 @@ +#include +#include + +#include "value.hh" +#include "nixexpr.hh" +#include "eval.hh" +#include "eval-inline.hh" +#include "store-api.hh" + +#include "tests/libstore.hh" + +namespace nix { + class LibExprTest : public LibStoreTest { + public: + static void SetUpTestSuite() { + LibStoreTest::SetUpTestSuite(); + initGC(); + } + + protected: + LibExprTest() + : LibStoreTest() + , state({}, store) + { + } + Value eval(std::string input, bool forceValue = true) { + Value v; + Expr * e = state.parseExprFromString(input, ""); + assert(e); + state.eval(e, v); + if (forceValue) + state.forceValue(v, noPos); + return v; + } + + Symbol createSymbol(const char * value) { + return state.symbols.create(value); + } + + EvalState state; + }; + + MATCHER(IsListType, "") { + return arg != nList; + } + + MATCHER(IsList, "") { + return arg.type() == nList; + } + + MATCHER(IsString, "") { + return arg.type() == nString; + } + + MATCHER(IsNull, "") { + return arg.type() == nNull; + } + + MATCHER(IsThunk, "") { + return arg.type() == nThunk; + } + + MATCHER(IsAttrs, "") { + return arg.type() == nAttrs; + } + + MATCHER_P(IsStringEq, s, fmt("The string is equal to \"%1%\"", s)) { + if (arg.type() != nString) { + return false; + } + return std::string_view(arg.string.s) == s; + } + + MATCHER_P(IsIntEq, v, fmt("The string is equal to \"%1%\"", v)) { + if (arg.type() != nInt) { + return false; + } + return arg.integer == v; + } + + MATCHER_P(IsFloatEq, v, fmt("The float is equal to \"%1%\"", v)) { + if (arg.type() != nFloat) { + return false; + } + return arg.fpoint == v; + } + + MATCHER(IsTrue, "") { + if (arg.type() != nBool) { + return false; + } + return arg.boolean == true; + } + + MATCHER(IsFalse, "") { + if (arg.type() != nBool) { + return false; + } + return arg.boolean == false; + } + + MATCHER_P(IsPathEq, p, fmt("Is a path equal to \"%1%\"", p)) { + if (arg.type() != nPath) { + *result_listener << "Expected a path got " << arg.type(); + return false; + } else if (std::string_view(arg.string.s) != p) { + *result_listener << "Expected a path that equals \"" << p << "\" but got: " << arg.string.s; + return false; + } + return true; + } + + + MATCHER_P(IsListOfSize, n, fmt("Is a list of size [%1%]", n)) { + if (arg.type() != nList) { + *result_listener << "Expected list got " << arg.type(); + return false; + } else if (arg.listSize() != (size_t)n) { + *result_listener << "Expected as list of size " << n << " got " << arg.listSize(); + return false; + } + return true; + } + + MATCHER_P(IsAttrsOfSize, n, fmt("Is a set of size [%1%]", n)) { + if (arg.type() != nAttrs) { + *result_listener << "Expected set got " << arg.type(); + return false; + } else if (arg.attrs->size() != (size_t)n) { + *result_listener << "Expected a set with " << n << " attributes but got " << arg.attrs->size(); + return false; + } + return true; + } + + +} /* namespace nix */ diff --git a/src/libexpr/tests/libexprtests.hh b/src/libexpr/tests/libexprtests.hh deleted file mode 100644 index 03e468fbb..000000000 --- a/src/libexpr/tests/libexprtests.hh +++ /dev/null @@ -1,137 +0,0 @@ -#include -#include - -#include "value.hh" -#include "nixexpr.hh" -#include "eval.hh" -#include "eval-inline.hh" -#include "store-api.hh" - - -namespace nix { - class LibExprTest : public ::testing::Test { - public: - static void SetUpTestSuite() { - initLibStore(); - initGC(); - } - - protected: - LibExprTest() - : store(openStore("dummy://")) - , state({}, store) - { - } - Value eval(std::string input, bool forceValue = true) { - Value v; - Expr * e = state.parseExprFromString(input, ""); - assert(e); - state.eval(e, v); - if (forceValue) - state.forceValue(v, noPos); - return v; - } - - Symbol createSymbol(const char * value) { - return state.symbols.create(value); - } - - ref store; - EvalState state; - }; - - MATCHER(IsListType, "") { - return arg != nList; - } - - MATCHER(IsList, "") { - return arg.type() == nList; - } - - MATCHER(IsString, "") { - return arg.type() == nString; - } - - MATCHER(IsNull, "") { - return arg.type() == nNull; - } - - MATCHER(IsThunk, "") { - return arg.type() == nThunk; - } - - MATCHER(IsAttrs, "") { - return arg.type() == nAttrs; - } - - MATCHER_P(IsStringEq, s, fmt("The string is equal to \"%1%\"", s)) { - if (arg.type() != nString) { - return false; - } - return std::string_view(arg.string.s) == s; - } - - MATCHER_P(IsIntEq, v, fmt("The string is equal to \"%1%\"", v)) { - if (arg.type() != nInt) { - return false; - } - return arg.integer == v; - } - - MATCHER_P(IsFloatEq, v, fmt("The float is equal to \"%1%\"", v)) { - if (arg.type() != nFloat) { - return false; - } - return arg.fpoint == v; - } - - MATCHER(IsTrue, "") { - if (arg.type() != nBool) { - return false; - } - return arg.boolean == true; - } - - MATCHER(IsFalse, "") { - if (arg.type() != nBool) { - return false; - } - return arg.boolean == false; - } - - MATCHER_P(IsPathEq, p, fmt("Is a path equal to \"%1%\"", p)) { - if (arg.type() != nPath) { - *result_listener << "Expected a path got " << arg.type(); - return false; - } else if (std::string_view(arg.string.s) != p) { - *result_listener << "Expected a path that equals \"" << p << "\" but got: " << arg.string.s; - return false; - } - return true; - } - - - MATCHER_P(IsListOfSize, n, fmt("Is a list of size [%1%]", n)) { - if (arg.type() != nList) { - *result_listener << "Expected list got " << arg.type(); - return false; - } else if (arg.listSize() != (size_t)n) { - *result_listener << "Expected as list of size " << n << " got " << arg.listSize(); - return false; - } - return true; - } - - MATCHER_P(IsAttrsOfSize, n, fmt("Is a set of size [%1%]", n)) { - if (arg.type() != nAttrs) { - *result_listener << "Expected set got " << arg.type(); - return false; - } else if (arg.attrs->size() != (size_t)n) { - *result_listener << "Expected a set with " << n << " attributes but got " << arg.attrs->size(); - return false; - } - return true; - } - - -} /* namespace nix */ diff --git a/src/libexpr/tests/local.mk b/src/libexpr/tests/local.mk index e483575a4..6b766d82a 100644 --- a/src/libexpr/tests/local.mk +++ b/src/libexpr/tests/local.mk @@ -2,6 +2,8 @@ check: libexpr-tests_RUN programs += libexpr-tests +libexpr-tests_NAME := libnixexpr-tests + libexpr-tests_DIR := $(d) libexpr-tests_INSTALL_DIR := @@ -12,6 +14,6 @@ libexpr-tests_SOURCES := \ libexpr-tests_CXXFLAGS += -I src/libexpr -I src/libutil -I src/libstore -I src/libexpr/tests -libexpr-tests_LIBS = libexpr libutil libstore libfetchers +libexpr-tests_LIBS = libstore-tests libexpr libutil libstore libfetchers libexpr-tests_LDFLAGS := $(GTEST_LIBS) -lgmock diff --git a/src/libexpr/tests/primops.cc b/src/libexpr/tests/primops.cc index 9cdcf64a1..e1d3ac503 100644 --- a/src/libexpr/tests/primops.cc +++ b/src/libexpr/tests/primops.cc @@ -1,7 +1,7 @@ #include #include -#include "libexprtests.hh" +#include "tests/libexpr.hh" namespace nix { class CaptureLogger : public Logger diff --git a/src/libexpr/tests/trivial.cc b/src/libexpr/tests/trivial.cc index 8ce276e52..171727ac7 100644 --- a/src/libexpr/tests/trivial.cc +++ b/src/libexpr/tests/trivial.cc @@ -1,4 +1,4 @@ -#include "libexprtests.hh" +#include "tests/libexpr.hh" namespace nix { // Testing of trivial expressions diff --git a/src/libexpr/tests/value/context.cc b/src/libexpr/tests/value/context.cc index d5c9d3bce..0bcd79625 100644 --- a/src/libexpr/tests/value/context.cc +++ b/src/libexpr/tests/value/context.cc @@ -1,6 +1,6 @@ #include "value/context.hh" -#include "libexprtests.hh" +#include "tests/libexpr.hh" namespace nix { diff --git a/src/libstore/tests/libstore.hh b/src/libstore/tests/libstore.hh new file mode 100644 index 000000000..05397659b --- /dev/null +++ b/src/libstore/tests/libstore.hh @@ -0,0 +1,23 @@ +#include +#include + +#include "store-api.hh" + +namespace nix { + +class LibStoreTest : public ::testing::Test { + public: + static void SetUpTestSuite() { + initLibStore(); + } + + protected: + LibStoreTest() + : store(openStore("dummy://")) + { } + + ref store; +}; + + +} /* namespace nix */ diff --git a/src/libstore/tests/libstoretests.hh b/src/libstore/tests/libstoretests.hh deleted file mode 100644 index 05397659b..000000000 --- a/src/libstore/tests/libstoretests.hh +++ /dev/null @@ -1,23 +0,0 @@ -#include -#include - -#include "store-api.hh" - -namespace nix { - -class LibStoreTest : public ::testing::Test { - public: - static void SetUpTestSuite() { - initLibStore(); - } - - protected: - LibStoreTest() - : store(openStore("dummy://")) - { } - - ref store; -}; - - -} /* namespace nix */ diff --git a/src/libstore/tests/local.mk b/src/libstore/tests/local.mk index a2cf8a0cf..c24712ccf 100644 --- a/src/libstore/tests/local.mk +++ b/src/libstore/tests/local.mk @@ -1,6 +1,20 @@ -check: libstore-tests_RUN +check: libstore-tests-exe_RUN -programs += libstore-tests +programs += libstore-tests-exe + +libstore-tests-exe_NAME = libnixstore-tests + +libstore-tests-exe_DIR := $(d) + +libstore-tests-exe_INSTALL_DIR := + +libstore-tests-exe_LIBS = libstore-tests + +libstore-tests-exe_LDFLAGS := $(GTEST_LIBS) + +libraries += libstore-tests + +libstore-tests_NAME = libnixstore-tests libstore-tests_DIR := $(d) diff --git a/src/libstore/tests/path.cc b/src/libstore/tests/path.cc index 8ea252c92..2fc6d7e48 100644 --- a/src/libstore/tests/path.cc +++ b/src/libstore/tests/path.cc @@ -7,7 +7,8 @@ #include "path-regex.hh" #include "store-api.hh" -#include "libstoretests.hh" +#include "tests/libstore.hh" +#include "tests/path.hh" namespace nix { @@ -73,11 +74,6 @@ void showValue(const StorePath & p, std::ostream & os) { namespace rc { using namespace nix; -template<> -struct Arbitrary { - static Gen arbitrary(); -}; - Gen Arbitrary::arbitrary() { auto len = *gen::inRange(1, StorePath::MaxPathLen); diff --git a/src/libstore/tests/path.hh b/src/libstore/tests/path.hh new file mode 100644 index 000000000..0ac9bb9f2 --- /dev/null +++ b/src/libstore/tests/path.hh @@ -0,0 +1,15 @@ +#pragma once + +#include + +#include "path.hh" + +namespace rc { +using namespace nix; + +template<> +struct Arbitrary { + static Gen arbitrary(); +}; + +} diff --git a/src/libutil/tests/local.mk b/src/libutil/tests/local.mk index 815e18560..4f2a3f294 100644 --- a/src/libutil/tests/local.mk +++ b/src/libutil/tests/local.mk @@ -2,6 +2,8 @@ check: libutil-tests_RUN programs += libutil-tests +libutil-tests_NAME := libnixutil-tests + libutil-tests_DIR := $(d) libutil-tests_INSTALL_DIR := -- cgit v1.2.3