A Galeria de Fixtures também está disponível como documento PDF e um wiki FitNesse online. Acesse http://gojko.net/fitnesse/fixturegallery para mais informações.
Página anterior: Introdução Próxima página: Fixtures básicas de FIT Página acima: Sumário

Código-fonte

Você pode baixar o código-fonte para todos os exemplos deste documento diretamente do SourceForge (http://sourceforge.net/projects/fixturegallery). Muitos dos exemplos deste documento mostram como trabalhar com serviços e objetos do domínio de negócio. Para manter tudo o mais simples possível e ainda mostrar todas as funcionalidades relevantes, todos estes exemplos usam as seguintes classes de negócio:

Código do domínio em Java

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;
	}
}

Código do domínio em .NET

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;
        }
    }


}

Código do domínio em Python

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

Código do domínio em Smalltalk

'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]! !

Página anterior: Introdução Próxima página: Fixtures básicas de FIT Página acima: Sumário


Personal Tools