Permalink
Browse files

Adding an example for custom delegates

  • Loading branch information...
agross committed Jan 20, 2013
1 parent ff0827e commit a055c913693c0f8bc4e25988d5eb318878441db2
View

Large diffs are not rendered by default.

Oops, something went wrong.
@@ -0,0 +1,26 @@
+using System;
+
+namespace Machine.Specifications.Example.CustomDelegates
+{
+ public class Account
+ {
+ private decimal _balance;
+
+ public decimal Balance
+ {
+ get { return _balance; }
+ set { _balance = value; }
+ }
+
+ public void Transfer(decimal amount, Account toAccount)
+ {
+ if (amount > _balance)
+ {
+ throw new Exception(String.Format("Cannot transfer ${0}. The available balance is ${1}.", amount, _balance));
+ }
+
+ _balance -= amount;
+ toAccount.Balance += amount;
+ }
+ }
+}
@@ -0,0 +1,46 @@
+using System;
+
+namespace Machine.Specifications.Example.CustomDelegates
+{
+ [Subject(typeof(Account), "Funds transfer")]
+ public class when_transferring_between_two_accounts
+ {
+ static Account fromAccount;
+ static Account toAccount;
+
+ Given accounts = () =>
+ {
+ fromAccount = new Account {Balance = 1m};
+ toAccount = new Account {Balance = 1m};
+ };
+
+ When transfer_is_made =
+ () => fromAccount.Transfer(1m, toAccount);
+
+ Then should_debit_the_from_account_by_the_amount_transferred =
+ () => fromAccount.Balance.ShouldEqual(0m);
+
+ Then should_credit_the_to_account_by_the_amount_transferred =
+ () => toAccount.Balance.ShouldEqual(2m);
+ }
+
+ [Subject(typeof(Account), "Funds transfer")]
+ public class when_transferring_an_amount_larger_than_the_balance_of_the_from_account
+ {
+ static Account fromAccount;
+ static Account toAccount;
+ static Exception exception;
+
+ Given accounts = () =>
+ {
+ fromAccount = new Account {Balance = 1m};
+ toAccount = new Account {Balance = 1m};
+ };
+
+ Because transfer_is_made =
+ () => exception = Catch.Exception(() => fromAccount.Transfer(2m, toAccount));
+
+ Then should_not_allow_the_transfer =
+ () => exception.ShouldBeOfType<Exception>();
+ }
+}
@@ -0,0 +1,11 @@
+namespace Machine.Specifications.Example.CustomDelegates
+{
+ [DelegateUsage(DelegateUsage.Setup)]
+ public delegate void Given();
+
+ [DelegateUsage(DelegateUsage.Act)]
+ public delegate void When();
+
+ [DelegateUsage(DelegateUsage.Assert)]
+ public delegate void Then();
+}
@@ -0,0 +1,65 @@
+<?xml version="1.0" encoding="utf-8"?>
+<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
+ <Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
+ <PropertyGroup>
+ <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
+ <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
+ <ProjectGuid>{844788DF-F12F-462F-BDE8-C46F8FFECD11}</ProjectGuid>
+ <OutputType>Library</OutputType>
+ <AppDesignerFolder>Properties</AppDesignerFolder>
+ <RootNamespace>Machine.Specifications.Example.CustomDelegates</RootNamespace>
+ <AssemblyName>Machine.Specifications.Example.CustomDelegates</AssemblyName>
+ <TargetFrameworkVersion>v3.5</TargetFrameworkVersion>
+ <FileAlignment>512</FileAlignment>
+ </PropertyGroup>
+ <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
+ <DebugSymbols>true</DebugSymbols>
+ <DebugType>full</DebugType>
+ <Optimize>false</Optimize>
+ <OutputPath>..\..\Build\Debug\Tests\</OutputPath>
+ <DefineConstants>DEBUG;TRACE</DefineConstants>
+ <ErrorReport>prompt</ErrorReport>
+ <WarningLevel>4</WarningLevel>
+ </PropertyGroup>
+ <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
+ <DebugType>pdbonly</DebugType>
+ <Optimize>true</Optimize>
+ <OutputPath>..\..\Build\Release\Tests\</OutputPath>
+ <DefineConstants>TRACE</DefineConstants>
+ <ErrorReport>prompt</ErrorReport>
+ <WarningLevel>4</WarningLevel>
+ </PropertyGroup>
+ <ItemGroup>
+ <Reference Include="System" />
+ <Reference Include="System.Core" />
+ </ItemGroup>
+ <ItemGroup>
+ <Compile Include="..\SharedAssemblyInfo.cs">
+ <Link>Properties\SharedAssemblyInfo.cs</Link>
+ </Compile>
+ <Compile Include="..\VersionInfo.cs">
+ <Link>Properties\VersionInfo.cs</Link>
+ </Compile>
+ <Compile Include="Account.cs" />
+ <Compile Include="Delegates.cs" />
+ <Compile Include="Properties\AssemblyInfo.cs">
+ <SubType>
+ </SubType>
+ </Compile>
+ <Compile Include="AccountSpec.cs" />
+ </ItemGroup>
+ <ItemGroup>
+ <ProjectReference Include="..\Machine.Specifications\Machine.Specifications.csproj">
+ <Project>{CCD02629-1262-4F78-9E9F-AC97B942D0E7}</Project>
+ <Name>Machine.Specifications</Name>
+ </ProjectReference>
+ </ItemGroup>
+ <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
+ <!-- To modify your build process, add your task inside one of the targets below and uncomment it.
+ Other similar extension points exist, see Microsoft.Common.targets.
+ <Target Name="BeforeBuild">
+ </Target>
+ <Target Name="AfterBuild">
+ </Target>
+ -->
+</Project>
@@ -0,0 +1,21 @@
+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("Machine.Specifications.Example.CustomDelegates")]
+[assembly: AssemblyDescription("")]
+[assembly: AssemblyConfiguration("")]
+[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("adddc1ee-800c-4e95-80e1-254b3aff6efa")]
@@ -74,6 +74,7 @@
<Compile Include="ExceptionResultSpecs.cs" />
<Compile Include="Explorers\AssemblyExplorerSpecs.cs" />
<Compile Include="Factories\ContextFactorySpecs.cs" />
+ <Compile Include="Runner\CustomDelegatesSpecs.cs" />
<Compile Include="ShouldBeLikeSpecs.cs" />
<Compile Include="Utility\Internal\ErrorMessageSpecs.cs" />
<Compile Include="Utility\Internal\NamingSpecs.cs" />
@@ -94,6 +95,10 @@
<Compile Include="Utility\ObjectGraphHelperSpecs.cs" />
</ItemGroup>
<ItemGroup>
+ <ProjectReference Include="..\Machine.Specifications.Example.CustomDelegates\Machine.Specifications.Example.CustomDelegates.csproj">
+ <Project>{844788df-f12f-462f-bde8-c46f8ffecd11}</Project>
+ <Name>Machine.Specifications.Example.CustomDelegates</Name>
+ </ProjectReference>
<ProjectReference Include="..\Machine.Specifications.Example.CleanupFailure\Machine.Specifications.Example.CleanupFailure.csproj">
<Project>{0C0F5B79-50B7-4B6E-9EAB-9A7DC70F92AC}</Project>
<Name>Machine.Specifications.Example.CleanupFailure</Name>
@@ -0,0 +1,15 @@
+
+
+using Machine.Specifications.Example.CustomDelegates;
+
+namespace Machine.Specifications.Specs.Runner
+{
+ public class when_running_specs_with_custom_delegates : running_specs
+ {
+ Because of =
+ () => runner.RunAssembly(typeof(Account).Assembly);
+
+ It should_run_them_all =
+ () => listener.SpecCount.ShouldEqual(3);
+ }
+}
@@ -1,7 +1,4 @@
using System;
-using System.Collections.Generic;
-using System.Linq;
-using System.Text;
namespace Machine.Specifications
{

0 comments on commit a055c91

Please sign in to comment.