-
Notifications
You must be signed in to change notification settings - Fork 266
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Plugging in Assertion extensibility (#117)
* Plugged in Assertion extensibility as defined by https://github.com/Microsoft/testfx-docs/blob/master/RFCs/002-Framework-Extensibility-Custom-Assertions.md. * Added an E2E test as well. * Fixing a doc typo.
- Loading branch information
1 parent
787b06c
commit 9192d83
Showing
18 changed files
with
608 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT license. See LICENSE file in the project root for full license information. | ||
|
||
namespace MSTest.Extensibility.Samples | ||
{ | ||
using Microsoft.VisualStudio.TestTools.UnitTesting; | ||
|
||
public static class AssertEx | ||
{ | ||
private static AssertIs assertis; | ||
|
||
/// <summary> | ||
/// A simple assert extension to validate if an object is of a given type. | ||
/// </summary> | ||
/// <typeparam name="T">The type to check.</typeparam> | ||
/// <param name="assert">Assert class.</param> | ||
/// <param name="obj">The object.</param> | ||
/// <returns>True if object is of the given type.</returns> | ||
/// <exception cref="AssertFailedException">If object is not of the given type.</exception> | ||
public static bool IsOfType<T>(this Assert assert, object obj) | ||
{ | ||
if (obj is T) | ||
{ | ||
return true; | ||
} | ||
|
||
throw new AssertFailedException( | ||
string.Format( | ||
"Expected object of type {0} but found object of type {1}", | ||
typeof(T), | ||
obj ?? obj.GetType())); | ||
} | ||
|
||
/// <summary> | ||
/// A chain/grouping of assert statements. | ||
/// </summary> | ||
/// <param name="assert">The Assert class.</param> | ||
/// <returns>The class that contains the assert methods for this grouping.</returns> | ||
public static AssertIs Is(this Assert assert) | ||
{ | ||
if (assertis == null) | ||
{ | ||
assertis = new AssertIs(); | ||
} | ||
|
||
return assertis; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT license. See LICENSE file in the project root for full license information. | ||
|
||
namespace MSTest.Extensibility.Samples | ||
{ | ||
using Microsoft.VisualStudio.TestTools.UnitTesting; | ||
|
||
/// <summary> | ||
/// A grouping of Assert statements with similar functionality. | ||
/// </summary> | ||
public class AssertIs | ||
{ | ||
/// <summary> | ||
/// Determines if the 'divisor' is actually a divisor of 'number'. | ||
/// </summary> | ||
/// <param name="number">A number.</param> | ||
/// <param name="divisor">Its proclaimed divisor.</param> | ||
/// <returns>True if it is a divisor</returns> | ||
/// <exception cref="AssertFailedException">If it is not a divisor.</exception> | ||
public bool Divisor(int number, int divisor) | ||
{ | ||
if (number % divisor == 0) | ||
{ | ||
return true; | ||
} | ||
|
||
throw new AssertFailedException(string.Format("{0} is not a divisor of {1}", divisor, number)); | ||
} | ||
|
||
/// <summary> | ||
/// Determines if a number is positive. | ||
/// </summary> | ||
/// <param name="number">The number.</param> | ||
/// <returns>True if it is positive.</returns> | ||
/// <exception cref="AssertFailedException">If the number is not positive.</exception> | ||
public bool Positive(int number) | ||
{ | ||
if (number > 0) | ||
{ | ||
return true; | ||
} | ||
|
||
throw new AssertFailedException(string.Format("{0} is not positive", number)); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> | ||
<PropertyGroup> | ||
<TestFxRoot Condition="$(TestFxRoot) == ''">..\..\</TestFxRoot> | ||
</PropertyGroup> | ||
<Import Project="$(TestFxRoot)scripts\build\TestFx.Settings.targets" /> | ||
<PropertyGroup> | ||
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration> | ||
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform> | ||
<ProjectGuid>{A82770C0-1FF5-43C7-8790-471D5E4F8D6E}</ProjectGuid> | ||
<OutputType>Library</OutputType> | ||
<AppDesignerFolder>Properties</AppDesignerFolder> | ||
<RootNamespace>MSTest.Extensibility.Samples</RootNamespace> | ||
<AssemblyName>MSTest.Extensibility.Samples</AssemblyName> | ||
<TargetFrameworkVersion>v4.5.2</TargetFrameworkVersion> | ||
<FileAlignment>512</FileAlignment> | ||
</PropertyGroup> | ||
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' "> | ||
<DebugSymbols>true</DebugSymbols> | ||
<DebugType>full</DebugType> | ||
<Optimize>false</Optimize> | ||
<DefineConstants>DEBUG;TRACE</DefineConstants> | ||
<ErrorReport>prompt</ErrorReport> | ||
<WarningLevel>4</WarningLevel> | ||
</PropertyGroup> | ||
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' "> | ||
<DebugType>pdbonly</DebugType> | ||
<Optimize>true</Optimize> | ||
<DefineConstants>TRACE</DefineConstants> | ||
<ErrorReport>prompt</ErrorReport> | ||
<WarningLevel>4</WarningLevel> | ||
</PropertyGroup> | ||
<ItemGroup> | ||
<Reference Include="System" /> | ||
</ItemGroup> | ||
<ItemGroup> | ||
<Compile Include="AssertEx.cs" /> | ||
<Compile Include="AssertIs.cs" /> | ||
<Compile Include="Properties\AssemblyInfo.cs" /> | ||
</ItemGroup> | ||
<ItemGroup> | ||
<ProjectReference Include="..\..\src\TestFramework\MSTest.Core\MSTest.Core.csproj"> | ||
<Project>{7252d9e3-267d-442c-96bc-c73aef3241d6}</Project> | ||
<Name>MSTest.Core</Name> | ||
</ProjectReference> | ||
</ItemGroup> | ||
<Import Project="$(TestFxRoot)scripts\build\TestFx.targets" /> | ||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT license. See LICENSE file in the project root for full license information. | ||
|
||
using System.Reflection; | ||
using System.Runtime.InteropServices; | ||
|
||
// General Information about an assembly is controlled through the following | ||
// set of attributes. Change these attribute values to modify the information | ||
// associated with an assembly. | ||
[assembly: AssemblyTitle("MSTest.Extensibility.Samples")] | ||
[assembly: AssemblyDescription("")] | ||
[assembly: AssemblyConfiguration("")] | ||
[assembly: AssemblyCompany("")] | ||
[assembly: AssemblyProduct("FxExtensibility")] | ||
[assembly: AssemblyCopyright("Copyright © 2017")] | ||
[assembly: AssemblyTrademark("")] | ||
[assembly: AssemblyCulture("")] | ||
|
||
// Setting ComVisible to false makes the types in this assembly not visible | ||
// to COM components. If you need to access a type in this assembly from | ||
// COM, set the ComVisible attribute to true on that type. | ||
[assembly: ComVisible(false)] | ||
|
||
// The following GUID is for the ID of the typelib if this project is exposed to COM | ||
[assembly: Guid("a82770c0-1ff5-43c7-8790-471d5e4f8d6e")] | ||
|
||
// Version information for an assembly consists of the following four values: | ||
// | ||
// Major Version | ||
// Minor Version | ||
// Build Number | ||
// Revision | ||
// | ||
// You can specify all the values or you can default the Build and Revision Numbers | ||
// by using the '*' as shown below: | ||
// [assembly: AssemblyVersion("1.0.*")] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.