Skip to content

Instantly share code, notes, and snippets.

@Fristi
Created March 18, 2013 20:20
Show Gist options
  • Save Fristi/5190459 to your computer and use it in GitHub Desktop.
Save Fristi/5190459 to your computer and use it in GitHub Desktop.
module BaseTest =
[<TestFixture>]
[<AbstractClass>]
type public AggregateTest<'TState, 'TCommand, 'TEvent>() =
abstract member For: Aggregate<'TState, 'TCommand, 'TEvent>
abstract member Given: 'TEvent list
abstract member When: 'TCommand
abstract member Then: ('TState -> unit)
abstract member Fail: (System.Exception -> unit)
[<Test>]
member this.Test() =
let aggregate = this.For
let events = this.Given
let state = events |> Seq.fold aggregate.Apply aggregate.Zero
try
aggregate.Execute state this.When
|> aggregate.Apply state
|> this.Then
with
| :? System.Exception as ex -> this.Fail(ex)
module GameboardTest =
[<AbstractClass>]
type public GameboardTest() =
inherit AggregateTest<State, Command, Event>()
override this.Fail = fun e -> ()
override this.Then = fun s -> ()
override this.For = {
Zero = State.Zero;
Apply = Apply;
Execute = Execute
}
let player1 = new Player("player1")
let player2 = new Player("player2")
let participant1 = new Participant(player1, Blue)
let participant2 = new Participant(player2, Red)
type public CreateTest() =
inherit GameboardTest()
override this.Given = []
override this.When = Create
override this.Then = fun s -> Assert.IsNotEmpty s.Tiles
type public BuildRoadTest() =
inherit GameboardTest()
override this.Given = [Created(CreateBoard())]
override this.When = BuildRoad({P1 = {X = 0.0; Y = 320.0}; P2 = {X = 0.0; Y = 400.0}}, participant1)
override this.Then = fun s ->
Assert.IsNotEmpty(s.Roads)
let roadCreated = s.Roads |> Seq.head
Assert.AreEqual(participant1, roadCreated.Participant)
Assert.AreEqual({P1 = {X = 0.0; Y = 320.0}; P2 = {X = 0.0; Y = 400.0}}, roadCreated.Property)
type public BuildRoadAlreadyOccupiedTest() =
inherit GameboardTest()
override this.Given = [
Created(CreateBoard());
RoadBuilt({P1 = {X = 0.0; Y = 320.0}; P2 = {X = 0.0; Y = 400.0}}, participant1)
]
//flip the points (complimentary should work aswell)
override this.When = BuildRoad({P2 = {X = 0.0; Y = 320.0}; P1 = {X = 0.0; Y = 400.0}}, participant1)
override this.Fail = fun (e) ->
Assert.AreEqual("This edge is already occupied", e.Message)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment