File size: 8,511 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
// SPDX-License-Identifier: LGPL-2.1-or-later
/***************************************************************************
 *   Copyright (c) 2023 David Carter <dcarter@david.carter.ca>             *
 *                                                                         *
 *   This file is part of FreeCAD.                                         *
 *                                                                         *
 *   FreeCAD is free software: you can redistribute it and/or modify it    *
 *   under the terms of the GNU Lesser General Public License as           *
 *   published by the Free Software Foundation, either version 2.1 of the  *
 *   License, or (at your option) any later version.                       *
 *                                                                         *
 *   FreeCAD is distributed in the hope that it will be useful, but        *
 *   WITHOUT ANY WARRANTY; without even the implied warranty of            *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU      *
 *   Lesser General Public License for more details.                       *
 *                                                                         *
 *   You should have received a copy of the GNU Lesser General Public      *
 *   License along with FreeCAD. If not, see                               *
 *   <https://www.gnu.org/licenses/>.                                      *
 *                                                                         *
 **************************************************************************/

#include <gtest/gtest.h>

#include <memory>

#include <QMetaType>
#include <QString>

#include <App/Application.h>
#include <Gui/MetaTypes.h>

#include <Mod/Material/App/MaterialManager.h>
#include <Mod/Material/App/Model.h>
#include <Mod/Material/App/ModelManager.h>
#include <Mod/Material/App/ModelUuids.h>

class TestMaterialProperties: public ::testing::Test
{
protected:
    static void SetUpTestSuite()
    {}

    void SetUp() override
    {
        // 2D Properties
        modelProp = Materials::ModelProperty(
            QStringLiteral("Density"),  // Name
            QStringLiteral("D"),        // Header
            QStringLiteral("2DArray"),  // Type
            QStringLiteral(""),         // Units
            QStringLiteral(""),         // URL
            QStringLiteral("desc")
        );  // Description
        modelProp1 = Materials::ModelProperty(
            QStringLiteral("Temperature"),
            QStringLiteral("T"),
            QStringLiteral("Quantity"),
            QStringLiteral("C"),
            QStringLiteral(""),
            QStringLiteral("desc1")
        );
        modelProp2 = Materials::ModelProperty(
            QStringLiteral("Density"),
            QStringLiteral("D"),
            QStringLiteral("Quantity"),
            QStringLiteral("kg/m^3"),
            QStringLiteral("https://en.wikipedia.org/wiki/Density"),
            QStringLiteral("desc2")
        );

        modelProp.addColumn(modelProp1);
        modelProp.addColumn(modelProp2);

        // 3D properties
        model3DProp = Materials::ModelProperty(
            QStringLiteral("StressStrain"),     // Name
            QStringLiteral("Stress / Strain"),  // Header
            QStringLiteral("3DArray"),          // Type
            QStringLiteral(""),                 // Units
            QStringLiteral(""),                 // URL
            QStringLiteral(
                "3 Dimensional array showing stress and strain as a function of "
                "temperature"
            )
        );  // Description
        model3DProp1 = Materials::ModelProperty(
            QStringLiteral("Temperature"),
            QStringLiteral("T"),
            QStringLiteral("Quantity"),
            QStringLiteral("C"),
            QStringLiteral(""),
            QStringLiteral("desc1")
        );
        model3DProp2 = Materials::ModelProperty(
            QStringLiteral("Stress"),
            QStringLiteral("Stress"),
            QStringLiteral("Quantity"),
            QStringLiteral("MPa"),
            QStringLiteral(""),
            QStringLiteral("desc2")
        );
        model3DProp3 = Materials::ModelProperty(
            QStringLiteral("Strain"),
            QStringLiteral("Strain"),
            QStringLiteral("Quantity"),
            QStringLiteral("MPa"),
            QStringLiteral(""),
            QStringLiteral("desc3")
        );

        model3DProp.addColumn(model3DProp1);
        model3DProp.addColumn(model3DProp2);
        model3DProp.addColumn(model3DProp3);
    }

    // void TearDown() override {}

    Materials::ModelProperty modelProp;
    Materials::ModelProperty modelProp1;
    Materials::ModelProperty modelProp2;
    Materials::ModelProperty model3DProp;
    Materials::ModelProperty model3DProp1;
    Materials::ModelProperty model3DProp2;
    Materials::ModelProperty model3DProp3;
};

