File size: 7,898 Bytes
985c397 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 | // SPDX-License-Identifier: LGPL-2.1-or-later
#include <gtest/gtest.h>
#include <boost/core/ignore_unused.hpp>
#include "Mod/Part/App/FeaturePartCommon.h"
#include <src/App/InitApplication.h>
#include <BRepBuilderAPI_MakeVertex.hxx>
#include "PartTestHelpers.h"
#include "App/MappedElement.h"
using namespace Part;
using namespace PartTestHelpers;
class FeaturePartTest: public ::testing::Test, public PartTestHelperClass
{
protected:
static void SetUpTestSuite()
{
tests::initApplication();
}
void SetUp() override
{
createTestDoc();
_common = _doc->addObject<Common>();
}
void TearDown() override
{}
Common* _common = nullptr; // NOLINT Can't be private in a test framework
};
TEST_F(FeaturePartTest, testGetElementName)
{
// Arrange
_common->Base.setValue(_boxes[0]);
_common->Tool.setValue(_boxes[1]);
// Act
_common->execute();
const TopoShape& ts = _common->Shape.getShape();
auto namePair = _common->getElementName("test");
auto namePairExport = _common->getElementName("test", App::GeoFeature::Export);
auto namePairSelf = _common->getElementName(nullptr);
// Assert
EXPECT_STREQ(namePair.newName.c_str(), "");
EXPECT_STREQ(namePair.oldName.c_str(), "test");
EXPECT_STREQ(namePairExport.newName.c_str(), "");
EXPECT_STREQ(namePairExport.oldName.c_str(), "test");
EXPECT_STREQ(namePairSelf.newName.c_str(), "");
EXPECT_STREQ(namePairSelf.oldName.c_str(), "");
EXPECT_EQ(ts.getElementMap().size(), 26);
// TBD
}
TEST_F(FeaturePartTest, create)
{
// Arrange
// A shape that will be passed to the various calls of Feature::create
auto shape {TopoShape(BRepBuilderAPI_MakeVertex(gp_Pnt(1.0, 1.0, 1.0)).Vertex(), 1)};
auto otherDocName {App::GetApplication().getUniqueDocumentName("otherDoc")};
// Another document where it will be created a shape
auto otherDoc {App::GetApplication().newDocument(otherDocName.c_str(), "otherDocUser")};
// _doc is populated by PartTestHelperClass::createTestDoc. Making it an empty document
_doc->clearDocument();
// Setting the active document back to _doc otherwise the first 3 calls to Feature::create will
// act on otherDoc
App::GetApplication().setActiveDocument(_doc);
// Act
// A feature with an empty TopoShape
auto featureNoShape {Feature::create(TopoShape())};
// A feature with a TopoShape
auto featureNoName {Feature::create(shape)};
// A feature with a TopoShape and a name
auto featureNoDoc {Feature::create(shape, "Vertex")};
// A feature with a TopoShape and a name in the document otherDoc
auto feature {Feature::create(shape, "Vertex", otherDoc)};
// Assert
// Check that the shapes have been added. Only featureNoShape should return an empty shape, the
// others should have it as TopoShape shape is passed as argument
EXPECT_TRUE(featureNoShape->Shape.getValue().IsNull());
EXPECT_FALSE(featureNoName->Shape.getValue().IsNull());
EXPECT_FALSE(featureNoDoc->Shape.getValue().IsNull());
EXPECT_FALSE(feature->Shape.getValue().IsNull());
// Check the features names
// Without a name the feature's name will be set to "Shape"
EXPECT_STREQ(_doc->getObjectName(featureNoShape), "Shape");
// In _doc there's already a shape with name "Shape" and, as there can't be duplicated names in
// the same document, the other feature will get an unique name that will still contain "Shape"
EXPECT_STREQ(_doc->getObjectName(featureNoName), "Shape001");
// There aren't other features with name "Vertex" in _doc, therefore that name will be assigned
// without modifications
EXPECT_STREQ(_doc->getObjectName(featureNoDoc), "Vertex");
// The feature is created in otherDoc, which doesn't have other features and thertherefore the
// feature's name will be assigned without modifications
EXPECT_STREQ(otherDoc->getObjectName(feature), "Vertex");
// Check that the features have been created in the correct document
// The first 3 calls to Feature::create acts on _doc, which is empty, and therefore the number
// of features in that document is the same of the features created with Feature::create
EXPECT_EQ(_doc->getObjects().size(), 3);
// The last call to Feature::create acts on otherDoc, which is empty, and therefore that
// document will have only 1 feature
EXPECT_EQ(otherDoc->getObjects().size(), 1);
}
TEST_F(FeaturePartTest, getElementHistory)
{
// Arrange
const char* name2 = "Edge2"; // Edge, Vertex, or Face. will work here.
// Act
auto result = Feature::getElementHistory(_boxes[0], name2, true, false);
Data::HistoryItem histItem = result.front();
// Assert
EXPECT_EQ(result.size(), 1);
EXPECT_NE(histItem.tag, 0); // Make sure we have one. It will vary.
EXPECT_EQ(histItem.index.getIndex(), 2);
EXPECT_STREQ(histItem.index.getType(), "Edge");
EXPECT_STREQ(histItem.element.toString().c_str(), name2);
EXPECT_EQ(histItem.obj, _boxes[0]);
}
TEST_F(FeaturePartTest, getRelatedElements)
{
// Arrange
_common->Base.setValue(_boxes[0]);
_common->Tool.setValue(_boxes[1]);
// Act
_common->execute();
auto label1 = _common->Label.getValue();
auto label2 = _boxes[1]->Label.getValue();
const TopoShape& ts = _common->Shape.getShape();
boost::ignore_unused(ts);
auto result = Feature::getRelatedElements(
_doc->getObject(label1),
"Edge2",
HistoryTraceType::followTypeChange,
true
);
auto result2 = Feature::getRelatedElements(
_doc->getObject(label2),
"Edge1",
HistoryTraceType::followTypeChange,
true
);
// Assert
EXPECT_EQ(result.size(), 1); // Found the one.
EXPECT_EQ(result2.size(), 0); // No element map, so no related elements
// The results are always going to vary, so we can't test for specific values:
// EXPECT_STREQ(result.front().name.toString().c_str(),"Edge3;:M;CMN;:H38d:7,E");
}
// Note that this test is pretty trivial and useless .. but the method in question is never
// called in the codebase.
TEST_F(FeaturePartTest, getElementFromSource)
{
// Arrange
_common->Base.setValue(_boxes[0]);
_common->Tool.setValue(_boxes[1]);
App::DocumentObject sourceObject;
// const char *sourceSubElement;
// Act
_common->execute();
auto label1 = _common->Label.getValue();
auto label2 = _boxes[1]->Label.getValue();
const TopoShape& ts = _common->Shape.getShape();
boost::ignore_unused(label1);
boost::ignore_unused(label2);
boost::ignore_unused(ts);
auto element = Feature::getElementFromSource(
_common,
"Part__Box001", // "Edge1",
_boxes[0],
"Face1", // "Edge1",
false
);
// Assert
EXPECT_EQ(element.size(), 0);
}
TEST_F(FeaturePartTest, getSubObject)
{
// Arrange
_common->Base.setValue(_boxes[0]);
_common->Tool.setValue(_boxes[1]);
App::DocumentObject sourceObject;
PyObject* pyObj;
// Act
_common->execute();
auto result = _boxes[1]->getSubObject("Face5", &pyObj, nullptr, false, 10);
// Assert
ASSERT_NE(result, nullptr);
EXPECT_STREQ(result->getNameInDocument(), "Part__Box001");
}
TEST_F(FeaturePartTest, getElementTypes)
{
Part::Feature pf;
std::vector<const char*> types = pf.getElementTypes();
EXPECT_EQ(types.size(), 3);
EXPECT_STREQ(types[0], "Face");
EXPECT_STREQ(types[1], "Edge");
EXPECT_STREQ(types[2], "Vertex");
}
TEST_F(FeaturePartTest, getComplexElementTypes)
{
Part::TopoShape shape;
std::vector<const char*> types = shape.getElementTypes();
EXPECT_EQ(types.size(), 3);
EXPECT_STREQ(types[0], "Face");
EXPECT_STREQ(types[1], "Edge");
EXPECT_STREQ(types[2], "Vertex");
}
|