Skip to content

Commit

Permalink
Merge pull request #507 from fhoeben/ScenarioCreatesParentScriptClass
Browse files Browse the repository at this point in the history
Ensure scenarios invoked from scripts use the ScriptTable class of the calling script
  • Loading branch information
amolenaar committed Aug 23, 2014
2 parents 860c7b2 + 5994fa6 commit 9d8fc86
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 14 deletions.
20 changes: 17 additions & 3 deletions src/fitnesse/testsystems/slim/tables/ScenarioTable.java
Original file line number Diff line number Diff line change
Expand Up @@ -169,19 +169,33 @@ public String substitute(String content) throws SyntaxError {
}
});
ScenarioTestContext testContext = new ScenarioTestContext(parentTable.getTestContext());
ScriptTable t = createChild(testContext, newTable);
ScriptTable t = createChild(testContext, parentTable, newTable);
parentTable.addChildTable(t, row);
List<SlimAssertion> assertions = t.getAssertions();
assertions.add(makeAssertion(Instruction.NOOP_INSTRUCTION, new ScenarioExpectation(t, row)));
return assertions;
}

protected ScriptTable createChild(SlimTestContext testContext, Table newTable) {
ScriptTable scriptTable = new ScriptTable(newTable, id, testContext);
protected ScriptTable createChild(ScenarioTestContext testContext, SlimTable parentTable, Table newTable) {
ScriptTable scriptTable;
if (parentTable instanceof ScriptTable && !parentTable.getClass().equals(ScriptTable.class)) {
scriptTable = createChild((ScriptTable) parentTable, newTable, testContext);
} else {
scriptTable = new ScriptTable(newTable, id, testContext);
}
scriptTable.setCustomComparatorRegistry(customComparatorRegistry);
return scriptTable;
}

protected ScriptTable createChild(ScriptTable parentScriptTable, Table newTable, SlimTestContext testContext) {
Class<? extends ScriptTable> parentTableClass = parentScriptTable.getClass();
try {
return SlimTableFactory.createTable(parentTableClass, newTable, id, testContext);
} catch (Exception e) {
throw new RuntimeException("Unable to create child table of type: " + parentTableClass.getName(), e);
}
}

public List<SlimAssertion> call(String[] args, ScriptTable parentTable, int row) throws SyntaxError {
Map<String, String> scenarioArguments = new HashMap<String, String>();

Expand Down
7 changes: 2 additions & 5 deletions src/fitnesse/testsystems/slim/tables/ScriptTable.java
Original file line number Diff line number Diff line change
Expand Up @@ -145,15 +145,12 @@ protected List<SlimAssertion> action(int row) throws SyntaxError {
if (assertions.isEmpty()) {
// Invoke fixture:
int lastCol = table.getColumnCountInRow(row) - 1;
String actionName = getActionNameStartingAt(0, lastCol, row);
String[] args = getArgumentsStartingAt(1, lastCol, row, assertions);
assertions.add(makeAssertion(callFunction(getTableType() + "Actor", actionName, (Object[]) args),
new ScriptActionExpectation(0, row)));
return invokeAction(0, lastCol, row, new ScriptActionExpectation(0, row));
}
return assertions;
}

private List<SlimAssertion> assertionsFromScenario(int row) throws SyntaxError {
protected List<SlimAssertion> assertionsFromScenario(int row) throws SyntaxError {
int lastCol = table.getColumnCountInRow(row) - 1;
String actionName = getActionNameStartingAt(0, lastCol, row);
ScenarioTable scenario = getTestContext().getScenario(Disgracer.disgraceClassName(actionName));
Expand Down
13 changes: 11 additions & 2 deletions src/fitnesse/testsystems/slim/tables/SlimTableFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

public class SlimTableFactory {
private static final Logger LOG = Logger.getLogger(SlimTableFactory.class.getName());
private static final Map<Class<? extends SlimTable>, Constructor<? extends SlimTable>> CONSTRUCTOR_MAP = new HashMap<Class<? extends SlimTable>, Constructor<? extends SlimTable>>();

private final Map<String, Class<? extends SlimTable>> tableTypes;
private final Map<String, String> tableTypeArrays;
Expand Down Expand Up @@ -82,14 +83,22 @@ private Class<? extends SlimTable> getTableType(String tableType) {
private SlimTable newTableForType(Class<? extends SlimTable> tableClass,
Table table, String tableId, SlimTestContext slimTestContext) {
try {
Constructor<? extends SlimTable> constructor = tableClass.getConstructor(Table.class, String.class, SlimTestContext.class);
return constructor.newInstance(table, tableId, slimTestContext);
return createTable(tableClass, table, tableId, slimTestContext);
} catch (Exception e) {
LOG.log(Level.WARNING, "Can not create new table instance for class " + tableClass, e);
return new SlimErrorTable(table, tableId, slimTestContext);
}
}

public static <T extends SlimTable> T createTable(Class<T> tableClass, Table table, String tableId, SlimTestContext slimTestContext) throws Exception {
Constructor<? extends SlimTable> constructor = CONSTRUCTOR_MAP.get(tableClass);
if (constructor == null) {
constructor = tableClass.getConstructor(Table.class, String.class, SlimTestContext.class);
CONSTRUCTOR_MAP.put(tableClass, constructor);
}
return (T) constructor.newInstance(table, tableId, slimTestContext);
}

private String getFullTableName(String tableName) {
if (hasColon(tableName)) {
return tableName;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -216,9 +216,9 @@ public static class ScenarioTableWithDifferentScript extends ScenarioTable {
public ScenarioTableWithDifferentScript(Table table, String tableId, SlimTestContext testContext) {
super(table, tableId, testContext);
}

@Override
protected ScriptTable createChild(SlimTestContext testContext, Table newTable) {
protected ScriptTable createChild(ScenarioTestContext testContext, SlimTable parentTable, Table newTable) {
return new DiffScriptTable(newTable, id, testContext);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,11 @@ private SlimTestContextImpl makeTables(String tableText) throws Exception {
Table t = ts.getTable(0);
ScenarioTable st = new ScenarioTable(t, "s_id", testContext);
t = ts.getTable(1);
script = new ScriptTable(t, "id", testContext);
if (t.getCellContents(0,0).equals("script")) {
script = new ScriptTable(t, "id", testContext);
} else {
script = new ScriptTableTest.LocalizedScriptTable(t, "id", testContext);
}
assertions.addAll(st.getAssertions());
assertions.addAll(script.getAssertions());
return testContext;
Expand All @@ -66,6 +70,22 @@ public void oneInput() throws Exception {
assertEquals(expectedInstructions, instructions());
}

@Test
public void oneInputDifferentScriptClass() throws Exception {
makeTables(
"!|scenario|myScenario|input|\n" +
"|function|@input|\n" +
"\n" +
"!|localisedScript|\n" +
"|myScenario|7|\n"
);
List<CallInstruction> expectedInstructions =
list(
new CallInstruction("localizedScriptTable_id_0/localizedScriptTable_s_id_0", "localizedScriptTableActor", "function", new Object[]{"7"})
);
assertEquals(expectedInstructions, instructions());
}

@Test
public void simpleNameWithUnnamedArgument() throws Exception {
makeTables(
Expand Down Expand Up @@ -128,6 +148,33 @@ public void simpleInputAndOutputPassing() throws Exception {
assertEquals(0, testContext.getTestSummary().getExceptions());
}

@Test
public void differentScriptSimpleInputAndOutputPassing() throws Exception {
SlimTestContextImpl testContext = makeTables(
"!|scenario|echo|input|giving|output|\n" +
"|localized check|echo|@input|@output|\n" +
"\n" +
"!|localisedScript|\n" +
"|echo|7|giving|7|\n"
);
Map<String, Object> pseudoResults = SlimCommandRunningClient.resultToMap(
list(
list("localizedScriptTable_id_0/localizedScriptTable_s_id_0", "7")
)
);

SlimAssertion.evaluateExpectations(assertions, pseudoResults);

String scriptTable = script.getChildren().get(0).getTable().toString();
String expectedScript =
"[[scenario, echo, input, giving, output], [localized check, echo, 7, pass(7)]]";
assertEquals(expectedScript, scriptTable);
assertEquals(1, testContext.getTestSummary().getRight());
assertEquals(0, testContext.getTestSummary().getWrong());
assertEquals(0, testContext.getTestSummary().getIgnores());
assertEquals(0, testContext.getTestSummary().getExceptions());
}

@Test
public void simpleInputAndOutputFailing() throws Exception {
SlimTestContextImpl testContext = makeTables(
Expand Down
2 changes: 1 addition & 1 deletion test/fitnesse/testsystems/slim/tables/ScriptTableTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public class ScriptTableTest {
private List<SlimAssertion> assertions;
public ScriptTable st;

private class LocalizedScriptTable extends ScriptTable {
static class LocalizedScriptTable extends ScriptTable {

public LocalizedScriptTable(Table table, String tableId, SlimTestContext context) {
super(table, tableId, context);
Expand Down

0 comments on commit 9d8fc86

Please sign in to comment.