Create a gist now

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Time Travel DSL
using System;
using System.Linq;
namespace TimeTravelDsl
{
public static class Extensions
{
// for example: 45.Minutes()
public static TimeSpan Minutes(this int value)
{
return TimeSpan.FromMinutes(value);
}
// for example: 8.Hours()
public static TimeSpan Hours(this int value)
{
return TimeSpan.FromHours(value);
}
// for example: 4.Days()
public static TimeSpan Days(this int value)
{
return TimeSpan.FromDays(value);
}
// for example: 6.MonthsAgo()
public static DateTime MonthsAgo(this int months)
{
return SystemTime.Now.AddMonths(-months);
}
// for example: 45.To(60)
public static int To(this int start, int end)
{
var range = Enumerable.Range(start, end - start);
var random = new Random();
return range.ElementAt(random.Next(range.Count()));
}
}
}
using System.Text;
namespace TimeTravelDsl
{
public class Order
{
private StringBuilder history = new StringBuilder();
public static Order Create()
{
var order = new Order();
order.history.AppendLine("Created - " + SystemTime.Now);
return order;
}
public void Submit()
{
history.AppendLine("Submitted - " + SystemTime.Now);
}
public void Dispatch()
{
history.AppendLine("Dispatched - " + SystemTime.Now);
}
public string ReportHistory()
{
return history.ToString();
}
}
}
using System;
namespace TimeTravelDsl
{
class Program
{
static void Main(string[] args)
{
using (TimeTravel.To(2.MonthsAgo()))
{
var order = Order.Create();
using (TimeTravel.Forward(45.To(60).Minutes()))
{
order.Submit();
using (TimeTravel.Forward(4.To(6).Days() + 2.To(8).Hours()))
{
order.Dispatch();
}
}
Console.WriteLine(order.ReportHistory());
}
}
}
}
using System;
namespace TimeTravelDsl
{
public static class SystemTime
{
public static TimeSpan Offset;
public static DateTime Now { get { return DateTime.Now + Offset; } }
public static DateTime Today { get { return SystemTime.Now.Date; } }
}
}
using System;
namespace TimeTravelDsl
{
public class TimeTravel : IDisposable
{
private readonly TimeSpan previousOffset;
public TimeTravel()
{
previousOffset = SystemTime.Offset;
}
public void Dispose()
{
SystemTime.Offset = previousOffset;
}
// for example: TimeTravel.Forward(5.Minutes())
public static TimeTravel Forward(TimeSpan offset)
{
var token = new TimeTravel();
SystemTime.Offset += offset;
return token;
}
// for example: TimeTravel.Back(5.Days())
public static TimeTravel Back(TimeSpan offset)
{
var token = new TimeTravel();
SystemTime.Offset -= offset;
return token;
}
// for example: TimeTravel.To(6.MonthsAgo())
public static TimeTravel To(DateTime date)
{
return TimeTravel.Forward(date - SystemTime.Now);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment