/****************************************************************************
 * This file is part of the project AqHome.
 * 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_dbwrite-t.h"
#include "vars_dbread.h"


#ifdef AQHOME_ENABLE_TESTCODE


/* ------------------------------------------------------------------------------------------------
 * forward declarations
 * ------------------------------------------------------------------------------------------------
 */

static int GWENHYWFAR_CB test1(GWEN_TEST_MODULE *mod);



/* ------------------------------------------------------------------------------------------------
 * implementations
 * ------------------------------------------------------------------------------------------------
 */

int AQH_Vars_DbWrite_AddTests(GWEN_TEST_MODULE *mod)
{
  GWEN_TEST_MODULE *newMod;

  newMod=GWEN_Test_Module_AddModule(mod, "AQH_Vars_DbWrite", NULL);

  GWEN_Test_Module_AddTest(newMod, "test 1: read, write and read groups and vars", test1, NULL);

  return 0;
}



int GWENHYWFAR_CB test1(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=\"s%65condValue\"\n"
    "  }\n"
    "}";
  const char *s;
  GWEN_BUFFER *obuf;
  int rv;
  int i;

  vtRoot=AQH_Vars_ReadDbFormat(testData);
  if (vtRoot==NULL) {
    DBG_ERROR(AQH_LOGDOMAIN, "Error reading dbformat");
    return GWEN_ERROR_GENERIC;
  }

  obuf=GWEN_Buffer_new(0, 256, 0, 1);
  rv=AQH_Vars_WriteDbFormat(vtRoot, obuf);
  if (rv<0) {
    GWEN_Buffer_free(obuf);
    AQH_Vars_free(vtRoot);
    return rv;
  }

  vtRoot=AQH_Vars_ReadDbFormat(GWEN_Buffer_GetStart(obuf));
  if (vtRoot==NULL) {
    DBG_ERROR(AQH_LOGDOMAIN, "Error reading dbformat");
    GWEN_Buffer_free(obuf);
    return GWEN_ERROR_GENERIC;
  }
  GWEN_Buffer_free(obuf);

  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







