watermelon v0.1.1 Watermelon.Expression

Link to this section Summary

Functions

Creates new expression using provided type

Run matching against expression

Register new type definition for Cucumber expressions

Link to this section Types

Link to this opaque

match_spec() (opaque)

Link to this type

t()
t() :: %Watermelon.Expression{match: Regex.t() | match_spec(), raw: binary()}

Link to this section Functions

Link to this function

from(regex)
from(Regex.t() | binary()) :: t()

Creates new expression using provided type.

When regular expression is passed then it will assume that this will be exactly the matcher for the given step, it will not use any transformations and will return all matched groups (but not whole match) to the match function.

When string is passed it will parse it as a Cucumber expression and will be parsed as Cucumber expression.

To match resulting value against your string, see match/2.

Default types registered

  • {int} that will match \d+ regular expression and will return number which is represented by decimal values that were matched
  • {float} that will match \d*\.\d+ and will return float represented by the decimal value that is matched, it also allows floats without digits before dot, so ".1" will be matched and will result with 0.1
  • {word} that will match any whitespace delimited word
  • {string} that will match any quoted/double-quoted string (escaping of the nested quotes is not supported)
  • {} what will match any string, this can be used only once at the end of the string, otherwise will always cause match failures

Examples

iex> Watermelon.Expression.from(~r/^foo and (\d+) bar(:?s)$/)
#Watermelon.Expression<~r/^foo and (\d+) bar(:?s)$/>

iex> Watermelon.Expression.from("foo and {int} bar(s)")
#Watermelon.Expression<foo and {int} bar(s)>

Above expressions are will match exactly the same steps, however the second one (one using Cucumber expressions) will automatically convert matched value to integer while the one using regular expression will leave it as a string.

Link to this function

match(expression, data)
match(match :: t(), data :: binary()) :: {:ok, [term()]} | :error

Run matching against expression.

Link to this function

register_type(name, regexp, transform \\ &(&1))
register_type(name :: binary(), regexp :: Regex.t(), transform) :: :ok
when transform: (... -> term())

Register new type definition for Cucumber expressions.

Example

iex> Watermelon.Expression.register_type(:bin, ~r/[01]+/, &String.to_integer(&1, 2))
iex>
iex> match = Watermelon.Expression.from("match {bin}")
iex> Watermelon.Expression.match(match, "match 101")
{:ok, [5]}