Code Documentation

Metamatcher is a matcher that checks that another matcher behaves correctly.

When new matchers are developed, it is vital to check that they match as expected and produce helpful desriptions and mismatch_descriptions.

This metamatcher does exactly that.

Say, you have written a matcher called is_twice_as_big_as, and you want it to compare ints. You intend to use it like this:

assert_that(4, is_twice_as_big_as(2))

Under the hood, the following is called:

is_twice_as_big_as(2)._matches(4)

Keeping that in mind, here’s how you can check your matcher with the metamatcher:

def test_is_twice_as_big_as(...)
    assert_that(
        # Your initialized matcher
        is_twice_as_big_as(2),
        # The metamatcher specifying the value for matching
        matches(4)
    )

This will fail if your is_twice_as_big_as matcher doesn’t match.

To check that your matcher produces the correct description:

def test_is_twice_as_big_as(...)
    assert_that(
        is_twice_as_big_as(2),
        matches(4).with_description("An int twice as big as <2>")
    )

This will fail if your is_twice_as_big_as matcher doesn’t match, if the description it produces is wrong, or both.

You can also check that your matcher doesn’t match in certain situations. To do that, use the doesnt_match function, and to check the mismatch description, call the with_mismatch_description method.

Note, that you can use the with_description method with the doesnt_match metamatcher, but calling with_mismatch_description with the matches flavour of the metamatcher, will throw an exception.

matches

pyhamcrest_metamatchers.metamatchers.matches(a_matcher)[source]

Checks that the matcher under test matches the value

Parameters:a_matcher – The matcher that needs to be checked.
Returns:pyhamcrest_metamatchers.metamatchers.MetaMatcher

doesnt_match

pyhamcrest_metamatchers.metamatchers.doesnt_match(a_matcher)[source]

Checks that the matcher under test doesn’t match the value

Parameters:a_matcher – The matcher that needs to be checked.
Returns:pyhamcrest_metamatchers.metamatchers.MetaMatcher

with_description

MetaMatcher.with_description(description)[source]

Adds the check for the description generated by the matcher that is being tested. If this method is not called, the matcher will not check the description at all.

If this method _is_ called, then the description, generated by the matcher under test, will be checked. If the actual description doesn’t match the one set here, the metamatcher will not match.

with_mismatch_description

MetaMatcher.with_mismatch_description(mismatch_description)[source]

Adds the check for the mismatch description generated by the matcher being tested. The logic is the same as with :pyhamcrest_metamatchers.metamatchers.MetaMatcher.with_description()