Cucumberex.DataTable (cucumberex v0.2.1)

Copy Markdown View Source

Wrapper around Cucumber pickle table data with convenience accessors.

Methods mirror cucumber-ruby's DataTable: raw/1 — [[cell, ...], ...] hashes/1 — [%{header => value}, ...] rows/1 — rows without header rows_hash/1 — first column as key, second as value transpose/1 — swap rows/columns map_headers/2 — rename headers diff!/2 — compare two tables (raises on mismatch)

Summary

Functions

Compare two tables; raise if rows differ, return :ok otherwise.

Treat the first row as headers and return each subsequent row as a map.

Apply a transform function to every value in a named column.

Rename headers using a map of old -> new or a 1-arity function.

Wrap a list of rows in a DataTable.

Return the raw rows list.

Return rows without the header.

Treat each row as a key/value pair (first column = key, second = value).

Like hashes/1 but converts header keys to atoms.

Swap rows and columns.

Raise if col_name is not among the header row; return :ok otherwise.

Raise if any row's length differs from width; return :ok otherwise.

Types

t()

@type t() :: %Cucumberex.DataTable{rows: [[String.t()]]}

Functions

diff!(data_table1, data_table2)

Compare two tables; raise if rows differ, return :ok otherwise.

Examples

iex> a = Cucumberex.DataTable.new([["h"], ["1"]])
iex> Cucumberex.DataTable.diff!(a, a)
:ok

hashes(data_table)

Treat the first row as headers and return each subsequent row as a map.

Examples

iex> Cucumberex.DataTable.new([["name", "age"], ["alice", "30"]])
...> |> Cucumberex.DataTable.hashes()
[%{"name" => "alice", "age" => "30"}]

iex> Cucumberex.DataTable.new([]) |> Cucumberex.DataTable.hashes()
[]

map_column(data_table, col_name, fun)

Apply a transform function to every value in a named column.

Examples

iex> Cucumberex.DataTable.new([["n", "v"], ["a", "1"], ["b", "2"]])
...> |> Cucumberex.DataTable.map_column("v", &String.to_integer/1)
...> |> Cucumberex.DataTable.raw()
[["n", "v"], ["a", 1], ["b", 2]]

map_headers(dt, mapping)

Rename headers using a map of old -> new or a 1-arity function.

Examples

iex> Cucumberex.DataTable.new([["full name"], ["alice"]])
...> |> Cucumberex.DataTable.map_headers(%{"full name" => "name"})
...> |> Cucumberex.DataTable.raw()
[["name"], ["alice"]]

iex> Cucumberex.DataTable.new([["name"], ["alice"]])
...> |> Cucumberex.DataTable.map_headers(&String.upcase/1)
...> |> Cucumberex.DataTable.raw()
[["NAME"], ["alice"]]

new(rows)

Wrap a list of rows in a DataTable.

Examples

iex> Cucumberex.DataTable.new([["a", "b"], ["1", "2"]]).rows
[["a", "b"], ["1", "2"]]

raw(data_table)

Return the raw rows list.

Examples

iex> Cucumberex.DataTable.new([["a"], ["1"]]) |> Cucumberex.DataTable.raw()
[["a"], ["1"]]

rows(data_table)

Return rows without the header.

Examples

iex> Cucumberex.DataTable.new([["h"], ["1"], ["2"]]) |> Cucumberex.DataTable.rows()
[["1"], ["2"]]

iex> Cucumberex.DataTable.new([]) |> Cucumberex.DataTable.rows()
[]

rows_hash(data_table)

Treat each row as a key/value pair (first column = key, second = value).

Examples

iex> Cucumberex.DataTable.new([["name", "alice"], ["age", "30"]])
...> |> Cucumberex.DataTable.rows_hash()
%{"name" => "alice", "age" => "30"}

symbolic_hashes(dt)

Like hashes/1 but converts header keys to atoms.

Only call this when headers are known, bounded values (e.g. fixture table headers that match struct fields). Avoid with arbitrary user-supplied table headers — use hashes/1 instead to prevent unbounded atom creation.

Examples

iex> Cucumberex.DataTable.new([["name"], ["alice"]])
...> |> Cucumberex.DataTable.symbolic_hashes()
[%{name: "alice"}]

transpose(data_table)

Swap rows and columns.

Examples

iex> Cucumberex.DataTable.new([["a", "b"], ["1", "2"]])
...> |> Cucumberex.DataTable.transpose()
...> |> Cucumberex.DataTable.raw()
[["a", "1"], ["b", "2"]]

verify_column!(data_table, col_name)

Raise if col_name is not among the header row; return :ok otherwise.

Examples

iex> Cucumberex.DataTable.new([["name", "age"]]) |> Cucumberex.DataTable.verify_column!("name")
:ok

verify_table_width!(data_table, width)

Raise if any row's length differs from width; return :ok otherwise.

Examples

iex> Cucumberex.DataTable.new([["a", "b"], ["1", "2"]])
...> |> Cucumberex.DataTable.verify_table_width!(2)
:ok