Skip to content

Commit

Permalink
Temporary checkin for samples.
Browse files Browse the repository at this point in the history
  • Loading branch information
forki committed Jun 28, 2010
1 parent c3ba232 commit 6d3a877
Show file tree
Hide file tree
Showing 10 changed files with 179 additions and 1 deletion.
33 changes: 33 additions & 0 deletions src/test/CarSellingLib/Account.cs
@@ -0,0 +1,33 @@
using System;
using System.Linq;

namespace Machine.Specifications.Example
{
public class Account
{
private decimal _balance;

public int AccountNo { get; set; }
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;
}

public override string ToString()
{
return string.Format("\"Account {0}\"", AccountNo);
}
}
}
4 changes: 4 additions & 0 deletions src/test/CarSellingLib/Article.cs
@@ -0,0 +1,4 @@
namespace xUnit.BDDExtensions.SampleCode.UI
{
public class Article {}
}
24 changes: 24 additions & 0 deletions src/test/CarSellingLib/ArticleSearchViewModel.cs
@@ -0,0 +1,24 @@
using System.Collections.Generic;
using Microsoft.FSharp.Collections;

namespace xUnit.BDDExtensions.SampleCode.UI
{
public class ArticleSearchViewModel
{
private readonly IArticleSource _articleSource;

public ArticleSearchViewModel(IArticleSource articleSource)
{
_articleSource = articleSource;
}

public string SearchTerm { get; set; }

public FSharpList<Article> Results { get; set; }

public void StartSearch()
{
Results = _articleSource.Search(SearchTerm);
}
}
}
5 changes: 5 additions & 0 deletions src/test/CarSellingLib/CarSellingLib.csproj
Expand Up @@ -37,6 +37,7 @@
<CodeAnalysisRuleSet>AllRules.ruleset</CodeAnalysisRuleSet>
</PropertyGroup>
<ItemGroup>
<Reference Include="FSharp.Core, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL" />
<Reference Include="System" />
<Reference Include="System.Core">
<RequiredTargetFramework>3.5</RequiredTargetFramework>
Expand Down Expand Up @@ -67,9 +68,13 @@
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="Account.cs" />
<Compile Include="Article.cs" />
<Compile Include="ArticleSearchViewModel.cs" />
<Compile Include="Car.cs" />
<Compile Include="CarType.cs" />
<Compile Include="Dealer.cs" />
<Compile Include="IArticleSource.cs" />
<Compile Include="IDealer.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
Expand Down
4 changes: 4 additions & 0 deletions src/test/CarSellingLib/Dealer.cs
Expand Up @@ -27,4 +27,8 @@ public override string ToString()
return Name;
}
}
}

namespace Machine.Specifications.Example
{
}
10 changes: 10 additions & 0 deletions src/test/CarSellingLib/IArticleSource.cs
@@ -0,0 +1,10 @@
using System.Collections.Generic;
using Microsoft.FSharp.Collections;

namespace xUnit.BDDExtensions.SampleCode.UI
{
public interface IArticleSource
{
FSharpList<Article> Search(string searchTerm);
}
}
27 changes: 27 additions & 0 deletions src/test/Spec.CarSellingLib/AccountSpec.fs
@@ -0,0 +1,27 @@
module AccountSpec

open NaturalSpec
open Machine.Specifications.Example

let transferring_money_to money toAccount (fromAccount:Account) =
toSpec <| (sprintf "transferring %A to %A" money toAccount)
fromAccount.Transfer(money,toAccount)
fromAccount

let balance amount (account:Account) =
printMethod amount
account.Balance = amount



[<Scenario>]
let ``When transferring between two account``() =
let fromAccount = new Account(AccountNo=1,Balance=1m)
let toAccount = new Account(AccountNo=2,Balance=1m)

Given fromAccount // Arrange
|> When transferring_money_to 1m toAccount // Act
|> It should have (balance 0m) // Assert
|> Whereas toAccount
|> It should have (balance 2m)
|> Verify
33 changes: 33 additions & 0 deletions src/test/Spec.CarSellingLib/ArticleSearchViewModelSpec.fs
@@ -0,0 +1,33 @@
module ArticleSearchViewModelSpec

open NaturalSpec

open xUnit.BDDExtensions.SampleCode.UI
open System.Collections.Generic

let starting_search (vm:ArticleSearchViewModel) =
printMethod ""
vm.StartSearch()
vm

let articleCount count (vm:ArticleSearchViewModel) =
printMethod count
Seq.length vm.Results = count



[<Scenario>]
let ``Given an existing search term when starting for articles``() =
let empty : Article list = []
let service =
mock<IArticleSource> "Service"
|> setup <@fun x -> x.Search @> (fun x -> if x = "Term" then empty else failwith "Error")

let viewModel = new ArticleSearchViewModel(service,SearchTerm = "Term")

Given viewModel
|> When starting_search
|> It should have (articleCount 0)
|> Verify


2 changes: 2 additions & 0 deletions src/test/Spec.CarSellingLib/Spec.CarSellingLib.fsproj
Expand Up @@ -49,6 +49,8 @@
<ItemGroup>
<Compile Include="CarSpec.fs" />
<Compile Include="DealerMockingSample.fs" />
<Compile Include="AccountSpec.fs" />
<Compile Include="ArticleSearchViewModelSpec.fs" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\app\NaturalSpec\NaturalSpec.fsproj">
Expand Down
38 changes: 37 additions & 1 deletion src/test/Spec.Store/StoreMock.fs
Expand Up @@ -26,4 +26,40 @@ let ``Creating a ProductsPresenter should set ViewCategories``() =
|> When creating_presenter_with view
|> Whereas view
|> Called <@fun x -> x.SetCategories @> list
|> Verify
|> Verify
//
//
//[<Scenario>]
//let ``CategorieSelection should set Products``() =
// let list = [Category(Name="Islay");Category(Name="Highland")]
// let catalog =
// mock<ICatalogService> "Catalog"
// |> setup <@fun x -> x.GetCategories @> (fun _ -> list)
// let view =
// mock<IProductsView> "View"
// |> setup <@fun x -> x.SetCategories @> (fun _ -> ())
// |> setup <@fun x -> x.CategorySelected.AddHandler @> (fun _ -> ())
// let presenter = new ProductsPresenter(catalog, view)
//
// //view.CategorySelected
// Given view
// |> Called <@fun x -> x.SetProducts @> list
// |> Verify

//
// [Test]
// public void ShouldCategorySelectionSetProducts()
// {
// // Arrange
// var catalog = new Mock<ICatalogService>();
// var view = new Mock<IProductsView>();
// var presenter = new ProductsPresenter(catalog.Object, view.Object);
//
// // Act
// view.Raise(
// v => v.CategorySelected += null,
// new CategoryEventArgs(new Category { Id = 1 }));
//
// // Assert
// view.Verify(v => v.SetProducts(It.IsAny<IEnumerable<Product>>()));
// }

0 comments on commit 6d3a877

Please sign in to comment.