Core API¶
BaseDescription¶
-
class
hamcrest.core.base_description.BaseDescription¶ Bases:
hamcrest.core.description.DescriptionBase class for all
Descriptionimplementations.-
append_description_of(value)¶ Appends description of given value to this description.
If the value implements
describe_to, then it will be used.- Returns
self, for chaining- Parameters
value (Any) –
- Return type
-
append_list(start, separator, end, list)¶ Appends a list of objects to the description.
- Parameters
- Returns
self, for chaining- Return type
-
BaseMatcher¶
-
class
hamcrest.core.base_matcher.BaseMatcher(*args, **kwds)¶ Bases:
hamcrest.core.matcher.Matcher[hamcrest.core.base_matcher.T]Base class for all
Matcherimplementations.Most implementations can just implement
_matches, leaving the handling of any mismatch description to thematchesmethod. But if it makes more sense to generate the mismatch description during the matching, overridematchesinstead.-
describe_match(item, match_description)¶ Generates a description of why the matcher has accepted the item.
The description may be part of a larger description of why a matching failed, so it should be concise.
This method assumes that
matches(item)isTrue, but will not check this.- Parameters
item (T) – The item that the
Matcherhas accepted.match_description (hamcrest.core.description.Description) – The description to be built or appended to.
- Return type
-
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 (T) – The item that the
Matcherhas rejected.mismatch_description (hamcrest.core.description.Description) – The description to be built or appended to.
- Return type
-
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 (T) – The object against which the matcher is evaluated.
mismatch_description (Optional[hamcrest.core.description.Description]) –
- Returns
Trueifitemmatches, otherwiseFalse.- Return type
-
Description¶
-
class
hamcrest.core.description.Description¶ Bases:
objectA description of a
Matcher.A
Matcherwill describe itself to a description which can later be used for reporting.-
append_description_of(value)¶ Appends description of given value to this description.
If the value implements
describe_to, then it will be used.- Returns
self, for chaining- Parameters
value (Any) –
- Return type
-
append_list(start, separator, end, list)¶ Appends a list of objects to the description.
- Parameters
- Returns
self, for chaining- Return type
-
Matcher¶
-
class
hamcrest.core.matcher.Matcher(*args, **kwds)¶ Bases:
Generic[hamcrest.core.matcher.T],hamcrest.core.selfdescribing.SelfDescribingA matcher over acceptable values.
A matcher is able to describe itself to give feedback when it fails.
Matcher implementations should not directly implement this protocol. Instead, extend the
BaseMatcherclass, which will ensure that theMatcherAPI can grow to support new features and remain compatible with allMatcherimplementations.-
describe_match(item, match_description)¶ Generates a description of why the matcher has accepted the item.
The description may be part of a larger description of why a matching failed, so it should be concise.
This method assumes that
matches(item)isTrue, but will not check this.- Parameters
item (T) – The item that the
Matcherhas accepted.match_description (hamcrest.core.description.Description) – The description to be built or appended to.
- Return type
-
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 (T) – The item that the
Matcherhas rejected.mismatch_description (hamcrest.core.description.Description) – The description to be built or appended to.
- Return type
-
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 (T) – The object against which the matcher is evaluated.
mismatch_description (Optional[hamcrest.core.description.Description]) –
- Returns
Trueifitemmatches, otherwiseFalse.- Return type
-
SelfDescribing¶
-
class
hamcrest.core.selfdescribing.SelfDescribing¶ Bases:
objectThe ability of an object to describe itself.
-
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
-
SelfDescribingValue¶
-
class
hamcrest.core.selfdescribingvalue.SelfDescribingValue(value)¶ Bases:
hamcrest.core.selfdescribing.SelfDescribingWrap any value in a
SelfDescribingValueto satisfy theSelfDescribinginterface.Deprecated: No need for this class now that
append_description_ofhandles any type of value.-
describe_to(description)¶ Generates a description of the value.
- Parameters
description (hamcrest.core.description.Description) –
- Return type
-
StringDescription¶
-
class
hamcrest.core.string_description.StringDescription¶ Bases:
hamcrest.core.base_description.BaseDescriptionA
Descriptionthat is stored as a string.
-
hamcrest.core.string_description.tostring(selfdescribing)¶ Returns the description of a
SelfDescribingobject as a string.- Parameters
selfdescribing (hamcrest.core.selfdescribing.SelfDescribing) – The object to be described.
- Returns
The description of the object.
- Return type