Skip to content

Instantly share code, notes, and snippets.

@ToJans
Created August 18, 2010 16:13
Show Gist options
  • Save ToJans/535285 to your computer and use it in GitHub Desktop.
Save ToJans/535285 to your computer and use it in GitHub Desktop.
This is an example of a BDD script I am currently using for a customer's project
tojans@twitter
Define a pocketapp
using HoTouch.BDD.APocketApp
from HoTouch.BDD.DLL
-----------------------------------------------------------------
Story Startup
is about a pocketapp
As a waiter
I want the app
To get all the latest settings and configuration from the pocket server during startup
So that I can use my pocket with up to date settings and articles
Scenario Startup with a failing connection
Given there is no connection
When it starts up
Then it should show the connection details screen
Scenario Startup with a mismatch between server and client time
Given there is a connection
And the server time does not match the client time
When it starts up
Then it should show the error message screen
Scenario Startup with out of date articles and buttons on the client
Given there is a connection
And the server article timestamp does not match the client's
And the server button timestamp does not match the client's
When it starts up
Then it should update the article data
And it should update the button data
And it should show the login screen
Scenario Startup with the latest versions of articles and buttons on the client
Given there is a connection
And the server button timestamp matches the client's
And the server article timestamp matches the client's
When it starts up
Then it should not update the button data
And it should not update the article data
And it should show the login screen
-----------------------------------------------------------------
Story Settings
is about a pocketapp
As a waiter
I want the app
To allow me to modify the server connection settings, or continue without a connection
So that I can start using the pocket
Scenario Startup with a failing connection and a missing config
Given I am in the connection details screen
And the previous server pocket configuration is unavailable
When I click Ok
Then it should show the error message screen
Scenario Startup with a failing connection and an available config
Given I am in the connection details screen
And the previous server pocket configuration is available
When I click Ok
Then it should show the login screen
using System;
using System.IO;
using System.Linq;
using Aubergine.Model;
using HoTouch.BDD.Stubs;
using HoTouch.Core.Controller;
using HoTouch.Core.Model;
using HoTouch.Core.Services;
using HoTouch.Core.Services.Controllers;
using HoTouch.Core.Services.Impl.Commands;
using HoTouch.Core.Services.Impl.Mappers;
using HoTouch.Winforms.Services;
using Ninject;
namespace HoTouch.BDD
{
public class APocketApp
{
// state vars
bool HasQuit = false;
protected IOCStub ioc = new IOCStub();
IMapper smService = null;
IMapper smUIToParameter = null;
IMapper smVMToUi = null;
ViewmodelBinder sVMB = null;
protected HoTouchStub cHoTouch = null;
protected DocumentDatabaseStub DocDb = null;
protected AppLoop sApploop = null;
ILog sLog = null;
ControllerCommandInvoker cci = null;
public APocketApp()
{
ioc.Reset();
sLog = ioc.Resolve<ILog>();
smUIToParameter = new UIToParameter(sLog);
smService = new ServiceToService(sLog);
smVMToUi = new VMToUI(sLog);
sVMB = new ViewmodelBinder(smVMToUi,sLog);
ioc.Bind<Winforms.MainForm>().ToConstant(new Winforms.MainForm());
ioc.Bind<IMapper>().To<VMToUI>().Named("ViewmodelToView");
ioc.Bind<AppLoop>()
.ToMethod(c=> new AppLoop( sVMB
,c.Kernel.Get<Winforms.MainForm>()
,cci
,smUIToParameter
,c.Kernel.Get<ISystemStatus>()
,()=> HasQuit = true));
DocDb = new DocumentDatabaseStub();
cHoTouch = new HoTouchStub(smService,DocDb);
ioc.Bind<IIOC>().ToConstant(ioc);
ioc.Bind<IHoTouchChannel>().ToConstant(cHoTouch);
ioc.Bind<IDocumentDatabase>().ToConstant(DocDb);
cci = new ControllerCommandInvoker(ioc,sLog,ioc.Get<ISystemStatus>());
ioc.Bind<ControllerCommandInvoker>().ToConstant(cci);
ioc.Bind<IWorkerService>().ToMethod(x=>new WorkerService(cHoTouch,DocDb,x.Kernel.Get<IUnitOfWork>(),smService));
ioc.Bind<LocalSettings>().ToMethod(x=>DocDb.Load<LocalSettings>(DocDb.DefaultId));
ioc.Bind<RemoteSettings>().ToMethod(x=>DocDb.Load<RemoteSettings>(DocDb.DefaultId));
ioc.Bind<IOrderService>().ToMethod(x=>new OrderService(x.Kernel.Get<LocalSettings>()
,x.Kernel.Get<IWorkerService>()
,x.Kernel.Get<ISystemStatus>()
,act=>act(x.Kernel.Get<IWorkerService>())));
cHoTouch.RegisterServerResponse(@"Request@SendServerDateAndTime@.*",x=>smService.Get<string>(DateTime.Now));
cHoTouch.RegisterServerResponse(@"update@Settings.*",x=>File.ReadAllText("FakeData\\RemoteSettings.Xml"));
cci.RegisterControllers(typeof(MainController));
sVMB
.RegisterView<Winforms.Views.ReceiptView>()
.RegisterView<Winforms.Views.SettingsScreen>()
.RegisterView<Winforms.Views.MessageView>()
.RegisterView<Winforms.Views.ShowOpenTables>()
.RegisterView<Winforms.Views.Splash>()
.RegisterView<Winforms.Views.ProductsView>()
.RegisterView<Winforms.Views.InputOpenTable>();
sApploop = ioc.Resolve<AppLoop>();
DocDb.cache.Add(new LocalSettings() { Id = DocDb.DefaultId, PocketNr = "1" });
DocDb.cache.Add(new RemoteSettings() { Id = DocDb.DefaultId,AskForWaiterId = true,WaiterId = "1"});
ioc.Get<Winforms.MainForm>().Show();
cHoTouch.RegisterServerResponse(@"UPDATE@Buttons@.*",x=>{throw new NoNewDataException();});
cHoTouch.RegisterServerResponse(@"UPDATE@Articles@.*",x=>{throw new NoNewDataException();});
}
[DSL]
protected void it_starts_up()
{
sApploop.Run(MainController.BootControllerResult);
}
[DSL("there is (?<yesno>a|no) connection")]
protected void there_is_x_connection(string yesno)
{
cHoTouch.IsConnected = yesno.ToLower()=="a";
}
[DSL]
protected void the_server_time_does_not_match_the_client_time()
{
cHoTouch.RegisterServerResponse(@"Request@SendServerDateAndTime@.*",smService.Get<string>(DateTime.Now.AddHours(1)));
}
[DSL("the server (?<subject>button|article) timestamp (?<doesit>matches|does not match) the client's")]
protected void ServerdoesnotmatchMismatch(string subject,string doesit)
{
if (doesit.ToLower()!="matches") // nondefault
{
var s = subject.Substring(0,1).ToString().ToUpper()+subject.Substring(1);
cHoTouch.RegisterServerResponse(@"UPDATE@"+s+"s@.*",x=>
File.ReadAllText("FakeData\\HorecaTouch"+subject+"s.Xml")
);
}
}
[DSL]
protected bool it_should_update_the_article_data()
{
var cat = DocDb.Load<Catalog>(DocDb.DefaultId)??new Catalog();
return cat.Products.Any();
}
[DSL]
protected bool it_should_not_update_the_article_data()
{
return !it_should_update_the_article_data();
}
[DSL]
protected bool it_should_update_the_button_data()
{
var cat = DocDb.Load<Buttons>(DocDb.DefaultId)??new Buttons();
return cat.Group.Any();
}
[DSL]
protected bool it_should_not_update_the_button_data()
{
return !it_should_update_the_button_data();
}
[DSL("the previous server pocket configuration is (?<not>un)?available")]
protected void the_previous_server_pocket_configuration_is_available(string not)
{
if (not.ToLower()=="un")
DocDb.cache.RemoveAll(x=>typeof(RemoteSettings).IsAssignableFrom(x.GetType())) ;
}
public T GetView<T>()
{
return ioc.Get<Winforms.MainForm>().Controls.OfType<T>().FirstOrDefault();
}
[DSL("I am in the (?<name>.+) screen")]
public void I_am_in_the_name_screen(string name)
{
switch(name.ToLower())
{
case "connection details":
sApploop.Run(new ControllerResult() { Redirect = new ControllerCommand("Main","Settings")});
break;
}
}
[DSL("it should show the (?<name>.+) screen")]
protected bool it_should_show_a_screen(string name)
{
Type t = null;
switch(name.ToLower())
{
case "error message":
t = typeof(Winforms.Views.MessageView);
break;
case "connection details":
t = typeof(Winforms.Views.SettingsScreen);
break;
case "login":
t = typeof(Winforms.Views.InputOpenTable);
break;
}
return ioc.Get<Winforms.MainForm>().Controls.Cast<object>().Any(x=>t.IsAssignableFrom(x.GetType()));
}
[DSL("I click (?<name>.+)")]
void I_press_a_button(string name)
{
var ctrl =ioc.Resolve<Winforms.MainForm>().Controls.Find("Raise"+name,true).First();
var btn = ctrl as System.Windows.Forms.Button;
btn.PerformClick();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment