vars: added code to read AQH_VARS from GWEN_DB strings.
This commit is contained in:
207
aqhome/data/vars_dbread-t.c
Normal file
207
aqhome/data/vars_dbread-t.c
Normal file
@@ -0,0 +1,207 @@
|
||||
/****************************************************************************
|
||||
* This file is part of the project Gwenhywfar.
|
||||
* AqHome (c) by 2024 Martin Preuss, all rights reserved.
|
||||
*
|
||||
* The license for this file can be found in the file COPYING which you
|
||||
* should have received along with this file.
|
||||
****************************************************************************/
|
||||
|
||||
/* This file is included by "vars_dbread.c" */
|
||||
|
||||
|
||||
#include <gwenhywfar/testframework.h>
|
||||
#include "vars_dbread-t.h"
|
||||
|
||||
|
||||
#ifdef AQHOME_ENABLE_TESTCODE
|
||||
|
||||
|
||||
/* ------------------------------------------------------------------------------------------------
|
||||
* forward declarations
|
||||
* ------------------------------------------------------------------------------------------------
|
||||
*/
|
||||
|
||||
static int GWENHYWFAR_CB test1(GWEN_TEST_MODULE *mod);
|
||||
static int GWENHYWFAR_CB test2(GWEN_TEST_MODULE *mod);
|
||||
static int GWENHYWFAR_CB test3(GWEN_TEST_MODULE *mod);
|
||||
static int GWENHYWFAR_CB test4(GWEN_TEST_MODULE *mod);
|
||||
|
||||
|
||||
|
||||
/* ------------------------------------------------------------------------------------------------
|
||||
* implementations
|
||||
* ------------------------------------------------------------------------------------------------
|
||||
*/
|
||||
|
||||
int AQH_Vars_DbRead_AddTests(GWEN_TEST_MODULE *mod)
|
||||
{
|
||||
GWEN_TEST_MODULE *newMod;
|
||||
|
||||
newMod=GWEN_Test_Module_AddModule(mod, "AQH_Vars_DbRead", NULL);
|
||||
|
||||
GWEN_Test_Module_AddTest(newMod, "test 1: read group", test1, NULL);
|
||||
GWEN_Test_Module_AddTest(newMod, "test 2: read var", test2, NULL);
|
||||
GWEN_Test_Module_AddTest(newMod, "test 3: read group and var", test3, NULL);
|
||||
GWEN_Test_Module_AddTest(newMod, "test 4: read multi groups and vars", test4, NULL);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
|
||||
int GWENHYWFAR_CB test1(GWEN_UNUSED GWEN_TEST_MODULE *mod)
|
||||
{
|
||||
AQH_VARS *vtRoot;
|
||||
const char *testData="testGroup1 {\n}\n";
|
||||
|
||||
vtRoot=AQH_Vars_ReadDbFormat(testData);
|
||||
if (vtRoot==NULL) {
|
||||
DBG_ERROR(AQH_LOGDOMAIN, "Error reading dbformat");
|
||||
return GWEN_ERROR_GENERIC;
|
||||
}
|
||||
|
||||
if (NULL==AQH_Vars_GetGroup(vtRoot, "testGroup1", AQH_PATH_FLAGS_PATHMUSTEXIST)) {
|
||||
DBG_ERROR(AQH_LOGDOMAIN, "Freshly read group \"testGroup1\" not found");
|
||||
AQH_Vars_free(vtRoot);
|
||||
return GWEN_ERROR_GENERIC;
|
||||
}
|
||||
|
||||
AQH_Vars_free(vtRoot);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
|
||||
int GWENHYWFAR_CB test2(GWEN_UNUSED GWEN_TEST_MODULE *mod)
|
||||
{
|
||||
AQH_VARS *vtRoot;
|
||||
const char *testData="char testVar1=\"value1\"\n";
|
||||
const char *s;
|
||||
|
||||
vtRoot=AQH_Vars_ReadDbFormat(testData);
|
||||
if (vtRoot==NULL) {
|
||||
DBG_ERROR(AQH_LOGDOMAIN, "Error reading dbformat");
|
||||
return GWEN_ERROR_GENERIC;
|
||||
}
|
||||
|
||||
s=AQH_Vars_GetCharValue(vtRoot, "testVar1", 0, NULL);
|
||||
if (!(s && strcasecmp(s, "value1")==0)) {
|
||||
DBG_ERROR(AQH_LOGDOMAIN, "Freshly read var \"testVar1\" with bad value (%s)", s?s:"<empty>");
|
||||
AQH_Vars_free(vtRoot);
|
||||
return GWEN_ERROR_GENERIC;
|
||||
}
|
||||
|
||||
AQH_Vars_free(vtRoot);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
|
||||
int GWENHYWFAR_CB test3(GWEN_UNUSED GWEN_TEST_MODULE *mod)
|
||||
{
|
||||
AQH_VARS *vtRoot;
|
||||
const char *testData=
|
||||
"testGroup1 {\n"
|
||||
" char testVar1=\"value1\"\n"
|
||||
"}";
|
||||
const char *s;
|
||||
|
||||
vtRoot=AQH_Vars_ReadDbFormat(testData);
|
||||
if (vtRoot==NULL) {
|
||||
DBG_ERROR(AQH_LOGDOMAIN, "Error reading dbformat");
|
||||
return GWEN_ERROR_GENERIC;
|
||||
}
|
||||
|
||||
if (NULL==AQH_Vars_GetGroup(vtRoot, "testGroup1", AQH_PATH_FLAGS_PATHMUSTEXIST)) {
|
||||
DBG_ERROR(AQH_LOGDOMAIN, "Freshly read group \"testGroup1\" not found");
|
||||
AQH_Vars_free(vtRoot);
|
||||
return GWEN_ERROR_GENERIC;
|
||||
}
|
||||
|
||||
s=AQH_Vars_GetCharValue(vtRoot, "testGroup1/testVar1", 0, NULL);
|
||||
if (!(s && strcasecmp(s, "value1")==0)) {
|
||||
DBG_ERROR(AQH_LOGDOMAIN, "Freshly read var \"testVar1\" with bad value (%s)", s?s:"<empty>");
|
||||
AQH_Vars_free(vtRoot);
|
||||
return GWEN_ERROR_GENERIC;
|
||||
}
|
||||
|
||||
AQH_Vars_free(vtRoot);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
|
||||
int GWENHYWFAR_CB test4(GWEN_UNUSED GWEN_TEST_MODULE *mod)
|
||||
{
|
||||
AQH_VARS *vtRoot;
|
||||
const char *testData=
|
||||
"testGroup1 {\n"
|
||||
" char testVar1=\"value1\"\n"
|
||||
" secondGroup { # here starts the 2nd group\n"
|
||||
"# this is the second comment\n"
|
||||
" int firstVarOfSecondGroup=\"1234\";\n"
|
||||
" secondVarOfSecondGroup=\"secondValue\"\n"
|
||||
" }\n"
|
||||
"}";
|
||||
const char *s;
|
||||
int i;
|
||||
|
||||
vtRoot=AQH_Vars_ReadDbFormat(testData);
|
||||
if (vtRoot==NULL) {
|
||||
DBG_ERROR(AQH_LOGDOMAIN, "Error reading dbformat");
|
||||
return GWEN_ERROR_GENERIC;
|
||||
}
|
||||
|
||||
if (NULL==AQH_Vars_GetGroup(vtRoot, "testGroup1", AQH_PATH_FLAGS_PATHMUSTEXIST)) {
|
||||
DBG_ERROR(AQH_LOGDOMAIN, "Freshly read group \"testGroup1\" not found");
|
||||
AQH_Vars_free(vtRoot);
|
||||
return GWEN_ERROR_GENERIC;
|
||||
}
|
||||
|
||||
s=AQH_Vars_GetCharValue(vtRoot, "testGroup1/testVar1", 0, NULL);
|
||||
if (!(s && strcasecmp(s, "value1")==0)) {
|
||||
DBG_ERROR(AQH_LOGDOMAIN, "Freshly read var \"testVar1\" with bad value (%s)", s?s:"<empty>");
|
||||
AQH_Vars_free(vtRoot);
|
||||
return GWEN_ERROR_GENERIC;
|
||||
}
|
||||
|
||||
i=AQH_Vars_GetIntValue(vtRoot, "testGroup1/secondGroup/firstVarOfSecondGroup", 0, -1);
|
||||
if (i!=1234) {
|
||||
DBG_ERROR(AQH_LOGDOMAIN, "Freshly read var \"testGroup1/secondGroup/firstVarOfSecondGroup\" with bad value (%i)", i);
|
||||
AQH_Vars_free(vtRoot);
|
||||
return GWEN_ERROR_GENERIC;
|
||||
}
|
||||
|
||||
s=AQH_Vars_GetCharValue(vtRoot, "testGroup1/secondGroup/secondVarOfSecondGroup", 0, NULL);
|
||||
if (!(s && strcasecmp(s, "secondValue")==0)) {
|
||||
DBG_ERROR(AQH_LOGDOMAIN, "Freshly read var \"testGroup1/secondGroup/secondVarOfSecondGroup\" with bad value (%s)", s?s:"<empty>");
|
||||
AQH_Vars_free(vtRoot);
|
||||
return GWEN_ERROR_GENERIC;
|
||||
}
|
||||
|
||||
AQH_Vars_free(vtRoot);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
#else
|
||||
|
||||
int AQH_Vars_DbRead_AddTests(GWEN_TEST_MODULE *mod)
|
||||
{
|
||||
DBG_ERROR(GWEN_LOGDOMAIN, "AqHome was compiled without test code enabled.");
|
||||
return GWEN_ERROR_GENERIC;
|
||||
}
|
||||
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user