From ab4d1efe0b7d3529b49ce8c012f068a4d435ef37 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matija=20Pintari=C4=87?= <99442742+mpintaric55334@users.noreply.github.com> Date: Tue, 25 Jul 2023 13:30:02 +0200 Subject: [PATCH] Overload << operator for mgp::Value::Type (#1080) --- include/mgp.hpp | 37 +++++++++++++++++++++++++++++++++++++ tests/unit/cpp_api.cpp | 27 +++++++++++++++++++++++++++ 2 files changed, 64 insertions(+) diff --git a/include/mgp.hpp b/include/mgp.hpp index 51494e198..8ea94cc82 100644 --- a/include/mgp.hpp +++ b/include/mgp.hpp @@ -2536,6 +2536,7 @@ inline void Path::Expand(const Relationship &relationship) { mgp::path_expand(pt inline bool Path::operator==(const Path &other) const { return util::PathsEqual(ptr_, other.ptr_); } inline bool Path::operator!=(const Path &other) const { return !(*this == other); } + /* #endregion */ /* #region Temporal types (Date, LocalTime, LocalDateTime, Duration) */ @@ -3185,6 +3186,42 @@ inline bool Value::IsDuration() const { return mgp::value_is_duration(ptr_); } inline bool Value::operator==(const Value &other) const { return util::ValuesEqual(ptr_, other.ptr_); } inline bool Value::operator!=(const Value &other) const { return !(*this == other); } + +inline std::ostream &operator<<(std::ostream &os, const mgp::Type &type) { + switch (type) { + case mgp::Type::Null: + return os << "null"; + case mgp::Type::Bool: + return os << "bool"; + case mgp::Type::Int: + return os << "int"; + case mgp::Type::Double: + return os << "double"; + case mgp::Type::String: + return os << "string"; + case mgp::Type::List: + return os << "list"; + case mgp::Type::Map: + return os << "map"; + case mgp::Type::Node: + return os << "vertex"; + case mgp::Type::Relationship: + return os << "edge"; + case mgp::Type::Path: + return os << "path"; + case mgp::Type::Date: + return os << "date"; + case mgp::Type::LocalTime: + return os << "local_time"; + case mgp::Type::LocalDateTime: + return os << "local_date_time"; + case mgp::Type::Duration: + return os << "duration"; + default: + throw ValueException("Unknown type"); + } +} + /* #endregion */ /* #region Record */ diff --git a/tests/unit/cpp_api.cpp b/tests/unit/cpp_api.cpp index 95c96426a..7741b2dfd 100644 --- a/tests/unit/cpp_api.cpp +++ b/tests/unit/cpp_api.cpp @@ -10,6 +10,7 @@ // licenses/APL.txt. #include +#include #include #include @@ -471,3 +472,29 @@ TYPED_TEST(CppApiTestFixture, TestNodeProperties) { ASSERT_EQ(node_1.Properties()["b"].ValueString(), "b"); ASSERT_EQ(node_1.GetProperty("b").ValueString(), "b"); } + +TYPED_TEST(CppApiTestFixture, TestTypeOperatorStream) { + std::string string1 = "string"; + int64_t int1 = 4; + mgp::List list = mgp::List(); + + mgp::Value string_value = mgp::Value(string1); + mgp::Value int_value = mgp::Value(int1); + mgp::Value list_value = mgp::Value(list); + + std::ostringstream oss_str; + oss_str << string_value.Type(); + std::string str_test = oss_str.str(); + + std::ostringstream oss_int; + oss_int << int_value.Type(); + std::string int_test = oss_int.str(); + + std::ostringstream oss_list; + oss_list << list_value.Type(); + std::string list_test = oss_list.str(); + + ASSERT_EQ(str_test, "string"); + ASSERT_EQ(int_test, "int"); + ASSERT_EQ(list_test, "list"); +}