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

org.dmfs.jems2.hamcrest.matchers.generatable.GeneratableMatcher 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.generatable;

import org.dmfs.jems2.Generatable;
import org.dmfs.jems2.Generator;
import org.dmfs.jems2.iterable.Mapped;
import org.dmfs.jems2.iterable.Seq;
import org.hamcrest.CoreMatchers;
import org.hamcrest.Description;
import org.hamcrest.Matcher;
import org.hamcrest.TypeSafeDiagnosingMatcher;


/**
 * A {@link Matcher} to test {@link Generatable}s for the correct start sequence. Note, due do the infinite nature of a {@link Generatable} it is not possible
 * to test all the results.
 */
public final class GeneratableMatcher extends TypeSafeDiagnosingMatcher>
{
    private final Iterable> mExpectedStartSequence;


    @SafeVarargs
    public static  Matcher> startsWith(T... elements)
    {
        return new GeneratableMatcher<>(new Mapped<>(CoreMatchers::is, new Seq<>(elements)));
    }


    @SafeVarargs
    public static  Matcher> startsWith(Matcher... elements)
    {
        return new GeneratableMatcher<>(new Seq<>(elements));
    }


    public static  Matcher> startsWith(Iterable> elements)
    {
        return new GeneratableMatcher<>(elements);
    }


    public GeneratableMatcher(Iterable> expectedStartSequence)
    {
        mExpectedStartSequence = expectedStartSequence;
    }


    @Override
    protected boolean matchesSafely(Generatable item, Description mismatchDescription)
    {
        int pos = 0;
        Generator generator = item.generator();
        for (Matcher matcher : mExpectedStartSequence)
        {
            T next = generator.next();
            if (!matcher.matches(next))
            {
                mismatchDescription.appendText(String.format("element at position %d ", pos));
                matcher.describeMismatch(next, mismatchDescription);
                return false;
            }
            pos += 1;
        }
        return true;
    }


    @Override
    public void describeTo(Description description)
    {
        description.appendText("Generatable with start sequence ");
        boolean first = true;
        for (Matcher matcher : mExpectedStartSequence)
        {
            if (first)
            {
                first = false;
            }
            else
            {
                description.appendText(", ");
            }
            matcher.describeTo(description);
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy