Created
August 13, 2023 14:38
-
-
Save ijrussell/73d7f685cc5c9e65812f37de1585daf0 to your computer and use it in GitHub Desktop.
Simple BDD {Based on a 2009 blog post from Garry Shutler]
This file contains hidden or 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
namespace TestsXunit.Framework; | |
public abstract class BehaviourTest | |
{ | |
protected void Setup() | |
{ | |
Given(); | |
When(); | |
} | |
protected abstract void Given(); | |
protected abstract void When(); | |
} |
This file contains hidden or 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
namespace BDDTestConsole; | |
public class RecentlyUsedList<T> | |
{ | |
private readonly List<T> _data; | |
private readonly int _capacity; | |
public RecentlyUsedList(int capacity) | |
{ | |
_data = new List<T>(); | |
_capacity = capacity; | |
} | |
public void Add(T item) | |
{ | |
_data.Remove(item); | |
if (_data.Count == _capacity) | |
{ | |
_data.RemoveAt(0); | |
} | |
_data.Add(item); | |
} | |
public IReadOnlyList<T> Items => _data.AsReadOnly(); | |
} |
This file contains hidden or 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
using BDDTestConsole; | |
using TestsXunit.Framework; | |
namespace TestsXunit.RecentlyUsedList; | |
public class WhenAddingAnItemToANewList : BehaviourTest | |
{ | |
private const string AddedItem = "Item1"; | |
private RecentlyUsedList<string> _sut; | |
public WhenAddingAnItemToANewList() | |
{ | |
Setup(); | |
} | |
[Fact] | |
public void ShouldContainOneItem() | |
{ | |
Assert.Equal(1, _sut.Items.Count); | |
} | |
[Fact] | |
public void ShouldContainExpectedItem() | |
{ | |
var expected = new List<string> { AddedItem }; | |
Assert.Equal(expected, _sut.Items); | |
} | |
protected override void Given() | |
{ | |
_sut = new RecentlyUsedList<string>(3); | |
} | |
protected override void When() | |
{ | |
_sut.Add(AddedItem); | |
} | |
} |
This file contains hidden or 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
using TestsXunit.Framework; | |
namespace TestsXunit.RecentlyUsedList; | |
public class WhenCreatingANewList : BehaviourTest | |
{ | |
private RecentlyUsedList<string> _sut; | |
public WhenCreatingANewList() | |
{ | |
Setup(); | |
} | |
[Fact] | |
public void ShouldContainNoItems() | |
{ | |
Assert.Equal(0, _sut.Items.Count); | |
} | |
protected override void Given() | |
{ | |
_sut = new RecentlyUsedList<string>(3); | |
} | |
protected override void When() | |
{ | |
// Nothing to do | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment