paysages3d/src/tests/Layers_Test.cpp
Michael Lemaire 0fc10fd28b Added FloatNode and smart save/load of definition tree
Node children are now saved with their name, and if a child is not found,
it is skipped. This will allow for backward/forward compatibility of saves.
2015-08-13 23:46:50 +02:00

107 lines
2.6 KiB
C++

#include "BaseTestCase.h"
#include "Layers.h"
#include "PackStream.h"
DefinitionNode* _construc1(Layers*)
{
return new DefinitionNode(NULL, "test");
}
DefinitionNode* _construc2(Layers* parent)
{
DefinitionNode* result = new DefinitionNode(parent, "test");
return result;
}
TEST(Layers, Constructor)
{
Layers layers1(NULL, "test", _construc1);
EXPECT_EQ(0, layers1.count());
}
TEST(Layers, NullLayer)
{
Layers layers1(NULL, "test", _construc1);
for (int i = -2; i < 5; i++)
{
DefinitionNode* layer = layers1.getLayer(0);
EXPECT_NE((DefinitionNode*)NULL, layer);
EXPECT_EQ(NULL, layer->getParent());
EXPECT_EQ(layer, layer->getRoot());
EXPECT_EQ("test", layer->getName());
}
EXPECT_EQ(0, layers1.count());
}
TEST(Layers, copy)
{
Layers layers1(NULL, "test", _construc2);
layers1.addLayer();
EXPECT_EQ(1, layers1.count());
// Copy with the same constructor
Layers layers2(NULL, "test", _construc2);
EXPECT_EQ(0, layers2.count());
layers1.copy(&layers2);
EXPECT_EQ(1, layers2.count());
EXPECT_EQ("test", layers1.getLayer(0)->getName());
EXPECT_EQ(&layers1, layers1.getLayer(0)->getParent());
EXPECT_EQ("test", layers2.getLayer(0)->getName());
EXPECT_EQ(&layers2, layers2.getLayer(0)->getParent());
EXPECT_NE(layers1.getLayer(0), layers2.getLayer(0));
}
TEST(Layers, maxLayerCount)
{
Layers layers1(NULL, "test", _construc1);
layers1.setMaxLayerCount(2);
EXPECT_EQ(0, layers1.count());
layers1.addLayer();
EXPECT_EQ(1, layers1.count());
layers1.addLayer();
EXPECT_EQ(2, layers1.count());
layers1.addLayer();
EXPECT_EQ(2, layers1.count());
layers1.addLayer();
EXPECT_EQ(2, layers1.count());
layers1.removeLayer(0);
EXPECT_EQ(1, layers1.count());
layers1.addLayer();
EXPECT_EQ(2, layers1.count());
layers1.addLayer();
EXPECT_EQ(2, layers1.count());
}
TEST(Layers, saveLoad)
{
PackStream *stream;
Layers layers1(NULL, "test", _construc1);
layers1.addLayer();
layers1.addLayer();
ASSERT_EQ(2, layers1.count());
layers1.getLayer(0)->setName("first");
layers1.getLayer(1)->setName("second");
stream = new PackStream();
stream->bindToFile("/tmp/test_paysages_pack", true);
layers1.save(stream);
delete stream;
Layers layers2(NULL, "test", _construc1);
stream = new PackStream();
stream->bindToFile("/tmp/test_paysages_pack");
layers2.load(stream);
delete stream;
ASSERT_EQ(2, layers2.count());
EXPECT_EQ("first", layers2.getLayer(0)->getName());
EXPECT_EQ("second", layers2.getLayer(1)->getName());
}