tests

package
v0.23.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 25, 2018 License: AGPL-3.0 Imports: 14 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var XFalseResult = XTestResult{}

XFalseResult can be used as a singleton for false result values

View Source
var XTESTS = map[string]functions.XFunction{
	"is_error":  functions.OneArgFunction(IsError),
	"has_value": functions.OneArgFunction(HasValue),

	"has_group":          functions.TwoArgFunction(HasGroup),
	"has_wait_timed_out": functions.OneArgFunction(HasWaitTimedOut),

	"is_text_eq":      functions.TwoTextFunction(IsTextEQ),
	"has_phrase":      functions.TwoTextFunction(HasPhrase),
	"has_only_phrase": functions.TwoTextFunction(HasOnlyPhrase),
	"has_any_word":    functions.TwoTextFunction(HasAnyWord),
	"has_all_words":   functions.TwoTextFunction(HasAllWords),
	"has_beginning":   functions.TwoTextFunction(HasBeginning),
	"has_text":        functions.OneTextFunction(HasText),
	"has_pattern":     functions.TwoTextFunction(HasPattern),

	"has_number":         functions.OneTextFunction(HasNumber),
	"has_number_between": functions.ThreeArgFunction(HasNumberBetween),
	"has_number_lt":      functions.TextAndNumberFunction(HasNumberLT),
	"has_number_lte":     functions.TextAndNumberFunction(HasNumberLTE),
	"has_number_eq":      functions.TextAndNumberFunction(HasNumberEQ),
	"has_number_gte":     functions.TextAndNumberFunction(HasNumberGTE),
	"has_number_gt":      functions.TextAndNumberFunction(HasNumberGT),

	"has_date":    functions.OneTextFunction(HasDate),
	"has_date_lt": functions.TextAndDateFunction(HasDateLT),
	"has_date_eq": functions.TextAndDateFunction(HasDateEQ),
	"has_date_gt": functions.TextAndDateFunction(HasDateGT),

	"has_phone": functions.InitialTextFunction(0, 1, HasPhone),
	"has_email": functions.OneTextFunction(HasEmail),

	"has_state":    functions.OneTextFunction(HasState),
	"has_district": HasDistrict,
	"has_ward":     HasWard,
}

XTESTS is our mapping of the excellent test names to their actual functions

Functions

func HasAllWords

func HasAllWords(env utils.Environment, text types.XText, test types.XText) types.XValue

HasAllWords tests whether all the `words` are contained in `text`

The words can be in any order and may appear more than once.

@(has_all_words("the quick brown FOX", "the fox")) -> true
@(has_all_words("the quick brown FOX", "the fox").match) -> the FOX
@(has_all_words("the quick brown fox", "red fox")) -> false

@test has_all_words(text, words)

func HasAnyWord

func HasAnyWord(env utils.Environment, text types.XText, test types.XText) types.XValue

HasAnyWord tests whether any of the `words` are contained in the `text`

Only one of the words needs to match and it may appear more than once.

@(has_any_word("The Quick Brown Fox", "fox quick")) -> true
@(has_any_word("The Quick Brown Fox", "red fox")) -> true
@(has_any_word("The Quick Brown Fox", "red fox").match) -> Fox

@test has_any_word(text, words)

func HasBeginning

func HasBeginning(env utils.Environment, text types.XText, beginning types.XText) types.XValue

HasBeginning tests whether `text` starts with `beginning`

Both text values are trimmed of surrounding whitespace, but otherwise matching is strict without any tokenization.

@(has_beginning("The Quick Brown", "the quick")) -> true
@(has_beginning("The Quick Brown", "the quick").match) -> The Quick
@(has_beginning("The Quick Brown", "the   quick")) -> false
@(has_beginning("The Quick Brown", "quick brown")) -> false

@test has_beginning(text, beginning)

func HasDate

func HasDate(env utils.Environment, text types.XText) types.XValue

HasDate tests whether `text` contains a date formatted according to our environment

@(has_date("the date is 2017-01-15")) -> true
@(has_date("the date is 2017-01-15").match) -> 2017-01-15T13:24:30.123456-05:00
@(has_date("there is no date here, just a year 2017")) -> false

@test has_date(text)

func HasDateEQ

func HasDateEQ(env utils.Environment, text types.XText, date types.XDateTime) types.XValue

HasDateEQ tests whether `text` a date equal to `date`

@(has_date_eq("the date is 2017-01-15", "2017-01-15")) -> true
@(has_date_eq("the date is 2017-01-15", "2017-01-15").match) -> 2017-01-15T13:24:30.123456-05:00
@(has_date_eq("the date is 2017-01-15 15:00", "2017-01-15")) -> true
@(has_date_eq("there is no date here, just a year 2017", "2017-06-01")) -> false
@(has_date_eq("there is no date here, just a year 2017", "not date")) -> ERROR

