Add BEJ tree API.
This API allows us to represent a JSON object with a linked list
representation.
Signed-off-by: Kasun Athukorala <kasunath@google.com>
Change-Id: I7894e7c10e4db2ad02a79b01bb4d7a48d5a197cd
diff --git a/test/bej_tree_test.cpp b/test/bej_tree_test.cpp
new file mode 100644
index 0000000..82d763b
--- /dev/null
+++ b/test/bej_tree_test.cpp
@@ -0,0 +1,137 @@
+#include "bej_tree.h"
+
+#include <gmock/gmock-matchers.h>
+#include <gmock/gmock.h>
+#include <gtest/gtest.h>
+
+namespace libbej
+{
+
+TEST(BejTreeTest, InitSet)
+{
+ const char* name = "SomeProperty";
+ struct RedfishPropertyParent node;
+ bejTreeInitSet(&node, name);
+
+ EXPECT_THAT(node.nodeAttr.name, name);
+ EXPECT_THAT(node.nodeAttr.format.principalDataType, bejSet);
+ EXPECT_THAT(node.nodeAttr.format.deferredBinding, 0);
+ EXPECT_THAT(node.nodeAttr.format.readOnlyProperty, 0);
+ EXPECT_THAT(node.nodeAttr.format.nullableProperty, 0);
+ EXPECT_THAT(node.nodeAttr.sibling, nullptr);
+ EXPECT_THAT(node.nChildren, 0);
+ EXPECT_THAT(node.firstChild, nullptr);
+ EXPECT_THAT(node.lastChild, nullptr);
+}
+
+TEST(BejTreeTest, InitArray)
+{
+ const char* name = "SomeProperty";
+ struct RedfishPropertyParent node;
+ bejTreeInitArray(&node, name);
+
+ EXPECT_THAT(node.nodeAttr.name, name);
+ EXPECT_THAT(node.nodeAttr.format.principalDataType, bejArray);
+ EXPECT_THAT(node.nodeAttr.format.deferredBinding, 0);
+ EXPECT_THAT(node.nodeAttr.format.readOnlyProperty, 0);
+ EXPECT_THAT(node.nodeAttr.format.nullableProperty, 0);
+ EXPECT_THAT(node.nodeAttr.sibling, nullptr);
+ EXPECT_THAT(node.nChildren, 0);
+ EXPECT_THAT(node.firstChild, nullptr);
+ EXPECT_THAT(node.lastChild, nullptr);
+}
+
+TEST(BejTreeTest, InitAnnotatedProp)
+{
+ const char* name = "SomeProperty";
+ struct RedfishPropertyParent node;
+ bejTreeInitPropertyAnnotated(&node, name);
+
+ EXPECT_THAT(node.nodeAttr.name, name);
+ EXPECT_THAT(node.nodeAttr.format.principalDataType, bejPropertyAnnotation);
+ EXPECT_THAT(node.nodeAttr.format.deferredBinding, 0);
+ EXPECT_THAT(node.nodeAttr.format.readOnlyProperty, 0);
+ EXPECT_THAT(node.nodeAttr.format.nullableProperty, 0);
+ EXPECT_THAT(node.nodeAttr.sibling, nullptr);
+ EXPECT_THAT(node.nChildren, 0);
+ EXPECT_THAT(node.firstChild, nullptr);
+ EXPECT_THAT(node.lastChild, nullptr);
+}
+
+TEST(BejTreeTest, ChildLinking)
+{
+ struct RedfishPropertyParent parent;
+ struct RedfishPropertyLeafInt child1;
+ struct RedfishPropertyLeafInt child2;
+
+ bejTreeInitSet(&parent, nullptr);
+ EXPECT_THAT(parent.nChildren, 0);
+ EXPECT_THAT(parent.firstChild, nullptr);
+ EXPECT_THAT(parent.lastChild, nullptr);
+
+ bejTreeAddInteger(&parent, &child1, nullptr, 1024);
+ EXPECT_THAT(parent.nChildren, 1);
+ EXPECT_THAT(parent.firstChild, &child1);
+ EXPECT_THAT(parent.lastChild, &child1);
+
+ bejTreeAddInteger(&parent, &child2, nullptr, 20);
+ EXPECT_THAT(parent.nChildren, 2);
+ EXPECT_THAT(parent.firstChild, &child1);
+ EXPECT_THAT(parent.lastChild, &child2);
+
+ // child2 should be a sibling of child1.
+ EXPECT_THAT(child1.leaf.nodeAttr.sibling, &child2);
+}
+
+TEST(BejTreeTest, AddInteger)
+{
+ const char* name = "SomeProperty";
+ struct RedfishPropertyParent parent;
+ struct RedfishPropertyLeafInt child;
+
+ bejTreeInitSet(&parent, nullptr);
+ bejTreeAddInteger(&parent, &child, name, 1024);
+
+ EXPECT_THAT(child.leaf.nodeAttr.name, name);
+ EXPECT_THAT(child.leaf.nodeAttr.format.principalDataType, bejInteger);
+ EXPECT_THAT(child.leaf.nodeAttr.format.deferredBinding, 0);
+ EXPECT_THAT(child.leaf.nodeAttr.format.readOnlyProperty, 0);
+ EXPECT_THAT(child.leaf.nodeAttr.format.nullableProperty, 0);
+ EXPECT_THAT(child.leaf.nodeAttr.sibling, nullptr);
+ EXPECT_THAT(child.value, 1024);
+}
+
+TEST(BejTreeTest, SetInteger)
+{
+ const char* name = "SomeProperty";
+ struct RedfishPropertyParent parent;
+ struct RedfishPropertyLeafInt child;
+
+ bejTreeInitSet(&parent, nullptr);
+ bejTreeAddInteger(&parent, &child, name, 1024);
+
+ EXPECT_THAT(child.value, 1024);
+ bejTreeSetInteger(&child, 20);
+ EXPECT_THAT(child.value, 20);
+}
+
+TEST(BejTreeTest, AddEnum)
+{
+ const char* name = "SomeProperty";
+ const char* enumValue = "EnumValue";
+ struct RedfishPropertyParent parent;
+ struct RedfishPropertyLeafEnum child;
+
+ bejTreeInitSet(&parent, nullptr);
+ bejTreeAddEnum(&parent, &child, name, enumValue);
+
+ EXPECT_THAT(child.leaf.nodeAttr.name, name);
+ EXPECT_THAT(child.leaf.nodeAttr.format.principalDataType, bejEnum);
+ EXPECT_THAT(child.leaf.nodeAttr.format.deferredBinding, 0);
+ EXPECT_THAT(child.leaf.nodeAttr.format.readOnlyProperty, 0);
+ EXPECT_THAT(child.leaf.nodeAttr.format.nullableProperty, 0);
+ EXPECT_THAT(child.leaf.nodeAttr.sibling, nullptr);
+ EXPECT_THAT(child.value, enumValue);
+}
+
+} // namespace libbej
diff --git a/test/meson.build b/test/meson.build
index 8178dc1..07807c4 100644
--- a/test/meson.build
+++ b/test/meson.build
@@ -19,6 +19,7 @@
'bej_decoder',
'bej_common',
'bej_dictionary',
+ 'bej_tree',
]
foreach t : gtests
test(t, executable(t.underscorify(), t + '_test.cpp',