Permalink
Browse files

Merge pull request #401 from mbarchfe/commentedcolumns

Comment columns for slim decision and query tables
  • Loading branch information...
2 parents 4045fd4 + 52c0643 commit ae914681ac12f4ffa467d3c750016ce217c14cc2 @amolenaar amolenaar committed Feb 11, 2014
@@ -94,3 +94,19 @@ When exceptions occur in a decision table, they're displayed in line.
| foo | bar | baz? |
| - | | / |
| | * | |
+
+!4 Documentation
+
+If you want to document the data given in a particular row you can just add an extra cell to the right.
+
+| should I buy milk |
+| cash in wallet | credit card | pints of milk remaining | go to store? |
+| 0 | no | 0 | no |
+| 10 | no | 0 | yes | plenty of money but no need for a pint |
+
+For more detailed documentation you can add designated comment columns anywhere. Just prefix the header with a hash symbol.
+
+| should I buy milk |
+| cash in wallet | # comment | credit card | pints of milk remaining | go to store? |
+| -2 | we actually allow negative amounts, meaning debt | no | 0 | no |
+| 1 | must have at least 2 euros | no | 0 | no |
@@ -58,3 +58,10 @@ The first thing to notice is the ''Query:'' in the first cell of the table. Thi
Each row in the table is checked to see if there is a match in the query response. The fields are matched left to right. If the leftmost field matches, then the row is considered to be "found". Fields that don't matche are marked in error as in the 10-Oct-1974 field above. A cell that is left blank in the table will be filled in from the result and counted as ignored. If the first cell of a row cannot be matched, then the row is considered ''missing''. If there is an unmatched row remaining in the query response, it is added to the table and marked ''surplus''. The order of the rows is irrelevant.
If a !style_code(table) method is declared in the fixture it will be called before the !style_code(query) function is called. It will be passed a list of rows which are themselves lists of cells. The rows and cells represent the all but the first row of the table. This is the same format at the !style_code(doTable) method of Table table, and the !style_code(table) method of Decision table.
+
+In analogy to decision tables you can define comment columns by prefixing the header with the hash symbol.
+
+|Query:employees hired before|10-Dec-1980 |
+|last name|hire date|# comment|
+|Grenning | ||
+|Martin | |we hired him first|
@@ -0,0 +1,38 @@
+package fitnesse.slim.test;
+
+//Copyright (C) 2003-2009 by Object Mentor, Inc. All rights reserved.
+//Released under the terms of the CPL Common Public License version 1.0.
+
+import java.util.List;
+
+public class ShouldIBuyMilk {
+ private int dollars;
+ private int pints;
+ private boolean creditCard;
+
+ public void setCashInWallet(int dollars) {
+ this.dollars = dollars;
+ }
+
+ public void setPintsOfMilkRemaining(int pints) {
+ this.pints = pints;
+ }
+
+ public void setCreditCard(String valid) {
+ creditCard = "yes".equals(valid);
+ }
+
+ public String goToStore() {
+ if (pints == 0 && (dollars > 2 || creditCard))
+ return "yes";
+ else
+ return "no";
+ }
+
+ public void execute() {
+
+ }
+
+ public void table(List<List<String>> table) {
+ }
+}
@@ -100,11 +100,13 @@ protected void gatherFunctionsAndVariablesFromColumnHeader() {
private void putColumnHeaderInFunctionOrVariableList(int col) {
String cell = table.getCellContents(col, 1);
- if (cell.endsWith("?") || cell.endsWith("!")) {
- String funcName = cell.substring(0, cell.length() - 1);
- funcStore.add(funcName, col);
- } else {
- varStore.add(cell, col);
+ if (!cell.startsWith("#")) {
+ if (cell.endsWith("?") || cell.endsWith("!")) {
+ String funcName = cell.substring(0, cell.length() - 1);
+ funcStore.add(funcName, col);
+ } else {
+ varStore.add(cell, col);
+ }
}
}
@@ -14,10 +14,10 @@
import fitnesse.testsystems.slim.Table;
import fitnesse.testsystems.slim.results.SlimExceptionResult;
import fitnesse.testsystems.slim.results.SlimTestResult;
-
import static util.ListUtility.list;
public class QueryTable extends SlimTable {
+ private static final String COMMENT_COLUMN_MARKER = "#";
protected List<String> fieldNames = new ArrayList<String>();
private String queryId;
@@ -165,7 +165,10 @@ protected TestResult markField(int tableRow, int matchedRow, int col, QueryResul
String actualValue = queryResults.getCell(fieldName, matchedRow);
String expectedValue = table.getCellContents(col, tableRow);
SlimTestResult testResult;
- if (actualValue == null)
+ if (fieldName.startsWith(COMMENT_COLUMN_MARKER)) {
+ testResult = SlimTestResult.plain();
+ }
+ else if (actualValue == null)
testResult = SlimTestResult.fail(String.format("field %s not present", fieldName), expectedValue);
else if (expectedValue == null || expectedValue.length() == 0)
testResult = SlimTestResult.ignore(actualValue);
@@ -259,9 +262,11 @@ public int findBestMatch(int tableRow) {
private void eliminateRowsThatDontMatchField() {
String fieldName = fieldNames.get(fieldIndex);
- Iterator<Integer> rowIterator = matchCandidates.iterator();
- while (rowIterator.hasNext())
- eliminateUnmatchingRow(rowIterator, fieldName);
+ if (!fieldName.startsWith(COMMENT_COLUMN_MARKER)) {
+ Iterator<Integer> rowIterator = matchCandidates.iterator();
+ while (rowIterator.hasNext())
+ eliminateUnmatchingRow(rowIterator, fieldName);
+ }
}
private void eliminateUnmatchingRow(Iterator<Integer> rowIterator, String fieldName) {
@@ -341,6 +341,46 @@ public void translatesTestTablesIntoLiteralTables() throws Exception {
"]";
assertEquals(expectedColorizedTable, colorizedTable);
}
+
+ @Test
+ public void commentColumn() throws Exception {
+ // TODO: a lot of copy and paste from the previous test
+ String decisionTableWithComment =
+ "|DT:fixture|argument||\n" +
+ "|var|func?|#comment|\n" +
+ "|3|5|comment|\n" +
+ "|7|9||\n";
+ DecisionTable dt = makeDecisionTableAndBuildInstructions(decisionTableWithComment);
+ int n=0;
+ Map<String, Object> pseudoResults = SlimCommandRunningClient.resultToMap(
+ list(
+ list(id(n++), "OK"),
+ list(id(n++), VoidConverter.VOID_TAG),
+ list(id(n++), VoidConverter.VOID_TAG), //beginTable
+ list(id(n++), VoidConverter.VOID_TAG), //reset
+ list(id(n++), VoidConverter.VOID_TAG), //set
+ list(id(n++), VoidConverter.VOID_TAG), //execute
+ list(id(n++), "5"),
+ list(id(n++), VoidConverter.VOID_TAG),
+ list(id(n++), VoidConverter.VOID_TAG),
+ list(id(n++), VoidConverter.VOID_TAG),
+ list(id(n++), "5"),
+ list(id(n++), VoidConverter.VOID_TAG) //endTable
+ )
+ );
+ SlimAssertion.evaluateExpectations(assertions, pseudoResults);
+
+ String colorizedTable = dt.getTable().toString();
+ String expectedColorizedTable =
+ "[" +
+ "[pass(DT:fixture), argument, ], " +
+ "[var, func?, #comment], " +
+ "[3, pass(5), comment], " +
+ "[7, fail(a=5;e=9), ]" +
+ "]";
+ assertEquals(expectedColorizedTable, colorizedTable);
+ }
+
@Test
public void canEvaluateReturnValuesAndColorizeTableForMultipleCallsToSameFunction() throws Exception {
@@ -283,6 +283,33 @@ public void variablesAreReplacedInMatch() throws Exception {
}
@Test
+ public void commentColumn() throws Exception {
+ queryTableHeader =
+ "|" + tableType() + ":fixture|argument|\n" +
+ "|#comment1|n|#comment2|\n";
+
+
+ assertQueryResults(
+ "|first|1|comment|\n"+
+ "|second|2||\n" ,
+ ListUtility.<Object>list(
+ // trying to ensure that the comment field does not participate in
+ // row matching
+ // if comments were not ignored, the best match would be set from the comment fields
+ list(list("#comment1", "second"), list("n", "1"), list("#comment2", "")),
+ list(list("#comment1", "first"), list("n", "2"), list("#comment2", "comment"))
+ ),
+ "[" +
+ headRow +
+ "[#comment1, n, #comment2], " +
+ "[first, pass(1), comment], " +
+ "[second, pass(2), ]" +
+ "]"
+ );
+
+ }
+
+ @Test
public void variablesAreReplacedInExpected() throws Exception {
makeQueryTableAndBuildInstructions(queryTableHeader + "|2|$V|\n");
qt.setSymbol("V", "5");

0 comments on commit ae91468

Please sign in to comment.