Skip to content

Commit 7ed6277

Browse files
committed
Merge pull request #1 from olegsych/develop
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 .
2 parents 0e8f14e + 326d895 commit 7ed6277

30 files changed

+619
-673
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
*.*sdf
33
*.user
44
*.suo
5+
*.VC.opendb
56
/ipch
67
/.vs
78
/packages

README.md

Lines changed: 59 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,63 @@
1-
# simply::com
1+
# simply::assert
22

3-
A small C++ library for asserting in unit tests.
3+
A small C++ library for asserting in unit tests. It was born out of frustration with the CppUnitTest framework
4+
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)
5+
class with a TDD-focused, rich, modern assertion APIs comparable to [Xunit.Assert](https://github.com/xunit/xunit/tree/master/src/xunit.assert)'s.
46

5-
## building
7+
## use
8+
9+
Add the [simply.assert](http://www.nuget.org/packages/simply.assert/) NuGet package to your Visual C++ Native Unit Test project
10+
using the [Package Manager Console](http://docs.nuget.org/consume/package-manager-console) or
11+
using the [Package Manager Dialog](http://docs.nuget.org/consume/Package-Manager-Dialog).
12+
13+
``` PowerShell
14+
Install-Package simply.assert
15+
```
16+
17+
Include the `simply/assert.h` header to your C++ file and use the `simply` namespace.
18+
19+
``` C++
20+
#include <simply/assert.h>
21+
using namespace simply;
22+
```
23+
24+
Use assert functions.
25+
26+
``` C++
27+
// fail (with overloads for std::string and std::wstring)
28+
assert::fail("foo");
29+
30+
// bool
31+
assert::is_true(true);
32+
assert::is_false(false);
33+
34+
// equality
35+
assert::is_equal(42, 42);
36+
assert::is_not_equal(0, 42);
37+
38+
// exceptions
39+
unique_ptr<exception> e = assert::throws<exception>([] { throw exception("foo"}; );
40+
41+
// null (with overloads for typed and void pointers)
42+
assert::is_null(nullptr);
43+
assert::is_not_null(reinterpret_cast<void*>(0x42));
44+
45+
// string (with overloads for string, wstring, char* and wchar_t*)
46+
size_t position = assert::find("bar", "foo bar baz");
47+
assert::is_equal("foo", "foo");
48+
assert::is_not_equal("foo", "bar");
49+
50+
// type traits
51+
assert::is_abstract<foo>();
52+
assert::is_base_of<foo, bar>();
53+
assert::is_concrete<foo>();
54+
assert::is_copy_assignable<foo>();
55+
assert::is_copy_constructible<foo>();
56+
assert::is_destructible();
57+
assert::is_same<foo, foo>();
58+
```
59+
60+
## build
661
762
[![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)
863
@@ -17,7 +72,7 @@ msbuild simply.assert.sln /p:Platform=x86
1772
msbuild simply.assert.sln /p:Platform=x64
1873
```
1974
20-
## testing
75+
## test
2176
2277
From Visual Studio 2015:
2378
- Select _Run_ / _All Tests_ from the _Test_ menu

appveyor.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,4 +42,4 @@ deploy:
4242

4343
nuget:
4444
project_feed: true
45-
disable_publish_on_pr: true
45+
account_feed: true

simply.assert.sln

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11

22
Microsoft Visual Studio Solution File, Format Version 12.00
33
# Visual Studio 14
4-
VisualStudioVersion = 14.0.23107.0
4+
VisualStudioVersion = 14.0.24720.0
55
MinimumVisualStudioVersion = 10.0.40219.1
66
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{85735094-AFD5-44F8-92DC-99D4B1D3CC27}"
77
ProjectSection(SolutionItems) = preProject
@@ -20,26 +20,16 @@ Global
2020
GlobalSection(SolutionConfigurationPlatforms) = preSolution
2121
Debug|x64 = Debug|x64
2222
Debug|x86 = Debug|x86
23-
Release|x64 = Release|x64
24-
Release|x86 = Release|x86
2523
EndGlobalSection
2624
GlobalSection(ProjectConfigurationPlatforms) = postSolution
2725
{328B3C35-755F-4326-A504-443D3A481710}.Debug|x64.ActiveCfg = Debug|x64
2826
{328B3C35-755F-4326-A504-443D3A481710}.Debug|x64.Build.0 = Debug|x64
2927
{328B3C35-755F-4326-A504-443D3A481710}.Debug|x86.ActiveCfg = Debug|Win32
3028
{328B3C35-755F-4326-A504-443D3A481710}.Debug|x86.Build.0 = Debug|Win32
31-
{328B3C35-755F-4326-A504-443D3A481710}.Release|x64.ActiveCfg = Release|x64
32-
{328B3C35-755F-4326-A504-443D3A481710}.Release|x64.Build.0 = Release|x64
33-
{328B3C35-755F-4326-A504-443D3A481710}.Release|x86.ActiveCfg = Release|Win32
34-
{328B3C35-755F-4326-A504-443D3A481710}.Release|x86.Build.0 = Release|Win32
3529
{2E430331-FF8E-42FE-8165-94062FCE970F}.Debug|x64.ActiveCfg = Debug|x64
3630
{2E430331-FF8E-42FE-8165-94062FCE970F}.Debug|x64.Build.0 = Debug|x64
3731
{2E430331-FF8E-42FE-8165-94062FCE970F}.Debug|x86.ActiveCfg = Debug|Win32
3832
{2E430331-FF8E-42FE-8165-94062FCE970F}.Debug|x86.Build.0 = Debug|Win32
39-
{2E430331-FF8E-42FE-8165-94062FCE970F}.Release|x64.ActiveCfg = Release|x64
40-
{2E430331-FF8E-42FE-8165-94062FCE970F}.Release|x64.Build.0 = Release|x64
41-
{2E430331-FF8E-42FE-8165-94062FCE970F}.Release|x86.ActiveCfg = Release|Win32
42-
{2E430331-FF8E-42FE-8165-94062FCE970F}.Release|x86.Build.0 = Release|Win32
4333
EndGlobalSection
4434
GlobalSection(SolutionProperties) = preSolution
4535
HideSolutionNode = FALSE

src/simply.assert.targets

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,4 @@
55
<AdditionalIncludeDirectories>$(MSBuildThisFileDirectory);$(VCInstallDir)UnitTest\include;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
66
</ClCompile>
77
</ItemDefinitionGroup>
8-
9-
<ItemGroup>
10-
<ClCompile Include="$(MSBuildThisFileDirectory)\simply\assert\implementation.cpp">
11-
<PrecompiledHeader>NotUsing</PrecompiledHeader>
12-
</ClCompile>
13-
</ItemGroup>
148
</Project>

src/simply/assert/bool.h

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,17 @@
22

33
#include <string>
44
#include <simply/assert/fail.h>
5+
#include <simply/assert/framework.h>
56

67
namespace simply { namespace assert
78
{
89
/// <summary>Verifies that the <paramref name="actual"/> value is <c>true</c>.</summary>
9-
inline void is_true(bool actual)
10+
template<typename framework = simply::assert::framework::default>
11+
void is_true(bool actual)
1012
{
1113
if (!actual)
1214
{
13-
fail("Expected: true\nActual: false");
15+
fail<framework>("Expected: true\nActual: false");
1416
}
1517
}
1618

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

3033
/// <summary>Verifies that the <paramref name="actual"/> value is <c>false</c>.</summary>
31-
inline void is_false(bool actual)
34+
template<typename framework = simply::assert::framework::default>
35+
void is_false(bool actual)
3236
{
3337
if (actual)
3438
{
35-
fail("Expected: false\nActual: true");
39+
fail<framework>("Expected: false\nActual: true");
3640
}
3741
}
3842

@@ -41,11 +45,12 @@ namespace simply { namespace assert
4145
/// This function overloads <see cref="is_false(bool"/> to enable testing C-style <c>BOOL</c> values without
4246
/// converting them to C++ <c>bool</c>, which triggers performance warning C4800 in Visual C++.
4347
/// </remarks>
44-
inline void is_false(int actual)
48+
template<typename framework = simply::assert::framework::default>
49+
void is_false(int actual)
4550
{
4651
if (actual)
4752
{
48-
fail("Expected: FALSE(0)\nActual: TRUE(" + std::to_string(actual) + ")");
53+
fail<framework>("Expected: FALSE(0)\nActual: TRUE(" + std::to_string(actual) + ")");
4954
}
5055
}
5156
}}

src/simply/assert/equality.h

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,30 +2,31 @@
22

33
#include <sstream>
44
#include <simply/assert/fail.h>
5+
#include <simply/assert/framework.h>
56

67
namespace simply { namespace assert
78
{
8-
template<typename t>
9+
template<typename t, typename framework = simply::assert::framework::default>
910
void is_equal(const t& expected, const t& actual)
1011
{
1112
if (!(expected == actual))
1213
{
1314
std::ostringstream message;
1415
message << "Expected: <" << expected << ">\n";
1516
message << "Actual: <" << actual << ">";
16-
fail(message);
17+
fail<framework>(message.str());
1718
}
1819
}
1920

20-
template<typename t>
21+
template<typename t, typename framework = simply::assert::framework::default>
2122
void is_not_equal(const t& expected, const t& actual)
2223
{
2324
if (expected == actual)
2425
{
2526
std::ostringstream message;
2627
message << "Not expected: <" << expected << ">\n";
2728
message << "Actual: <" << actual << ">";
28-
fail(message);
29+
fail<framework>(message.str());
2930
}
3031
}
3132
}}

src/simply/assert/exception.h

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,12 @@
44
#include <memory>
55
#include <sstream>
66
#include <simply/assert/fail.h>
7-
#include <simply/utility/type_name.h>
7+
#include <simply/assert/framework.h>
8+
#include <simply/assert/implementation.h>
89

910
namespace simply { namespace assert
1011
{
11-
template<typename exception_t, typename functor_t>
12+
template<typename exception_t, typename functor_t, typename framework = simply::assert::framework::default>
1213
std::unique_ptr<exception_t> throws(functor_t&& functor)
1314
{
1415
std::exception_ptr actual_exception;
@@ -27,7 +28,7 @@ namespace simply { namespace assert
2728
}
2829

2930
std::ostringstream message;
30-
message << "Expected exception of type: <" << utility::type_name<exception_t>() << ">";
31+
message << "Expected exception of type: <" << implementation::type_name<exception_t>() << ">";
3132

3233
if (actual_exception)
3334
{
@@ -41,7 +42,7 @@ namespace simply { namespace assert
4142
}
4243
}
4344

44-
fail(message);
45+
fail<framework>(message.str());
4546
return std::unique_ptr<exception_t> { nullptr };
4647
}
4748
}}

src/simply/assert/fail.h

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,25 +3,21 @@
33
#include <codecvt>
44
#include <locale>
55
#include <string>
6-
#include <simply/assert/implementation.h>
6+
#include <simply/assert/framework.h>
77

88
namespace simply { namespace assert
99
{
10+
template<typename framework = simply::assert::framework::default>
1011
inline void fail(const std::string& message)
1112
{
1213
std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>> converter;
1314
std::wstring wmessage = converter.from_bytes(message);
14-
implementation::fail(wmessage);
15+
fail<framework>(wmessage);
1516
}
1617

18+
template<typename framework = framework::default>
1719
inline void fail(const std::wstring& message)
1820
{
19-
implementation::fail(message);
20-
}
21-
22-
template<class char_t>
23-
inline void fail(const std::basic_ostringstream<char_t>& message)
24-
{
25-
fail(message.str());
21+
framework::fail(message);
2622
}
2723
}}

src/simply/assert/framework.h

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#pragma once
2+
3+
#include <string>
4+
#include <CppUnitTest.h>
5+
6+
namespace simply { namespace assert { namespace framework
7+
{
8+
struct cpp_unit_test
9+
{
10+
static void fail(const std::wstring& message)
11+
{
12+
std::wstring separated_message { L"\n" + message };
13+
Microsoft::VisualStudio::CppUnitTestFramework::Assert::Fail(separated_message.c_str());
14+
}
15+
};
16+
17+
using default = cpp_unit_test;
18+
}}}

0 commit comments

Comments
 (0)