Utils: Fix more than one template replace
There is a bug in templateCharReplace function that only the first
template gets replaced correctly and additional templates are left
unchanged.
Add a new test case twoReplacementsWithMath2 to show the issue.
Difference with the existing passing test case is key string
"ADDRESS" is less than "BAR".
nlohmann::json j = {{"foo", "4 / 2 equals $ADDRESS / 2 $BAR"}};
data["ADDRESS"] = 4;
data["BAR"] = std::string("bar");
With this change the new test case passes.
Tested:
All unit tests passed.
Signed-off-by: Zhikui Ren <zhikui.ren@intel.com>
Change-Id: I121ec8c0fcfce645f52518ed4429228549ff9371
diff --git a/src/Utils.cpp b/src/Utils.cpp
index f859eb6..48c600a 100644
--- a/src/Utils.cpp
+++ b/src/Utils.cpp
@@ -373,9 +373,13 @@
}
keyPair.value() = result;
- // We probably just invalidated the pointer above, so set it to null
- strPtr = nullptr;
- break;
+ // We probably just invalidated the pointer abovei,
+ // reset and continue to handle multiple templates
+ strPtr = keyPair.value().get_ptr<std::string*>();
+ if (strPtr == nullptr)
+ {
+ break;
+ }
}
}
diff --git a/test/test_entity-manager.cpp b/test/test_entity-manager.cpp
index 1464774..deaf06c 100644
--- a/test/test_entity-manager.cpp
+++ b/test/test_entity-manager.cpp
@@ -178,6 +178,19 @@
templateCharReplace(it, data, 0);
nlohmann::json expected = "4 / 2 equals 2 bar";
+}
+
+TEST(TemplateCharReplace, twoReplacementsWithMath2)
+{
+ nlohmann::json j = {{"foo", "4 / 2 equals $ADDRESS / 2 $BAR"}};
+ auto it = j.begin();
+ boost::container::flat_map<std::string, BasicVariantType> data;
+ data["ADDRESS"] = 4;
+ data["BAR"] = std::string("bar");
+
+ templateCharReplace(it, data, 0);
+
+ nlohmann::json expected = "4 / 2 equals 2 bar";
EXPECT_EQ(expected, j["foo"]);
}