@test has_date_eq(text, date)

func HasDateGT

func HasDateGT(env utils.Environment, text types.XText, date types.XDateTime) types.XValue

HasDateGT tests whether `text` a date after the date `min`

@(has_date_gt("the date is 2017-01-15", "2017-01-01")) -> true
@(has_date_gt("the date is 2017-01-15", "2017-01-01").match) -> 2017-01-15T13:24:30.123456-05:00
@(has_date_gt("the date is 2017-01-15", "2017-03-15")) -> false
@(has_date_gt("there is no date here, just a year 2017", "2017-06-01")) -> false
@(has_date_gt("there is no date here, just a year 2017", "not date")) -> ERROR

@test has_date_gt(text, min)

func HasDateLT

func HasDateLT(env utils.Environment, text types.XText, date types.XDateTime) types.XValue

HasDateLT tests whether `text` contains a date before the date `max`

@(has_date_lt("the date is 2017-01-15", "2017-06-01")) -> true
@(has_date_lt("the date is 2017-01-15", "2017-06-01").match) -> 2017-01-15T13:24:30.123456-05:00
@(has_date_lt("there is no date here, just a year 2017", "2017-06-01")) -> false
@(has_date_lt("there is no date here, just a year 2017", "not date")) -> ERROR

@test has_date_lt(text, max)

func HasDistrict

func HasDistrict(env utils.Environment, args ...types.XValue) types.XValue

HasDistrict tests whether a district name is contained in the `text`. If `state` is also provided then the returned district must be within that state.

@(has_district("Gasabo", "Kigali")) -> true
@(has_district("I live in Gasabo", "Kigali")) -> true
@(has_district("I live in Gasabo", "Kigali").match) -> Rwanda > Kigali City > Gasabo
@(has_district("Gasabo", "Boston")) -> false
@(has_district("Gasabo")) -> true

@test has_district(text, state)

func HasEmail

func HasEmail(env utils.Environment, text types.XText) types.XValue

HasEmail tests whether an email is contained in `text`

@(has_email("my email is [email protected], please respond")) -> true
@(has_email("my email is [email protected], please respond").match) -> [email protected]
@(has_email("my email is <[email protected]>")) -> true
@(has_email("i'm not sharing my email")) -> false

@test has_email(text)

func HasGroup

func HasGroup(env utils.Environment, arg1 types.XValue, arg2 types.XValue) types.XValue

HasGroup returns whether the `contact` is part of group with the passed in UUID

@(has_group(contact, "b7cf0d83-f1c9-411c-96fd-c511a4cfa86d")) -> true
@(has_group(contact, "b7cf0d83-f1c9-411c-96fd-c511a4cfa86d").match) -> Testers
@(has_group(contact, "97fe7029-3a15-4005-b0c7-277b884fc1d5")) -> false

@test has_group(contact, group_uuid)

func HasNumber

func HasNumber(env utils.Environment, text types.XText) types.XValue

HasNumber tests whether `text` contains a number

@(has_number("the number is 42")) -> true
@(has_number("the number is 42").match) -> 42
@(has_number("the number is forty two")) -> false

@test has_number(text)

func HasNumberBetween

func HasNumberBetween(env utils.Environment, arg1 types.XValue, arg2 types.XValue, arg3 types.XValue) types.XValue

HasNumberBetween tests whether `text` contains a number between `min` and `max` inclusive

@(has_number_between("the number is 42", 40, 44)) -> true
@(has_number_between("the number is 42", 40, 44).match) -> 42
@(has_number_between("the number is 42", 50, 60)) -> false
@(has_number_between("the number is not there", 50, 60)) -> false
@(has_number_between("the number is not there", "foo", 60)) -> ERROR

@test has_number_between(text, min, max)

func HasNumberEQ

func HasNumberEQ(env utils.Environment, text types.XText, num types.XNumber) types.XValue

HasNumberEQ tests whether `text` contains a number equal to the `value`

@(has_number_eq("the number is 42", 42)) -> true
@(has_number_eq("the number is 42", 42).match) -> 42
@(has_number_eq("the number is 42", 40)) -> false
@(has_number_eq("the number is not there", 40)) -> false
@(has_number_eq("the number is not there", "foo")) -> ERROR

@test has_number_eq(text, value)

func HasNumberGT

func HasNumberGT(env utils.Environment, text types.XText, num types.XNumber) types.XValue

HasNumberGT tests whether `text` contains a number greater than `min`

