mirror of
https://gitee.com/zlgopen/awtk.git
synced 2024-11-30 19:18:53 +08:00
118 lines
3.2 KiB
C++
118 lines
3.2 KiB
C++
#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) {
|
|
tk_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);
|
|
TK_OBJECT_UNREF(obj);
|
|
}
|
|
|
|
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;
|
|
tk_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);
|
|
TK_OBJECT_UNREF(obj);
|
|
}
|
|
|
|
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);
|
|
}
|
|
|
|
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);
|
|
|
|
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);
|
|
}
|
|
|
|
#ifndef TKC_ONLY
|
|
TEST(AppConf, reset) {
|
|
app_conf_init_json("conf_test");
|
|
|
|
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);
|
|
|
|
ASSERT_EQ(app_conf_save(), RET_OK);
|
|
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);
|
|
}
|
|
#endif /*TKC_ONLY*/
|