Trace: » modes » sourcecode
Table of Contents
The Fixture Gallery is also available as a PDF document and a live FitNesse wiki. See http://gojko.net/fitnesse/fixturegallery for more information.
Previous page: Introduction Next page: Basic FIT fixtures Parent page: Table of contents
Source code
You can get the source code for all the examples from this document from SourceForge (http://sourceforge.net/projects/fixturegallery). A lot of examples in this document demonstrate how to work with business domain services and objects. To keep things as simple as possible, and still show all relevant features, all those examples use the following "business" classes:
Java domain code
package info.fitnesse.fixturegallery.domain;
import java.util.ArrayList;
import java.util.List;
public class Player{
public String name;
public String postCode;
public double balance;
public double getCreditLimit(){
return balance;
}
public Player(){
}
public Player(String name, String postCode, double balance){
this.name=name; this.postCode=postCode; this.balance=balance;
}
public static List<Player> players=new ArrayList<Player>();
public static void addPlayer(String name, String postCode, double balance){
players.add(new Player(name,postCode,balance));
}
}
package info.fitnesse.fixturegallery.domain;
import java.util.LinkedList;
public class Queue {
private LinkedList<String> ll=new java.util.LinkedList<String>();
public String dequeue(){
return ll.poll();
}
public void enqueue(String s){
ll.add(s);
}
public int getCount(){
return ll.size();
}
}
package info.fitnesse.fixturegallery.domain;
public class TaxCalculator
{
public double GetTax(String code, double price)
{
if (code.startsWith("B")) return 0;
return 0.1 * price;
}
}
package info.fitnesse.fixturegallery.domain;
public class Words {
public String firstPart;
public String secondPart;
public String together(){
return firstPart+", "+secondPart;
}
public String getSentence(){
return together();
}
public static Words[] firstExample(){
Words[] someWords=new Words[2];
someWords[0]=new Words();
someWords[0].firstPart="Hello";
someWords[0].secondPart="World";
someWords[1]=new Words();
someWords[1].firstPart="Houston";
someWords[1].secondPart="We Have a Problem";
return someWords;
}
}
.NET domain code
using System;
using System.Collections.Generic;
using System.Text;
namespace info.fitnesse.fixturegallery
{
public class Player
{
public static List<Player> players=new List<Player>();
private string _name;
public string Name
{
get { return _name; }
set { _name = value; }
}
private string _postCode;
public string PostCode
{
get { return _postCode; }
set { _postCode= value; }
}
private decimal _balance;
public decimal Balance
{
get { return _balance; }
set { _balance = value; }
}
public decimal CreditLimit()
{
return _balance;
}
}
public class Text
{
public String word;
public Text(String w)
{
word = w;
}
public int TotalLength
{
get { return word.Length; }
}
}
public class TaxCalculator
{
public decimal GetTax(String code, decimal price)
{
if (code.StartsWith("B")) return 0;
return 0.1m * price;
}
}
}
Python domain code
class Player(object):
_typeDict = {
"name": "String",
"postCode": "String",
"balance": "Float"
}
def __init__(self, name="", postCode="", balance=0.0):
self.name = name
self.postCode = postCode
self.balance = balance
def getCreditLimit(self):
return self.balance
_typeDict["creditLimit"] = "Float"
creditLimit = property(getCreditLimit)
# -- DATA STORE: Players object store, simplistic database.
players = [] #< CLASS-ATTRIBUTE
@classmethod
def addPlayer(cls, name, postCode, balance):
cls.players.append(Player(name, postCode, balance))
class Queue(object):
_typeDict = {
"Enqueue.RenameTo": "enqueue" #< NEEDED-FOR: SystemUnderTestExample
}
def __init__(self):
self.ll = []
_typeDict["dequeue"] = "String"
_typeDict["dequeue.types"] = [ "String" ]
def dequeue(self):
return self.ll.pop(0)
_typeDict["enqueue"] = "String"
_typeDict["enqueue.types"] = [ None, "String" ]
def enqueue(self, s):
return self.ll.append(s)
def getCount(self):
return len(self.ll)
_typeDict["count"] = "Integer"
count = property(getCount)
import types
class TaxCalculator(object):
def getTax(self, code, price):
assert isinstance(code, types.StringTypes)
assert type(price) == types.FloatType
if code.startswith("B"):
return 0
return 0.1 * price
class Words(object):
_typeDict = {
"firstPart": "String",
"secondPart": "String"
}
def __init__(self, firstPart="", secondPart=""):
self.firstPart = firstPart
self.secondPart = secondPart
_typeDict["together"] = "String"
def together(self):
return "%s, %s" % (self.firstPart, self.secondPart)
_typeDict["getSentence"] = "String"
def getSentence(self):
return self.together()
@staticmethod
def firstExample():
someWords = [ None, None ]
someWords[0] = Words(firstPart="Hello", secondPart="World")
someWords[1] = Words(firstPart="Houston", secondPart="We Have a Problem")
return someWords
Smalltalk domain code
'From VisualWorks®, 7.6 of March 3, 2008 on July 3, 2008 at 1:31:48 pm'! Smalltalk defineNameSpace: #Info private: false imports: ' private Smalltalk.* ' category: ''! Smalltalk.Info defineNameSpace: #Fitnesse private: false imports: ' private Smalltalk.* ' category: ''! Info.Fitnesse defineNameSpace: #Fixturegallery private: false imports: ' private Smalltalk.* private Fit.* private Info.Fitnesse.Fixturegallery.Domain.* ' category: ''! Info.Fitnesse.Fixturegallery defineNameSpace: #Domain private: false imports: ' private Smalltalk.* ' category: ''!
'From VisualWorks®, 7.6 of March 3, 2008 on July 3, 2008 at 1:31:55 pm'!
Info.Fitnesse.Fixturegallery.Domain defineClass: #Player
superclass: #{Core.Object}
indexedType: #none
private: false
instanceVariableNames: 'name postCode balance '
classInstanceVariableNames: ''
imports: ''
category: ''!
Info.Fitnesse.Fixturegallery.Domain.Player defineSharedVariable: #Players
private: false
constant: false
category: 'accessing'
initializer: 'OrderedCollection new'!
#{Info.Fitnesse.Fixturegallery.Domain.Player.Players} initialize!
"-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- "!
!Info.Fitnesse.Fixturegallery.Domain.Player class methodsFor: 'players'!
addPlayerNamed: nameString postCode: postCodeString balance: aNumber
Players
add: (self name: nameString postCode: postCodeString balance: aNumber)!
clearPlayers
Players removeAll!
players
^Players! !
!Info.Fitnesse.Fixturegallery.Domain.Player class methodsFor: 'instance creation'!
name: nameString postCode: postCodeString balance: aNumber
^(self new)
name: nameString;
postCode: postCodeString;
balance: aNumber;
yourself! !
!Info.Fitnesse.Fixturegallery.Domain.Player class methodsFor: 'type access'!
signatureFor: aSymbol
(#(#creditLimit #balance) includes: aSymbol) ifTrue: [^Number].
^String! !
"-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- "!
!Info.Fitnesse.Fixturegallery.Domain.Player methodsFor: 'accessing'!
balance
^balance!
balance: aNumber
balance := aNumber!
creditLimit
^balance!
name
^name!
name: aString
name := aString!
postCode
^postCode!
postCode: aString
postCode := aString! !
!Info.Fitnesse.Fixturegallery.Domain.Player methodsFor: 'type access'!
signatureFor: aSymbol
^self class signatureFor: aSymbol! !
'From VisualWorks®, 7.6 of March 3, 2008 on June 27, 2008 at 3:35:59 pm'!
Info.Fitnesse.Fixturegallery.Domain defineClass: #Queue
superclass: #{Core.Object}
indexedType: #none
private: false
instanceVariableNames: 'queue '
classInstanceVariableNames: ''
imports: ''
category: ''!
"-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- "!
!Info.Fitnesse.Fixturegallery.Domain.Queue class methodsFor: 'instance creation'!
new
^super new initialize! !
"-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- "!
!Info.Fitnesse.Fixturegallery.Domain.Queue methodsFor: 'accessing'!
count
^queue size! !
!Info.Fitnesse.Fixturegallery.Domain.Queue methodsFor: 'operations'!
dequeue
^queue removeFirst!
enqueue: aString
queue add: aString! !
!Info.Fitnesse.Fixturegallery.Domain.Queue methodsFor: 'initialize-release'!
initialize
queue := OrderedCollection new! !
!Info.Fitnesse.Fixturegallery.Domain.Queue methodsFor: 'type access'!
signatureFor: aSymbol
aSymbol == #enqueue: ifTrue: [^Fit.MethodSignature with: String].
aSymbol == #count ifTrue: [^Integer].
aSymbol == #dequeue ifTrue: [^String]! !
'From VisualWorks®, 7.6 of March 3, 2008 on June 27, 2008 at 3:36:04 pm'!
Info.Fitnesse.Fixturegallery.Domain defineClass: #TaxCalculator
superclass: #{Core.Object}
indexedType: #none
private: false
instanceVariableNames: ''
classInstanceVariableNames: ''
imports: ''
category: ''!
!Info.Fitnesse.Fixturegallery.Domain.TaxCalculator methodsFor: 'accessing'!
taxOn: aNumber forCode: aString
^aString first = $B
ifTrue: [0]
ifFalse: [0.1 * aNumber]! !
Previous page: Introduction Next page: Basic FIT fixtures Parent page: Table of contents
