Decorator Matchers¶
Matchers that decorate other matchers for better expression.
described_as¶
- class hamcrest.core.core.described_as.DescribedAs(description_template, matcher, *values)¶
Bases:
hamcrest.core.base_matcher.BaseMatcher[Any]- Parameters:
description_template (str) –
matcher (hamcrest.core.matcher.Matcher[Any]) –
values (Tuple[Any, ...]) –
- Return type:
None
- describe_mismatch(item, mismatch_description)¶
Generates a description of why the matcher has not accepted the item.
The description will be part of a larger description of why a matching failed, so it should be concise.
This method assumes that
matches(item)isFalse, but will not check this.- Parameters:
item (Any) – The item that the
Matcherhas rejected.mismatch_description (hamcrest.core.description.Description) – The description to be built or appended to.
- Return type:
None
- describe_to(description)¶
Generates a description of the object.
The description may be part of a description of a larger object of which this is just a component, so it should be worded appropriately.
- Parameters:
description (hamcrest.core.description.Description) – The description to be built or appended to.
- Return type:
None
- matches(item, mismatch_description=None)¶
Evaluates the matcher for argument item.
If a mismatch is detected and argument
mismatch_descriptionis provided, it will generate a description of why the matcher has not accepted the item.- Parameters:
item (Any) – The object against which the matcher is evaluated.
mismatch_description (Optional[hamcrest.core.description.Description]) –
- Returns:
Trueifitemmatches, otherwiseFalse.- Return type:
bool
- hamcrest.core.core.described_as.described_as(description, matcher[, value1[, ...]])¶
Adds custom failure description to a given matcher.
- Parameters:
description (str) – Overrides the matcher’s description.
matcher (hamcrest.core.matcher.Matcher[Any]) – The matcher to satisfy.
value1,... – Optional comma-separated list of substitution values.
- Return type:
The description may contain substitution placeholders %0, %1, etc. These will be replaced by any values that follow the matcher.
is_¶
- class hamcrest.core.core.is_.Is(matcher)¶
Bases:
hamcrest.core.base_matcher.BaseMatcher[hamcrest.core.core.is_.T]- Parameters:
matcher (hamcrest.core.matcher.Matcher[hamcrest.core.core.is_.T]) –
- Return type:
None
- describe_mismatch(item, mismatch_description)¶
Generates a description of why the matcher has not accepted the item.
The description will be part of a larger description of why a matching failed, so it should be concise.
This method assumes that
matches(item)isFalse, but will not check this.- Parameters:
item (hamcrest.core.core.is_.T) – The item that the
Matcherhas rejected.mismatch_description (hamcrest.core.description.Description) – The description to be built or appended to.
- Return type:
None
- describe_to(description)¶
Generates a description of the object.
The description may be part of a description of a larger object of which this is just a component, so it should be worded appropriately.
- Parameters:
description (hamcrest.core.description.Description) – The description to be built or appended to.
- matches(item, mismatch_description=None)¶
Evaluates the matcher for argument item.
If a mismatch is detected and argument
mismatch_descriptionis provided, it will generate a description of why the matcher has not accepted the item.- Parameters:
item (hamcrest.core.core.is_.T) – The object against which the matcher is evaluated.
mismatch_description (Optional[hamcrest.core.description.Description]) –
- Returns:
Trueifitemmatches, otherwiseFalse.- Return type:
bool
- hamcrest.core.core.is_.is_(x: Type) hamcrest.core.matcher.Matcher[Any]¶
- hamcrest.core.core.is_.is_(x: hamcrest.core.matcher.Matcher[hamcrest.core.core.is_.T]) hamcrest.core.matcher.Matcher[hamcrest.core.core.is_.T]
- hamcrest.core.core.is_.is_(x: hamcrest.core.core.is_.T) hamcrest.core.matcher.Matcher[hamcrest.core.core.is_.T]
Decorates another matcher, or provides shortcuts to the frequently used
is(equal_to(x))andis(instance_of(x)).- Parameters:
x – The matcher to satisfy, or a type for
instance_ofmatching, or an expected value forequal_tomatching.
This matcher compares the evaluated object to the given matcher.
Note
PyHamcrest’s
is_matcher is unrelated to Python’sisoperator. The matcher for object identity issame_instance.If the
xargument is a matcher, its behavior is retained, but the test may be more expressive. For example:assert_that(value, less_than(5)) assert_that(value, is_(less_than(5)))
If the
xargument is a type, it is wrapped in aninstance_ofmatcher. This makes the following statements equivalent:assert_that(cheese, instance_of(Cheddar)) assert_that(cheese, is_(instance_of(Cheddar))) assert_that(cheese, is_(Cheddar))
Otherwise, if the
xargument is not a matcher, it is wrapped in anequal_tomatcher. This makes the following statements equivalent:assert_that(cheese, equal_to(smelly)) assert_that(cheese, is_(equal_to(smelly))) assert_that(cheese, is_(smelly))
Choose the style that makes your expression most readable. This will vary depending on context.