@(has_number_gt("the number is 42", 40)) -> true
@(has_number_gt("the number is 42", 40).match) -> 42
@(has_number_gt("the number is 42", 42)) -> false
@(has_number_gt("the number is not there", 40)) -> false
@(has_number_gt("the number is not there", "foo")) -> ERROR

@test has_number_gt(text, min)

func HasNumberGTE

func HasNumberGTE(env utils.Environment, text types.XText, num types.XNumber) types.XValue

HasNumberGTE tests whether `text` contains a number greater than or equal to `min`

@(has_number_gte("the number is 42", 42)) -> true
@(has_number_gte("the number is 42", 42).match) -> 42
@(has_number_gte("the number is 42", 45)) -> false
@(has_number_gte("the number is not there", 40)) -> false
@(has_number_gte("the number is not there", "foo")) -> ERROR

@test has_number_gte(text, min)

func HasNumberLT

func HasNumberLT(env utils.Environment, text types.XText, num types.XNumber) types.XValue

HasNumberLT tests whether `text` contains a number less than `max`

@(has_number_lt("the number is 42", 44)) -> true
@(has_number_lt("the number is 42", 44).match) -> 42
@(has_number_lt("the number is 42", 40)) -> false
@(has_number_lt("the number is not there", 40)) -> false
@(has_number_lt("the number is not there", "foo")) -> ERROR

@test has_number_lt(text, max)

func HasNumberLTE

func HasNumberLTE(env utils.Environment, text types.XText, num types.XNumber) types.XValue

HasNumberLTE tests whether `text` contains a number less than or equal to `max`

@(has_number_lte("the number is 42", 42)) -> true
@(has_number_lte("the number is 42", 44).match) -> 42
@(has_number_lte("the number is 42", 40)) -> false
@(has_number_lte("the number is not there", 40)) -> false
@(has_number_lte("the number is not there", "foo")) -> ERROR

@test has_number_lte(text, max)

func HasOnlyPhrase

func HasOnlyPhrase(env utils.Environment, text types.XText, test types.XText) types.XValue

HasOnlyPhrase tests whether the `text` contains only `phrase`

The phrase must be the only text in the text to match

@(has_only_phrase("The Quick Brown Fox", "quick brown")) -> false
@(has_only_phrase("Quick Brown", "quick brown")) -> true
@(has_only_phrase("the Quick Brown fox", "")) -> false
@(has_only_phrase("", "")) -> true
@(has_only_phrase("Quick Brown", "quick brown").match) -> Quick Brown
@(has_only_phrase("The Quick Brown Fox", "red fox")) -> false

@test has_only_phrase(text, phrase)

func HasPattern

func HasPattern(env utils.Environment, text types.XText, pattern types.XText) types.XValue

HasPattern tests whether `text` matches the regex `pattern`

Both text values are trimmed of surrounding whitespace and matching is case-insensitive.

@(has_pattern("Sell cheese please", "buy (\w+)")) -> false
@(has_pattern("Buy cheese please", "buy (\w+)")) -> true
@(has_pattern("Buy cheese please", "buy (\w+)").match) -> Buy cheese

@test has_pattern(text, pattern)

func HasPhone

func HasPhone(env utils.Environment, text types.XText, args ...types.XValue) types.XValue

HasPhone tests whether `text` contains a phone number. The optional `country_code` argument specifies the country to use for parsing.

@(has_phone("my number is +12067799294")) -> true
@(has_phone("my number is 2067799294", "US")) -> true
@(has_phone("my number is 206 779 9294", "US").match) -> +12067799294
@(has_phone("my number is none of your business", "US")) -> false

@test has_phone(text, country_code)

func HasPhrase

func HasPhrase(env utils.Environment, text types.XText, test types.XText) types.XValue

HasPhrase tests whether `phrase` is contained in `text`

The words in the test phrase must appear in the same order with no other words in between.

@(has_phrase("the quick brown fox", "brown fox")) -> true
@(has_phrase("the Quick Brown fox", "quick fox")) -> false
@(has_phrase("the Quick Brown fox", "")) -> true
@(has_phrase("the.quick.brown.fox", "the quick").match) -> the quick

@test has_phrase(text, phrase)

func HasState

func HasState(env utils.Environment, text types.XText) types.XValue

HasState tests whether a state name is contained in the `text`

@(has_state("Kigali")) -> true
@(has_state("Boston")) -> false
@(has_state("¡Kigali!")) -> true
@(has_state("¡Kigali!").match) -> Rwanda > Kigali City
@(has_state("I live in Kigali")) -> true

@test has_state(text)

func HasText

func HasText(env utils.Environment, text types.XText) types.XValue

HasText tests whether there the text has any characters in it

