Packages

This repo contains a testing library used in Carla end-to-end tests. It provides a library to describe and test conversations.

Current section

Files

Jump to
carla_test_helper lib carla_test_helper assertions.ex
Raw

lib/carla_test_helper/assertions.ex

defmodule CarlaTestHelper.Assertions do
@moduledoc """
Assertions used by CarlaTestHelper
"""
import ExUnit.Assertions
import CarlaTestHelper.Util
@doc """
Helper function for asserting a response uri contains a uri.
Parameters:
* uri - The resulting uri
* expected_uri - The expected uri to be contained in the resulting uri
"""
def assert_uri(uri, expected_uri) do
String.contains?(uri, expected_uri)
|> assert("Expected '#{uri}' to contain '#{expected_uri}'")
end
@doc """
Helper function for asserting a response uri contains a uri
and parameters.
Parameters:
* uri - The resulting uri
* expected_uri - The expected uri to be contained in the resulting uri
* expected_params - The expected paramms to be contained in the resulting uri
"""
def assert_uri(uri, expected_uri, expected_params) do
assert_uri(uri, expected_uri)
actual_query_string =
uri
|> String.split("?", parts: 2)
|> Enum.at(1)
actual_query =
actual_query_string
|> URI.query_decoder
|> Enum.to_list
expected_params
|> Enum.map(fn(param) ->
expected_key_value =
param
|> String.split("=", parts: 2)
|> List.to_tuple
actual_query
|> Enum.reduce(false, fn(actual_key_value, acc) ->
if acc === false do
actual_key_value === expected_key_value
else
acc
end
end)
|> assert("Expected '#{uri}' to contain param '#{elem(expected_key_value, 0)}=#{elem(expected_key_value, 1)}'")
end)
end
@doc """
Helpers function for asserting a resulting response action
with an expected action.
Parameters:
* expected_action - Action expected from the response
* actual_action - Resulting action from the response
"""
def assert_action(expected_action, actual_action) do
assert expected_action == actual_action
end
@doc """
Helper function for asserting that the first button in `response` that has
the label `label`.
Parameters:
* response - The response to search through
* label - The label of the button to find
"""
def assert_button(response, label) do
response
|> get_button(label)
|> assert("Cannot find a button with the label '#{label}'")
end
@doc """
Helper function for asserting that the first button in `response` that has
the label `label` contains the given `value`
Parameters:
* response - The response to search through
* label - The label of the button to find
* value - Expected value of that button
"""
def assert_button_payload(response, label, value) do
assert_button(response, label)
message = response
|> get_button_payload(label)
String.contains?(String.downcase(message), String.downcase(value))
|> assert("Expected button '#{label}' to contain '#{value}', but has '#{message}'")
end
@doc """
Helper function for asserting that the first button in `response` that has
the label `label` contains the given `uri`
Parameters:
* response - The response to search through
* label - The label of the button to find
* uri - Expected value of the button
"""
def assert_button_url(response, label, expected_uri) do
assert_button(response, label)
actual_uri = response
|> get_button_payload(label)
|> URI.decode
assert_uri(actual_uri, expected_uri)
end
@doc """
Helper function for asserting that the first button in `response` that has
the label `label` contains the given `uri` and `params`
Parameters:
* response - The response to search through
* label - The label of the button to find
* uri - Expected value of the button
* params - Expected parameter values of the uri
"""
def assert_button_url(response, label, expected_uri, expected_params) do
assert_button(response, label)
actual_uri = response
|> get_button_payload(label)
|> URI.decode
assert_uri(actual_uri, expected_uri, expected_params)
end
@doc """
Helper function for asserting a gallery card at `index` is not null
Parameters:
* response - The response to search through
* index - The index of the gallery card
"""
def assert_gallery_card(response, index) do
response
|> get_gallery_card(index)
|> assert("Gallery card at position '#{index}' does not exist")
end
@doc """
Helper function for asserting the gallery card at `index` contains
a button with the label `label`
Parameters:
* response - The response to search through
* index - The index of the gallery card
* label - The label of the button to find
"""
def assert_gallery_card_button(response, index, label) do
assert_gallery_card(response, index)
response
|> get_gallery_card_button(index, label)
|> assert("Gallery card at position '#{index}' to contain button with the label #{label}")
end
@doc """
Helper function for asserting the gallery card at `index` contains
a button with the label `label` and contains the given `uri`
Parameters:
* response - The response to search through
* index - The index of the gallery card
* label - The label of the button to find
* uri - Expected value of the button
"""
def assert_gallery_card_button_url(response, index, label, expected_uri) do
assert_gallery_card_button(response, index, label)
actual_uri = response
|> get_gallery_card_button_payload(index, label)
|> URI.decode
assert_uri(actual_uri, expected_uri)
end
@doc """
Helper function for asserting the gallery card at `index` contains
a button with the label `label` and contains the given `uri` and `params`
Parameters:
* response - The response to search through
* index - The index of the gallery card
* label - The label of the button to find
* uri - Expected value of the button
* params - Expected parameter values of the uri
"""
def assert_gallery_card_button_url(response, index, label, expected_uri, expected_params) do
assert_gallery_card_button(response, index, label)
actual_uri = response
|> get_gallery_card_button_payload(index, label)
|> URI.decode
assert_uri(actual_uri, expected_uri, expected_params)
end
end