Search in sources :

Example 71 with Description

use of org.hamcrest.Description in project gradle by gradle.

the class DefaultDomainObjectCollectionTest method allCallsActionForEachNewObjectAddedByTheAction.

@Test
public void allCallsActionForEachNewObjectAddedByTheAction() {
    @SuppressWarnings("unchecked") final Action<CharSequence> action = context.mock(Action.class);
    context.checking(new Expectations() {

        {
            oneOf(action).execute("a");
            will(new org.jmock.api.Action() {

                public Object invoke(Invocation invocation) throws Throwable {
                    container.add("c");
                    return null;
                }

                public void describeTo(Description description) {
                    description.appendText("add 'c'");
                }
            });
            oneOf(action).execute("b");
            oneOf(action).execute("c");
        }
    });
    container.add("a");
    container.add("b");
    container.all(action);
}
Also used : Expectations(org.jmock.Expectations) Action(org.gradle.api.Action) Description(org.hamcrest.Description) Invocation(org.jmock.api.Invocation) Test(org.junit.Test)

Example 72 with Description

use of org.hamcrest.Description in project iosched by google.

the class MatchersHelper method approximateTimeUriMatcher.

/**
     * Some {@link Uri} include specific times, for example all sessions after a given time. Even
     * though time can be set in tests, {@link com.google.samples.apps.iosched.util.TimeUtils
     * .getCurrentTime()} takes into account the passage of time so two separate calls to it will
     * return slightly different values. This matcher applies an approximation of 10 seconds.
     * <p/>
     * Note: it assumes the time portion of the {@link Uri} is the last segment.
     */
public static Matcher<Uri> approximateTimeUriMatcher(final Uri expectedUri) {
    return new BaseMatcher<Uri>() {

        @Override
        public void describeTo(final Description description) {
            description.appendText("approximateTimeUriMatcher");
        }

        @Override
        public boolean matches(final Object item) {
            if (item instanceof Uri) {
                Uri actualUri = (Uri) item;
                // Check the last segment is a time
                long expectedTime = 0L;
                long actualTime = 0L;
                try {
                    expectedTime = Long.parseLong(expectedUri.getLastPathSegment());
                } catch (NumberFormatException e) {
                    return false;
                }
                try {
                    actualTime = Long.parseLong(actualUri.getLastPathSegment());
                } catch (NumberFormatException e) {
                    return false;
                }
                // If the times are within 10 seconds of each other, check other segments
                if (Math.abs(actualTime - expectedTime) < TimeUtils.SECOND * 10) {
                    List<String> actualSegments = actualUri.getPathSegments();
                    List<String> expectedSegments = expectedUri.getPathSegments();
                    // If same number of segments
                    if (actualSegments.size() == expectedSegments.size()) {
                        boolean diffFound = false;
                        // Check each segment except the last
                        for (int i = 0; i < actualSegments.size() - 1; i++) {
                            if (!actualSegments.get(i).equals(expectedSegments.get(i))) {
                                diffFound = true;
                            }
                        }
                        // They match only if no differences were found
                        return !diffFound;
                    }
                }
            }
            return false;
        }

        @Override
        public void describeMismatch(final Object item, final Description mismatchDescription) {
            mismatchDescription.appendText("item doesn't match uri " + expectedUri + " with an approximataion of 10 seconds for the last time segment");
        }
    };
}
Also used : Description(org.hamcrest.Description) BaseMatcher(org.hamcrest.BaseMatcher) Uri(android.net.Uri)

Example 73 with Description

use of org.hamcrest.Description in project gradle by gradle.

the class LoggingTest method expectLogMessage.

private void expectLogMessage(final LogLevel level, final String text) {
    final Matcher<LogEvent> matcher = new BaseMatcher<LogEvent>() {

        public void describeTo(Description description) {
            description.appendText("level: ").appendValue(level).appendText(", text:").appendValue(text);
        }

        public boolean matches(Object o) {
            LogEvent event = (LogEvent) o;
            return event.getLogLevel() == level && event.getMessage().equals(text);
        }
    };
    context.checking(new Expectations() {

        {
            one(outputEventListener).onOutput(with(matcher));
        }
    });
}
Also used : Expectations(org.jmock.Expectations) Description(org.hamcrest.Description) BaseMatcher(org.hamcrest.BaseMatcher) LogEvent(org.gradle.internal.logging.events.LogEvent)

Example 74 with Description

use of org.hamcrest.Description in project gocd by gocd.

the class GoConfigServiceTest method hasValues.

private Matcher<PipelineSelections> hasValues(final List<String> isVisible, final List<String> isNotVisible, final Date today, final Long userId) {
    return new BaseMatcher<PipelineSelections>() {

        public boolean matches(Object o) {
            PipelineSelections pipelineSelections = (PipelineSelections) o;
            assertHasSelected(pipelineSelections, isVisible);
            assertHasSelected(pipelineSelections, isNotVisible, false);
            assertThat(pipelineSelections.lastUpdated(), is(today));
            assertThat(pipelineSelections.userId(), is(userId));
            return true;
        }

        public void describeTo(Description description) {
        }
    };
}
Also used : PipelineSelections(com.thoughtworks.go.server.domain.user.PipelineSelections) Description(org.hamcrest.Description) BaseMatcher(org.hamcrest.BaseMatcher)

Example 75 with Description

use of org.hamcrest.Description in project neo4j by neo4j.

the class QueryResultsSerializationTest method restContainsNestedDeleted.

/**
     * This matcher is hardcoded to check for a list containing one deleted node and one map with a deleted node mapped to the key `someKey`.
     */
public static Matcher<? super Response> restContainsNestedDeleted() {
    return new TypeSafeMatcher<Response>() {

        @Override
        protected boolean matchesSafely(HTTP.Response response) {
            try {
                JsonNode list = TransactionMatchers.getJsonNodeWithName(response, "rest").iterator().next();
                assertThat(list.get(0).get("metadata").get("deleted").asBoolean(), equalTo(Boolean.TRUE));
                assertThat(list.get(1).get("someKey").get("metadata").get("deleted").asBoolean(), equalTo(Boolean.TRUE));
                return true;
            } catch (JsonParseException e) {
                return false;
            }
        }

        @Override
        public void describeTo(Description description) {
        }
    };
}
Also used : Response(org.neo4j.test.server.HTTP.Response) TypeSafeMatcher(org.hamcrest.TypeSafeMatcher) Description(org.hamcrest.Description) JsonNode(org.codehaus.jackson.JsonNode) JsonParseException(org.neo4j.server.rest.domain.JsonParseException)

Aggregations

Description (org.hamcrest.Description)122 Test (org.junit.Test)38 TypeSafeMatcher (org.hamcrest.TypeSafeMatcher)35 StringDescription (org.hamcrest.StringDescription)29 BaseMatcher (org.hamcrest.BaseMatcher)27 View (android.view.View)22 ViewParent (android.view.ViewParent)11 TextView (android.widget.TextView)11 ViewGroup (android.view.ViewGroup)8 Expectations (org.jmock.Expectations)8 URL (java.net.URL)7 Matcher (org.hamcrest.Matcher)7 Invocation (org.jmock.api.Invocation)7 BoundedMatcher (android.support.test.espresso.matcher.BoundedMatcher)6 ImageView (android.widget.ImageView)6 File (java.io.File)6 IOException (java.io.IOException)6 URI (java.net.URI)6 List (java.util.List)6 JsonNode (org.codehaus.jackson.JsonNode)6