TEST_F(TestMaterialProperties, TestEmpty)
{
    Materials::MaterialProperty prop;
    EXPECT_EQ(prop.getType(), Materials::MaterialValue::None);
    EXPECT_TRUE(prop.isNull());
    auto variant = prop.getValue();
    EXPECT_TRUE(variant.isNull());
    EXPECT_FALSE(variant.canConvert<QString>());
    EXPECT_TRUE(variant.toString().isNull());
    EXPECT_TRUE(variant.toString().isEmpty());
    EXPECT_EQ(variant.toString().size(), 0);
}

TEST_F(TestMaterialProperties, TestSingle)
{
    Materials::MaterialProperty prop(modelProp1, QStringLiteral("sampleUUID"));
    EXPECT_EQ(prop.getType(), Materials::MaterialValue::Quantity);
    EXPECT_EQ(prop.getModelUUID(), QStringLiteral("sampleUUID"));
    EXPECT_TRUE(prop.isNull());
    auto variant = prop.getValue();
    EXPECT_TRUE(variant.canConvert<Base::Quantity>());
    EXPECT_FALSE(variant.value<Base::Quantity>().isValid());
    EXPECT_FALSE(variant.canConvert<QString>());
    EXPECT_TRUE(variant.toString().isNull());
    EXPECT_TRUE(variant.toString().isEmpty());
    EXPECT_EQ(variant.toString().size(), 0);
}

void check2DArray(Materials::MaterialProperty& prop)
{
    EXPECT_EQ(prop.getType(), Materials::MaterialValue::Array2D);
    EXPECT_EQ(prop.getModelUUID(), QStringLiteral("sampleUUID"));
    EXPECT_TRUE(prop.isNull());
    auto array = std::static_pointer_cast<Materials::Array2D>(prop.getMaterialValue());
    EXPECT_EQ(array->rows(), 0);
    auto variant = prop.getValue();  // Throw an error?
    EXPECT_FALSE(variant.canConvert<QString>());
    EXPECT_TRUE(variant.toString().isNull());
    EXPECT_TRUE(variant.toString().isEmpty());
    EXPECT_EQ(variant.toString().size(), 0);

    // Check the columns
    EXPECT_EQ(prop.columns(), 2);
}

TEST_F(TestMaterialProperties, Test2DArray)
{
    Materials::MaterialProperty prop(modelProp, QStringLiteral("sampleUUID"));
    check2DArray(prop);
}

TEST_F(TestMaterialProperties, Test2DArrayCopy)
{
    Materials::MaterialProperty propBase(modelProp, QStringLiteral("sampleUUID"));
    Materials::MaterialProperty prop(propBase);
    check2DArray(prop);
}

TEST_F(TestMaterialProperties, Test2DArrayAssignment)
{
    Materials::MaterialProperty propBase(modelProp, QStringLiteral("sampleUUID"));
    Materials::MaterialProperty prop;

    prop = propBase;
    check2DArray(prop);
}

void check3DArray(Materials::MaterialProperty& prop)
{
    EXPECT_EQ(prop.getType(), Materials::MaterialValue::Array3D);
    EXPECT_EQ(prop.getModelUUID(), QStringLiteral("sampleUUID"));
    EXPECT_TRUE(prop.isNull());
    auto array = std::static_pointer_cast<Materials::Array3D>(prop.getMaterialValue());
    EXPECT_EQ(array->depth(), 0);
    auto variant = prop.getValue();  // Throw an error?
    EXPECT_FALSE(variant.canConvert<QString>());
    EXPECT_TRUE(variant.toString().isNull());
    EXPECT_TRUE(variant.toString().isEmpty());
    EXPECT_EQ(variant.toString().size(), 0);

    // Check the columns
    EXPECT_EQ(prop.columns(), 3);
}

TEST_F(TestMaterialProperties, Test3DArray)
{
    Materials::MaterialProperty prop(model3DProp, QStringLiteral("sampleUUID"));
    check3DArray(prop);
}

TEST_F(TestMaterialProperties, Test3DArrayCopy)
{
    Materials::MaterialProperty propBase(model3DProp, QStringLiteral("sampleUUID"));
    Materials::MaterialProperty prop(propBase);
    check3DArray(prop);
}

TEST_F(TestMaterialProperties, Test3DArrayAssignment)
{
    Materials::MaterialProperty propBase(model3DProp, QStringLiteral("sampleUUID"));
    Materials::MaterialProperty prop;

    prop = propBase;
    check3DArray(prop);
}

// clang-format off

// clang-format on