Skip to content

Instantly share code, notes, and snippets.

@RhysC
Last active August 29, 2015 14:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save RhysC/9e413483c26cddda97e0 to your computer and use it in GitHub Desktop.
Save RhysC/9e413483c26cddda97e0 to your computer and use it in GitHub Desktop.
Cucumber feature file to BDDFy fluent Api
<Query Kind="Program">
<Namespace>System.Globalization</Namespace>
</Query>
void Main()
{
Func<string,string> ToTitleCase= new CultureInfo("en-US",false).TextInfo.ToTitleCase;
var stepMethods = new List<string>();
var classSb = new StringBuilder();
classSb.AppendLine("using System;");
classSb.AppendLine("using TestStack.BDDfy;");
classSb.AppendLine("using Xunit;");
classSb.AppendLine("namespace YourNamespace");
classSb.AppendLine("{");
classSb.AppendLine("public partial class YourFeature");
classSb.AppendLine("{");
foreach(var text in feature
.Split(new[]{Environment.NewLine + Environment.NewLine}, StringSplitOptions.RemoveEmptyEntries)
.Select(s=>s.Trim())
.Where (s => s.StartsWith("Scenario:")))
{
var sb = new StringBuilder();
var lines = text
.Split(new[]{Environment.NewLine}, StringSplitOptions.RemoveEmptyEntries)
.Where(l=>!l.StartsWith("#"))
.Select(l=> ToTitleCase(l.Trim()));
var Method = lines.First();
if(!Method.StartsWith("Scenario:"))throw new Exception("needs Scenario '"+Method+"'");
sb.AppendLine();
sb.AppendLine("[Fact]");
sb.Append(Method.Replace(" ","").Replace("Scenario:", "public void "));
sb.AppendLine("()");
sb.AppendLine("{");
sb.Append("this");
var testLines = lines.Skip(1);
foreach (var line in testLines)
{
var words = line.Split(new[]{" "}, StringSplitOptions.RemoveEmptyEntries);
var methodName = ToTitleCase(line.Remove(0,words[0].Length)).Replace(" ","");
stepMethods.Add(methodName);
sb.AppendFormat(".{0}(s => s.{1}())", words[0], methodName);
sb.AppendLine();
}
sb.AppendLine(".BDDfy();");
sb.AppendLine("}");
classSb.Append(sb.ToString());
}
classSb.AppendLine("}");//End scenarios definitions
classSb.AppendLine("public partial class YourFeature");//Step definition
classSb.AppendLine("{");
foreach (var stepMethod in stepMethods.Distinct())
{
classSb.AppendLine("void "+stepMethod+"(){}");
}
classSb.AppendLine("}");//End Step definition
classSb.AppendLine("}");
classSb.ToString().Dump();
}
//The feature you are turning into a BDDfy class
string feature =
@"Feature: Manage Leads
In to build a sales pipeline
As a sales user
I want to be able to receive and manage leads
Scenario: Create a lead
Given an anonymous user using the api
When they submit their contact details
Then the new Id is returned
And a Created is returned
And a SalesAdmin user can retrieve the lead
And LeadCreated event is raised
Scenario: Create a lead with opportunity details
Given an anonymous user using the api
When they submit a fullly populated lead
Then the new Id is returned
And a Created is returned
And a SalesAdmin user can retrieve the lead
And the opportunity details are on the lead
And LeadCreated event is raised
Scenario: Continue to add to a lead
Given an anonymous user using the api
And they have submited their contact details
When an update command is made with the lead id
Then a SalesAdmin user can retrieve the lead
And a Ok is returned
And LeadUpdated event is raised
Scenario: Sales user update a Lead
Given an sales user using the api
And a valid Lead exists
When an update command is made with the lead id
Then a Ok is returned
And LeadUpdated event is raised
And a SalesAdmin user can retrieve the lead
Scenario: Attempt to Create a lead without a name
Given an anonymous user using the api
When they submit their contact details with no name
Then a BadRequest is returned
And no leads are created
And no events are raised
Scenario: Attempt to Create a lead without a contact
Given an anonymous user using the api
When they submit their contact details with no contact
Then a BadRequest is returned
And no leads are created
And no events are raised
Scenario: Attempt to Update a lead without a contact
Given an anonymous user using the api
And they have submited their contact details
When they modify their contact details with no contact
Then a BadRequest is returned
And no LeadUpdated event is raised
Scenario: Attempt to Update a lead without a name
Given an anonymous user using the api
And they have submited their contact details
When they modify their contact details with no name
Then a BadRequest is returned
And no LeadUpdated event is raised
Scenario: Attempt to Update an invalid lead
Given an sales user using the api
And a valid Lead exists
When an update command is made with an invalid id
Then a NotFound is returned
And no LeadUpdated event is raised
Scenario: Delete A non existant Lead
Given an sales user using the api
And a valid Lead exists
When a delete command is made with an invalid id
Then a NotFound is returned
And no LeadDeleted event is raised
And a SalesAdmin user can retrieve the lead
Scenario: Delete A deleted Lead
Given an sales user using the api
And a Lead has been previously deleted
When a delete command is made with the lead id
Then a NotFound is returned
And no LeadDeleted event is raised
And the lead can not be retrieved by id
Scenario: Delete A Lead
Given an sales user using the api
And a valid Lead exists
When a delete command is made with the lead id
Then a NoContent is returned
And the lead can not be retrieved by id
And the lead does not appear in the lead list
And LeadDeleted event is raised
Scenario: Vetting a Lead
Given an sales user using the api
And a valid Lead exists
When the lead is vetted
Then a Created is returned
And the lead can not be retrieved by id
And the lead does not appear in the lead list
And the opportunity can be retrieved by id
And LeadVetted event is raised
And OpportunityCreated event is raised
#
#Given a sales user is logged on
#And a Lead is vetted
#When the lead is updated, an EntityNotFoundException is thrown
#And no event is raised
";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment