Skip to content

Instantly share code, notes, and snippets.

@JamesMaroney
Created October 26, 2011 13:20
Show Gist options
  • Save JamesMaroney/1316328 to your computer and use it in GitHub Desktop.
Save JamesMaroney/1316328 to your computer and use it in GitHub Desktop.
specflow transformations
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using TechTalk.SpecFlow;
namespace Specs.Cucumber.Support.Transformations
{
[Binding]
public static class dynamicTransformations
{
public static Dictionary<string, List<dynamic>> DefinedInstances = new Dictionary<string, List<dynamic>>();
public static Dictionary<string,dynamic> ActiveInstances = new Dictionary<string, dynamic>();
[StepArgumentTransformation(@"a new (\w+)(?: instance)?")]
public static dynamic AnInstanceOf(string className) {
var fullyQualifiedClassName = Assembly.CreateQualifiedName("TNW.PolicyManagement-Core", "TNW.PolicyManagement.Entities." + className);
var instance = Activator.CreateInstance(Type.GetType(fullyQualifiedClassName));
if(! DefinedInstances.ContainsKey(className))
DefinedInstances.Add(className, new List<dynamic>());
DefinedInstances[className].Add(instance);
ActiveInstances[className] = instance;
return instance;
}
[StepArgumentTransformation(@"(\w+) (\w+) instances")]
public static dynamic XInstancesOf(int count, string className) {
var instances = new List<dynamic>();
for(var i=0;i<count;i++) {
instances.Add(AnInstanceOf(className));
}
return instances;
}
[StepArgumentTransformation(@"the (\w+)")]
public static dynamic TheCurrentInstanceOf(string className) {
return ActiveInstances[className];
}
}
}
[StepArgumentTransformation]
public static DateTime GetDateTime(string match)
{
return DateTime.Parse(match);
}
[StepArgumentTransformation(@"(.*) (?:to|-|through) (.*)")]
public static Range<DateTime> DateRange(string lowerBound, string upperBound) {
return new Range<DateTime> { Lower = DateTime.Parse(lowerBound), Upper = DateTime.Parse(upperBound) };
}
[StepArgumentTransformation(@"(.*) (?:to|-|through) (.*)")]
public static Range<int> DateRange(int lowerBound, int upperBound)
{
return new Range<int> { Lower = lowerBound, Upper = upperBound };
}
[Given(@"the Timeline goes from (.*)")]
public static Timeline GivenATimeline(Range<DateTime> range, string doc)
{
var timeline = new Timeline(range.Lower, range.Upper, doc.Length - 1);
if (doc.Contains("|"))
{
timeline.Today = timeline.StepToDate(doc.IndexOf('|'));
}
ScenarioContext.Current["CurrentTimeline"] = timeline;
return timeline;
}
public static Timeline GetCurrentTimeline()
{
return (Timeline)ScenarioContext.Current["CurrentTimeline"];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment