Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
2016-07-30-bdd-frameworks-for-dotnet-csharp
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<section name="lightbdd" type="LightBDD.Configuration.LightBDDConfiguration, LightBDD"/>
</configSections>
<lightbdd>
<summaryWriters>
<!-- FeaturesSummary.xml is added by default. Use <clear /> to remove it.-->
<clear/>
<add formatter="LightBDD.Results.Formatters.HtmlResultFormatter, LightBDD" output="Reports/FeaturesSummary.html"/>
</summaryWriters>
</lightbdd>
</configuration>
<!DOCTYPE html>
<html xmlns:concordion="http://www.concordion.org/2007/concordion">
<head>
<meta content="text/html; charset=utf-8" http-equiv="content-type" />
</head>
<body>
<h1>Stack</h1>
<pre>
In order to support last-in-first-out (LIFO) operations
As an developer
I want to use a stack
</pre>
<h2>Empty</h2>
<p concordion:execute="GivenAnEmptyStack()">Given an empty stack</p>
<p concordion:assert-true="IsEmpty()">Then it has no elements</p>
<p concordion:assert-true="ShouldThrowWhenPop()">And it throws an exception when calling pop</p>
<p concordion:assert-true="ShouldThrowWhenPeek()">And it throws an exception when calling peek</p>
<h2>Not empty</h2>
<p concordion:execute="GivenANonEmptyStack()">Given a non empty stack</p>
<p concordion:execute="#result = Peek()">When calling peek</p>
<p concordion:assert-true="#result == TopElement">Then it returns the top element</p>
<p concordion:assert-true="Contains(#result)">But it does not remove the top element</p>
<p concordion:execute="#result = Pop()">When calling pop</p>
<p concordion:assert-true="#result == TopElement">Then it returns the top element</p>
<p concordion:assert-true="DoesNotContain(#result)">And it removes the top element</p>
</body>
</html>
using System;
using System.Collections.Generic;
using Concordion.NET.Integration;
using Concordion.Runners.NUnit;
using NUnit.Framework;
namespace ConductOfCode.Concordion
{
[TestFixture]
[ConcordionTest]
public class StackTest : ExecutableSpecification
{
private Stack<int> stack;
public int TopElement => 3;
// Empty
public void GivenAnEmptyStack()
{
stack = new Stack<int>();
}
public bool IsEmpty()
{
return stack.Count == 0;
}
public bool ShouldThrowWhenPop()
{
return ShouldThrowWhen(() => stack.Pop());
}
public bool ShouldThrowWhenPeek()
{
return ShouldThrowWhen(() => stack.Peek());
}
private bool ShouldThrowWhen(Action action)
{
try
{
action();
}
catch
{
return true;
}
return false;
}
// Not empty
public void GivenANonEmptyStack()
{
stack = new Stack<int>(new[] { 1, 2, TopElement });
}
public int Pop()
{
return stack.Pop();
}
public int Peek()
{
return stack.Peek();
}
public bool Contains(int value)
{
return stack.Contains(value);
}
public bool DoesNotContain(int value)
{
return !stack.Contains(value);
}
}
}
using LightBDD;
using NUnit.Framework;
namespace ConductOfCode.LightBDD
{
[TestFixture]
[FeatureDescription(
@"In order to support last-in-first-out (LIFO) operations
As an developer
I want to use a stack")]
[Label("LIFO")]
public partial class Stack_feature
{
[Test]
[Label("Empty")]
public void Empty_stack()
{
Runner.RunScenario(
given => an_empty_stack(),
then => it_has_no_elements(),
and => it_throws_an_exception_when_calling_pop(),
and => it_throws_an_exception_when_calling_peek());
}
[Test]
[Label("Not empty")]
public void Non_empty_stack()
{
Runner.RunScenario(
given => a_non_empty_stack(),
when => calling_peek(),
then => it_returns_the_top_element(),
but => it_does_not_remove_the_top_element(),
when => calling_pop(),
then => it_returns_the_top_element(),
and => it_removes_the_top_element());
}
}
}
using System;
using System.Collections.Generic;
using LightBDD;
using NUnit.Framework;
namespace ConductOfCode.LightBDD
{
public partial class Stack_feature : FeatureFixture
{
private Stack<int> stack;
private int result;
// Empty
private void an_empty_stack()
{
stack = new Stack<int>();
}
private void it_has_no_elements()
{
Assert.That(stack, Is.Empty);
}
private void it_throws_an_exception_when_calling_pop()
{
Assert.Throws<InvalidOperationException>(() => stack.Pop());
}
private void it_throws_an_exception_when_calling_peek()
{
Assert.Throws<InvalidOperationException>(() => stack.Peek());
}
// Not empty
private void a_non_empty_stack()
{
stack = new Stack<int>(new[] { 1, 2, 3 });
}
private void calling_peek()
{
result = stack.Peek();
}
private void it_returns_the_top_element()
{
Assert.That(result, Is.EqualTo(3));
}
private void it_does_not_remove_the_top_element()
{
Assert.That(stack.Contains(3), Is.True);
}
private void calling_pop()
{
result = stack.Pop();
}
private void it_removes_the_top_element()
{
Assert.That(stack.Contains(result), Is.False);
}
}
}
using System;
using System.Collections.Generic;
using Machine.Specifications;
namespace ConductOfCode.MSpec
{
public class StackSpecs
{
[Subject(typeof(Stack<>))]
public class When_empty
{
Establish context = () =>
{
Subject = new Stack<int>();
};
It should_have_no_elements = () => Subject.ShouldBeEmpty();
It should_throw_an_exception_when_calling_pop = () => Catch.Exception(() => Subject.Pop()).ShouldBeOfExactType<InvalidOperationException>();
It should_throw_an_exception_when_calling_peek = () => Catch.Exception(() => Subject.Peek()).ShouldBeOfExactType<InvalidOperationException>();
static Stack<int> Subject;
}
[Subject(typeof(Stack<>))]
public class When_not_empty
{
Establish context = () =>
{
Subject = new Stack<int>(new[] { 1, 2, 3 });
};
It should_return_the_top_element_when_calling_peek = () => Subject.Peek().ShouldEqual(3);
It should_not_remove_the_top_element_when_calling_peek = () =>
{
Subject.Peek();
Subject.ShouldContain(3);
};
It should_return_the_top_element_when_calling_pop = () => Subject.Pop().ShouldEqual(3);
It should_remove_the_top_element_when_calling_pop = () =>
{
Subject.Pop();
Subject.ShouldNotContain(3);
};
static Stack<int> Subject;
}
}
}
using System;
using System.Collections.Generic;
using NSpec;
namespace ConductOfCode.NSpec
{
class stack_specs : nspec
{
void empty_()
{
before = () => stack = new Stack<int>();
it["has no elements"] = () => stack.Count.should_be(0);
it["throws an exception when calling pop"] = () => expect<InvalidOperationException>(() => stack.Pop());
it["throws an exception when calling peek"] = () => expect<InvalidOperationException>(() => stack.Peek());
}
void not_empty()
{
before = () => stack = new Stack<int>(new[] { 1, 2, 3 });
it["returns the top element when calling peek"] = () => stack.Peek().should_be(3);
it["does not remove the top element when calling peek"] = () =>
{
stack.Peek();
stack.should_contain(3);
};
it["returns the top element when calling pop"] = () => stack.Pop().should_be(3);
it["removes the top element when calling pop"] = () =>
{
stack.Pop();
stack.should_not_contain(3);
};
}
Stack<int> stack;
}
}
Feature: Stack
In order to support last-in-first-out (LIFO) operations
As an developer
I want to use a stack
Scenario: Empty stack
Given an empty stack
Then it has no elements
And it throws an exception when calling pop
And it throws an exception when calling peek
Scenario: Non empty stack
Given a non empty stack
When calling peek
Then it returns the top element
But it does not remove the top element
When calling pop
Then it returns the top element
And it removes the top element
using System;
using System.Collections.Generic;
using TechTalk.SpecFlow;
using Xunit;
namespace ConductOfCode.SpecFlow
{
[Binding]
public class StackSteps
{
private Stack<int> stack;
private int result;
// Empty
[Given(@"an empty stack")]
public void GivenAnEmptyStack()
{
stack = new Stack<int>();
}
[Then(@"it has no elements")]
public void ThenItHasNoElements()
{
Assert.Empty(stack);
}
[Then(@"it throws an exception when calling pop")]
public void ThenItThrowsAnExceptionWhenCallingPop()
{
Assert.Throws<InvalidOperationException>(() => stack.Pop());
}
[Then(@"it throws an exception when calling peek")]
public void ThenItThrowsAnExceptionWhenCallingPeek()
{
Assert.Throws<InvalidOperationException>(() => stack.Peek());
}
// Not empty
[Given(@"a non empty stack")]
public void GivenANonEmptyStack()
{
stack = new Stack<int>(new[] { 1, 2, 3 });
}
[When(@"calling peek")]
public void WhenCallingPeek()
{
result = stack.Peek();
}
[Then(@"it returns the top element")]
public void ThenItReturnsTheTopElement()
{
Assert.Equal(3, result);
}
[Then(@"it does not remove the top element")]
public void ThenItDoesNotRemoveTheTopElement()
{
Assert.Contains(3, stack);
}
[When(@"calling pop")]
public void WhenCallingPop()
{
result = stack.Pop();
}
[Then(@"it removes the top element")]
public void ThenItRemovesTheTopElement()
{
Assert.DoesNotContain(3, stack);
}
}
}
using System;
using System.Collections.Generic;
using NUnit.Framework;
using Should;
using SpecsFor;
namespace ConductOfCode.SpecsFor
{
public class StackSpecs
{
public class given_an_empty_stack : SpecsFor<Stack<int>>
{
[Test]
public void then_it_has_no_elements()
{
SUT.Count.ShouldEqual(0);
}
[Test]
[ExpectedException(typeof(InvalidOperationException))]
public void then_it_throws_an_exception_when_calling_pop()
{
SUT.Pop();
}
[Test]
[ExpectedException(typeof(InvalidOperationException))]
public void then_it_throws_an_exception_when_calling_peek()
{
SUT.Peek();
}
}
public class given_a_non_empty_stack : SpecsFor<Stack<int>>
{
protected override void BeforeEachTest()
{
SUT = new Stack<int>(new[] { 1, 2, 3 });
}
[Test]
public void then_it_returns_the_top_element_when_calling_peek()
{
SUT.Peek().ShouldEqual(3);
}
[Test]
public void then_it_does_not_remove_the_top_element_when_calling_peek()
{
SUT.Peek();
SUT.ShouldContain(3);
}
[Test]
public void then_it_returns_the_top_element_when_calling_pop()
{
SUT.Pop().ShouldEqual(3);
}
[Test]
public void then_it_removes_the_top_element_when_calling_pop()
{
SUT.Pop();
SUT.ShouldNotContain(3);
}
}
}
}
using System;
using System.Collections.Generic;
using Xbehave;
using Xunit;
namespace ConductOfCode.Xbehave
{
public class StackFeature
{
[Scenario]
public void Empty(Stack<int> stack)
{
"Given an empty stack"
.x(() => stack = new Stack<int>());
"Then it has no elements"
.x(() => Assert.Empty(stack));
"And it throws an exception when calling pop"
.x(() => Assert.Throws<InvalidOperationException>(() => stack.Pop()));
"And it throws an exception when calling peek"
.x(() => Assert.Throws<InvalidOperationException>(() => stack.Peek()));
}
[Scenario]
public void NotEmpty(Stack<int> stack, int result)
{
"Given a non empty stack"
.x(() => stack = new Stack<int>(new[] { 1, 2, 3 }));
"When calling peek"
.x(() => result = stack.Peek());
"Then it returns the top element"
.x(() => Assert.Equal(3, result));
"But it does not remove the top element"
.x(() => Assert.Contains(3, stack));
"When calling pop"
.x(() => result = stack.Pop());
"The it returns the top element"
.x(() => Assert.Equal(3, result));
"And it removes the top element"
.x(() => Assert.DoesNotContain(3, stack));
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.