Skip to content

Commit

Permalink
Implement assert::is_convertible<from, to>(bool) in assert/type_traits.h
Browse files Browse the repository at this point in the history
  • Loading branch information
olegsych committed Jul 9, 2017
1 parent 79e41d4 commit 7e81055
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 0 deletions.
13 changes: 13 additions & 0 deletions src/simply/assert/type_traits.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,19 @@ namespace simply { namespace assert
}
}

template<typename from_t, typename to_t, typename framework = simply::assert::framework::default>
void is_convertible(bool expected = true)
{
if (expected != std::is_convertible<from_t, to_t>())
{
std::ostringstream message;
message << "Type <" << implementation::type_name<from_t>() << "> is ";
message << (expected ? "not " : "");
message << "convertible to type <" << implementation::type_name<to_t>() << ">";
fail<framework>(message.str());
}
}

template<typename actual_t, typename framework = simply::assert::framework::default>
void is_copy_assignable()
{
Expand Down
34 changes: 34 additions & 0 deletions test/type_traits_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,40 @@ namespace simply

#pragma endregion

#pragma region is_convertible<from_t, to_t>()

TEST_METHOD(is_convertible_succeeds_when_source_type_is_convertible_to_target_type)
{
assert::is_convertible<char*, void*, stub>();

Assert::AreEqual<size_t>(0, stub::output.length(), stub::output.c_str());
}

TEST_METHOD(is_convertible_fails_when_source_type_is_not_convertible_to_target_type)
{
assert::is_convertible<void*, char*, stub>();

Assert::AreNotEqual(wstring::npos, stub::output.find(L"Type <void *"), stub::output.c_str());
Assert::AreNotEqual(wstring::npos, stub::output.find(L"is not convertible to type <char *"), stub::output.c_str());
}

TEST_METHOD(is_convertible_succeeds_when_source_type_is_not_convertible_to_target_type_and_expected_is_false)
{
assert::is_convertible<void*, char*, stub>(false);

Assert::AreEqual<size_t>(0, stub::output.length(), stub::output.c_str());
}

TEST_METHOD(is_convertible_fails_when_source_type_is_convertible_to_target_type_and_expected_is_false)
{
assert::is_convertible<char*, void*, stub>(false);

Assert::AreNotEqual(wstring::npos, stub::output.find(L"Type <char *"), stub::output.c_str());
Assert::AreNotEqual(wstring::npos, stub::output.find(L"is convertible to type <void *"), stub::output.c_str());
}

#pragma endregion

#pragma region is_copy_assignable<actual_t>()

TEST_METHOD(is_copy_assignable_fails_when_type_is_not_copy_assignable)
Expand Down

0 comments on commit 7e81055

Please sign in to comment.