Skip to content

Commit

Permalink
Merge pull request #1 from olegsych/develop
Browse files Browse the repository at this point in the history
v0.1.48. 
- Remove static fail variable and the need to include  implementation.cpp  in the NuGet package, which was always displayed by the projects consuming the package.
- Add Usage section to the  readme.md .
  • Loading branch information
olegsych committed Dec 30, 2015
2 parents 0e8f14e + 326d895 commit 7ed6277
Show file tree
Hide file tree
Showing 30 changed files with 619 additions and 673 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
*.*sdf
*.user
*.suo
*.VC.opendb
/ipch
/.vs
/packages
Expand Down
63 changes: 59 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,63 @@
# simply::com
# simply::assert

A small C++ library for asserting in unit tests.
A small C++ library for asserting in unit tests. It was born out of frustration with the CppUnitTest framework
used by the Visual Studio Native Unit Test projects and meant to replace its rudimentary [Assert](https://msdn.microsoft.com/en-us/library/hh694604.aspx)
class with a TDD-focused, rich, modern assertion APIs comparable to [Xunit.Assert](https://github.com/xunit/xunit/tree/master/src/xunit.assert)'s.

## building
## use

Add the [simply.assert](http://www.nuget.org/packages/simply.assert/) NuGet package to your Visual C++ Native Unit Test project
using the [Package Manager Console](http://docs.nuget.org/consume/package-manager-console) or
using the [Package Manager Dialog](http://docs.nuget.org/consume/Package-Manager-Dialog).

``` PowerShell
Install-Package simply.assert
```

Include the `simply/assert.h` header to your C++ file and use the `simply` namespace.

``` C++
#include <simply/assert.h>
using namespace simply;
```

Use assert functions.

``` C++
// fail (with overloads for std::string and std::wstring)
assert::fail("foo");

// bool
assert::is_true(true);
assert::is_false(false);

// equality
assert::is_equal(42, 42);
assert::is_not_equal(0, 42);

// exceptions
unique_ptr<exception> e = assert::throws<exception>([] { throw exception("foo"}; );

// null (with overloads for typed and void pointers)
assert::is_null(nullptr);
assert::is_not_null(reinterpret_cast<void*>(0x42));

// string (with overloads for string, wstring, char* and wchar_t*)
size_t position = assert::find("bar", "foo bar baz");
assert::is_equal("foo", "foo");
assert::is_not_equal("foo", "bar");

// type traits
assert::is_abstract<foo>();
assert::is_base_of<foo, bar>();
assert::is_concrete<foo>();
assert::is_copy_assignable<foo>();
assert::is_copy_constructible<foo>();
assert::is_destructible();
assert::is_same<foo, foo>();
```
## build
[![Build status](https://ci.appveyor.com/api/projects/status/github/olegsych/simply.assert?branch=master&retina=true)](https://ci.appveyor.com/project/olegsych/simply-assert/branch/master)
Expand All @@ -17,7 +72,7 @@ msbuild simply.assert.sln /p:Platform=x86
msbuild simply.assert.sln /p:Platform=x64
```
## testing
## test
From Visual Studio 2015:
- Select _Run_ / _All Tests_ from the _Test_ menu
Expand Down
2 changes: 1 addition & 1 deletion appveyor.yml
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,4 @@ deploy:

nuget:
project_feed: true
disable_publish_on_pr: true
account_feed: true
12 changes: 1 addition & 11 deletions simply.assert.sln
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 14
VisualStudioVersion = 14.0.23107.0
VisualStudioVersion = 14.0.24720.0
MinimumVisualStudioVersion = 10.0.40219.1
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{85735094-AFD5-44F8-92DC-99D4B1D3CC27}"
ProjectSection(SolutionItems) = preProject
Expand All @@ -20,26 +20,16 @@ Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|x64 = Debug|x64
Debug|x86 = Debug|x86
Release|x64 = Release|x64
Release|x86 = Release|x86
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{328B3C35-755F-4326-A504-443D3A481710}.Debug|x64.ActiveCfg = Debug|x64
{328B3C35-755F-4326-A504-443D3A481710}.Debug|x64.Build.0 = Debug|x64
{328B3C35-755F-4326-A504-443D3A481710}.Debug|x86.ActiveCfg = Debug|Win32
{328B3C35-755F-4326-A504-443D3A481710}.Debug|x86.Build.0 = Debug|Win32
{328B3C35-755F-4326-A504-443D3A481710}.Release|x64.ActiveCfg = Release|x64
{328B3C35-755F-4326-A504-443D3A481710}.Release|x64.Build.0 = Release|x64
{328B3C35-755F-4326-A504-443D3A481710}.Release|x86.ActiveCfg = Release|Win32
{328B3C35-755F-4326-A504-443D3A481710}.Release|x86.Build.0 = Release|Win32
{2E430331-FF8E-42FE-8165-94062FCE970F}.Debug|x64.ActiveCfg = Debug|x64
{2E430331-FF8E-42FE-8165-94062FCE970F}.Debug|x64.Build.0 = Debug|x64
{2E430331-FF8E-42FE-8165-94062FCE970F}.Debug|x86.ActiveCfg = Debug|Win32
{2E430331-FF8E-42FE-8165-94062FCE970F}.Debug|x86.Build.0 = Debug|Win32
{2E430331-FF8E-42FE-8165-94062FCE970F}.Release|x64.ActiveCfg = Release|x64
{2E430331-FF8E-42FE-8165-94062FCE970F}.Release|x64.Build.0 = Release|x64
{2E430331-FF8E-42FE-8165-94062FCE970F}.Release|x86.ActiveCfg = Release|Win32
{2E430331-FF8E-42FE-8165-94062FCE970F}.Release|x86.Build.0 = Release|Win32
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
6 changes: 0 additions & 6 deletions src/simply.assert.targets
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,4 @@
<AdditionalIncludeDirectories>$(MSBuildThisFileDirectory);$(VCInstallDir)UnitTest\include;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
</ClCompile>
</ItemDefinitionGroup>

<ItemGroup>
<ClCompile Include="$(MSBuildThisFileDirectory)\simply\assert\implementation.cpp">
<PrecompiledHeader>NotUsing</PrecompiledHeader>
</ClCompile>
</ItemGroup>
</Project>
21 changes: 13 additions & 8 deletions src/simply/assert/bool.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,17 @@

#include <string>
#include <simply/assert/fail.h>
#include <simply/assert/framework.h>

namespace simply { namespace assert
{
/// <summary>Verifies that the <paramref name="actual"/> value is <c>true</c>.</summary>
inline void is_true(bool actual)
template<typename framework = simply::assert::framework::default>
void is_true(bool actual)
{
if (!actual)
{
fail("Expected: true\nActual: false");
fail<framework>("Expected: true\nActual: false");
}
}

Expand All @@ -19,20 +21,22 @@ namespace simply { namespace assert
/// This function overloads <see cref="is_true(bool"/> to enable testing C-style <c>BOOL</c> values without
/// converting them to C++ <c>bool</c>, which triggers performance warning C4800 in Visual C++.
/// </remarks>
inline void is_true(int actual)
template<typename framework = simply::assert::framework::default>
void is_true(int actual)
{
if (!actual)
{
fail("Expected: TRUE(!0)\nActual: FALSE(0)");
fail<framework>("Expected: TRUE(!0)\nActual: FALSE(0)");
}
}

/// <summary>Verifies that the <paramref name="actual"/> value is <c>false</c>.</summary>
inline void is_false(bool actual)
template<typename framework = simply::assert::framework::default>
void is_false(bool actual)
{
if (actual)
{
fail("Expected: false\nActual: true");
fail<framework>("Expected: false\nActual: true");
}
}

Expand All @@ -41,11 +45,12 @@ namespace simply { namespace assert
/// This function overloads <see cref="is_false(bool"/> to enable testing C-style <c>BOOL</c> values without
/// converting them to C++ <c>bool</c>, which triggers performance warning C4800 in Visual C++.
/// </remarks>
inline void is_false(int actual)
template<typename framework = simply::assert::framework::default>
void is_false(int actual)
{
if (actual)
{
fail("Expected: FALSE(0)\nActual: TRUE(" + std::to_string(actual) + ")");
fail<framework>("Expected: FALSE(0)\nActual: TRUE(" + std::to_string(actual) + ")");
}
}
}}
9 changes: 5 additions & 4 deletions src/simply/assert/equality.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,31 @@

#include <sstream>
#include <simply/assert/fail.h>
#include <simply/assert/framework.h>

namespace simply { namespace assert
{
template<typename t>
template<typename t, typename framework = simply::assert::framework::default>
void is_equal(const t& expected, const t& actual)
{
if (!(expected == actual))
{
std::ostringstream message;
message << "Expected: <" << expected << ">\n";
message << "Actual: <" << actual << ">";
fail(message);
fail<framework>(message.str());
}
}

template<typename t>
template<typename t, typename framework = simply::assert::framework::default>
void is_not_equal(const t& expected, const t& actual)
{
if (expected == actual)
{
std::ostringstream message;
message << "Not expected: <" << expected << ">\n";
message << "Actual: <" << actual << ">";
fail(message);
fail<framework>(message.str());
}
}
}}
9 changes: 5 additions & 4 deletions src/simply/assert/exception.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@
#include <memory>
#include <sstream>
#include <simply/assert/fail.h>
#include <simply/utility/type_name.h>
#include <simply/assert/framework.h>
#include <simply/assert/implementation.h>

namespace simply { namespace assert
{
template<typename exception_t, typename functor_t>
template<typename exception_t, typename functor_t, typename framework = simply::assert::framework::default>
std::unique_ptr<exception_t> throws(functor_t&& functor)
{
std::exception_ptr actual_exception;
Expand All @@ -27,7 +28,7 @@ namespace simply { namespace assert
}

std::ostringstream message;
message << "Expected exception of type: <" << utility::type_name<exception_t>() << ">";
message << "Expected exception of type: <" << implementation::type_name<exception_t>() << ">";

if (actual_exception)
{
Expand All @@ -41,7 +42,7 @@ namespace simply { namespace assert
}
}

fail(message);
fail<framework>(message.str());
return std::unique_ptr<exception_t> { nullptr };
}
}}
14 changes: 5 additions & 9 deletions src/simply/assert/fail.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,21 @@
#include <codecvt>
#include <locale>
#include <string>
#include <simply/assert/implementation.h>
#include <simply/assert/framework.h>

namespace simply { namespace assert
{
template<typename framework = simply::assert::framework::default>
inline void fail(const std::string& message)
{
std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>> converter;
std::wstring wmessage = converter.from_bytes(message);
implementation::fail(wmessage);
fail<framework>(wmessage);
}

template<typename framework = framework::default>
inline void fail(const std::wstring& message)
{
implementation::fail(message);
}

template<class char_t>
inline void fail(const std::basic_ostringstream<char_t>& message)
{
fail(message.str());
framework::fail(message);
}
}}
18 changes: 18 additions & 0 deletions src/simply/assert/framework.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#pragma once

#include <string>
#include <CppUnitTest.h>

namespace simply { namespace assert { namespace framework
{
struct cpp_unit_test
{
static void fail(const std::wstring& message)
{
std::wstring separated_message { L"\n" + message };
Microsoft::VisualStudio::CppUnitTestFramework::Assert::Fail(separated_message.c_str());
}
};

using default = cpp_unit_test;
}}}
13 changes: 0 additions & 13 deletions src/simply/assert/implementation.cpp

This file was deleted.

34 changes: 30 additions & 4 deletions src/simply/assert/implementation.h
Original file line number Diff line number Diff line change
@@ -1,9 +1,35 @@
#pragma once

#include <functional>
#include <string>
#include <type_traits>

namespace simply { namespace assert { namespace implementation
namespace simply { namespace assert { namespace implementation
{
extern std::function<void(const std::wstring&)> fail;
}}}
template<typename t>
typename std::enable_if<!std::is_reference<t>::value, const std::string>::type
type_name()
{
std::string result { typeid(t).name() };

if (std::is_const<t>())
{
result.insert(0, "const ");
}

if (std::is_volatile<t>())
{
result.insert(0, "volatile ");
}

return result;
}

template<typename t>
typename std::enable_if<std::is_reference<t>::value, const std::string>::type
type_name()
{
std::string result = type_name<std::remove_reference<t>::type>();
result.append("&");
return result;
}
}}}
Loading

0 comments on commit 7ed6277

Please sign in to comment.