@(has_text("quick brown")) -> true
@(has_text("quick brown").match) -> quick brown
@(has_text("")) -> false
@(has_text(" \n")) -> false
@(has_text(123)) -> true

@test has_text(text)

func HasValue

func HasValue(env utils.Environment, value types.XValue) types.XValue

HasValue returns whether `value` is non-nil and not an error

Note that `contact.fields` and `run.results` are considered dynamic, so it is not an error to try to retrieve a value from fields or results which don't exist, rather these return an empty value.

@(has_value(datetime("foo"))) -> false
@(has_value(not.existing)) -> false
@(has_value(contact.fields.unset)) -> false
@(has_value("")) -> false
@(has_value("hello")) -> true

@test has_value(value)

func HasWaitTimedOut

func HasWaitTimedOut(env utils.Environment, value types.XValue) types.XValue

HasWaitTimedOut returns whether the last wait timed out.

@(has_wait_timed_out(run)) -> false

@test has_wait_timed_out(run)

func HasWard

func HasWard(env utils.Environment, args ...types.XValue) types.XValue

HasWard tests whether a ward name is contained in the `text`

@(has_ward("Gisozi", "Gasabo", "Kigali")) -> true
@(has_ward("I live in Gisozi", "Gasabo", "Kigali")) -> true
@(has_ward("I live in Gisozi", "Gasabo", "Kigali").match) -> Rwanda > Kigali City > Gasabo > Gisozi
@(has_ward("Gisozi", "Gasabo", "Brooklyn")) -> false
@(has_ward("Gisozi", "Brooklyn", "Kigali")) -> false
@(has_ward("Brooklyn", "Gasabo", "Kigali")) -> false
@(has_ward("Gasabo")) -> false
@(has_ward("Gisozi")) -> true

@test has_ward(text, district, state)

func IsError

func IsError(env utils.Environment, value types.XValue) types.XValue

IsError returns whether `value` is an error

Note that `contact.fields` and `run.results` are considered dynamic, so it is not an error to try to retrieve a value from fields or results which don't exist, rather these return an empty value.

@(is_error(datetime("foo"))) -> true
@(is_error(run.not.existing)) -> true
@(is_error(contact.fields.unset)) -> true
@(is_error("hello")) -> false

@test is_error(value)

func IsTextEQ added in v0.9.0

func IsTextEQ(env utils.Environment, text1 types.XText, text2 types.XText) types.XValue

IsTextEQ returns whether two text values are equal (case sensitive). In the case that they are, it will return the text as the match.

@(is_text_eq("foo", "foo")) -> true
@(is_text_eq("foo", "FOO")) -> false
@(is_text_eq("foo", "bar")) -> false
@(is_text_eq("foo", " foo ")) -> false
@(is_text_eq(run.status, "completed")) -> true
@(is_text_eq(results.webhook.category, "Success")) -> true
@(is_text_eq(results.webhook.category, "Failure")) -> false

@test is_text_eq(text1, text2)

func ParseDecimalFuzzy added in v0.21.2

func ParseDecimalFuzzy(val string, format *utils.NumberFormat) (decimal.Decimal, error)

ParseDecimalFuzzy parses a decimal from a string

func RegisterXTest added in v0.11.0

func RegisterXTest(name string, function functions.XFunction)

RegisterXTest registers a new router test (and Excellent function)

Types

type XTestResult

type XTestResult struct {
	// contains filtered or unexported fields
}

XTestResult encapsulates not only if the test was true but what the match was

func NewTrueResult added in v0.11.0

func NewTrueResult(match types.XValue) XTestResult

NewTrueResult creates a new matched result

func NewTrueResultWithExtra added in v0.16.0

func NewTrueResultWithExtra(match types.XValue, extra map[string]string) XTestResult

NewTrueResultWithExtra creates a new matched result with extra info about the match

func (XTestResult) Describe added in v0.10.1

func (t XTestResult) Describe() string

Describe returns a representation of this type for error messages

func (XTestResult) Extra added in v0.16.0

func (t XTestResult) Extra() map[string]string

Extra returns the extra data about the match

func (XTestResult) Match

func (t XTestResult) Match() types.XValue

Match returns the item which was matched

func (XTestResult) Matched

func (t XTestResult) Matched() bool

Matched returns whether the test matched

func (XTestResult) Reduce

Reduce is called when this object needs to be reduced to a primitive

func (XTestResult) Resolve

func (t XTestResult) Resolve(env utils.Environment, key string) types.XValue

Resolve resolves the given key when this result is referenced in an expression

func (XTestResult) ToXJSON

func (t XTestResult) ToXJSON(env utils.Environment) types.XText

ToXJSON is called when this type is passed to @(json(...))

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL