MulticomponentMatcher

class pyhamcrest_toolbox.multicomponent.MulticomponentMatcher(*args, **kwargs)[source]

The class that is the base for writing multicomponent matchers, i.e. the ones that have the form of:

assert_that(someting, matches_someting(x).and_something_else(y))

In your subclass, all you have to do is write your and_something_else() methods that register your matcher plugins. NOTE that you have to return self from such methods, as (a) that is required for chaining, and (b) the final instance returned by the chain must be a matcher.

You can write your main matching logic in the traditional way, but you can also register a matcher plugin from your __init__ method.

The MatcherPlugin entities are still matchers and can be used outside of MulticomponentMatchers. They can be reused in several MulticomponentMatchers, or they can even be grouped into mixins and plugged in as bunches.

Here’s an example of what a subclass of the MulticomponentMatcher could look like:

class GrailMatcher(MulticomponentMatcher):
    def __init__(self, is_holy):
        self.register(GrailHolynessMatcher(is_holy))

    def with_width(width):
        self.register(GrailWidthMatcher(wrap_matcher(width)))
        return self

    def with_height(height):
        self.register(GrailHeightMatcher(wrap_matcher(height)))
        return self

And this is all it takes to write your multicomponent matcher. All the descriptions and mismatch descriptions will be build automatically from the plugins.

register

MulticomponentMatcher.register(plugin)[source]

Call this method to register your plugins to your matcher, either from your additional matcher methods (with_something or and_someting) or from the __init__ method. NOTE that you must return self from those additional matcher methods.

Parameters:plugin – Instances of MatcherPluginMixin
Returns:

MatcherPlugin

class pyhamcrest_toolbox.multicomponent.MatcherPlugin(*args, **kwargs)[source]

This is the class to extend when you create your matcher plugins for a multicomponent matcher. Instead of overriding the usual BaseMatcher methods, you need to override the ones below.

The original standard BaseMatcher methods are replaced with these ones because they are overridden in the MatcherPlugin class to work with the MulticomponentMatcher.

component_matches

MatcherPlugin.component_matches(item)[source]

Return True if it matches, False otherwise.

describe_to

MatcherPlugin.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 – The description to be built or appended to.

The same as desribe_to in Matcher (hamcrest.core.selfdescribing.SelfDescribing.describe_to()). Add the description of the object you expect to the description provided.

describe_component_mismatch

MatcherPlugin.describe_component_mismatch(item, mismatch_description)[source]

Basically, the same as hamcrest.core.matcher.Matcher.describe_mismatch().

Here is an example of what a GrailHolynessMatcher might look like: