All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.dmfs.jems2.hamcrest.matchers.function.FragileFunctionMatcher Maven / Gradle / Ivy

/*
 * Copyright 2021 dmfs GmbH
 *
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.dmfs.jems2.hamcrest.matchers.function;

import org.dmfs.jems2.FragileFunction;
import org.dmfs.jems2.hamcrest.matchers.fragile.BrokenFragileMatcher;
import org.hamcrest.Description;
import org.hamcrest.Matcher;
import org.hamcrest.TypeSafeDiagnosingMatcher;

import static org.dmfs.jems2.hamcrest.matchers.LambdaMatcher.having;
import static org.hamcrest.Matchers.is;


/**
 * A {@link Matcher} to test {@link FragileFunction}s.
 */
public final class FragileFunctionMatcher extends TypeSafeDiagnosingMatcher>
{
    private final Argument mArgument;
    private final Matcher mResultMatcher;


    /**
     * Returns a {@link Matcher} which verifies that the tested {@link FragileFunction} throws the given throwable to the given argument.
     */
    public static  Matcher> throwing(
        Argument argument,
        Matcher throwableMatcher)
    {
        return having("value Fragile", testee -> () -> testee.value(argument), is(BrokenFragileMatcher.throwing(throwableMatcher)));
    }


    /**
     * Returns a {@link Matcher} which verifies that the tested {@link FragileFunction} associates the given result to the given argument.
     */
    public static  Matcher> associates(Argument argument, Result result)
    {
        return new FragileFunctionMatcher<>(argument, is(result));
    }


    /**
     * Returns a {@link Matcher} which verifies that the tested {@link FragileFunction} associates a result satisfying the given {@link Matcher} to the given
     * argument.
     */
    public static  Matcher> associates(
        Argument argument, Matcher resultMatcher)
    {
        return new FragileFunctionMatcher<>(argument, resultMatcher);
    }


    public FragileFunctionMatcher(Argument mArgument, Matcher mResultMatcher)
    {
        this.mArgument = mArgument;
        this.mResultMatcher = mResultMatcher;
    }


    @Override
    protected boolean matchesSafely(FragileFunction item, Description mismatchDescription)
    {
        Result result = null;
        try
        {
            result = item.value(mArgument);
        }
        catch (Throwable throwable)
        {
            mismatchDescription.appendText(String.format("threw %s for argument %s", throwable, mArgument));
            return false;
        }
        if (!mResultMatcher.matches(result))
        {
            mismatchDescription.appendText(String.format("result for argument %s ", mArgument));
            mResultMatcher.describeMismatch(result, mismatchDescription);
            return false;
        }
        return true;
    }


    @Override
    public void describeTo(Description description)
    {
        description
            .appendText(String.format("result for argument %s ", mArgument))
            .appendDescriptionOf(mResultMatcher);
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy