This appendix contains full source code of all examples used in the book. We have built some examples gradually throughout the book, so some files contain several versions of test classes. In those cases, you will see different stages put into namespaces such as FirstTry, SecondTry etc. You can also download all these files from http://gojko.net/fitnese.
1 namespace HelloWorld
2 {
3 public class OurFirstTest : fit.ColumnFixture
4 {
5 public string string1;
6 public string string2;
7 public string Concatenate()
8 {
9 return string1 + " " + string2;
10 }
11 }
12 }
1 using System;
2 using Selenium;
3 namespace SeleniumTest
4 {
5 class Console
6 {
7 static void Main(string[] args)
8 {
9 ISelenium sel = new DefaultSelenium("localhost",
10 4444, "*iehta", "http://www.google.com");
11 sel.Start();
12 sel.Open("http://www.google.com/");
13 sel.Type("q", "FitNesse");
14 sel.Click("btnG");
15 sel.WaitForPageToLoad("3000");
16 }
17 }
18 }
1 using System;
2 using System.Collections.Generic;
3 using System.Text;
4
5 namespace Tristan
6 {
7 public interface IDraw
8 {
9 DateTime DrawDate { get; }
10 bool IsOpen { get; }
11 decimal TotalPoolSize { get;}
12
13 ITicket[] Tickets { get;}
14 void AddTicket(ITicket ticket);
15 }
16 }
1 using System;
2 using System.Collections.Generic;
3 using System.Text;
4
5 namespace Tristan
6 {
7 public class DrawNotOpenException : ApplicationException
8 {
9 public DrawNotOpenException()
10 : base("Draw is closed")
11 {
12 }
13 }
14 interface IDrawManager
15 {
16 IDraw GetDraw(DateTime date);
17 IDraw CreateDraw(DateTime drawDate);
18 void PurchaseTicket(DateTime drawDate, int playerId,
19 int[] numbers, decimal value);
20 void SettleDraw(DateTime drawDate, int[] results);
21 decimal OperatorDeductionFactor { get; }
22 List GetOpenTickets(int playerId);
23 List GetTickets(DateTime drawDate, int playerId);
24 }
25 }
1 using System;
2 using System.Collections.Generic;
3 using System.Text;
4
5 namespace Tristan
6 {
7 public interface IPlayerInfo
8 {
9 string Name { get;}
10 string Address { get;}
11 string City { get;}
12 string PostCode { get;}
13 string Country { get;}
14 string Username { get;}
15 decimal Balance { get;}
16 int PlayerId { get;}
17 bool IsVerified { get;}
18 }
19 }
1 using System;
2 using System.Collections.Generic;
3 using System.Text;
4
5 namespace Tristan
6 {
7 public class UnknownPlayerException : ApplicationException
8 {
9 public UnknownPlayerException() : base("Unknown user") { }
10 }
11 public class InvalidPasswordException: ApplicationException
12 {
13 public InvalidPasswordException():base("Invalid password"){}
14 }
15 public class DuplicateUsernameException : ApplicationException
16 {
17 public DuplicateUsernameException() : base("Duplicate username") { }
18 }
19 public class NotEnoughFundsException : ApplicationException
20 {
21 public NotEnoughFundsException() : base("Not enough funds") { }
22 }
23 public class TransactionDeclinedException : ApplicationException
24 {
25 public TransactionDeclinedException() : base("Transaction declined") { }
26 }
27
28 public interface IPlayerManager
29 {
30 int RegisterPlayer(IPlayerRegistrationInfo p);
31 IPlayerInfo GetPlayer(int id);
32 IPlayerInfo GetPlayer(String username);
33 int LogIn(String username, String password);
34 void AdjustBalance(int playerId, decimal amount);
35 void DepositWithCard(int playerId, String cardNumber,
36 String expiryDate, decimal amount);
37 }
38 }
1 using System;
2 using System.Collections.Generic;
3 using System.Text;
4
5 namespace Tristan
6 {
7 public interface IPlayerRegistrationInfo
8 {
9 string Name { get;}
10 string Address { get;}
11 string City { get;}
12 string PostCode { get;}
13 string Country { get;}
14 string Username { get;}
15 string Password { get;}
16 }
17 }
1 using System;
2 using System.Collections.Generic;
3 using System.Text;
4
5 namespace Tristan
6 {
7 public interface ITicket
8 {
9 int[] Numbers { get;}
10 IPlayerInfo Holder { get;}
11 decimal Value {get;}
12 bool IsOpen { get;}
13 decimal Winnings { get; }
14 DateTime draw { get; }
15 }
16 }
1 namespace Tristan
2 {
3 public class WinningsCalculator
4 {
5 public int GetPoolPercentage(int combination)
6 {
7 throw new Exception("Not implemented");
8 }
9 public decimal GetPrizePool(int combination, decimal payoutPool)
10 {
11 throw new Exception("Not implemented");
12 }
13 }
14 }
1 namespace Tristan
2 {
3 public class WinningsCalculator
4 {
5 public int GetPoolPercentage(int combination)
6 {
7 switch(combination) {
8 case 6: return 68;
9 case 5: return 10;
10 case 4: return 10;
11 case 3: return 12;
12 default: return 0;
13 }
14 }
15 public decimal GetPrizePool(int combination, decimal payoutPool)
16 {
17 return payoutPool * GetPoolPercentage(combination) / 100;
18 }
19 }
20 }
1 using System;
2 using System.Collections.Generic;
3 using System.Text;
4 using Tristan;
5 namespace Tristan.inproc
6 {
7 class Draw:IDraw
8 {
9 public Draw(DateTime drawDate)
10 {
11 this._totalSize = 0;
12 this._drawDate = drawDate;
13 this._isOpen = true;
14 this._tickets = new List();
15 }
16 private DateTime _drawDate;
17 public DateTime DrawDate
18 {
19 get { return _drawDate; }
20 }
21 private bool _isOpen;
22 public bool IsOpen
23 {
24 get { return _isOpen; }
25 set { _isOpen = value; }
26 }
27 private decimal _totalSize;
28 public decimal TotalPoolSize
29 {
30 get { return _totalSize; }
31 }
32 private List _tickets;
33 public ITicket[] Tickets
34 {
35 get { return _tickets.ToArray();}
36 }
37 public void AddTicket(ITicket ticket)
38 {
39 _tickets.Add(ticket);
40 _totalSize += ticket.Value;
41 }
42 }
43 }
1 using System;
2 using System.Collections.Generic;
3 using System.Text;
4 using Tristan;
5 namespace Tristan.inproc
6 {
7 public class DrawManager : IDrawManager
8 {
9 private Dictionary _draws=new Dictionary();
10 private IPlayerManager _playerManager;
11 public DrawManager(IPlayerManager playerMgr)
12 {
13 this._playerManager = playerMgr;
14 }
15 public IDraw GetDraw(DateTime date)
16 {
17 return _draws[date];
18 }
19 public IDraw CreateDraw(DateTime drawDate)
20 {
21 Draw d = new Draw(drawDate);
22 _draws[drawDate] = d;
23 return d;
24 }
25 public void PurchaseTicket(DateTime drawDate, int playerId, int[] numbers, decimal value)
26 {
27 if (!_draws.ContainsKey(drawDate))
28 throw new DrawNotOpenException();
29 Draw d = _draws[drawDate];
30 IPlayerInfo player=_playerManager.GetPlayer(playerId);
31 _playerManager.AdjustBalance(playerId, -1 * value);
32 d.AddTicket(new Ticket(player,drawDate, numbers,value));
33 }
34 private int CountCommonElements(int[] array1, int[] array2)
35 {
36 int common = 0;
37 foreach (int i in array1)
38 foreach (int j in array2)
39 if (i == j) common++;
40 return common;
41 }
42 public void SettleDraw(DateTime drawDate, int[] results)
43 {
44 WinningsCalculator wc = new WinningsCalculator();
45 Draw d = _draws[drawDate];
46 d.IsOpen = false;
47 Dictionary> ticketCategories=SplitTicketsIntoCategories(results, d);
48 for (int i = 0; i <= results.Length; i++)
49 {
50 decimal prizePool=wc.GetPrizePool(i, d.TotalPoolSize * (1-OperatorDeductionFactor));
51 foreach (Ticket t in ticketCategories[i])
52 {
53 t.IsOpen = false;
54 if (prizePool > 0)
55 {
56 decimal totalTicketValue = GetTotalTicketValue(ticketCategories[i]);
57 t.Winnings = t.Value * prizePool / totalTicketValue;
58 _playerManager.AdjustBalance(t.Holder.PlayerId,
59 t.Winnings);
60 }
61 }
62 }
63 }
64
65 private static decimal GetTotalTicketValue(List tickets)
66 {
67 decimal totalTicketValue = 0;
68 foreach (Ticket t in tickets)
69 totalTicketValue += t.Value;
70 return totalTicketValue;
71 }
72 public decimal OperatorDeductionFactor { get { return 0.5m; } }
73 private Dictionary> SplitTicketsIntoCategories(int[] results, Draw d)
74 {
75 Dictionary> ticketcategories = new Dictionary>();
76 for (int i = 0; i <= results.Length; i++)
77 ticketcategories[i] = new List();
78 foreach (Ticket t in d.Tickets)
79 {
80 int c = CountCommonElements(t.Numbers, results);
81 ticketcategories[c].Add(t);
82 }
83 return ticketcategories;
84 }
85
86
87 public List GetOpenTickets(int playerId)
88 {
89 List tickets = new List();
90 foreach (Draw d in _draws.Values)
91 {
92 if (d.IsOpen){
93 tickets.AddRange(GetTickets(d.DrawDate,playerId));
94 }
95 }
96 return tickets;
97 }
98 public List GetTickets(DateTime drawDate, int playerId)
99 {
100 List tickets = new List();
101 foreach (Ticket t in _draws[drawDate].Tickets)
102 {
103 if (t.Holder.PlayerId == playerId)
104 tickets.Add(t);
105 }
106 return tickets;
107 }
108 }
109 }
1 using System;
2 using System.Collections.Generic;
3 using System.Text;
4 using Tristan;
5 namespace Tristan.inproc
6 {
7 public class PlayerInfo:IPlayerInfo
8 {
9 private static int nextId=1;
10 public PlayerInfo(IPlayerRegistrationInfo reg)
11 {
12 this._address = reg.Address;
13 this._balance = 0;
14 this._city = reg.City;
15 this._country = reg.Country;
16 this._name = reg.Name;
17 this._postcode = reg.PostCode;
18 this._playerId = nextId++;
19 this._username = reg.Username;
20 this._password = reg.Password;
21 }
22
23 private string _password;
24 internal string Password { get { return _password; } }
25
26 private string _name;
27 public string Name
28 {
29 get { return _name; }
30 set { _name = value; }
31 }
32
33 private string _address;
34 public string Address
35 {
36 get { return _address; }
37 set { _address = value; }
38 }
39
40 private string _city;
41 public string City
42 {
43 get { return _city; }
44 set { _city = value; }
45 }
46
47 private string _postcode;
48 public string PostCode
49 {
50 get { return _postcode; }
51 set { _postcode = value; }
52 }
53 private string _country;
54 public string Country
55 {
56 get { return _country; }
57 set { _country = value; }
58 }
59
60 private string _username;
61 public string Username
62 {
63 get { return _username; }
64 set { _username = value; }
65 }
66
67 private decimal _balance;
68 public decimal Balance
69 {
70 get { return _balance; }
71 set { _balance = value; }
72 }
73 private int _playerId;
74 public int PlayerId
75 {
76 get { return _playerId; }
77 set { _playerId = value; }
78 }
79 private bool _verified;
80 public bool IsVerified {
81 get { return _verified; }
82 set { _verified = value; }
83 }
84 }
85 }
1 using System;
2 using System.Collections.Generic;
3 using System.Text;
4 using Tristan;
5 namespace Tristan.inproc
6 {
7 public class PlayerManager:IPlayerManager
8 {
9 private Dictionary _players = new Dictionary();
10 private Dictionary _playersByName = new Dictionary();
11 public PlayerManager() { }
12 public int RegisterPlayer(IPlayerRegistrationInfo p)
13 {
14 if (_playersByName.ContainsKey(p.Username)) throw new DuplicateUsernameException();
15 PlayerInfo np = new PlayerInfo(p);
16 _players.Add(np.PlayerId, np);
17 _playersByName.Add(np.Username, np);
18 return np.PlayerId;
19 }
20
21 public IPlayerInfo GetPlayer(int id)
22 {
23 return _players[id];
24 }
25 public IPlayerInfo GetPlayer(String username)
26 {
27 if (!_playersByName.ContainsKey(username)) throw new UnknownPlayerException();
28 PlayerInfo pi = _playersByName[username];
29 return pi;
30 }
31
32 public int LogIn(String username, String password)
33 {
34 if (!_playersByName.ContainsKey(username)) throw new UnknownPlayerException();
35 PlayerInfo pi = _playersByName[username];
36 if (password.Equals(pi.Password)) return pi.PlayerId;
37 throw new InvalidPasswordException();
38 }
39
40 public void AdjustBalance(int playerId, decimal amount)
41 {
42 PlayerInfo pi = _players[playerId];
43 if (amount < 0 && pi.Balance < (-1 * amount))
44 throw new NotEnoughFundsException();
45 pi.Balance += amount;
46 }
47
48 public void DepositWithCard(int playerId, string cardNumber, string expiryDate, decimal amount)
49 {
50 if (cardNumber.EndsWith("2"))
51 throw new TransactionDeclinedException();
52 PlayerInfo pi = _players[playerId];
53 pi.Balance += amount;
54 }
55 }
56 }
1 using System;
2 using System.Collections.Generic;
3 using System.Text;
4 using Tristan.Test;
5 namespace Tristan.inproc
6 {
7 public class PlayerRegistrationInfo: IPlayerRegistrationInfo
8 {
9 private string _name;
10 public string Name { get { return _name; } set { _name = value; } }
11
12 private string _address;
13 public string Address { get { return _address; } set { _address = value; } }
14
15 private string _city;
16 public string City { get { return _city; } set { _city = value; } }
17
18 private string _postCode;
19 public string PostCode { get { return _postCode; } set { _postCode = value; } }
20
21 private string _country;
22 public string Country { get { return _country; } set { _country = value; } }
23
24 private string _username;
25 public string Username { get { return _username; } set { _username = value; } }
26
27 private string _password;
28 public string Password { get { return _password; } set { _password = value; } }
29 }
30 }
1 using System;
2 using System.Collections.Generic;
3 using System.Text;
4
5 namespace Tristan.inproc
6 {
7 class Ticket:ITicket
8 {
9 public Ticket(IPlayerInfo holder, DateTime draw, int[] numbers, decimal value)
10 {
11 _numbers = new int[numbers.Length];
12 System.Array.Copy(numbers, _numbers, numbers.Length);
13 _holder = holder;
14 _value = value;
15 _open = true;
16 _winnings = 0;
17 _drawDate = draw;
18 }
19 private int[] _numbers;
20 public int[] Numbers
21 {
22 get { return _numbers; }
23 }
24
25 private IPlayerInfo _holder;
26 public IPlayerInfo Holder
27 {
28 get { return _holder; }
29 }
30 private decimal _value;
31 public decimal Value
32 {
33 get { return _value; }
34 }
35
36 private bool _open;
37 public bool IsOpen
38 {
39 get { return _open; }
40 set { _open = value; }
41 }
42 private decimal _winnings;
43 public decimal Winnings
44 {
45 get { return _winnings; }
46 set { _winnings = value; }
47 }
48 private DateTime _drawDate;
49 public DateTime draw
50 {
51 get { return _drawDate; }
52 }
53 }
54 }
1 namespace Tristan.Test
2 {
3 public class PayoutTable:fit.ColumnFixture
4 {
5 private WinningsCalculator wc=new WinningsCalculator();
6 public int winningCombination;
7 public decimal payoutPool;
8 public int PoolPercentage()
9 {
10 return wc.GetPoolPercentage(winningCombination);
11 }
12 public decimal PrizePool()
13 {
14 return wc.GetPrizePool(winningCombination, payoutPool);
15 }
16 }
17 }
1 using fit;
2 using Tristan.inproc;
3 using System;
4 namespace Tristan.Test
5 {
6 public class SetUpTestEnvironment : Fixture
7 {
8 internal static IPlayerManager playerManager;
9 public SetUpTestEnvironment()
10 {
11 playerManager = new PlayerManager();
12 }
13 }
14 }
15 namespace Tristan.Test.FirstTry
16 {
17 public class PlayerRegisters : ColumnFixture
18 {
19 public string Username;
20 public string Password;
21 public int PlayerId()
22 {
23 PlayerRegistrationInfo reg = new PlayerRegistrationInfo();
24 reg.Username = Username;
25 reg.Password = Password;
26 return SetUpTestEnvironment.playerManager.RegisterPlayer(reg);
27 }
28 }
29 public class CheckStoredDetails : ColumnFixture
30 {
31 public int PlayerId;
32 public string Username
33 {
34 get
35 {
36 return SetUpTestEnvironment.playerManager.
37 GetPlayer(PlayerId).Username;
38 }
39 }
40 public decimal Balance
41 {
42 get
43 {
44 return SetUpTestEnvironment.playerManager.
45 GetPlayer(PlayerId).Balance;
46 }
47 }
48 }
49 public class CheckLogIn:ColumnFixture{
50 public string Username;
51 public string Password;
52 public bool CanLogIn()
53 {
54 try
55 {
56 SetUpTestEnvironment.playerManager.LogIn(Username, Password);
57 return true;
58 }
59 catch (ApplicationException)
60 {
61 return false;
62 }
63 }
64 }
65 }
66 namespace Tristan.Test.SecondTry
67 {
68 public class PlayerRegisters : ColumnFixture
69 {
70 public class ExtendedPlayerRegistrationInfo: PlayerRegistrationInfo
71 {
72 public int PlayerId()
73 {
74 return SetUpTestEnvironment.playerManager.RegisterPlayer(this);
75 }
76 }
77 private ExtendedPlayerRegistrationInfo to =
78 new ExtendedPlayerRegistrationInfo();
79 public override object GetTargetObject()
80 {
81 return to;
82 }
83 }
84 public class CheckStoredDetailsFor : ColumnFixture
85 {
86 public override object GetTargetObject()
87 {
88 int newid=(int)Fixture.Recall(Args[0]);
89 return SetUpTestEnvironment.playerManager.GetPlayer(newid);
90 }
91 }
92 public class CheckLogIn : ColumnFixture
93 {
94 public string Username;
95 public string Password;
96 public int LoggedInAsPlayerId()
97 {
98 return SetUpTestEnvironment.playerManager.
99 LogIn(Username, Password);
100 }
101 }
102 }
1 using System;
2 using System.Collections.Generic;
3 using System.Text;
4 using Tristan.inproc;
5 using fit;
6 namespace Tristan.Test.PurchaseTicket
7 {
8 public class SetUpTestEnvironment : ColumnFixture
9 {
10 internal static IPlayerManager playerManager;
11 internal static IDrawManager drawManager;
12 public SetUpTestEnvironment()
13 {
14 playerManager = new PlayerManager();
15 drawManager = new DrawManager(playerManager);
16 }
17 public DateTime CreateDraw {
18 set
19 {
20 drawManager.CreateDraw(value);
21 }
22 }
23 }
24 public class PlayerRegisters : ColumnFixture
25 {
26 public class ExtendedPlayerRegistrationInfo:
27 PlayerRegistrationInfo
28 {
29 public int PlayerId()
30 {
31 return SetUpTestEnvironment.playerManager.
32 RegisterPlayer(this);
33 }
34 }
35 private ExtendedPlayerRegistrationInfo to =
36 new ExtendedPlayerRegistrationInfo();
37 public override object GetTargetObject()
38 {
39 return to;
40 }
41 }
42 public class PurchaseTicket : fitlibrary.DoFixture
43 {
44 public void PlayerDepositsDollarsWithCardAndExpiryDate(
45 string username, decimal amount, string card, string expiry)
46 {
47 int pid = SetUpTestEnvironment.playerManager.
48 GetPlayer(username).PlayerId;
49 SetUpTestEnvironment.playerManager.DepositWithCard(
50 pid, card, expiry, amount);
51 }
52 public bool PlayerHasDollars(String username, decimal amount)
53 {
54 return (SetUpTestEnvironment.playerManager.
55 GetPlayer(username).Balance == amount);
56 }
57 public void PlayerBuysATicketWithNumbersForDrawOn(
58 string username, int[] numbers, DateTime date)
59 {
60 PlayerBuysTicketsWithNumbersForDrawOn(
61 username, 1, numbers, date);
62 }
63 public void PlayerBuysTicketsWithNumbersForDrawOn(
64 string username, int tickets, int[] numbers, DateTime date)
65 {
66 int pid = SetUpTestEnvironment.playerManager.
67 GetPlayer(username).PlayerId;
68 SetUpTestEnvironment.drawManager.PurchaseTicket(
69 date, pid, numbers, 10*tickets);
70 }
71 public bool PoolValueForDrawOnIsDollars(DateTime date,
72 decimal amount)
73 {
74 return SetUpTestEnvironment.drawManager.GetDraw(date).
75 TotalPoolSize == amount;
76 }
77 private static bool CompareArrays(int[] sorted1, int[] unsorted2)
78 {
79 if (sorted1.Length != unsorted2.Length) return false;
80 Array.Sort(unsorted2);
81 for (int i = 0; i < sorted1.Length; i++)
82 {
83 if (sorted1[i] != unsorted2[i]) return false;
84 }
85 return true;
86 }
87 public bool
88 TicketWithNumbersForDollarsIsRegisteredForPlayerForDrawOn(
89 int[] numbers, decimal amount, string username, DateTime draw)
90 {
91 ITicket[] tck = SetUpTestEnvironment.
92 drawManager.GetDraw(draw).Tickets;
93 Array.Sort(numbers);
94 foreach (ITicket ticket in tck)
95 {
96 if (CompareArrays(numbers, ticket.Numbers) &&
97 amount == ticket.Value &&
98 username.Equals(ticket.Holder.Username))
99 return true;
100 }
101 return false;
102 }
103 public int TicketsInDrawOn(DateTime date)
104 {
105 return SetUpTestEnvironment.drawManager.
106 GetDraw(date).Tickets.Length;
107 }
108 public decimal PoolValueForDrawOnIs(DateTime date)
109 {
110 return SetUpTestEnvironment.drawManager.
111 GetDraw(date).TotalPoolSize;
112 }
113 public decimal AccountBalanceFor(String username)
114 {
115 return SetUpTestEnvironment.playerManager.
116 GetPlayer(username).Balance;
117 }
118 }
119 }
1 using System;
2 using System.Collections.Generic;
3 using System.Text;
4 using Tristan;
5 using Tristan.inproc;
6 using fit;
7 namespace Tristan.Test
8 {
9 public class ReviewTickets:fitlibrary.DoFixture
10 {
11 private IDrawManager _drawManager;
12 private IPlayerManager _playerManager;
13 public ReviewTickets()
14 {
15 _playerManager = new PlayerManager();
16 _drawManager = new DrawManager(_playerManager);
17 }
18 public void DrawOnIsOpen(DateTime drawDate)
19 {
20 _drawManager.CreateDraw(drawDate);
21 }
22 public void PlayerOpensAccountWithDollars(String player, decimal balance)
23 {
24 PlayerRegistrationInfo p = new PlayerRegistrationInfo();
25 p.Username = player; p.Name = player;
26 p.Password = "XXXXXX";
27 // define other mandatory properties
28 int playerId = _playerManager.RegisterPlayer(p);
29 _playerManager.AdjustBalance(playerId, balance);
30 }
31 public void PlayerBuysATicketWithNumbersForDrawOn(
32 string username, int[] numbers, DateTime date)
33 {
34 PlayerBuysTicketsWithNumbersForDrawOn(username, 1, numbers, date);
35 }
36
37 public void PlayerBuysTicketsWithNumbersForDrawOn(
38 string username, int tickets, int[] numbers, DateTime date)
39 {
40 int pid = _playerManager.GetPlayer(username).PlayerId;
41 _drawManager.PurchaseTicket(date, pid, numbers, 10 * tickets);
42 }
43 public IList PlayerListsOpenTickets(String player)
44 {
45 return _drawManager.GetOpenTickets(
46 _playerManager.GetPlayer(player).PlayerId);
47 }
48 public IList PlayerListsTicketsForDrawOn(
49 String player, DateTime date)
50 {
51 return _drawManager.GetTickets(
52 date,_playerManager.GetPlayer(player).PlayerId);
53 }
54 public void NumbersAreDrawnOn(int[] numbers, DateTime date)
55 {
56 _drawManager.SettleDraw(date, numbers);
57 }
58 }
59 public class ReviewTicketsWithRowFixture : fitlibrary.DoFixture
60 {
61 private IDrawManager _drawManager;
62 private IPlayerManager _playerManager;
63 public ReviewTicketsWithRowFixture()
64 {
65 _playerManager = new PlayerManager();
66 _drawManager = new DrawManager(_playerManager);
67 }
68 public void DrawOnIsOpen(DateTime drawDate)
69 {
70 _drawManager.CreateDraw(drawDate);
71 }
72 public void PlayerOpensAccountWithDollars(
73 String player, decimal balance){
74 PlayerRegistrationInfo p = new PlayerRegistrationInfo();
75 p.Username = player; p.Name = player;
76 p.Password = "XXXXXX";
77 // define other mandatory properties
78 int playerId = _playerManager.RegisterPlayer(p);
79 _playerManager.AdjustBalance(playerId, balance);
80 }
81 public void PlayerBuysATicketWithNumbersForDrawOn(
82 string username, int[] numbers, DateTime date)
83 {
84 PlayerBuysTicketsWithNumbersForDrawOn(
85 username, 1, numbers, date);
86 }
87
88 public void PlayerBuysTicketsWithNumbersForDrawOn(
89 string username, int tickets, int[] numbers, DateTime date)
90 {
91 int pid = _playerManager.GetPlayer(username).PlayerId;
92 _drawManager.PurchaseTicket(date, pid, numbers, 10 * tickets);
93 }
94 public RowFixture PlayerListsOpenTickets(String player)
95 {
96 return new TicketRowFixture(
97 _drawManager.GetOpenTickets(
98 _playerManager.GetPlayer(player).PlayerId));
99 }
100 public RowFixture PlayerListsTicketsForDrawOn(
101 String player, DateTime date)
102 {
103 return new TicketRowFixture(
104 _drawManager.GetTickets(date,
105 _playerManager.GetPlayer(player).PlayerId));
106 }
107 public void NumbersAreDrawnOn(int[] numbers, DateTime date)
108 {
109 _drawManager.SettleDraw(date, numbers);
110 }
111 }
112 public class TicketRowFixture : fit.RowFixture
113 {
114 private List _internalList;
115 public TicketRowFixture(List tickets)
116 {
117 _internalList = tickets;
118 }
119 public override Type GetTargetClass()
120 {
121 return typeof(ITicket);
122 }
123
124 public override object[] Query()
125 {
126 return _internalList.ToArray();
127 }
128 }
129 }
1 using System;
2 using System.Collections.Generic;
3 using System.Text;
4 using fit;
5 using Tristan.inproc;
6 namespace Tristan.Test
7 {
8 }
1 using System;
2 using System.Collections.Generic;
3 using System.Text;
4 using Tristan;
5 using Tristan.inproc;
6 using fitlibrary;
7 using fit;
8 namespace Tristan.Test.Settlement
9 {
10 internal class BalanceCheckFixture : ColumnFixture
11 {
12 private IPlayerManager _playerManager;
13 public BalanceCheckFixture(IPlayerManager pm)
14 {
15 _playerManager = pm;
16 }
17 public String player;
18 public decimal Balance
19 {
20 get
21 {
22 return _playerManager.GetPlayer(player).Balance;
23 }
24 }
25 }
26 internal class CreatePlayerFixture : SetUpFixture
27 {
28 private IPlayerManager _playerManager;
29 public CreatePlayerFixture(IPlayerManager pm)
30 {
31 _playerManager = pm;
32 }
33 public void PlayerBalance(String player, decimal balance)
34 {
35 PlayerRegistrationInfo p = new PlayerRegistrationInfo();
36 p.Username = player; p.Name = player;
37 p.Password = "XXXXXX";
38 // define other mandatory properties
39 int playerId = _playerManager.RegisterPlayer(p);
40 _playerManager.AdjustBalance(playerId, balance);
41 }
42 }
43 internal class TicketPurchaseFixture: SetUpFixture
44 {
45 private IDrawManager _drawManager;
46 private DateTime _drawDate;
47 private IPlayerManager _playerManager;
48
49 public TicketPurchaseFixture(IPlayerManager pm, IDrawManager dm,
50 DateTime drawDate)
51 {
52 _drawManager = dm;
53 _playerManager = pm;
54 _drawDate = drawDate;
55 }
56 public void PlayerNumbersValue(String player, int[] numbers, decimal value)
57 {
58 _drawManager.PurchaseTicket(_drawDate,
59 _playerManager.GetPlayer(player).PlayerId, numbers, value);
60 }
61 }
62 public class SettlementTest:DoFixture
63 {
64 private IDrawManager drawManager;
65 private IPlayerManager playerManager;
66 private DateTime drawDate;
67 public SettlementTest()
68 {
69 playerManager = new PlayerManager();
70 drawManager = new DrawManager(playerManager);
71 drawDate = DateTime.Now;
72 drawManager.CreateDraw(drawDate);
73 }
74 public Fixture TicketsInTheDraw()
75 {
76 return new TicketPurchaseFixture(playerManager, drawManager, drawDate);
77 }
78 public void DrawResultsAre(int[] numbers)
79 {
80 drawManager.SettleDraw(drawDate, numbers);
81 }
82 public Fixture AccountsAfterTheDraw()
83 {
84 return new BalanceCheckFixture(playerManager);
85 }
86 public Fixture AccountsBeforeTheDraw()
87 {
88 return new CreatePlayerFixture(playerManager);
89 }
90 }
91 }
1 using System;
2
3 namespace Tristan.Test
4 {
5 public class PrizeDistributionForPayoutPool:fit.ColumnFixture {
6 private WinningsCalculator wc = new WinningsCalculator();
7 public int winningCombination;
8 public int PoolPercentage()
9 {
10 return wc.GetPoolPercentage(winningCombination);
11 }
12 public decimal? payoutPool;
13 public decimal PrizePool()
14 {
15 if (payoutPool == null) payoutPool = Decimal.Parse(Args[0]);
16 return wc.GetPrizePool(winningCombination, payoutPool.Value);
17 }
18 }
19 }
1 using System;
2 using System.Collections.Generic;
3 using System.Text;
4 using fit;
5 using fitlibrary;
6 namespace info.fitnesse
7 {
8
9 public class LinkInsertFixture : ColumnFixture
10 {
11 private ILinkRepository repo;
12 Link l;
13 public LinkInsertFixture(ILinkRepository repo)
14 {
15 this.repo = repo;
16 }
17 public int Id
18 {
19 get
20 {
21 return repo.Save(l);
22 }
23 }
24 public override void Reset()
25 {
26 l = new Link(); SetSystemUnderTest(l);
27 }
28
29 }
30 public class LinkCheckFixture : ColumnFixture
31 {
32 private ILinkRepository repo;
33 public LinkCheckFixture(ILinkRepository repo)
34 {
35 this.repo = repo;
36 }
37 public int Id
38 {
39 set
40 {
41 SetSystemUnderTest(repo.FindById(value));
42 }
43 }
44 }
45 public class AlternatingSUT : DoFixture
46 {
47 private ILinkRepository repo = new MemoryLinkRepository();
48 public Fixture DefineLinks()
49 {
50 return new LinkInsertFixture(repo);
51 }
52 public Fixture CheckLinks()
53 {
54 return new LinkCheckFixture(repo);
55 }
56 }
57 }
1 using System;
2 using System.Collections.Generic;
3 using System.Text;
4 using fit;
5 namespace info.fitnesse
6 {
7 public class AutomaticDomainObjectWrapping: fitlibrary.DoFixture
8 {
9
10 }
11 }
1 using System;
2 using System.Collections.Generic;
3 using System.Text;
4
5 namespace info.fitnesse
6 {
7 public class Link
8 {
9 public Link()
10 {
11 }
12 public Link(String name, String url)
13 {
14 this.Name = name;
15 this.Url = url;
16 }
17 public int Id { get; set; }
18 public String Name { get; set; }
19 public String Url { get; set; }
20 public Boolean Valid
21 {
22 get
23 {
24 return
25 ((!String.IsNullOrEmpty(Name)) &&
26 (!String.IsNullOrEmpty(Url)) &&
27 Url.Contains("://"));
28 }
29 }
30 }
31 public interface ILinkRepository
32 {
33 Link FindById(int id);
34 IEnumerable FindAll();
35 int Save(Link l);
36
37 }
38 public class MemoryLinkRepository : ILinkRepository
39 {
40 private List links=new List();
41 public Link FindById(int id)
42 {
43 return links[id];
44 }
45 public IEnumerable FindAll()
46 {
47 return links;
48 }
49
50
51 public int Save(Link l)
52 {
53 int nextIndex = links.Count;
54 links.Add(l);
55 return nextIndex;
56 }
57 }
58 }
1 using System;
2 using System.Collections.Generic;
3 using fit;
4 using fitlibrary;
5 namespace info.fitnesse
6 {
7 public class LinkSetupFixture : SetUpFixture
8 {
9 private ILinkRepository repo;
10 public LinkSetupFixture(ILinkRepository repo)
11 {
12 this.repo = repo;
13 }
14 public void NameUrl(String name, String url)
15 {
16 repo.Save(new Link { Url = url, Name = name });
17 }
18 }
19 public class FlowCollections: DoFixture
20 {
21 private ILinkRepository repo = new MemoryLinkRepository();
22 public Fixture DefineLinks()
23 {
24 return new LinkSetupFixture(repo);
25 }
26 public IEnumerable ListLinks()
27 {
28 return repo.FindAll();
29 }
30 }
31 public class FlowSystemUnderTest : DoFixture
32 {
33 private ILinkRepository repo = new MemoryLinkRepository();
34 public Fixture DefineLinks()
35 {
36 return new LinkSetupFixture(repo);
37 }
38 public FlowSystemUnderTest()
39 {
40 SetSystemUnderTest(repo);
41 }
42 }
43 public class WithSystemUnderTest : DoFixture
44 {
45 public Fixture DefineLinks()
46 {
47 return new LinkSetupFixture((ILinkRepository) this.mySystemUnderTest);
48 }
49 }
50 }
1 using System;
2 using System.Collections.Generic;
3 using System.Text;
4
5 namespace info.fitnesse.sut
6 {
7 public class LinkValidityCheck:fit.ColumnFixture
8 {
9 public LinkValidityCheck()
10 {
11 SetSystemUnderTest(new Link());
12 }
13 public String comment;
14 }
15 }
1 using System;
2 using System.Collections.Generic;
3 using System.Text;
4
5 namespace info.fitnesse.to
6 {
7 public class LinkValidityCheck:fit.ColumnFixture
8 {
9 Link l = new Link();
10 public override Object GetTargetObject()
11 {
12 return l;
13 }
14 }
15 }
1 using fitSharp.Fit.Operators;
2 using fitSharp.Machine.Engine;
3 using fitSharp.Fit.Model;
4 using fitSharp.Machine.Model;
5 using System;
6
7 namespace extended
8 {
9 public class CurrencyParser: CellOperator, ParseOperator
10 {
11
12 public bool CanParse(Type type, TypedValue instance, Tree parameters)
13 {
14 return type == typeof(decimal) && parameters.Value.Text.StartsWith("$");
15 }
16
17 public TypedValue Parse(Type type, TypedValue instance, Tree| parameters)
18 {
19 return new TypedValue (Decimal.Parse(parameters.Value.Text.Substring(1)));
20 }
21 }
22 }
| | |
1 using System;
2 using System.Collections.Generic;
3 using System.Text;
4
5 namespace extended
6 {
7 class TaxCalculator
8 {
9 public decimal GetTax(String code, decimal price)
10 {
11 if (code.StartsWith("B")) return 0;
12 return 0.1m * price;
13 }
14 }
15 public class Invoice:fitnesse.fixtures.TableFixture
16 {
17 protected override void DoStaticTable(int rows)
18 {
19 TaxCalculator tc=new TaxCalculator();
20 decimal totaltax = 0;
21 for (int row = 1; row < rows - 3; row++)
22 {
23 totaltax += tc.GetTax(GetString(row, 1),
24 Decimal.Parse(GetString(row, 2)));
25 }
26 decimal taxintable = Decimal.Parse(GetString(rows - 2, 2));
27 if (taxintable == totaltax)
28 Right(rows - 2, 2);
29 else
30 Wrong(rows - 2, 2, totaltax.ToString());
31 }
32 }
33 }
1 using System.Text.RegularExpressions;
2 using fitnesse.handlers;
3 using fitSharp.Fit.Model;
4 using fitSharp.Machine.Engine;
5 using fitSharp.Machine.Model;
6 using fitSharp.Fit.Operators;
7 namespace extended
8 {
9 public class RegExHandler : CellOperator, CompareOperator
10 {
11 public bool CanCompare(TypedValue actual, Tree expected)
12 {
13
14 string searchString = expected.Value.Text;
15 System.Console.Out.WriteLine("searchString");
16 return searchString.StartsWith("/") &&
17 searchString.EndsWith("/");
18 }
19 public bool Compare(TypedValue actual, Tree| cell)
20 {
21
22 object actualValue = actual.Value;
23 Regex expected = new Regex(cell.Value.Text.Substring(1, cell.Value.Text.Length - 2));
24 return expected.IsMatch(actualValue.ToString());
25 }
26 }
27 }
| | |
1 using System;
2 using System.Collections.Generic;
3 using System.Text;
4 using Selenium;
5
6 namespace webfixture
7 {
8 public class WebTest : fitlibrary.DoFixture
9 {
10 private ISelenium instance;
11 public void StartBrowserWithSeleniumConsoleOnAtPortAndScriptsAt(
12 String browser, String rcServer, int rcPort, String seleniumURL)
13 {
14 instance = new DefaultSelenium(rcServer,
15 rcPort, browser, seleniumURL);
16 instance.Start();
17 }
18 public void ShutdownBrowser()
19 {
20 instance.Stop();
21 }
22 public static readonly string[] buttonLocators = new String[] {
23 "xpath=//input[@type='submit' and @name='{0}']",
24 "xpath=//input[@type='button' and @name='{0}']",
25 "xpath=//input[@type='submit' and @value='{0}']",
26 "xpath=//input[@type='button' and @value='{0}']",
27 "xpath=//input[@type='submit' and @id='{0}']",
28 "xpath=//input[@type='button' and @id='{0}']"};
29
30 public static readonly string[] selectLocators = new String[] {
31 "xpath=//select[@name='{0}']",
32 "xpath=//select[@id='{0}']"};
33
34 private String GetLocator(String caption, String[] possibleFormats)
35 {
36 foreach (String s in possibleFormats)
37 {
38 String locator = String.Format(s, caption);
39 if (instance.IsElementPresent(locator))
40 {
41 return locator;
42 }
43 }
44 throw new ApplicationException(
45 "Cannot find element by " + caption);
46 }
47 public void UserOpensURL(String s)
48 {
49 instance.Open(s);
50 }
51 public static readonly string[] textFieldLocators = new String[] {
52 "xpath=//input[@type='text' and @name='{0}']",
53 "xpath=//input[@type='password' and @name='{0}']",
54 "xpath=//textarea[@name='{0}']",
55 "xpath=//input[@type='text' and @id='{0}']",
56 "xpath=//input[@type='password' and @id='{0}']",
57 "xpath=//textarea[@id='{0}']"};
58
59 public void UserTypesIntoField(String what, String where)
60 {
61 instance.Type(GetLocator(
62 where.Replace(" ", ""), textFieldLocators), what);
63 }
64 public void UserClicksOn(String buttonCaption)
65 {
66 instance.Click(GetLocator(buttonCaption, buttonLocators));
67 }
68 public void PageReloadsInLessThanSeconds(String sec)
69 {
70 instance.WaitForPageToLoad(sec + "000");
71 }
72 public bool PageContainsText(String s)
73 {
74 return instance.IsTextPresent(s);
75 }
76 public bool PageURLIs(String s)
77 {
78 return s.Equals(instance.GetLocation());
79 }
80 public void UserSelectsFrom(String what, String where)
81 {
82 instance.Select(
83 GetLocator(where.Replace(" ", ""), selectLocators), what);
84 }
85 }
86 }



