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.
This commit is contained in:
Ahson Khan 2021-05-07 16:22:01 -07:00 committed by GitHub
parent 1a71c46cbd
commit 23136690af
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -456,3 +456,47 @@ TEST(Context, PreCondition)
ASSERT_DEATH(c2.TryGetValue<int>(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<std::string>(keyNotFound, strValue));
EXPECT_FALSE(c2.TryGetValue<int>(keyNotFound, intValue));
#if defined(NDEBUG)
// Release build won't provide assert msg
ASSERT_DEATH(c2.TryGetValue<std::string>(key, strValue), "");
#else
ASSERT_DEATH(
c2.TryGetValue<std::string>(key, strValue), "Type mismatch for Context::TryGetValue");
#endif
EXPECT_TRUE(strValue == "previous value");
EXPECT_TRUE(c2.TryGetValue<int>(key, intValue));
EXPECT_TRUE(intValue == 123);
#if defined(NDEBUG)
// Release build won't provide assert msg
ASSERT_DEATH(c3.TryGetValue<int>(key, intValue), "");
#else
ASSERT_DEATH(c3.TryGetValue<int>(key, intValue), "Type mismatch for Context::TryGetValue");
#endif
EXPECT_TRUE(intValue == 123);
EXPECT_TRUE(c3.TryGetValue<std::string>(key, strValue));
EXPECT_TRUE(strValue == s);
}