Skip to content

Commit

Permalink
Merge pull request #6 from LoversOfBehat/negative-steps
Browse files Browse the repository at this point in the history
Provide step definitions to perform negative checks
  • Loading branch information
pfrenssen authored Jun 21, 2018
2 parents 7b25d99 + 5fcb1b3 commit b291e9e
Show file tree
Hide file tree
Showing 2 changed files with 123 additions and 1 deletion.
24 changes: 24 additions & 0 deletions features/tables.feature
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ Feature: Inspecting HTML tables
Then I should see the simple table
And the simple table should have 3 columns
And the simple table should have 3 rows
And the simple table should not have 4 columns
And the simple table should not have 4 rows
And the simple table should contain:
| Header 1 | Header 2 | Header 3 |
| Row 1 Col 1 | Row 1 Col 2 | Row 1 Col 3 |
Expand All @@ -56,6 +58,8 @@ Feature: Inspecting HTML tables
| Header 1 |
| Row 2 Col 1 |
| Row 1 Col 1 |
And the simple table should not contain:
| A squirrel |

# The second table has 2 columns, of which the first is a vertical header.
And I should see the Algarve table
Expand Down Expand Up @@ -92,11 +96,31 @@ Feature: Inspecting HTML tables
| Argentina | 16 |
| Bahamas | 39 |
| World | 57 |
# Check that if we mix up data from different rows this is detected as not being present on the page.
And the "Population data" table should not contain the following columns:
| Country | Population density |
| Albania | 164 |
| Andorra | 105 |
# Check that mixed up column data is detected as not being present on the page.
And the "Population data" table should not contain the following columns:
| Country | Population density |
| Albania | 105 |
| 164 | Andorra |
| Algeria | 17 |
| Angola | 23 |
| Argentina | 16 |
| Bahamas | 39 |
| World | 57 |
# Check that we can verify non-consecutive rows by specifying the headers.
And the "Population data" table should contain the following rows:
| Andorra | 0.1 | 500 | 164 |
| Argentina | 43.8 | 2780400 | 16 |
| Bahamas | 0.4 | 13900 | 39 |
# Check that mixed up row data is detected as not being present on the page.
And the "Population data" table should not contain the following rows:
| Andorra | 0.1 | 2780400 | 164 |
| Argentina | 43.8 | 500 | 16 |
| Bahamas | 0.4 | 13900 | 39 |

# The fourth table has a rowspan on the first element.
And I should see the Employees table
Expand Down
100 changes: 99 additions & 1 deletion src/Context/TableContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ public function assertTableWithColumnCountExists(int $count): void
return;
}
}
throw new \RuntimeException("No table with $count columns is present on the page.");
throw new TableNotFoundException("No table with $count columns is present on the page.");
}

/**
Expand Down Expand Up @@ -136,6 +136,25 @@ public function assertTableColumnCount(string $name, int $count): void
throw new \RuntimeException("The $name table should have $count columns but it has $actual columns.");
}

/**
* Checks that the given table does not have the given number of columns.
*
* @param string $name
* The human readable name for the table.
* @param int $count
* The unexpected number of columns.
*
* @Then the :name table should not have :count column(s)
*/
public function assertNoTableColumnCount(string $name, int $count): void
{
$table = $this->getTable($name);
$actual = $table->getColumnCount();
if ($actual === $count) {
throw new \RuntimeException("The $name table should not have $count columns.");
}
}

/**
* Checks that the given table has the given number of rows.
*
Expand All @@ -156,6 +175,25 @@ public function assertTableRowCount(string $name, int $count): void
throw new \RuntimeException("The $name table should have $count rows but it has $actual rows.");
}

/**
* Checks that the given table does not have the given number of rows.
*
* @param string $name
* The human readable name for the table.
* @param int $count
* The unexpected number of rows.
*
* @Then the :name table should not have :count row(s)
*/
public function assertNoTableRowCount(string $name, int $count): void
{
$table = $this->getTable($name);
$actual = $table->getRowCount();
if ($actual === $count) {
throw new \RuntimeException("The $name table should not have $count rows.");
}
}

/**
* Checks that the given table contains the given data.
*
Expand All @@ -174,6 +212,27 @@ public function assertTableData(string $name, TableNode $data): void
$this->assertArraySubset($data->getRows(), $table->getData());
}

/**
* Checks that the given table does not contain the given data.
*
* This checks that the data is not present in the table, ignoring row and column ordering.
*
* @param string $name
* The human readable name for the table.
* @param TableNode $data
* The data that is expected to be present in the table.
*
* @Then the :name table should not contain:
*/
public function assertNoTableData(string $name, TableNode $data): void
{
try {
$this->assertTableData($name, $data);
throw new \RuntimeException("A table with the given data is present on the page, but should not be.");
} catch (NoArraySubsetException $e) {
}
}

/**
* Checks that the given table contains the given non-consecutive columns, identified by headers.
*
Expand All @@ -190,6 +249,25 @@ public function assertTableColumnData(string $name, TableNode $data): void
$this->assertArraySubset($data->getColumnsHash(), array_values($table->getColumnData($data->getRow(0))));
}

/**
* Checks that the given table does not contain the given non-consecutive columns, identified by headers.
*
* @param string $name
* The human readable name for the table.
* @param TableNode $data
* The data that should not be present in the table, with the first row identifying the columns to match.
*
* @Then the :name table should not contain the following column(s):
*/
public function assertNoTableColumnData(string $name, TableNode $data): void
{
try {
$this->assertTableColumnData($name, $data);
throw new \RuntimeException("A table with the given column data is present on the page, but should not be.");
} catch (NoArraySubsetException $e) {
}
}

/**
* Checks that the given table contains the given non-consecutive rows, identified by headers.
*
Expand All @@ -206,6 +284,26 @@ public function assertTableRowData(string $name, TableNode $data): void
$this->assertArraySubset($data->getRowsHash(), $table->getRowData($data->getColumn(0)));
}

/**
* Checks that the given table does not contain the given non-consecutive rows, identified by headers.
*
* @param string $name
* The human readable name for the table.
* @param TableNode $data
* The data that should not be present in the table, with the first column identifying the rows to match.
*
* @Then the :name table should not contain the following row(s):
*/
public function assertNoTableRowData(string $name, TableNode $data): void
{
try {
$this->assertTableRowData($name, $data);
} catch (NoArraySubsetException $e) {
return;
}
throw new \RuntimeException("A table with the given row data is present in the page but it was not expected to be.");
}

/**
* Checks that a table with the given number of columns does not exist in the page.
*
Expand Down

0 comments on commit b291e9e

Please sign in to comment.