Tabular v1.2.1 Tabular.TestSupport View Source

Functions to simplify testing with ascii tables.

Link to this section Summary

Functions

Asserts that the contents of 2 tables are equal. If the tables are not equal, the failure message includes the table highlighting the differences in the cells that disagree.

Compares two ascii tables, producing a matrix of the individual comparisons.

Examines the matrix produced by compare. Returns false if any cell is false. Otherwise returns true.

Link to this section Functions

Link to this function

assert_equal(results_table) View Source

Asserts that the contents of 2 tables are equal. If the tables are not equal, the failure message includes the table highlighting the differences in the cells that disagree.

Examples

iex> try do
...>   [
...>     ["name", "count"],
...>     [{"Malcolm", "Mike"}, "10"],
...>     ["Zoe", {"5", "20"}]
...>   ]
...>   |> Tabular.TestSupport.assert_equal()
...> rescue
...>   e in ExUnit.AssertionError ->
...>     e.message
...> end
"""
+--------------------------+------------------+
| name                     | count            |
+--------------------------+------------------+
| >>> Malcolm <=> Mike <<< | 10               |
| Zoe                      | >>> 5 <=> 20 <<< |
+--------------------------+------------------+
"""
Link to this function

compare(actual_table, expected_table, opts \\ [comparators: %{}]) View Source

Compares two ascii tables, producing a matrix of the individual comparisons.

An optional map of comparator functions can be used. Comparators are useful when values in the tables cannot be compared by simple equality, such as floating point values that may be consider equal when the 2 values are close.

Each cell in the matrix is either a single value or a tuple of values.

  • A single value indicates that both tables have the same value at that position. The value is the actual value found in both tables at that position or the value from the first table when using a comparator function

  • A tuple indicates that the two tables have different values at that position. The tuple contains the differing values from the two tables.

Comparators are functions that take 2 values as arguments and return true when the values are to be considered a match and false otherwise.

The keys in the comparator map indicate the table column where the comparator is to be used.

Example

iex> table1 = """
...>   +---------+-------+
...>   | name    | count |
...>   +---------+-------+
...>   | Malcolm | 10    |
...>   +---------+-------+
...>   | Zoe     | 5     |
...>   +---------+-------+
...> """
...>
...> table2 = """
...>   +---------+-------+
...>   | name    | count |
...>   +---------+-------+
...>   | Mike    | 11    |
...>   +---------+-------+
...>   | Zoe     | 20    |
...>   +---------+-------+
...> """
...>
...> comparators = %{"count" => &(abs(String.to_integer(&1) - String.to_integer(&2)) < 2)}
...>
...> Tabular.TestSupport.compare(table1, table2, comparators: comparators)
[
  ["name", "count"],
  [{"Malcolm", "Mike"}, "10"],
  ["Zoe", {"5", "20"}]
]

Examines the matrix produced by compare. Returns false if any cell is false. Otherwise returns true.

Examples

iex> results_table =
...> [
...>   ["name", "count"],
...>   [{"Malcolm", "Mike"}, "10"],
...>   ["Zoe", {"5", "20"}]
...> ]
...>
...> Tabular.TestSupport.equal?(results_table)
false