CollectionSubject

CollectionSubject.contains

CollectionSubject.contains(expected)

Asserts that expected is within the collection.

Method: CollectionSubject.contains

PARAMETERS

expected:

(str) the value that must be present.

CollectionSubject.contains_at_least

CollectionSubject.contains_at_least(expect_contains)

Assert that the collection is a subset of the given predicates.

Method: CollectionSubject.contains_at_least

The collection must contain all the values. It can contain extra elements. The multiplicity of values is respected. Checking that the relative order of matches is the same as the passed-in expected values order can done by calling in_order().

PARAMETERS

expect_contains:

(list) values that must be in the collection.

RETURNS

Ordered (see _ordered_incorrectly_new).

CollectionSubject.contains_at_least_predicates

CollectionSubject.contains_at_least_predicates(matchers)

Assert that the collection is a subset of the given predicates.

Method: CollectionSubject.contains_at_least_predicates

The collection must match all the predicates. It can contain extra elements. The multiplicity of matchers is respected. Checking that the relative order of matches is the same as the passed-in matchers order can done by calling in_order().

PARAMETERS

matchers:

(list of [Matcher]) (see matchers struct).

RETURNS

Ordered (see _ordered_incorrectly_new).

CollectionSubject.contains_exactly

CollectionSubject.contains_exactly(expected)

Check that a collection contains exactly the given elements.

Method: CollectionSubject.contains_exactly

  • Multiplicity is respected.

  • The collection must contain all the values, no more or less.

  • Checking that the order of matches is the same as the passed-in matchers order can be done by call in_order().

The collection must contain all the values and no more. Multiplicity of values is respected. Checking that the order of matches is the same as the passed-in matchers order can done by calling in_order().

PARAMETERS

expected:

(list) values that must exist.

RETURNS

Ordered (see _ordered_incorrectly_new).

CollectionSubject.contains_exactly_predicates

CollectionSubject.contains_exactly_predicates(expected)

Check that the values correspond 1:1 to the predicates.

Method: CollectionSubject.contains_exactly_predicates

  • There must be a 1:1 correspondence between the container values and the predicates.

  • Multiplicity is respected (i.e., if the same predicate occurs twice, then two distinct elements must match).

  • Matching occurs in first-seen order. That is, a predicate will “consume” the first value in actual_container it matches.

  • The collection must match all the predicates, no more or less.

  • Checking that the order of matches is the same as the passed-in matchers order can be done by call in_order().

Note that confusing results may occur if predicates with overlapping match conditions are used. For example, given: actual=[“a”, “ab”, “abc”], predicates=[, , ]

Then the result will be they aren’t equal: the first two predicates consume “a” and “ab”, leaving only “abc” for the predicate to match against, which fails.

PARAMETERS

expected:

(list of [Matcher]) that must match.

RETURNS

Ordered (see _ordered_incorrectly_new).

CollectionSubject.contains_none_of

CollectionSubject.contains_none_of(values)

Asserts the collection contains none of values.

Method: CollectionSubject.contains_none_of

PARAMETERS

values:

([collection]) values of which none of are allowed to exist.

CollectionSubject.contains_predicate

CollectionSubject.contains_predicate(matcher)

Asserts that matcher matches at least one value.

Method: CollectionSubject.contains_predicate

PARAMETERS

matcher:

([Matcher]) (see matchers struct).

CollectionSubject.has_size

CollectionSubject.has_size(expected)

Asserts that expected is the size of the collection.

Method: CollectionSubject.has_size

PARAMETERS

expected:

([int]) the expected size of the collection.

CollectionSubject.new

CollectionSubject.new(values, meta, container_name=”values”, sortable=True, element_plural_name=”elements”)

Creates a “CollectionSubject” struct.

Method: CollectionSubject.new

Public Attributes:

  • actual: The wrapped collection.

PARAMETERS

values:

([collection]) the values to assert against.

meta:

(ExpectMeta) the metadata about the call chain.

container_name:

(default "values") (str) conceptual name of the container.

sortable:

(default True) (bool) True if output should be sorted for display, False if not.

element_plural_name:

(default "elements") (str) the plural word for the values in the container.

RETURNS

CollectionSubject.

CollectionSubject.not_contains_predicate

CollectionSubject.not_contains_predicate(matcher)

Asserts that matcher matches no values in the collection.

Method: CollectionSubject.not_contains_predicate

PARAMETERS

matcher:

[Matcher] object (see matchers struct).

CollectionSubject.offset

CollectionSubject.offset(offset, factory)

Fetches an element from the collection as a subject.

PARAMETERS

offset:

([int]) the offset to fetch

factory:

([callable]). The factory function to use to create the subject for the offset’s value. It must have the following signature: def factory(value, *, meta).

RETURNS

Object created by factory.

CollectionSubject.transform

CollectionSubject.transform(desc=None, map_each=None, loop=None, filter=None)

Transforms a collections’s value and returns another CollectionSubject.

This is equivalent to applying a list comprehension over the collection values, but takes care of propagating context information and wrapping the value in a CollectionSubject.

transform(map_each=M, loop=L, filter=F) is equivalent to [M(v) for v in L(collection) if F(v)].

PARAMETERS

desc:

(default None) (optional str) a human-friendly description of the transform for use in error messages. Required when a description can’t be inferred from the other args. The description can be inferred if the filter arg is a named function (non-lambda) or Matcher object.

map_each:

(default None) (optional [callable]) function to transform an element in the collection. It takes one positional arg, the loop’s current iteration value, and its return value will be the element’s new value. If not specified, the values from the loop iteration are returned unchanged.

loop:

(default None) (optional [callable]) function to produce values from the original collection and whose values are iterated over. It takes one positional arg, which is the original collection. If not specified, the original collection values are iterated over.

filter:

(default None) (optional [callable]) function that decides what values are passed onto map_each for inclusion in the final result. It takes one positional arg, the value to match (which is the current iteration value before map_each is applied), and returns a bool (True if the value should be included in the result, False if it should be skipped).

RETURNS

CollectionSubject of the transformed values.