Zrythm v2.0.0-DEV
a highly automated and intuitive digital audio workstation
Loading...
Searching...
No Matches
uuid_identifiable_object_test.h
1// SPDX-FileCopyrightText: © 2025 Alexandros Theodotou <alex@zrythm.org>
2// SPDX-License-Identifier: LicenseRef-ZrythmLicense
3
4#pragma once
5
6#include "utils/uuid_identifiable_object.h"
7
8#include <gtest/gtest.h>
9
10using namespace zrythm::utils;
11
12using namespace zrythm;
13
14class BaseTestObject : public utils::UuidIdentifiableObject<BaseTestObject>
15{
16public:
17 BaseTestObject () = default;
18 explicit BaseTestObject (Uuid id)
19 : UuidIdentifiableObject<BaseTestObject> (id)
20 {
21 }
22 ~BaseTestObject () override = default;
23 BaseTestObject (const BaseTestObject &) = default;
24 BaseTestObject &operator= (const BaseTestObject &) = default;
25 BaseTestObject (BaseTestObject &&) = default;
26 BaseTestObject &operator= (BaseTestObject &&) = default;
27
28 friend void to_json (nlohmann::json &json_value, const BaseTestObject &obj)
29 {
30 to_json (json_value, static_cast<const UuidIdentifiableObject &> (obj));
31 }
32 friend void from_json (const nlohmann::json &json_value, BaseTestObject &obj)
33 {
34 from_json (json_value, static_cast<UuidIdentifiableObject &> (obj));
35 }
36
37 BOOST_DESCRIBE_CLASS (BaseTestObject, (UuidIdentifiableObject), (), (), ())
38};
39
40static_assert (UuidIdentifiable<BaseTestObject>);
41static_assert (!UuidIdentifiableQObject<BaseTestObject>);
42
43DEFINE_UUID_HASH_SPECIALIZATION (BaseTestObject::Uuid);
44
45using TestUuid = BaseTestObject::Uuid;
46
47class DerivedTestObject : public QObject, public BaseTestObject
48{
49 Q_OBJECT
50public:
51 // Used to test non-default constructable objects that require a dependency
52 // during deserialization
53 explicit DerivedTestObject (int some_dependency) { };
54 explicit DerivedTestObject (TestUuid id, std::string name)
55 : BaseTestObject (id), name_ (std::move (name))
56 {
57 }
58
59 [[nodiscard]] std::string name () const { return name_; }
60
61 friend void init_from (
62 DerivedTestObject &obj,
63 const DerivedTestObject &other,
64 ObjectCloneType clone_type)
65
66 {
67 obj.name_ = other.name_;
68 }
69
70 NLOHMANN_DEFINE_DERIVED_TYPE_INTRUSIVE (DerivedTestObject, BaseTestObject, name_)
71
72private:
73 std::string name_;
74
75 BOOST_DESCRIBE_CLASS (DerivedTestObject, (BaseTestObject), (), (), (name_))
76};
77
78class TestObjectBuilder
79{
80public:
81 TestObjectBuilder () = default;
82 TestObjectBuilder &with_int_dependency (int some_dependency)
83 {
84 some_dependency_ = some_dependency;
85 return *this;
86 }
87
88 template <typename ObjectT> auto build () const
89 {
90 return std::make_unique<ObjectT> (some_dependency_);
91 }
92
93private:
94 int some_dependency_{};
95};
97
98// FIXME!!!
99// static_assert (UuidIdentifiableQObject<DerivedTestObject>);
100
101class UuidIdentifiableObjectRegistryTest : public ::testing::Test
102{
103public:
104 using TestVariant = std::variant<DerivedTestObject *>;
106
107protected:
108 void SetUp () override
109 {
110 obj1_ = new DerivedTestObject (TestUuid{ QUuid::createUuid () }, "Object1");
111 obj2_ = new DerivedTestObject (TestUuid{ QUuid::createUuid () }, "Object2");
112 obj3_ = new DerivedTestObject (TestUuid{ QUuid::createUuid () }, "Object3");
113
114 registry_.register_object (obj1_);
115 registry_.register_object (obj2_);
116 registry_.register_object (obj3_);
117 }
118
119 TestRegistry registry_;
120 DerivedTestObject * obj1_{};
121 DerivedTestObject * obj2_{};
122 DerivedTestObject * obj3_{};
123};
124
125// ============================================================================
126// Test types for destruction order testing
127// ============================================================================
128
135class ContainerTestObject : public QObject, public BaseTestObject
136{
137 Q_OBJECT
138public:
139 // The registry type that will contain this object
140 using ContainerRegistry = utils::
141 OwningObjectRegistry<std::variant<ContainerTestObject *>, BaseTestObject>;
143
144 explicit ContainerTestObject (int /* unused dependency */) { }
145 explicit ContainerTestObject (TestUuid id, std::string name)
146 : BaseTestObject (id), name_ (std::move (name))
147 {
148 }
149
150 void set_contained_ref (UuidRef ref) { contained_ref_ = std::move (ref); }
151
152 [[nodiscard]] std::string name () const { return name_; }
153
154 friend void init_from (
155 ContainerTestObject &obj,
156 const ContainerTestObject &other,
157 ObjectCloneType clone_type)
158 {
159 obj.name_ = other.name_;
160 }
161
162 NLOHMANN_DEFINE_DERIVED_TYPE_INTRUSIVE (
163 ContainerTestObject,
164 BaseTestObject,
165 name_)
166
167private:
168 std::string name_;
169 std::optional<UuidRef> contained_ref_;
170
171 BOOST_DESCRIBE_CLASS (ContainerTestObject, (BaseTestObject), (), (), (name_))
172};
173
174// Builder for ContainerTestObject deserialization
176{
177public:
178 template <typename ObjectT> auto build () const
179 {
180 return std::make_unique<ObjectT> (42);
181 }
182};
184
185inline void
186from_json (const nlohmann::json &j, ContainerTestObject::ContainerRegistry &obj)
187{
188 auto builder = ContainerTestObjectBuilder{};
189 from_json_with_builder (j, obj, builder);
190}
A registry that owns and manages objects identified by a UUID.
Base class for objects that need to be uniquely identified by UUID.
A reference-counted RAII wrapper for a UUID in a registry.
Concept that checks if a type is a builder for objects.
Definition traits.h:241
String utilities.
Definition algorithms.h:12