Skip to content
Tim edited this page Sep 19, 2015 · 4 revisions

Assertions and Assumptions

These follow the syntax described in Syntax.

Both Assertions and Assumptions declare expectations. A Failed Assumption however only causes a test to be skipped.

Expect a value

Assertion/Assumption Expected value
true true
truthy not false, not nil
false false
falsy false or nil
nil nil
--- compare identity: `expected == actual`
equal(expected, actual)
equals(expected, actual)

tables/lists

--- deep compare table keys and values
same(expected, actual)
--- assert entries in lists to be unique
unique(actual, deep)

numbers

near(expected, actual, tolerance)

strings

match(pattern, actual, [init, [plain]])
matches(pattern, actual, [init, [plain]])

pattern, init are defined as with string.match. If plain is defined, string.find is used instead. Also see the chapter on patterns.

Expect a type()

  • boolean
  • number
  • string
  • table
  • nil
  • userdata
  • function
  • thread

Expect an outcome to a function call

error(func, [expected_error])
errors(func, [expected_error])

error_matches(func, pattern, [init, [plain]])
error_match(func, pattern, [init, [plain]])
matches_error(func, pattern, [init, [plain]])
match_error(func, pattern, [init, [plain]])

pattern, init are defined as with string.match. If plain is defined, string.find is used instead. Also see the chapter on patterns.

Expectations about metatables

--- test if of same type() and having the same metatable
same_class(expected, actual)

Expect a specific class

  • itemstack

Expectations about Spies/Stubs/Mocks

called()
called_at_least(count)
called_at_most(count)
called_more_than(count)
called_less_than(count)

-- takes parameters or matchers
called_with(expected, ...)
returned_with(expected, ...)

Expected parameters for *_with can be exact parameters or Matchers

Clone this wiki locally