awtk/tests/conf_node_test.cc

110 lines
4.0 KiB
C++
Raw Normal View History

2020-06-07 09:50:36 +08:00
#include "gtest/gtest.h"
2020-08-11 07:52:02 +08:00
#include "conf_io/conf_json.h"
2020-06-07 09:50:36 +08:00
#include "conf_io/conf_node.h"
TEST(ConfNode, basic) {
2023-05-26 17:11:44 +08:00
value_t v;
2020-06-07 09:50:36 +08:00
conf_doc_t* doc = conf_doc_create(100);
conf_node_t* node = NULL;
doc->root = conf_doc_create_node(doc, "root");
conf_doc_append_child(doc, doc->root, conf_doc_create_node(doc, "1"));
conf_doc_append_child(doc, doc->root, conf_doc_create_node(doc, "2"));
conf_doc_append_child(doc, doc->root, conf_doc_create_node(doc, "3"));
conf_doc_append_child(doc, doc->root, conf_doc_create_node(doc, "4"));
node = conf_node_find_child(doc->root, "1");
ASSERT_STREQ(conf_node_get_name(node), "1");
2023-05-26 17:11:44 +08:00
value_set_int(&v, 1);
ASSERT_EQ(conf_node_set_value(node, &v), RET_OK);
value_set_int(&v, 0);
ASSERT_EQ(conf_node_get_child_value(doc->root, "1", &v), RET_OK);
ASSERT_EQ(value_int(&v), 1);
ASSERT_EQ(conf_node_get_child_value_by_index(doc->root, 0, &v), RET_OK);
ASSERT_EQ(value_int(&v), 1);
2020-06-07 09:50:36 +08:00
node = conf_node_find_child(doc->root, "2");
ASSERT_STREQ(conf_node_get_name(node), "2");
2023-05-26 17:11:44 +08:00
value_set_int(&v, 2);
ASSERT_EQ(conf_node_set_value(node, &v), RET_OK);
value_set_int(&v, 0);
ASSERT_EQ(conf_node_get_child_value(doc->root, "2", &v), RET_OK);
ASSERT_EQ(value_int(&v), 2);
ASSERT_EQ(conf_node_get_child_value_by_index(doc->root, 1, &v), RET_OK);
ASSERT_EQ(value_int(&v), 2);
2020-06-07 09:50:36 +08:00
node = conf_node_find_child(doc->root, "3");
ASSERT_STREQ(conf_node_get_name(node), "3");
2023-05-26 17:11:44 +08:00
value_set_int(&v, 3);
ASSERT_EQ(conf_node_set_value(node, &v), RET_OK);
value_set_int(&v, 0);
ASSERT_EQ(conf_node_get_child_value(doc->root, "3", &v), RET_OK);
ASSERT_EQ(value_int(&v), 3);
ASSERT_EQ(conf_node_get_child_value_by_index(doc->root, 2, &v), RET_OK);
ASSERT_EQ(value_int(&v), 3);
2020-06-07 09:50:36 +08:00
node = conf_node_find_child(doc->root, "4");
ASSERT_STREQ(conf_node_get_name(node), "4");
2023-05-26 17:11:44 +08:00
value_set_int(&v, 4);
ASSERT_EQ(conf_node_set_value(node, &v), RET_OK);
value_set_int(&v, 0);
ASSERT_EQ(conf_node_get_child_value(doc->root, "4", &v), RET_OK);
ASSERT_EQ(value_int(&v), 4);
ASSERT_EQ(conf_node_get_child_value_by_index(doc->root, 3, &v), RET_OK);
ASSERT_EQ(value_int(&v), 4);
2020-06-07 09:50:36 +08:00
ASSERT_EQ(conf_doc_remove_child_by_name(doc, doc->root, "2"), RET_OK);
ASSERT_NE(conf_doc_remove_child_by_name(doc, doc->root, "2"), RET_OK);
ASSERT_EQ(conf_doc_remove_child_by_name(doc, doc->root, "1"), RET_OK);
ASSERT_NE(conf_doc_remove_child_by_name(doc, doc->root, "1"), RET_OK);
ASSERT_EQ(conf_doc_remove_child_by_name(doc, doc->root, "4"), RET_OK);
ASSERT_NE(conf_doc_remove_child_by_name(doc, doc->root, "4"), RET_OK);
node = conf_node_find_child(doc->root, "3");
ASSERT_EQ(conf_doc_remove_child(doc, doc->root, node), RET_OK);
ASSERT_NE(conf_doc_remove_child_by_name(doc, doc->root, "3"), RET_OK);
conf_doc_destroy(doc);
}
2020-08-11 07:35:10 +08:00
TEST(ConfNode, set_get) {
value_t v;
conf_doc_t* doc = conf_doc_create(100);
2020-08-20 10:14:39 +08:00
ASSERT_EQ(conf_doc_set(doc, "person.[0].name", value_set_str(&v, "jim")), RET_OK);
ASSERT_EQ(conf_doc_set(doc, "person.[1].name", value_set_str(&v, "tom")), RET_OK);
ASSERT_EQ(conf_doc_set(doc, "person.[2].name", value_set_str(&v, "mike")), RET_OK);
2020-08-11 07:35:10 +08:00
2020-08-20 10:14:39 +08:00
ASSERT_EQ(conf_doc_get(doc, "person.[0].name", &v), RET_OK);
2020-08-11 07:35:10 +08:00
ASSERT_STREQ(value_str(&v), "jim");
2020-08-20 10:14:39 +08:00
ASSERT_EQ(conf_doc_get(doc, "person.[1].name", &v), RET_OK);
2020-08-11 07:35:10 +08:00
ASSERT_STREQ(value_str(&v), "tom");
2020-08-20 10:14:39 +08:00
ASSERT_EQ(conf_doc_get(doc, "person.[2].name", &v), RET_OK);
2020-08-11 07:35:10 +08:00
ASSERT_STREQ(value_str(&v), "mike");
2020-08-20 10:14:39 +08:00
ASSERT_NE(conf_doc_set(doc, "person.[20].name", value_set_str(&v, "anny")), RET_OK);
2020-08-11 07:35:10 +08:00
conf_doc_destroy(doc);
}
2020-08-11 07:52:02 +08:00
TEST(ConfNode, set_get_str) {
value_t v;
str_t str;
conf_doc_t* doc = conf_doc_create(100);
str_init(&str, 0);
2020-08-20 10:14:39 +08:00
ASSERT_EQ(conf_doc_set(doc, "names.[0]", value_set_str(&v, "jim")), RET_OK);
ASSERT_EQ(conf_doc_set(doc, "names.[1]", value_set_str(&v, "tom")), RET_OK);
ASSERT_EQ(conf_doc_set(doc, "names.[2]", value_set_str(&v, "anny")), RET_OK);
2020-08-11 07:52:02 +08:00
ASSERT_EQ(conf_doc_save_json(doc, &str), RET_OK);
2020-08-20 10:14:39 +08:00
ASSERT_STREQ(
str.str,
"{\n \"names\" : [\n \"jim\",\n \"tom\",\n \"anny\"\n ]\n}");
2020-08-11 07:52:02 +08:00
conf_doc_destroy(doc);
2020-10-18 12:17:55 +08:00
str_reset(&str);
2020-08-11 07:52:02 +08:00
}