Skip to content

Commit

Permalink
Improved README tests; minor trim fix in Table.parse(..)
Browse files Browse the repository at this point in the history
  • Loading branch information
terzerm committed Apr 3, 2017
1 parent 5c4f8e1 commit a5b59c5
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 72 deletions.
61 changes: 27 additions & 34 deletions README.md
Expand Up @@ -13,51 +13,44 @@ public class UnrollMethodDataTest {

@Test
@Spockito.Unroll({
"| Name |",
"| Test 1 |",
"| Test 2 |"
"| Last Name | First Name |",
"| Jones | David |",
"| Jensen | Astrid |"
})
public void testUnrollSimple(final String name) {
Assert.assertNotNull("name should not be null", name);
Assert.assertTrue("name should start with Test", name.startsWith("Test "));
public void testUnrollNames(String lastName, String firstName) {
Assert.assertTrue("Last Name should start with J", lastName.startsWith("J"));
Assert.assertTrue("First Name should end with id", firstName.endsWith("id"));
}

@Test
@Spockito.Unroll({
"| Index | Name |",
"|-------|----------|",
"| 1 | Test 1 |",
"| 2 | Test 2 |"
"| Name | Year | Birthday |",
"|-------|------|------------|",
"| Henry | 1981 | 1981-11-28 |",
"| Jessy | 1965 | 1965-03-28 |"
})
@Spockito.Name("[{row}]: index={0}, name={1}")
public void testUnrollTwoColumns(int index, String name) {
Assert.assertTrue("index should be greater than 0", index > 0);
Assert.assertNotNull("name should be TestName", name);
Assert.assertTrue("name should start with Test", name.startsWith("Test "));
@Spockito.Name("[{row}]: Name={0}")
public void testUnrollBirthdays(String name, int year, LocalDate birthday) {
Assert.assertEquals("Name should have 5 characters", 5, name.length());
Assert.assertTrue("Year is before 1990", 1990 > year);
Assert.assertEquals("Day is 28th", 28, birthday.getDayOfMonth());
}

@Test
@Spockito.Unroll({
"| Index | TestName |",
"|=======|==========|",
"| 1 | Test 1 |",
"|------ |----------|",
"| 2 | Test 2 |",
"|------ |----------|",
"| 3 | Test 3 |",
"|------ |----------|",
"| 4 | Test 4 |",
"|------ |----------|",
"| Object | Vertices | Angle sum |",
"|==========|==========|===========|",
"| Triangle | 3 | 180 |",
"| Square | 4 | 360 |",
"| Pentagon | 5 | 540 |",
"|----------|----------|-----------|",
})
@Spockito.Name("[{row}]: index={Index}, name={TestName}")
public void testUnrollWithColumnRefs(@Spockito.Ref("TestName") String name,
@Spockito.Ref("Index") int index,
@Spockito.Ref("row") int row) {
Assert.assertTrue("index should be greater than 0", index > 0);
Assert.assertTrue("row should be greated or equal to 0", row >= 0);
Assert.assertTrue("index should be row + 1", index == row + 1);
Assert.assertNotNull("name should be TestName", name);
Assert.assertTrue("name should start with Test", name.startsWith("Test "));
@Spockito.Name("[{Object}]: ({Vertices}-2)*180 = {Angle sum}")
public void testUnrollAngularSums(@Spockito.Ref("Vertices") int n,
@Spockito.Ref("Angle sum") int degrees,
@Spockito.Ref("Object") String name) {
Assert.assertTrue("There should be 3 or more vertices", 3 <= n);
Assert.assertEquals("Angular sum of is wrong for: " + name, degrees, (n-2)*180);
}
}
```
Expand Down
Binary file modified ide-run-SpockitoTest.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion src/main/java/org/tools4j/spockito/Table.java
Expand Up @@ -116,7 +116,7 @@ private static TableRow parseRow(final Table table, final int row, final String
if (trimmed.length() < 2 || trimmed.charAt(0) != '|' || trimmed.charAt(trimmed.length() - 1) != '|') {
throw new IllegalArgumentException("Invalid table data: row " + row + " must start and end with '|'");
}
final TableRow tableRow = TableRow.parse(table, rowString);
final TableRow tableRow = TableRow.parse(table, trimmed);
if (row != 0) {
if (tableRow.size() > table.getColumnCount()) {
throw new IllegalArgumentException("Invalid table data: row " + row + " has more columns than header row: " + tableRow.size() + " > " + table.getColumnCount());
Expand Down
Expand Up @@ -40,9 +40,9 @@ public class SpockitoBeforeAfterTest extends UnrollMethodDataTest {
private static final List<String> EXPECTED_TESTS = Arrays.asList(
"oneMore",
"oneMoreUnroll[0]", "oneMoreUnroll[1]",
"testUnrollSimple[0]", "testUnrollSimple[1]",
"testUnrollTwoColumns[0]", "testUnrollTwoColumns[1]",
"testUnrollWithColumnRefs[0]", "testUnrollWithColumnRefs[1]", "testUnrollWithColumnRefs[2]", "testUnrollWithColumnRefs[3]"
"testUnrollAngularSums[Pentagon]", "testUnrollAngularSums[Square]", "testUnrollAngularSums[Triangle]",
"testUnrollBirthdays[0]", "testUnrollBirthdays[1]",
"testUnrollNames[0]", "testUnrollNames[1]"
);

@Rule
Expand Down
63 changes: 29 additions & 34 deletions src/test/java/org/tools4j/spockito/UnrollMethodDataTest.java
Expand Up @@ -27,55 +27,50 @@
import org.junit.Test;
import org.junit.runner.RunWith;

import java.time.LocalDate;

@RunWith(Spockito.class)
public class UnrollMethodDataTest {

@Test
@Spockito.Unroll({
"| Name |",
"| Test 1 |",
"| Test 2 |"
"| Last Name | First Name |",
"| Jones | David |",
"| Jensen | Astrid |"
})
public void testUnrollSimple(final String name) {
Assert.assertNotNull("name should not be null", name);
Assert.assertTrue("name should start with Test", name.startsWith("Test "));
public void testUnrollNames(String lastName, String firstName) {
Assert.assertTrue("Last Name should start with J", lastName.startsWith("J"));
Assert.assertTrue("First Name should end with id", firstName.endsWith("id"));
}

@Test
@Spockito.Unroll({
"| Index | Name |",
"|-------|----------|",
"| 1 | Test 1 |",
"| 2 | Test 2 |"
"| Name | Year | Birthday |",
"|-------|------|------------|",
"| Henry | 1981 | 1981-11-28 |",
"| Jessy | 1965 | 1965-03-28 |"
})
@Spockito.Name("[{row}]: index={0}, name={1}")
public void testUnrollTwoColumns(int index, String name) {
Assert.assertTrue("index should be greater than 0", index > 0);
Assert.assertNotNull("name should be TestName", name);
Assert.assertTrue("name should start with Test", name.startsWith("Test "));
@Spockito.Name("[{row}]: Name={0}")
public void testUnrollBirthdays(String name, int year, LocalDate birthday) {
Assert.assertEquals("Name should have 5 characters", 5, name.length());
Assert.assertTrue("Year is before 1990", 1990 > year);
Assert.assertEquals("Day is 28th", 28, birthday.getDayOfMonth());
}

@Test
@Spockito.Unroll({
"| Index | TestName |",
"|=======|==========|",
"| 1 | Test 1 |",
"|------ |----------|",
"| 2 | Test 2 |",
"|------ |----------|",
"| 3 | Test 3 |",
"|------ |----------|",
"| 4 | Test 4 |",
"|------ |----------|",
"| Object | Vertices | Angle sum |",
"|==========|==========|===========|",
"| Triangle | 3 | 180 |",
"| Square | 4 | 360 |",
"| Pentagon | 5 | 540 |",
"|----------|----------|-----------|",
})
@Spockito.Name("[{row}]: index={Index}, name={TestName}")
public void testUnrollWithColumnRefs(@Spockito.Ref("TestName") String name,
@Spockito.Ref("Index") int index,
@Spockito.Ref("row") int row) {
Assert.assertTrue("index should be greater than 0", index > 0);
Assert.assertTrue("row should be greated or equal to 0", row >= 0);
Assert.assertTrue("index should be row + 1", index == row + 1);
Assert.assertNotNull("name should be TestName", name);
Assert.assertTrue("name should start with Test", name.startsWith("Test "));
@Spockito.Name("[{Object}]: ({Vertices}-2)*180 = {Angle sum}")
public void testUnrollAngularSums(@Spockito.Ref("Vertices") int n,
@Spockito.Ref("Angle sum") int degrees,
@Spockito.Ref("Object") String name) {
Assert.assertTrue("There should be 3 or more vertices", 3 <= n);
Assert.assertEquals("Angular sum of is wrong for: " + name, degrees, (n-2)*180);
}
}

0 comments on commit a5b59c5

Please sign in to comment.