From 540cea831c11b5680454a148276388fd5e201a84 Mon Sep 17 00:00:00 2001 From: Abhinav Pandey <79640544+pixelcaliber@users.noreply.github.com> Date: Mon, 15 Jul 2024 23:27:54 +0530 Subject: [PATCH] test: enhance code coverage of format helper module by adding additional testcases (#802) * add missing testcases for word.cpp for improving coverage * add missing testcases for format_helper module to improve coverage --- tests/common/format_helper_test.cpp | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/tests/common/format_helper_test.cpp b/tests/common/format_helper_test.cpp index 20871114a..2b5e314ff 100644 --- a/tests/common/format_helper_test.cpp +++ b/tests/common/format_helper_test.cpp @@ -48,3 +48,32 @@ TEST_F(FormatHelperTest, shouldFormat) EXPECT_EQ(format("{} {}", "Hello", "World"), "Hello World"); EXPECT_EQ(format("{0} {1}", "Hello", "World"), "Hello World"); } + +TEST_F(FormatHelperTest, ThrowsExceptionForMissingTokenGenerator) +{ + const auto format = "{existing} {missing}"; + const auto dataGeneratorsMapping = std::unordered_map>{ + {"existing", []() { return "value"; }} + }; + + try { + fillTokenValues(format, dataGeneratorsMapping); + FAIL() << "Expected std::runtime_error"; + } + catch (const std::runtime_error& e) { + EXPECT_STREQ(e.what(), "Generator not found for token missing."); + } +} + +TEST_F(FormatHelperTest, PrecisionFormat) +{ + EXPECT_EQ(precisionFormat(Precision::ZeroDp, 3.14159), "3"); + EXPECT_EQ(precisionFormat(Precision::OneDp, 3.14159), "3.1"); + EXPECT_EQ(precisionFormat(Precision::FiveDp, 3.14159), "3.14159"); + EXPECT_EQ(precisionFormat(Precision::SevenDp, 3.14159), "3.1415900"); +} + +TEST_F(FormatHelperTest, ThrowsForInvalidPrecision) +{ + EXPECT_THROW(precisionFormat(static_cast(999), 3.14159), std::invalid_argument); +} \ No newline at end of file