From 23136690afd8d5d776b1a503c5ad13d68c0b65fe Mon Sep 17 00:00:00 2001 From: Ahson Khan Date: Fri, 7 May 2021 16:22:01 -0700 Subject: [PATCH] Add a key type pair context precondition test. (#2223) * Add a key type pair context precondition test. * Fixup test since after overriding the key type getting an int value should fail. --- sdk/core/azure-core/test/ut/context.cpp | 44 +++++++++++++++++++++++++ 1 file changed, 44 insertions(+) diff --git a/sdk/core/azure-core/test/ut/context.cpp b/sdk/core/azure-core/test/ut/context.cpp index 6240baf33..2e71f06ef 100644 --- a/sdk/core/azure-core/test/ut/context.cpp +++ b/sdk/core/azure-core/test/ut/context.cpp @@ -456,3 +456,47 @@ TEST(Context, PreCondition) ASSERT_DEATH(c2.TryGetValue(key, value), "Type mismatch for Context::TryGetValue"); #endif } + +TEST(Context, KeyTypePairPrecondition) +{ + Context context; + Context::Key const key; + Context::Key const keyNotFound; + + std::string s("Test String"); + + // New context from previous + auto c2 = context.WithValue(key, 123); + auto c3 = c2.WithValue(key, s); + + int intValue = -1; + std::string strValue = "previous value"; + + EXPECT_FALSE(c2.TryGetValue(keyNotFound, strValue)); + EXPECT_FALSE(c2.TryGetValue(keyNotFound, intValue)); + +#if defined(NDEBUG) + // Release build won't provide assert msg + ASSERT_DEATH(c2.TryGetValue(key, strValue), ""); +#else + ASSERT_DEATH( + c2.TryGetValue(key, strValue), "Type mismatch for Context::TryGetValue"); +#endif + + EXPECT_TRUE(strValue == "previous value"); + + EXPECT_TRUE(c2.TryGetValue(key, intValue)); + EXPECT_TRUE(intValue == 123); + +#if defined(NDEBUG) + // Release build won't provide assert msg + ASSERT_DEATH(c3.TryGetValue(key, intValue), ""); +#else + ASSERT_DEATH(c3.TryGetValue(key, intValue), "Type mismatch for Context::TryGetValue"); +#endif + + EXPECT_TRUE(intValue == 123); + + EXPECT_TRUE(c3.TryGetValue(key, strValue)); + EXPECT_TRUE(strValue == s); +}