28 #include <QJsonDocument>
29 #include <QJsonObject>
51 QJsonObject
object = converter.
to_json(model);
67 QJsonObject
object = converter.
to_json(model);
71 EXPECT_THROW(converter.
from_json(
object, target1), std::runtime_error);
76 EXPECT_NO_THROW(converter.
from_json(
object, target2));
80 EXPECT_NO_THROW(converter.
from_json(
object, target3));
93 QJsonObject
object = converter.
to_json(model);
100 EXPECT_EQ(reco_item->parent(), target.
rootItem());
101 EXPECT_EQ(reco_item->modelType(), item->modelType());
116 parent->setData(QVariant::fromValue(42));
121 QJsonObject
object = converter.
to_json(model);
129 auto reco_child = reco_parent->
getItem(
"", 0);
132 EXPECT_EQ(reco_parent->model(), &target);
134 EXPECT_EQ(reco_parent->parent(), target.
rootItem());
135 EXPECT_EQ(reco_parent->displayName(),
137 EXPECT_EQ(reco_parent->childrenCount(), 1);
138 EXPECT_EQ(reco_parent->identifier(), parent->identifier());
139 EXPECT_EQ(reco_parent->itemTags()->defaultTag(),
"defaultTag");
140 EXPECT_EQ(reco_parent->data<
int>(), 42);
143 EXPECT_EQ(reco_child->model(), &target);
145 EXPECT_EQ(reco_child->parent(), reco_parent);
146 EXPECT_EQ(reco_child->displayName(),
"Property");
147 EXPECT_EQ(reco_child->childrenCount(), 0);
148 EXPECT_EQ(reco_child->identifier(), child->identifier());
149 EXPECT_EQ(reco_child->itemTags()->defaultTag(),
"");
157 auto pool1 = std::make_shared<ItemPool>();
162 QJsonObject json_source = converter.
to_json(source);
165 auto pool2 = std::make_shared<ItemPool>();
167 converter.
from_json(json_source, target);
172 auto id2 = reco_parent->identifier();
176 QJsonObject json_target = converter.
to_json(target);
182 EXPECT_EQ(pool1->item_for_key(id1), parent1);
183 EXPECT_EQ(pool2->item_for_key(id2), reco_parent);
198 parent->setData(QVariant::fromValue(42));
203 auto object = converter.
to_json(model);
212 converter.
from_json(document.object(), target);
216 auto reco_child = reco_parent->
getItem(
"", 0);
219 EXPECT_EQ(reco_parent->model(), &target);
221 EXPECT_EQ(reco_parent->parent(), target.
rootItem());
222 EXPECT_EQ(reco_parent->displayName(),
"SessionItem");
223 EXPECT_EQ(reco_parent->childrenCount(), 1);
224 EXPECT_EQ(reco_parent->identifier(), parent->identifier());
225 EXPECT_EQ(reco_parent->itemTags()->defaultTag(),
"defaultTag");
226 EXPECT_EQ(reco_parent->data<
int>(), 42);
229 EXPECT_EQ(reco_child->model(), &target);
231 EXPECT_EQ(reco_child->parent(), reco_parent);
232 EXPECT_EQ(reco_child->displayName(),
"Property");
233 EXPECT_EQ(reco_child->childrenCount(), 0);
234 EXPECT_EQ(reco_child->identifier(), child->identifier());
235 EXPECT_EQ(reco_child->itemTags()->defaultTag(),
"");
243 auto pool = std::make_shared<ItemPool>();
251 auto item_id = item->identifier();
253 QJsonObject
object = converter.
to_json(model);
258 EXPECT_EQ(pool->size(), 2);
259 EXPECT_FALSE(pool->item_for_key(root_id) == model.
rootItem());
260 EXPECT_TRUE(model.
rootItem() != root_item);
263 EXPECT_EQ(pool->item_for_key(item_id), new_item);
Convenience class which creates a directory on disk for test content.
Checks JsonModel class and its ability to convert SessionModel to json and back.
~JsonModelConverterTest()
Converter of SessionModel to/from json object with posibility to select one of convertion modes.
void from_json(const QJsonObject &json, SessionModel &model) const override
Reads json object and build the model.
QJsonObject to_json(const SessionModel &model) const override
Writes content of model into json.
Item to carry concrete editable entity (e.g.
PropertyItem * setDisplayName(const std::string &name) override
Sets display name (fluent interface).
The main object representing an editable/displayable/serializable entity.
std::string identifier() const
Returns unique identifier.
SessionItem * getItem(const std::string &tag, int row=0) const
Returns item at given row of given tag.
void registerTag(const TagInfo &tagInfo, bool set_as_default=false)
Registers tag to hold items under given name.
int childrenCount() const
Returns total number of children in all tags.
std::vector< SessionItem * > children() const
Returns vector of children formed from all chidlren from all tags.
virtual SessionItem * setDisplayName(const std::string &name)
Sets display name (fluent interface).
Main class to hold hierarchy of SessionItem objects.
SessionItem * rootItem() const
Returns root item of the model.
T * insertItem(SessionItem *parent=nullptr, const TagRow &tagrow={})
Inserts item into given parent under given tagrow.
static TagInfo universalTag(std::string name, std::vector< std::string > modelTypes={})
Constructs universal tag intended for unlimited amount of various items.
TEST_F(JsonModelConverterTest, emptyModel)
Creation of json object: empty model.
const model_type PropertyType
const model_type BaseType
materialitems.h Collection of materials to populate MaterialModel.
@ project
selective copying for saving/loading the project (tags and data created by item, updated from JSON)
std::string TestFileName(const std::string &test_sub_dir, const std::string &file_name)
Returns full path to the file in test directory.
void SaveJson(const QJsonObject &object, const std::string &fileName)
QJsonDocument LoadJson(const std::string &fileName)
QString JsonToString(const QJsonObject &object)