Skip to content
Snippets Groups Projects
Commit 093c6b16 authored by Matthias Puchner's avatar Matthias Puchner
Browse files

adapt unit tests

parent 829132a6
No related branches found
No related tags found
1 merge request!570remove SessionModel/Item from SampleModel and all related items
#include "GUI/Model/Group/PropertyItem.h" #include "GUI/Model/Group/PropertyItem.h"
#include "GUI/Model/Instrument/BackgroundItems.h"
#include "GUI/Model/Instrument/InstrumentItems.h"
#include "GUI/Model/Instrument/InstrumentModel.h"
#include "GUI/Model/Job/JobModel.h"
#include "GUI/Model/Sample/FormFactorItems.h" #include "GUI/Model/Sample/FormFactorItems.h"
#include "GUI/Model/Sample/LayerItem.h" #include "GUI/Model/Sample/LayerItem.h"
#include "GUI/Model/Sample/MultiLayerItem.h" #include "GUI/Model/Sample/MultiLayerItem.h"
#include "GUI/Model/Sample/ParticleItem.h" #include "GUI/Model/Sample/ParticleItem.h"
#include "GUI/Model/Sample/ParticleLayoutItem.h"
#include "GUI/Model/Sample/SampleModel.h"
#include "GUI/Model/Session/Serializer.h"
#include "GUI/Model/Session/SessionModel.h" #include "GUI/Model/Session/SessionModel.h"
#include "Tests/GTestWrapper/google_test.h" #include "Tests/GTestWrapper/google_test.h"
#include <QXmlStreamReader> #include <QXmlStreamReader>
...@@ -25,6 +32,26 @@ void itemFromXML(QString buffer, SessionItem* item) ...@@ -25,6 +32,26 @@ void itemFromXML(QString buffer, SessionItem* item)
GUI::Session::XML::readItems(&reader, item); GUI::Session::XML::readItems(&reader, item);
} }
template <typename ItemType> QByteArray serialize(ItemType& t)
{
QByteArray a;
QXmlStreamWriter w(&a);
w.writeStartElement("start");
Serializer s(&w);
t.serialize(s);
w.writeEndElement();
return a;
}
template <typename ItemType> void deserialize(ItemType& t, const QByteArray& a)
{
QXmlStreamReader r(a);
Serializer sr(&r);
r.readNextStartElement();
ASSERT_EQ(r.name().toString(), "start");
t.serialize(sr);
}
} // namespace } // namespace
class TestSessionXML : public ::testing::Test { class TestSessionXML : public ::testing::Test {
...@@ -56,52 +83,37 @@ TEST_F(TestSessionXML, sessionItem) ...@@ -56,52 +83,37 @@ TEST_F(TestSessionXML, sessionItem)
//! Testing to/from xml: FullSphereItem //! Testing to/from xml: FullSphereItem
TEST_F(TestSessionXML, FullSphereItem) TEST_F(TestSessionXML, InstrumentItem)
{ {
// source model, to xml // source model, to xml
SessionModel source("TestModel"); InstrumentModel source;
auto* sphere = source.insertItem<FullSphereItem>(); auto* instrument = source.insertItem<SpecularInstrumentItem>();
SessionItem* radius = sphere->radiusItem(); SessionItem* backgroundItem = instrument->backgroundItem();
QString buffer = itemToXML(source.rootItem()); QString buffer = itemToXML(source.rootItem());
// target model, from xml // target model, from xml
SessionModel target("TestModel"); InstrumentModel target;
itemFromXML(buffer, target.rootItem()); itemFromXML(buffer, target.rootItem());
// checking top items in source and target models // checking top items in source and target models
SessionItem* t_item = target.topItem(); SessionItem* t_item = target.topItem();
EXPECT_TRUE(t_item->hasModelType<FullSphereItem>()); EXPECT_TRUE(t_item->hasModelType<SpecularInstrumentItem>());
auto* t_sphere = dynamic_cast<FullSphereItem*>(t_item); auto* t_sphere = dynamic_cast<SpecularInstrumentItem*>(t_item);
SessionItem* t_radius = t_sphere->radiusItem(); SessionItem* t_radius = t_sphere->backgroundItem();
EXPECT_EQ(sphere->parent()->tagFromItem(sphere), t_sphere->parent()->tagFromItem(t_sphere)); EXPECT_EQ(instrument->parent()->tagFromItem(instrument),
EXPECT_EQ(sphere->displayName(), t_sphere->displayName()); t_sphere->parent()->tagFromItem(t_sphere));
EXPECT_EQ(sphere->modelType(), t_sphere->modelType()); EXPECT_EQ(instrument->displayName(), t_sphere->displayName());
EXPECT_EQ(sphere->numberOfChildren(), t_sphere->numberOfChildren()); EXPECT_EQ(instrument->modelType(), t_sphere->modelType());
EXPECT_EQ(sphere->radius(), t_sphere->radius()); EXPECT_EQ(instrument->numberOfChildren(), t_sphere->numberOfChildren());
EXPECT_EQ(instrument->analyzerEfficiency(), t_sphere->analyzerEfficiency());
EXPECT_EQ(radius->parent()->tagFromItem(sphere), t_radius->parent()->tagFromItem(t_sphere));
EXPECT_EQ(radius->displayName(), t_radius->displayName()); EXPECT_EQ(backgroundItem->parent()->tagFromItem(instrument),
EXPECT_EQ(radius->modelType(), t_radius->modelType()); t_radius->parent()->tagFromItem(t_sphere));
EXPECT_EQ(radius->numberOfChildren(), t_radius->numberOfChildren()); EXPECT_EQ(backgroundItem->displayName(), t_radius->displayName());
EXPECT_EQ(radius->value().toDouble(), t_radius->value().toDouble()); EXPECT_EQ(backgroundItem->modelType(), t_radius->modelType());
EXPECT_EQ(backgroundItem->numberOfChildren(), t_radius->numberOfChildren());
// final XML comparison EXPECT_EQ(backgroundItem->value().toDouble(), t_radius->value().toDouble());
EXPECT_EQ(buffer, itemToXML(target.rootItem()));
}
TEST_F(TestSessionXML, twoFullSphereItems)
{
// source model, to xml
SessionModel source("TestModel");
auto* sphere1 = source.insertItem<FullSphereItem>();
sphere1->setRadius(1.0);
auto* sphere2 = source.insertItem<FullSphereItem>();
sphere2->setRadius(2.0);
QString buffer = itemToXML(source.rootItem());
SessionModel target("TestModel");
itemFromXML(buffer, target.rootItem());
// final XML comparison // final XML comparison
EXPECT_EQ(buffer, itemToXML(target.rootItem())); EXPECT_EQ(buffer, itemToXML(target.rootItem()));
...@@ -109,54 +121,50 @@ TEST_F(TestSessionXML, twoFullSphereItems) ...@@ -109,54 +121,50 @@ TEST_F(TestSessionXML, twoFullSphereItems)
TEST_F(TestSessionXML, emptyMultiLayer) TEST_F(TestSessionXML, emptyMultiLayer)
{ {
SessionModel source("TestModel"); MultiLayerItem source;
source.insertItem<MultiLayerItem>(); const auto a1 = serialize(source);
QString buffer = itemToXML(source.rootItem());
SessionModel target("TestModel"); MultiLayerItem target;
itemFromXML(buffer, target.rootItem()); deserialize(target, a1);
// final XML comparison // final XML comparison
EXPECT_EQ(buffer, itemToXML(target.rootItem())); const auto a2 = serialize(target);
EXPECT_EQ(a1, a2);
} }
TEST_F(TestSessionXML, Layer) TEST_F(TestSessionXML, Layer)
{ {
SessionModel source("TestModel"); LayerItem source;
source.insertItem<LayerItem>(); const auto a1 = serialize(source);
QString buffer = itemToXML(source.rootItem());
SessionModel target("TestModel"); LayerItem target;
itemFromXML(buffer, target.rootItem()); deserialize(target, a1);
// final XML comparison // final XML comparison
EXPECT_EQ(buffer, itemToXML(target.rootItem())); EXPECT_EQ(a1, serialize(target));
} }
TEST_F(TestSessionXML, Particle) TEST_F(TestSessionXML, Particle)
{ {
SessionModel source("TestModel"); ParticleItem source;
source.insertItem<ParticleItem>(); const auto a1 = serialize(source);
QString buffer = itemToXML(source.rootItem());
SessionModel target("TestModel"); ParticleItem target;
itemFromXML(buffer, target.rootItem()); deserialize(target, a1);
// final XML comparison // final XML comparison
EXPECT_EQ(buffer, itemToXML(target.rootItem())); EXPECT_EQ(a1, serialize(target));
} }
TEST_F(TestSessionXML, ParticleWithFF) TEST_F(TestSessionXML, ParticleWithFF)
{ {
SessionModel source("TestModel"); ParticleItem source;
auto* particle = source.insertItem<ParticleItem>(); source.setFormFactorType<AnisoPyramidItem>();
const auto a1 = serialize(source);
particle->setFormFactorType<AnisoPyramidItem>(); ParticleItem target;
QString buffer = itemToXML(source.rootItem()); deserialize(target, a1);
SessionModel target("TestModel");
itemFromXML(buffer, target.rootItem());
// final XML comparison // final XML comparison
EXPECT_EQ(buffer, itemToXML(target.rootItem())); EXPECT_EQ(a1, serialize(target));
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment