Skip to content

Commit

Permalink
JBEHAVE-1603 Preserve parsing of separators from table files (#81)
Browse files Browse the repository at this point in the history
  • Loading branch information
uarlouski authored Nov 5, 2024
1 parent 4443513 commit d2e70fc
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import java.util.Deque;

import org.apache.commons.lang3.StringUtils;
import org.jbehave.core.configuration.Configuration;
import org.jbehave.core.configuration.Keywords;
import org.jbehave.core.i18n.LocalizedKeywords;
Expand Down Expand Up @@ -104,9 +105,19 @@ public ExamplesTable createExamplesTable(String input) {
loadedTable = TableTransformersExecutor.applyTransformers(tableTransformers,
tablePropertiesQueue.getTable(), tableParsers, target, tableTransformerMonitor);
tablePropertiesQueue = tableParsers.parseProperties(loadedTable);
target = tablePropertiesQueue.getProperties();
TableProperties parsedProperties = tablePropertiesQueue.getProperties().getFirst();
if (StringUtils.isEmpty(parsedProperties.getPropertiesAsString())) {
TableProperties newProperties = new TableProperties("", keywords, parameterConverters);
newProperties.overrideSeparatorsFrom(target.peekLast());
target.clear();
target.addFirst(newProperties);
} else {
target = tablePropertiesQueue.getProperties();
}
}
properties.descendingIterator().forEachRemaining(target::addFirst);

tablePropertiesQueue = new TablePropertiesQueue(tablePropertiesQueue.getTable(), target);
}

return new ExamplesTable(tablePropertiesQueue, parameterConverters, parameterControls, tableParsers,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
package org.jbehave.core.model;

import static java.util.Arrays.asList;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.equalTo;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.jbehave.core.io.LoadFromClasspath;
import org.jbehave.core.io.ResourceLoader;
import org.junit.jupiter.api.Test;
Expand Down Expand Up @@ -167,4 +172,25 @@ void shouldFailIfExamplesTableParsingSeparatorsArePlacesOutsideOfExternalTableTh
+ "external table they belong to:%n{%s=!}\ndata.table", separator);
assertEquals(message, thrown.getMessage());
}

@Test
void shouldLoadTableFromPathAndPreserveSeparators() {
// Given
ExamplesTableFactory factory = new ExamplesTableFactory(new LoadFromClasspath(), new TableTransformers());

// When
ExamplesTable table = factory.createExamplesTable("data.table");

// Then
assertThat(table.getHeaders(), equalTo(asList("symbol", "threshold", "price", "status")));
List<Map<String, String>> rows = table.getRows();
assertThat(rows.size(), equalTo(3));
ensureRowContentIs(rows, 0, asList("STK1", "15.0", "5.0", "OFF"));
ensureRowContentIs(rows, 1, asList("STK1", "15.0", "10.0", "OFF"));
ensureRowContentIs(rows, 2, asList("STK1", "15.0", "16.0", "ON"));
}

private void ensureRowContentIs(List<Map<String, String>> rows, int row, List<String> expected) {
assertThat(new ArrayList<>(rows.get(row).values()), equalTo(expected));
}
}
6 changes: 6 additions & 0 deletions jbehave-core/src/test/resources/data.table
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{transformer=REPLACING, replacing=11.0, replacement=10.0, headerSeparator=!, valueSeparator=!}
{transformer=FROM_LANDSCAPE, headerSeparator=!, valueSeparator=!}
!symbol !STK1!STK1!STK1!
!threshold!15.0!15.0!15.0!
!price !5.0 !11.0!16.0!
!status !OFF !OFF !ON !

0 comments on commit d2e70fc

Please sign in to comment.