Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save hlaueriksson/3bd543bc6466e3eb5ecd1ab87d952563 to your computer and use it in GitHub Desktop.
Save hlaueriksson/3bd543bc6466e3eb5ecd1ab87d952563 to your computer and use it in GitHub Desktop.
2017-07-31-easy-approach-to-requirements-syntax-and-the-segue-to-behavior-driven-development
Feature: Stack<T>
In order to store instances of the same specified type in last-in-first-out (LIFO) sequence
As a developer
I want to use a Stack<T>
@Ubiquitous
Scenario: The Stack<T> shall store instances of the same specified type in last-in-first-out (LIFO) order
Given an empty stack
When the pushing the elements:
| Element |
| One |
| Two |
| Three |
Then the elements should be popped in the order:
| Element |
| Three |
| Two |
| One |
@Ubiquitous
Scenario: The Stack<T> shall return the number of elements contained when the property Count is invoked
Given a stack with the elements:
| Element |
| One |
| Two |
| Three |
When counting the elements
Then the result should be 3
@EventDriven
Scenario: When the method Push is invoked, the Stack<T> shall inserts the element at the top
Given a stack with the elements:
| Element |
| One |
| Two |
| Three |
When pushing the element "Four"
Then the element "Four" should be on top
@EventDriven
Scenario: When the method Pop is invoked, the Stack<T> shall remove and return the element at the top
Given a stack with the elements:
| Element |
| One |
| Two |
| Three |
When popping the element on top
Then the result should be "Three"
And the element "Two" should be on top
@EventDriven
Scenario: When the method Peek is invoked, the Stack<T> shall return the element at the top without removing it
Given a stack with the elements:
| Element |
| One |
| Two |
| Three |
When peeking at the element on top
Then the result should be "Three"
And the element "Three" should be on top
@StateDriven
Scenario: While an element is present, the Stack<T> shall return true when the method Contains is invoked
Given a stack with the elements:
| Element |
| One |
| Two |
| Three |
When determining if the stack contains the element "Three"
Then the result should be true
@StateDriven
Scenario: While an element is not present, the Stack<T> shall return false when the method Contains is invoked
Given a stack with the elements:
| Element |
| One |
| Two |
| Three |
When determining if the stack contains the element "Four"
Then the result should be false
@UnwantedBehavior
Scenario: If empty and the method Pop is invoked, then the Stack<T> shall throw InvalidOperationException
Given an empty stack
When popping the element on top
Then InvalidOperationException should be thrown
@UnwantedBehavior
Scenario: If empty and the method Peek is invoked, then the Stack<T> shall throw InvalidOperationException
Given an empty stack
When peeking at the element on top
Then InvalidOperationException should be thrown
@Optional
Scenario: Where instantiated with a specified collection, the Stack<T> shall be pre-populated with the elements of the collection
Given a stack with the elements:
| Element |
| One |
| Two |
| Three |
Then the stack should contain the elements:
| Element |
| Three |
| Two |
| One |
using Should;
using System;
using System.Collections.Generic;
using System.Linq;
using TechTalk.SpecFlow;
namespace ConductOfCode.Steps
{
[Binding]
public class StackSteps
{
private Stack<string> stack;
private string result;
private int count;
private bool contains;
private Exception exception;
[Given(@"an empty stack")]
public void GivenAnEmptyStack()
{
stack = new Stack<string>();
}
[Given(@"a stack with the elements:")]
public void GivenAStackWithTheElements(Table table)
{
var elements = table.Rows.Select(x => x.Values.First()).ToList();
stack = new Stack<string>(elements);
}
[When(@"the pushing the elements:")]
public void WhenThePushingTheElements(Table table)
{
var elements = table.Rows.Select(x => x.Values.First()).ToList();
foreach (var element in elements)
{
stack.Push(element);
}
}
[When(@"counting the elements")]
public void WhenCountingTheElements()
{
count = stack.Count;
}
[When(@"pushing the element ""(.*)""")]
public void WhenPushingTheElement(string element)
{
stack.Push(element);
}
[When(@"popping the element on top")]
public void WhenPoppingTheElementOnTop()
{
try
{
result = stack.Pop();
}
catch (Exception ex)
{
exception = ex;
}
}
[When(@"peeking at the element on top")]
public void WhenPeekingAtTheElementOnTop()
{
try
{
result = stack.Peek();
}
catch (Exception ex)
{
exception = ex;
}
}
[When(@"determining if the stack contains the element ""(.*)""")]
public void WhenDeterminingIfTheStackContainsTheElement(string element)
{
contains = stack.Contains(element);
}
[Then(@"the elements should be popped in the order:")]
public void ThenTheElementsShouldBePoppedInTheOrder(Table table)
{
var elements = table.Rows.Select(x => x.Values.First()).ToList();
foreach (var element in elements)
{
element.ShouldEqual(stack.Pop());
}
}
[Then(@"the result should be (.*)")]
public void ThenTheResultShouldBe(int expected)
{
count.ShouldEqual(expected);
}
[Then(@"the element ""(.*)"" should be on top")]
public void ThenTheElementShouldBeOnTop(string element)
{
element.ShouldEqual(stack.Peek());
}
[Then(@"the result should be ""(.*)""")]
public void ThenTheResultShouldBe(string expected)
{
result.ShouldEqual(expected);
}
[Then(@"the result should be true")]
public void ThenTheResultShouldBeTrue()
{
contains.ShouldBeTrue();
}
[Then(@"the result should be false")]
public void ThenTheResultShouldBeFalse()
{
contains.ShouldBeFalse();
}
[Then(@"InvalidOperationException should be thrown")]
public void ThenInvalidOperationExceptionShouldBeThrown()
{
exception.ShouldBeType<InvalidOperationException>();
}
[Then(@"the stack should contain the elements:")]
public void ThenTheStackShouldContainTheElements(Table table)
{
var elements = table.Rows.Select(x => x.Values.First()).ToList();
foreach (var element in elements)
{
stack.Contains(element).ShouldBeTrue();
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment