Skip to content
Snippets Groups Projects
Verified Commit f33ec19d authored by Timm Fitschen's avatar Timm Fitschen
Browse files

TST: add more tests for ToString

parent 090e7e3c
No related branches found
No related tags found
1 merge request!25F cpp to string
Pipeline #13928 passed
Pipeline: caosdb-cppinttest

#13930

    ......@@ -173,18 +173,18 @@ public:
    inline auto ToString() const noexcept -> const std::string override {
    if (this->size() == 0) {
    return "[]";
    return "[]\n";
    }
    std::string result("[");
    for (int i = 0; i < this->size(); i++) {
    std::string result("[\n");
    for (int i = 0; i < this->size();) {
    CAOSDB_DEBUG_MESSAGE_STRING(this->wrapped->at(i), next);
    result += next;
    if (i < this->size() - 1) {
    result += ",";
    if (++i < this->size()) {
    result.replace(result.size() - 1, 1, ",\n");
    }
    }
    return result.append(std::string("]"));
    return result.append(std::string("]\n"));
    }
    protected:
    ......@@ -302,7 +302,6 @@ auto RepeatedPtrFieldWrapper<T, P>::end() const -> const RepeatedPtrFieldWrapper
    */
    class Message : public ScalarProtoMessageWrapper<ProtoMessage> {
    public:
    // inline Message() {};
    /**
    * Get the code of this message.
    *
    ......@@ -469,11 +468,14 @@ public:
    }
    explicit inline Property(ProtoProperty *other)
    : ScalarProtoMessageWrapper<ProtoProperty>(other), value(this->wrapped->mutable_value()),
    data_type(this->wrapped->mutable_data_type()){};
    : ScalarProtoMessageWrapper<ProtoProperty>(other),
    value(this->wrapped->has_value() ? this->wrapped->mutable_value()
    : static_cast<ProtoValue *>(nullptr)),
    data_type(this->wrapped->has_data_type() ? this->wrapped->mutable_data_type()
    : static_cast<ProtoDataType *>(nullptr)){};
    inline Property()
    : ScalarProtoMessageWrapper<ProtoProperty>(), value(this->wrapped->mutable_value()),
    data_type(this->wrapped->mutable_data_type()){};
    : ScalarProtoMessageWrapper<ProtoProperty>(), value(static_cast<ProtoValue *>(nullptr)),
    data_type(static_cast<ProtoDataType *>(nullptr)){};
    /**
    * Return the id of this property
    ......
    ......@@ -23,7 +23,10 @@
    #define CAOSDB_PROTOBUF_HELPER_H
    #include <google/protobuf/arena.h> // for Arena
    #include <google/protobuf/generated_message_util.h> // for Arena
    // IWYU pragma: no_include "google/protobuf/extension_set.h"
    // IWYU pragma: no_include "google/protobuf/generated_message_util.h"
    //#include <google/protobuf/extension_set.h> // IWYU pragma: keep
    //#include <google/protobuf/generated_message_util.h> // IWYU pragma: keep
    #include <google/protobuf/util/json_util.h> // for JsonOptions, MessageToJs...
    #include <string> // for string
    ......
    ......@@ -570,4 +570,77 @@ TEST(test_entity, test_entity_to_string) {
    EXPECT_EQ(entity.ToString(), "{}\n");
    }
    TEST(test_entity, test_properties_to_string) {
    Entity entity;
    EXPECT_EQ(entity.GetProperties().ToString(), "[]\n");
    Property property;
    property.SetName("Prop1");
    entity.AppendProperty(property);
    EXPECT_EQ(entity.GetProperties().ToString(), "[\n{\n \"name\": \"Prop1\"\n}\n]\n");
    Property property2;
    property2.SetName("Prop2");
    entity.AppendProperty(property2);
    EXPECT_EQ(entity.GetProperties().ToString(),
    "[\n{\n \"name\": \"Prop1\"\n},\n{\n \"name\": \"Prop2\"\n}\n]\n");
    }
    TEST(test_entity, test_property_to_string) {
    Property property;
    EXPECT_EQ(property.ToString(), "{}\n");
    property.SetDataType(AtomicDataType::DOUBLE);
    EXPECT_EQ(property.ToString(),
    "{\n \"dataType\": {\n \"atomicDataType\": \"ATOMIC_DATA_TYPE_DOUBLE\"\n }\n}\n");
    }
    TEST(test_entity, test_parents_to_string) {
    Parent parent;
    parent.SetName("the name");
    Entity entity;
    entity.AppendParent(parent);
    EXPECT_EQ(entity.GetParents().ToString(), "[\n{\n \"name\": \"the name\"\n}\n]\n");
    }
    TEST(test_entity, test_parent_to_string) {
    Parent parent;
    EXPECT_EQ(parent.ToString(), "{}\n");
    parent.SetName("the name");
    EXPECT_EQ(parent.ToString(), "{\n \"name\": \"the name\"\n}\n");
    }
    TEST(test_entity, test_messages_to_string) {
    IdResponse idResponse;
    idResponse.set_id("entity_id");
    auto *error = idResponse.add_errors();
    error->set_code(MessageCode::ENTITY_DOES_NOT_EXIST);
    error->set_description("error_desc");
    Entity entity(&idResponse);
    // Messages are not printed, currently.
    EXPECT_EQ(entity.ToString(), "{\n \"id\": \"entity_id\",\n \"version\": {}\n}\n");
    EXPECT_EQ(entity.GetErrors().ToString(),
    "[\n{\n \"code\": 2,\n \"description\": \"error_desc\"\n}\n]\n");
    }
    TEST(test_entity, test_message_to_string) {
    IdResponse idResponse;
    idResponse.set_id("entity_id");
    auto *error = idResponse.add_errors();
    error->set_code(MessageCode::ENTITY_DOES_NOT_EXIST);
    error->set_description("error_desc");
    Entity entity(&idResponse);
    // Messages are not printed, currently.
    EXPECT_EQ(entity.ToString(), "{\n \"id\": \"entity_id\",\n \"version\": {}\n}\n");
    EXPECT_EQ(entity.GetErrors().at(0).ToString(),
    "{\n \"code\": 2,\n \"description\": \"error_desc\"\n}\n");
    }
    } // namespace caosdb::entity
    ......@@ -26,6 +26,7 @@
    #include "boost/json/value.hpp" // for value
    #include "caosdb/data_type.h" // for atomicdatatype_names
    #include "caosdb/entity.h" // for importance_names, role...
    #include "caosdb/status_code.h" // for get_status_description
    #include "caosdb/utility.h" // for base64_encode, load_js...
    #include "caosdb_test_utility.h" // for TEST_DATA_DIR
    #include <gtest/gtest-message.h> // for Message
    ......@@ -78,4 +79,10 @@ TEST(test_utility, enum_names) {
    std::out_of_range, "Could not find enum value for string 'Invalid name'.");
    }
    TEST(test_utility, test_status_code_description) {
    EXPECT_EQ(caosdb::get_status_description(12412323), "MISSING DESCRIPTION");
    EXPECT_EQ(caosdb::get_status_description(static_cast<int>(StatusCode::UNKNOWN)),
    "Unknown error. This is typically a bug (server or client).");
    }
    } // namespace caosdb::utility
    0% Loading or .
    You are about to add 0 people to the discussion. Proceed with caution.
    Please register or to comment