Trace: » constraintfixture » start » howthisisorganised » comparestoredqueriescommand » writingtestsinexcel » argumentosparafixtures
Argumentos para Fixtures
A primeira linha de uma tabela FIT é normalmente usada para iniciar a classe da fixture. Além disso, você pode passar argumentos para a fixture adicionando células depois do nome da classe nesta linha. Argumentos para fixtures funcionam parecido com argumentos para linha de comando de um método main e eles podem ser acessados usando o array args dentro da fixture.
Você pode usar essa funcionalidade para parametrizar suas fixtures e torná-las mais reutilizáveis. Por exemplo, argumentos permitem passar parâmetros para uma RowFixture :
!|ArgumentsTest|Hello World|Houston We Have a Problem| |word| |Hello| |World| |Houston| |We| |Have| |a| |Problem|
Código-fonte em Java
package info.fitnesse.fixturegallery;
import java.util.HashSet;
import java.util.Set;
import fitlibrary.SetFixture;
import fitlibrary.parse.Table;
public class ArgumentsTest extends SetFixture{
public class Word{
public String word;
public Word(String w){
this.word=w;
}
}
public void doTable(Table arg0) {
Set<Word> set=new HashSet<Word>();
for(String s: args){
for (String word: s.split(" ")) set.add(new Word(word));
}
this.setActualCollection(set);
super.doTable(arg0);
}
}
Código-fonte em .NET
using System;
using System.Collections.Generic;
using System.Text;
namespace info.fitnesse.fixturegallery
{
public class ArgumentsTest: fit.RowFixture
{
public override Type GetTargetClass()
{
return typeof(Text);
}
public override object[] Query()
{
List<Text> t = new List<Text>();
foreach (String s in Args)
{
foreach (String w in s.Split(new char[] { ' ' }))
{
t.Add(new Text(w));
}
}
return t.ToArray();
}
}
}
Código-fonte em Python
# REQUIRE: Python >= 2.4, due to set() usage
from fitLib.SetFixture import SetFixture
import types
class Word(object):
"""Simple ValueObject class to store a word as string."""
_typeDict = { "word": "String" }
def __init__(self, word):
assert isinstance(word, types.StringTypes)
self.word = word
class ArgumentsTest(SetFixture):
def getTargetClass(self):
return Word #< CLASS-HINT: For _typeDict lookup.
def doTable(self, table):
wordSet = set()
for s in self.args:
for word in s.split(" "):
wordSet.add( Word(word) )
# -- WEIRD: setActualCollection() takes no arg -> Preassign first.
self.paramCollection = wordSet
self.setActualCollection()
SetFixture.doTable(self, table)
Código-fonte Smalltalk
'From VisualWorks®, 7.6 of March 3, 2008 on June 27, 2008 at 3:36:15 pm'!
Info.Fitnesse.Fixturegallery defineClass: #ArgumentsTest
superclass: #{Fitlibrary.SetFixture}
indexedType: #none
private: false
instanceVariableNames: ''
classInstanceVariableNames: ''
imports: ''
category: ''!
!Info.Fitnesse.Fixturegallery.ArgumentsTest methodsFor: 'enumerating'!
doTableNode: aTableNode
| set |
set := Set new.
arguments
do:
[:each |
(each tokensBasedOn: Character space)
do: [:eachWord | set add: (Word word: eachWord)]].
self actualCollection: set.
super doTableNode: aTableNode! !
'From VisualWorks®, 7.6 of March 3, 2008 on June 27, 2008 at 3:36:57 pm'!
Info.Fitnesse.Fixturegallery defineClass: #Word
superclass: #{Core.Object}
indexedType: #none
private: false
instanceVariableNames: 'word '
classInstanceVariableNames: ''
imports: ''
category: ''!
"-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- "!
!Info.Fitnesse.Fixturegallery.Word class methodsFor: 'instance creation'!
word: aString
^self new word: aString! !
"-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- "!
!Info.Fitnesse.Fixturegallery.Word methodsFor: 'type access'!
signatureFor: aSymbol
^String! !
!Info.Fitnesse.Fixturegallery.Word methodsFor: 'accessing'!
word
^word!
word: aString
word := aString! !
Note que argumentos são apenas strings. Símbolos não funcionam automaticamente com argumentos, então tentar passar <<nomedosimbolo em .NET não funcionará por padrão. Se você quiser processar argumentos como células normais em .NET, você precisa usar o método GetArgumentInput (veja http://syterra.com/FixtureArguments.html).
Algo importante para se lembrar relacionado com argumentos é que eles não estão disponíveis no construtor da fixture uma vez que eles são inicializados depois que a classe é carregada. Por outro lado eles estarão disponíveis para outros métodos.
Próxima página: Flow Mode Página acima: Conceitos importantes
