awtk/tests/app_conf_test.cc

116 lines
3.1 KiB
C++
Raw Normal View History

2020-06-07 09:50:36 +08:00
#include "gtest/gtest.h"
#include "conf_io/app_conf.h"
#include "conf_io/app_conf_init_ini.h"
#include "conf_io/app_conf_init_json.h"
#include "conf_io/app_conf_init_ubjson.h"
#include "tkc/object_default.h"
TEST(AppConf, basic) {
object_t* obj = object_default_create();
ASSERT_EQ(app_conf_set_instance(obj), RET_OK);
ASSERT_EQ(app_conf_set_int("int", 123), RET_OK);
ASSERT_EQ(app_conf_get_int("int", 0), 123);
ASSERT_EQ(app_conf_set_int64("int64", 1234567890), RET_OK);
ASSERT_EQ(app_conf_get_int64("int64", 0), 1234567890);
ASSERT_EQ(app_conf_set_double("double", 123), RET_OK);
ASSERT_EQ(app_conf_get_double("double", 0), 123);
ASSERT_EQ(app_conf_set_str("str", "123"), RET_OK);
ASSERT_STREQ(app_conf_get_str("str", NULL), "123");
ASSERT_EQ(app_conf_deinit(), RET_OK);
2020-10-18 12:17:55 +08:00
OBJECT_UNREF(obj);
2020-06-07 09:50:36 +08:00
}
static ret_t on_prop_changed(void* ctx, event_t* e) {
int32_t* count = (int32_t*)ctx;
prop_change_event_t* evt = prop_change_event_cast(e);
*count = *count + 1;
log_debug("prop %s changed\n", evt->name);
return RET_OK;
}
TEST(AppConf, event) {
int32_t count = 0;
object_t* obj = object_default_create();
ASSERT_EQ(app_conf_set_instance(obj), RET_OK);
ASSERT_NE(app_conf_on_changed(on_prop_changed, &count), TK_INVALID_ID);
ASSERT_EQ(app_conf_set_int("int", 123), RET_OK);
ASSERT_EQ(app_conf_set_int64("int64", 1234567890), RET_OK);
ASSERT_EQ(count, 2);
ASSERT_EQ(app_conf_off_changed_by_ctx(&count), RET_OK);
ASSERT_EQ(app_conf_set_double("double", 123), RET_OK);
ASSERT_EQ(app_conf_set_str("str", "123"), RET_OK);
ASSERT_EQ(count, 2);
ASSERT_EQ(app_conf_deinit(), RET_OK);
2020-10-18 12:17:55 +08:00
OBJECT_UNREF(obj);
2020-06-07 09:50:36 +08:00
}
TEST(AppConf, init) {
app_conf_init_ini("conf_test");
ASSERT_EQ(app_conf_set_int("int", 123), RET_OK);
ASSERT_EQ(app_conf_set_int64("int64", 1234567890), RET_OK);
ASSERT_EQ(app_conf_save(), RET_OK);
ASSERT_EQ(app_conf_deinit(), RET_OK);
}
TEST(AppConf, wstr) {
wchar_t str[32];
app_conf_init_ini("conf_test");
ASSERT_EQ(app_conf_set_wstr("wstr", L"abc"), RET_OK);
ASSERT_EQ(app_conf_get_wstr("wstr", str, ARRAY_SIZE(str)), RET_OK);
ASSERT_STREQ(app_conf_get_str("wstr", NULL), "abc");
ASSERT_EQ(app_conf_deinit(), RET_OK);
}
2020-06-14 17:49:37 +08:00
TEST(AppConf, reload) {
app_conf_init_ini("conf_test");
ASSERT_EQ(app_conf_set_int("int", 123), RET_OK);
ASSERT_EQ(app_conf_save(), RET_OK);
2020-06-15 11:16:16 +08:00
2020-06-14 17:49:37 +08:00
ASSERT_EQ(app_conf_set_int("int", 456), RET_OK);
ASSERT_EQ(app_conf_get_int("int", 0), 456);
ASSERT_EQ(app_conf_reload(), RET_OK);
ASSERT_EQ(app_conf_get_int("int", 0), 123);
ASSERT_EQ(app_conf_deinit(), RET_OK);
}
2021-01-14 23:08:44 +08:00
TEST(AppConf, reset) {
app_conf_init_json("conf_test");
2021-01-20 13:41:57 +08:00
2021-01-14 23:08:44 +08:00
ASSERT_STREQ(app_conf_get_str("name", NULL), "test");
ASSERT_EQ(app_conf_get_int("age", 0), 100);
ASSERT_EQ(app_conf_set_str("name", "awtk"), RET_OK);
ASSERT_EQ(app_conf_set_int("age", 200), RET_OK);
ASSERT_STREQ(app_conf_get_str("name", NULL), "awtk");
ASSERT_EQ(app_conf_get_int("age", 0), 200);
2021-01-14 23:13:06 +08:00
ASSERT_EQ(app_conf_save(), RET_OK);
2021-01-14 23:08:44 +08:00
ASSERT_EQ(app_conf_reset(), RET_OK);
ASSERT_STREQ(app_conf_get_str("name", NULL), "test");
ASSERT_EQ(app_conf_get_int("age", 0), 100);
ASSERT_EQ(app_conf_deinit(), RET_OK);
}