From a8773a7ba197454c874287d9f30107c6aeb5556d Mon Sep 17 00:00:00 2001 From: Magnus Date: Thu, 19 Jan 2023 12:55:46 +0100 Subject: [PATCH] Fixed templating bug and update test cases --- platformio.ini | 5 +++-- src/templating.cpp | 13 +++++++++++++ src/templating.hpp | 4 +++- test/tests_templating.cpp | 22 ++++++++++++++++++++++ 4 files changed, 41 insertions(+), 3 deletions(-) diff --git a/platformio.ini b/platformio.ini index ca02814..2dbb3ab 100644 --- a/platformio.ini +++ b/platformio.ini @@ -39,7 +39,7 @@ build_flags = -DUSE_LITTLEFS=true -DUSER_SSID=\""\"" # =\""myssid\"" -DUSER_SSID_PWD=\""\"" # =\""mypwd\"" - -DCFG_APPVER="\"1.2.0\"" + -DCFG_APPVER="\"1.2.1\"" #-DCFG_GITREV=\""beta-3\"" !python script/git_rev.py lib_deps = @@ -120,9 +120,10 @@ lib_deps = https://github.com/bxparks/AUnit#v1.6.1 ${common_env_data.lib_deps} board = ${common_env_data.board} -build_type = release +build_type = debug board_build.filesystem = littlefs build_src_filter = +<*> - +<../test/tests*.cpp> +monitor_filters = esp8266_exception_decoder [env:gravity32-release] framework = ${common_env_data.framework} diff --git a/src/templating.cpp b/src/templating.cpp index 5a0b65a..0ed0741 100644 --- a/src/templating.cpp +++ b/src/templating.cpp @@ -194,4 +194,17 @@ const char* TemplatingEngine::create(TemplatingEngine::Templates idx, return ""; } +// added to support more unit test scenarios. +const char* TemplatingEngine::create(const char* formatTemplate) { + _baseTemplate = String(formatTemplate); + + // Insert data into template. + transform(); + _baseTemplate.clear(); + + if (_output) return _output; + + return ""; +} + // EOF diff --git a/src/templating.hpp b/src/templating.hpp index f6f0bac..349b8c5 100644 --- a/src/templating.hpp +++ b/src/templating.hpp @@ -160,7 +160,8 @@ class TemplatingEngine { } } } - strncat(_output, format + k, size - k); + // strncat(_output, format + k, size - k); + strncat(_output, format + k, strlen(format + k)); Log.notice(F("TPL : Transformed template %d chars to %d chars" CR), strlen(format), strlen(_output)); @@ -202,6 +203,7 @@ class TemplatingEngine { float tempC, float runTime); const char *create(TemplatingEngine::Templates idx, bool useDefaultTemplate = false); + const char *create(const char *formatTemplate); }; #endif // SRC_TEMPLATING_HPP_ diff --git a/test/tests_templating.cpp b/test/tests_templating.cpp index 8168ea3..ba69c8f 100644 --- a/test/tests_templating.cpp +++ b/test/tests_templating.cpp @@ -125,4 +125,26 @@ test(template_applyTemplate5) { assertEqual(s, v); } +test(template_applyTemplate6) { + TemplatingEngine e; + char buffer[20]; + myConfig.setMDNS("gravitymon"); + + const char* tpl = + "Densite1${gravity}" + "Batterie1${battery}" + "Temperature1${temp}"; + + e.initialize(45.0, 1.123, 1.223, 21.2, 2.98); + String s = e.create(tpl); + String batt = + convertFloatToString(myBatteryVoltage.getVoltage(), &buffer[0], 2); + batt.trim(); + + String v = "Densite11.1230" + "Batterie1" + batt + "" + "Temperature121.2"; + assertEqual(s, v); +} + // EOF