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

com.codereligion.cherry.test.hamcrest.StringContains Maven / Gradle / Ivy

The newest version!
/**
 * Copyright 2015 www.codereligion.com
 *
 * 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 com.codereligion.cherry.test.hamcrest;

import org.hamcrest.Matcher;
import org.hamcrest.core.SubstringMatcher;
import static com.google.common.base.Preconditions.checkArgument;

/**
 * A matcher which expects a string to contain a given string. This matcher basically does the same as Hamcrest's {@link org.hamcrest.core.StringContains}, with
 * the difference that it allows varargs which will be used to format the given string before the actual matching happens.
 *
 * @author Sebastian Gröbler
 * @since 05.04.2015
 */
public class StringContains extends SubstringMatcher {

    /**
     * Creates a matcher that matches if the examined {@link String} contains the specified {@link String} anywhere. Optional arguments can be provided which
     * will be used to format the given {@code substring}.
     * 

* Example usage: {@code assertThat("some string", containsString("some %s", "string"))} * * @param substring the substring that the returned matcher will expect to find within any examined string * @param args the arguments to use for formatting the given {@code substring} * @return a new matcher */ public static Matcher containsString(final String substring, final Object... args) { checkArgument(substring != null, "substring must not be null."); return new StringContains(String.format(substring, args)); } private StringContains(final String substring) { super(substring); } @Override protected boolean evalSubstringOf(String s) { return s.indexOf(substring) >= 0; } @Override protected String relationship() { return "containing"; } }





© 2015 - 2025 Weber Informatics LLC | Privacy Policy