From 3a4228963f41be39a10d39aaeefd035656738d41 Mon Sep 17 00:00:00 2001 From: LinZhihao-723 Date: Thu, 8 Aug 2024 02:19:19 -0400 Subject: [PATCH] Refactor unit test. --- components/core/tests/test-ffi_Value.cpp | 201 +++++++++++++---------- 1 file changed, 112 insertions(+), 89 deletions(-) diff --git a/components/core/tests/test-ffi_Value.cpp b/components/core/tests/test-ffi_Value.cpp index 7f7a588ee..51caeb982 100644 --- a/components/core/tests/test-ffi_Value.cpp +++ b/components/core/tests/test-ffi_Value.cpp @@ -3,6 +3,7 @@ #include #include #include +#include #include #include @@ -36,6 +37,25 @@ requires(std::is_same_v [[nodiscard]] auto get_encoded_text_ast(std::string_view text ) -> clp::ir::EncodedTextAst; +/** + * Tests querying the underlying type of the given value against the given type. + * @tparam Type The type to query. + * @param value The value to test against. + */ +template +auto test_type_probing(Value const& value) -> void; + +/** + * Tests querying the typed value against the given type. + * @tparam Type The type to query. + * @param value The value to test against. + * @param typed_value The typed value to compare with. + */ +template +auto test_typed_value_probing(Value const& value, Type const& typed_value) -> void; + +// Implementation + template requires(std::is_same_v || std::is_same_v) @@ -55,118 +75,121 @@ auto get_encoded_text_ast(std::string_view text) -> clp::ir::EncodedTextAst{logtype, dict_vars, encoded_vars}; } + +template +// NOLINTNEXTLINE(readability-function-cognitive-complexity) +auto test_type_probing(Value const& value) -> void { + REQUIRE((std::is_same_v == value.is_null())); + REQUIRE((std::is_same_v == value.is())); + REQUIRE((std::is_same_v == value.is())); + REQUIRE((std::is_same_v == value.is())); + REQUIRE((std::is_same_v == value.is())); + REQUIRE((std::is_same_v == value.is())); + REQUIRE((std::is_same_v == value.is())); +} + +template +// NOLINTNEXTLINE(readability-function-cognitive-complexity) +auto test_typed_value_probing(Value const& value, Type const& typed_value) -> void { + if constexpr (std::is_same_v) { + REQUIRE((value.get_immutable_view() == typed_value)); + REQUIRE((std::is_same_v())>)); + } else { + REQUIRE_THROWS(value.get_immutable_view()); + } + + if constexpr (std::is_same_v) { + REQUIRE((value.get_immutable_view() == typed_value)); + REQUIRE((std::is_same_v())>)); + } else { + REQUIRE_THROWS(value.get_immutable_view()); + } + + if constexpr (std::is_same_v) { + REQUIRE((value.get_immutable_view() == typed_value)); + REQUIRE((std::is_same_v())>)); + } else { + REQUIRE_THROWS(value.get_immutable_view()); + } + + if constexpr (std::is_same_v) { + REQUIRE((value.get_immutable_view() == typed_value)); + REQUIRE((std::is_same_v())>)); + } else { + REQUIRE_THROWS(value.get_immutable_view()); + } + + if constexpr (std::is_same_v) { + REQUIRE((value.get_immutable_view().get_logtype() == typed_value.get_logtype())); + REQUIRE((value.get_immutable_view().get_dict_vars() == typed_value.get_dict_vars())); + REQUIRE( + (value.get_immutable_view().get_encoded_vars() + == typed_value.get_encoded_vars()) + ); + REQUIRE((std::is_same_v< + EightByteEncodedTextAst const&, + decltype(value.get_immutable_view())>)); + } else { + REQUIRE_THROWS(value.get_immutable_view()); + } + + if constexpr (std::is_same_v) { + REQUIRE((value.get_immutable_view().get_logtype() == typed_value.get_logtype())); + REQUIRE((value.get_immutable_view().get_dict_vars() == typed_value.get_dict_vars())); + REQUIRE( + (value.get_immutable_view().get_encoded_vars() + == typed_value.get_encoded_vars()) + ); + REQUIRE((std::is_same_v< + FourByteEncodedTextAst const&, + decltype(value.get_immutable_view())>)); + } else { + REQUIRE_THROWS(value.get_immutable_view()); + } +} } // namespace TEST_CASE("ffi_Value_basic", "[ffi][Value]") { Value const null_value; - REQUIRE(null_value.is_null()); - REQUIRE_FALSE(null_value.is()); - REQUIRE_FALSE(null_value.is()); - REQUIRE_FALSE(null_value.is()); - REQUIRE_FALSE(null_value.is()); - REQUIRE_FALSE(null_value.is()); - REQUIRE_FALSE(null_value.is()); - REQUIRE_THROWS(null_value.get_immutable_view()); - REQUIRE_THROWS(null_value.get_immutable_view()); - REQUIRE_THROWS(null_value.get_immutable_view()); - REQUIRE_THROWS(null_value.get_immutable_view()); - REQUIRE_THROWS(null_value.get_immutable_view()); - REQUIRE_THROWS(null_value.get_immutable_view()); + test_type_probing(null_value); + test_typed_value_probing(null_value, std::monostate{}); constexpr value_int_t cIntVal{1000}; Value const int_value{cIntVal}; - REQUIRE(int_value.is()); - REQUIRE((int_value.get_immutable_view() == cIntVal)); - REQUIRE_FALSE(int_value.is_null()); - REQUIRE_FALSE(int_value.is()); - REQUIRE_FALSE(int_value.is()); - REQUIRE_FALSE(int_value.is()); - REQUIRE_FALSE(int_value.is()); - REQUIRE_FALSE(int_value.is()); - REQUIRE_THROWS(int_value.get_immutable_view()); - REQUIRE_THROWS(int_value.get_immutable_view()); - REQUIRE_THROWS(int_value.get_immutable_view()); - REQUIRE_THROWS(int_value.get_immutable_view()); - REQUIRE_THROWS(int_value.get_immutable_view()); + test_type_probing(int_value); + test_typed_value_probing(int_value, cIntVal); constexpr value_float_t cFloatValue{1000.0001}; Value const float_value{cFloatValue}; - REQUIRE(float_value.is()); - REQUIRE((float_value.get_immutable_view() == cFloatValue)); - REQUIRE_FALSE(float_value.is_null()); - REQUIRE_FALSE(float_value.is()); - REQUIRE_FALSE(float_value.is()); - REQUIRE_FALSE(float_value.is()); - REQUIRE_FALSE(float_value.is()); - REQUIRE_FALSE(float_value.is()); - REQUIRE_THROWS(float_value.get_immutable_view()); - REQUIRE_THROWS(float_value.get_immutable_view()); - REQUIRE_THROWS(float_value.get_immutable_view()); - REQUIRE_THROWS(float_value.get_immutable_view()); - REQUIRE_THROWS(float_value.get_immutable_view()); + test_type_probing(float_value); + test_typed_value_probing(float_value, cFloatValue); constexpr value_bool_t cBoolVal{false}; Value const bool_value{cBoolVal}; - REQUIRE(bool_value.is()); - REQUIRE((bool_value.get_immutable_view() == cBoolVal)); - REQUIRE_FALSE(bool_value.is_null()); - REQUIRE_FALSE(bool_value.is()); - REQUIRE_FALSE(bool_value.is()); - REQUIRE_FALSE(bool_value.is()); - REQUIRE_FALSE(bool_value.is()); - REQUIRE_FALSE(bool_value.is()); - REQUIRE_THROWS(bool_value.get_immutable_view()); - REQUIRE_THROWS(bool_value.get_immutable_view()); - REQUIRE_THROWS(bool_value.get_immutable_view()); - REQUIRE_THROWS(bool_value.get_immutable_view()); - REQUIRE_THROWS(bool_value.get_immutable_view()); + test_type_probing(bool_value); + test_typed_value_probing(bool_value, cBoolVal); constexpr std::string_view cStringVal{"This is a test string message"}; Value const string_value{string{cStringVal}}; - REQUIRE(string_value.is()); - REQUIRE((string_value.get_immutable_view() == cStringVal)); - REQUIRE_FALSE(string_value.is_null()); - REQUIRE_FALSE(string_value.is()); - REQUIRE_FALSE(string_value.is()); - REQUIRE_FALSE(string_value.is()); - REQUIRE_FALSE(string_value.is()); - REQUIRE_FALSE(string_value.is()); - REQUIRE_THROWS(string_value.get_immutable_view()); - REQUIRE_THROWS(string_value.get_immutable_view()); - REQUIRE_THROWS(string_value.get_immutable_view()); - REQUIRE_THROWS(string_value.get_immutable_view()); - REQUIRE_THROWS(string_value.get_immutable_view()); + test_type_probing(string_value); + test_typed_value_probing(string_value, string{cStringVal}); constexpr std::string_view cStringToEncode{"uid=0, CPU usage: 99.99%, \"user_name\"=YScope"}; Value const eight_byte_encoded_text_ast_value{ get_encoded_text_ast(cStringToEncode) }; - REQUIRE(eight_byte_encoded_text_ast_value.is()); - REQUIRE_FALSE(eight_byte_encoded_text_ast_value.is_null()); - REQUIRE_FALSE(eight_byte_encoded_text_ast_value.is()); - REQUIRE_FALSE(eight_byte_encoded_text_ast_value.is()); - REQUIRE_FALSE(eight_byte_encoded_text_ast_value.is()); - REQUIRE_FALSE(eight_byte_encoded_text_ast_value.is()); - REQUIRE_FALSE(eight_byte_encoded_text_ast_value.is()); - REQUIRE_THROWS(eight_byte_encoded_text_ast_value.get_immutable_view()); - REQUIRE_THROWS(eight_byte_encoded_text_ast_value.get_immutable_view()); - REQUIRE_THROWS(eight_byte_encoded_text_ast_value.get_immutable_view()); - REQUIRE_THROWS(eight_byte_encoded_text_ast_value.get_immutable_view()); - REQUIRE_THROWS(eight_byte_encoded_text_ast_value.get_immutable_view()); + test_type_probing(eight_byte_encoded_text_ast_value); + test_typed_value_probing( + eight_byte_encoded_text_ast_value, + get_encoded_text_ast(cStringToEncode) + ); Value const four_byte_encoded_text_ast_value{ get_encoded_text_ast(cStringToEncode) }; - REQUIRE(four_byte_encoded_text_ast_value.is()); - REQUIRE_FALSE(four_byte_encoded_text_ast_value.is_null()); - REQUIRE_FALSE(four_byte_encoded_text_ast_value.is()); - REQUIRE_FALSE(four_byte_encoded_text_ast_value.is()); - REQUIRE_FALSE(four_byte_encoded_text_ast_value.is()); - REQUIRE_FALSE(four_byte_encoded_text_ast_value.is()); - REQUIRE_FALSE(four_byte_encoded_text_ast_value.is()); - REQUIRE_THROWS(four_byte_encoded_text_ast_value.get_immutable_view()); - REQUIRE_THROWS(four_byte_encoded_text_ast_value.get_immutable_view()); - REQUIRE_THROWS(four_byte_encoded_text_ast_value.get_immutable_view()); - REQUIRE_THROWS(four_byte_encoded_text_ast_value.get_immutable_view()); - REQUIRE_THROWS(four_byte_encoded_text_ast_value.get_immutable_view()); + test_type_probing(four_byte_encoded_text_ast_value); + test_typed_value_probing( + four_byte_encoded_text_ast_value, + get_encoded_text_ast(cStringToEncode) + ); }