##// END OF EJS Templates
Creates builder used to facilitate creation of items (for test cases)
Alexandre Leroux -
r1080:d45cab418d5a
parent child
Show More
@@ -0,0 +1,73
1 #include "DataSourceItemBuilder.h"
2
3 DataSourceItemBuilder &DataSourceItemBuilder::root(const QString &name)
4 {
5 m_Root = std::make_shared<DataSourceItem>(DataSourceItemType::NODE, name);
6 m_Items.push(m_Root.get());
7 return *this;
8 }
9
10 DataSourceItemBuilder &DataSourceItemBuilder::root(QVariantHash data)
11 {
12 m_Root = std::make_shared<DataSourceItem>(DataSourceItemType::NODE, data);
13 m_Items.push(m_Root.get());
14 return *this;
15 }
16
17 DataSourceItemBuilder &DataSourceItemBuilder::node(const QString &name)
18 {
19 return append(DataSourceItemType::NODE, name);
20 }
21
22 DataSourceItemBuilder &DataSourceItemBuilder::node(QVariantHash data)
23 {
24 return append(DataSourceItemType::NODE, std::move(data));
25 }
26
27 DataSourceItemBuilder &DataSourceItemBuilder::product(const QString &name)
28 {
29 return append(DataSourceItemType::PRODUCT, name);
30 }
31
32 DataSourceItemBuilder &DataSourceItemBuilder::product(QVariantHash data)
33 {
34 return append(DataSourceItemType::PRODUCT, std::move(data));
35 }
36
37 DataSourceItemBuilder &DataSourceItemBuilder::component(const QString &name)
38 {
39 return append(DataSourceItemType::COMPONENT, name);
40 }
41
42 DataSourceItemBuilder &DataSourceItemBuilder::component(QVariantHash data)
43 {
44 return append(DataSourceItemType::COMPONENT, std::move(data));
45 }
46
47 DataSourceItemBuilder &DataSourceItemBuilder::end()
48 {
49 m_Items.pop();
50 return *this;
51 }
52
53 std::shared_ptr<DataSourceItem> DataSourceItemBuilder::build()
54 {
55 return m_Root;
56 }
57
58 DataSourceItemBuilder &DataSourceItemBuilder::append(DataSourceItemType type, const QString &name)
59 {
60 append(type, QVariantHash{{DataSourceItem::NAME_DATA_KEY, name}});
61 return *this;
62 }
63
64 DataSourceItemBuilder &DataSourceItemBuilder::append(DataSourceItemType type, QVariantHash data)
65 {
66 auto parentItem = m_Items.top();
67
68 auto insertIndex = parentItem->childCount();
69 parentItem->appendChild(std::make_unique<DataSourceItem>(type, std::move(data)));
70
71 m_Items.push(parentItem->child(insertIndex));
72 return *this;
73 }
@@ -0,0 +1,45
1 #ifndef SCIQLOP_DATASOURCEITEMBUILDER_H
2 #define SCIQLOP_DATASOURCEITEMBUILDER_H
3
4 #include <DataSource/DataSourceItem.h>
5
6 #include <memory>
7 #include <stack>
8
9 /**
10 * @brief The DataSourceItemBuilder class aims to facilitate the creation of a DataSourceItem for unit tests
11 * @sa DataSourceItem
12 */
13 class DataSourceItemBuilder {
14 public:
15 /// Inits root item
16 DataSourceItemBuilder & root(const QString &name);
17 DataSourceItemBuilder & root(QVariantHash data);
18
19 /// Adds node into the current item
20 DataSourceItemBuilder & node(const QString &name);
21 DataSourceItemBuilder & node(QVariantHash data);
22
23 /// Adds product into the current item
24 DataSourceItemBuilder & product(const QString &name);
25 DataSourceItemBuilder & product(QVariantHash data);
26
27 /// Adds component into the current item
28 DataSourceItemBuilder & component(const QString &name);
29 DataSourceItemBuilder & component(QVariantHash data);
30
31 /// Closes the build of the current item
32 DataSourceItemBuilder& end();
33
34 /// Creates the DataSourceItem
35 std::shared_ptr<DataSourceItem> build();
36
37 private:
38 DataSourceItemBuilder& append(DataSourceItemType type, const QString &name);
39 DataSourceItemBuilder& append(DataSourceItemType type, QVariantHash data);
40
41 std::shared_ptr<DataSourceItem> m_Root{nullptr};
42 std::stack<DataSourceItem*> m_Items;
43 };
44
45 #endif // SCIQLOP_DATASOURCEITEMBUILDER_H
@@ -20,7 +20,9 amdatest_sources = [
20 20 'Data/DataSeriesBuilders.h',
21 21 'Data/DataSeriesBuilders.cpp',
22 22 'Data/DataSeriesTestsUtils.h',
23 'Data/DataSeriesTestsUtils.cpp'
23 'Data/DataSeriesTestsUtils.cpp',
24 'DataSource/DataSourceItemBuilder.h',
25 'DataSource/DataSourceItemBuilder.cpp'
24 26 ]
25 27
26 28 foreach unit_test : tests
General Comments 0
You need to be logged in to leave comments. Login now