Java Code Examples for org.hamcrest.Matcher#describeTo()

The following examples show how to use org.hamcrest.Matcher#describeTo() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: PreferenceMatchers.java    From android-test with Apache License 2.0 6 votes vote down vote up
public static Matcher<Preference> withTitleText(final Matcher<String> titleMatcher) {
  return new TypeSafeMatcher<Preference>() {
    @Override
    public void describeTo(Description description) {
      description.appendText(" a preference with title matching: ");
      titleMatcher.describeTo(description);
    }

    @Override
    public boolean matchesSafely(Preference pref) {
      if (pref.getTitle() == null) {
        return false;
      }
      String title = pref.getTitle().toString();
      return titleMatcher.matches(title);
    }
  };
}
 
Example 2
Source File: AdapterViewTest.java    From android-test with Apache License 2.0 6 votes vote down vote up
private static Matcher<View> withAdaptedData(final Matcher<Object> dataMatcher) {
  return new TypeSafeMatcher<View>() {

    @Override
    public void describeTo(Description description) {
      description.appendText("with class name: ");
      dataMatcher.describeTo(description);
    }

    @Override
    public boolean matchesSafely(View view) {
      if (!(view instanceof AdapterView)) {
        return false;
      }
      @SuppressWarnings("rawtypes")
      Adapter adapter = ((AdapterView) view).getAdapter();
      for (int i = 0; i < adapter.getCount(); i++) {
        if (dataMatcher.matches(adapter.getItem(i))) {
          return true;
        }
      }
      return false;
    }
  };
}
 
Example 3
Source File: ListRecordingsTest.java    From go-bees with GNU General Public License v3.0 6 votes vote down vote up
private static Matcher<View> childAtPosition(
        final Matcher<View> parentMatcher, final int position) {

    return new TypeSafeMatcher<View>() {
        @Override
        public void describeTo(Description description) {
            description.appendText("Child at position " + position + " in parent ");
            parentMatcher.describeTo(description);
        }

        @Override
        public boolean matchesSafely(View view) {
            ViewParent parent = view.getParent();
            return parent instanceof ViewGroup && parentMatcher.matches(parent)
                    && view.equals(((ViewGroup) parent).getChildAt(position));
        }
    };
}
 
Example 4
Source File: ViewRecordingTest.java    From go-bees with GNU General Public License v3.0 6 votes vote down vote up
private static Matcher<View> childAtPosition(
        final Matcher<View> parentMatcher, final int position) {

    return new TypeSafeMatcher<View>() {
        @Override
        public void describeTo(Description description) {
            description.appendText("Child at position " + position + " in parent ");
            parentMatcher.describeTo(description);
        }

        @Override
        public boolean matchesSafely(View view) {
            ViewParent parent = view.getParent();
            return parent instanceof ViewGroup && parentMatcher.matches(parent)
                    && view.equals(((ViewGroup) parent).getChildAt(position));
        }
    };
}
 
Example 5
Source File: MainActivityTest.java    From Android-Developer-Fundamentals-Version-2 with GNU General Public License v3.0 6 votes vote down vote up
private static Matcher<View> childAtPosition(
        final Matcher<View> parentMatcher, final int position) {

    return new TypeSafeMatcher<View>() {
        @Override
        public void describeTo(Description description) {
            description.appendText("Child at position " + position + " in parent ");
            parentMatcher.describeTo(description);
        }

        @Override
        public boolean matchesSafely(View view) {
            ViewParent parent = view.getParent();
            return parent instanceof ViewGroup && parentMatcher.matches(parent)
                    && view.equals(((ViewGroup) parent).getChildAt(position));
        }
    };
}
 
Example 6
Source File: MainActivityTest.java    From otp-authenticator with MIT License 6 votes vote down vote up
public static Matcher<View> withResourceName(final Matcher<String> resourceNameMatcher) {
    return new TypeSafeMatcher<View>() {
        @Override
        public void describeTo(Description description) {
            description.appendText("with resource name: ");
            resourceNameMatcher.describeTo(description);
        }

        @Override
        public boolean matchesSafely(View view) {
            int id = view.getId();
            return id != View.NO_ID && id != 0 && view.getResources() != null
                    && resourceNameMatcher.matches(view.getResources().getResourceName(id));
        }
    };
}
 
Example 7
Source File: DeleteApiaryTest.java    From go-bees with GNU General Public License v3.0 6 votes vote down vote up
private static Matcher<View> childAtPosition(
        final Matcher<View> parentMatcher, final int position) {

    return new TypeSafeMatcher<View>() {
        @Override
        public void describeTo(Description description) {
            description.appendText("Child at position " + position + " in parent ");
            parentMatcher.describeTo(description);
        }

        @Override
        public boolean matchesSafely(View view) {
            ViewParent parent = view.getParent();
            return parent instanceof ViewGroup && parentMatcher.matches(parent)
                    && view.equals(((ViewGroup) parent).getChildAt(position));
        }
    };
}
 
Example 8
Source File: HasFeatureMatcherTest.java    From hamcrest-compose with Apache License 2.0 5 votes vote down vote up
@Test
public void describeToWhenNoNameUsesFunction()
{
	Matcher<String> matcher = hasFeature(stringToLength("x"), anything("y"));
	StringDescription description = new StringDescription();
	
	matcher.describeTo(description);
	
	assertThat(description.toString(), is("x y"));
}
 
Example 9
Source File: TestUtils.java    From android-schema-utils with Apache License 2.0 5 votes vote down vote up
static Matcher<Throwable> is(Class<? extends Throwable> exceptionClass) {
  final Matcher<Object> objectMatcher = instanceOf(exceptionClass);
  return new BaseMatcher<Throwable>() {
    @Override
    public boolean matches(Object item) {
      return objectMatcher.matches(item);
    }

    @Override
    public void describeTo(Description description) {
      objectMatcher.describeTo(description);
    }
  };
}
 
Example 10
Source File: TokenMatchers.java    From TokenAutoComplete with Apache License 2.0 5 votes vote down vote up
static Matcher<View> tokenCount(final Matcher<Integer> intMatcher) {
    checkNotNull(intMatcher);
    return new BoundedMatcher<View, ContactsCompletionView>(ContactsCompletionView.class) {
        @Override
        public void describeTo(Description description) {
            description.appendText("token count: ");
            intMatcher.describeTo(description);
        }
        @Override
        public boolean matchesSafely(ContactsCompletionView view) {
            return intMatcher.matches(view.getObjects().size());
        }
    };
}
 
Example 11
Source File: EspressoTestsMatchers.java    From droidcon-android-espresso with Apache License 2.0 5 votes vote down vote up
public static Matcher<View> withChildCount(final Matcher<Integer> numberMatcher) {
    return new BoundedMatcher<View, ViewGroup>(ViewGroup.class) {
        @Override
        protected boolean matchesSafely(ViewGroup viewGroup) {
            return numberMatcher.matches(viewGroup.getChildCount());
        }

        @Override
        public void describeTo(Description description) {
            description.appendText("number of child views ");
            numberMatcher.describeTo(description);
        }
    };
}
 
Example 12
Source File: CommonMatcher.java    From Lightweight-Stream-API with Apache License 2.0 5 votes vote down vote up
@Override
protected boolean matchesSafely(Matcher m, Description mismatchDescription) {
    StringDescription stringDescription = new StringDescription();
    m.describeTo(stringDescription);
    final String description = stringDescription.toString();

    if (!matcher.matches(description)) {
        mismatchDescription.appendText("description ");
        matcher.describeMismatch(description, mismatchDescription);
        return false;
    }
    return true;
}
 
Example 13
Source File: IsJsonTextTest.java    From java-hamcrest with Apache License 2.0 5 votes vote down vote up
@Test
public void testDescription() throws Exception {
  final Matcher<JsonNode> sut = jsonText(is(anything()));

  final StringDescription description = new StringDescription();
  sut.describeTo(description);

  assertThat(description.toString(), is(
      "a text node with value that is ANYTHING"
  ));
}
 
Example 14
Source File: IsJsonArrayTest.java    From java-hamcrest with Apache License 2.0 5 votes vote down vote up
@Test
public void testDescriptionForEmptyConstructor() throws Exception {
  final Matcher<JsonNode> sut = jsonArray();

  final StringDescription description = new StringDescription();
  sut.describeTo(description);

  assertThat(description.toString(), is(
      "an array node whose elements is ANYTHING"
  ));
}
 
Example 15
Source File: IsJsonNumberTest.java    From java-hamcrest with Apache License 2.0 5 votes vote down vote up
@Test
public void testDescriptionForEmptyConstructor() throws Exception {
  final Matcher<JsonNode> sut = jsonNumber();

  final StringDescription description = new StringDescription();
  sut.describeTo(description);

  assertThat(description.toString(), is(
      "a number node with value that is ANYTHING"
  ));
}
 
Example 16
Source File: Matchers.java    From Pushjet-Android with BSD 2-Clause "Simplified" License 5 votes vote down vote up
@Factory
public static Matcher<String> normalizedLineSeparators(final Matcher<String> matcher) {
    return new BaseMatcher<String>() {
        public boolean matches(Object o) {
            String string = (String) o;
            return matcher.matches(string.replace(SystemProperties.getLineSeparator(), "\n"));
        }

        public void describeTo(Description description) {
            matcher.describeTo(description);
            description.appendText(" (normalize line separators)");
        }
    };
}
 
Example 17
Source File: IsJsonBooleanTest.java    From java-hamcrest with Apache License 2.0 5 votes vote down vote up
@Test
public void testDescriptionForEmptyConstructor() throws Exception {
  final Matcher<JsonNode> sut = jsonBoolean();

  final StringDescription description = new StringDescription();
  sut.describeTo(description);

  assertThat(description.toString(), is(
      "a boolean node with value that is ANYTHING"
  ));
}
 
Example 18
Source File: AsyncChannelAsserts.java    From c5-replicator with Apache License 2.0 5 votes vote down vote up
private static <T> void helper(ChannelListener<T> listener,
                               Matcher<? super T> matcher,
                               boolean assertFail) throws Throwable {

  List<T> received = new ArrayList<>();
  while (true) {
    T msg = listener.messages.poll(5, TimeUnit.SECONDS);

    if (msg == null) {
      Description d = new StringDescription();
      matcher.describeTo(d);

      if (!received.isEmpty()) {
        d.appendText("we received messages:");
      }

      for (T m : received) {
        matcher.describeMismatch(m, d);
      }

      if (assertFail) {
        listener.throwables.add(new AssertionError("Failing waiting for " + d.toString()));
        MultipleFailureException.assertEmpty(listener.throwables);
      }

      return;
    }

    if (matcher.matches(msg)) {
      if (!listener.throwables.isEmpty()) {
        MultipleFailureException.assertEmpty(listener.throwables);
      }

      return;
    }

    received.add(msg);
  }
}
 
Example 19
Source File: AsyncChannelAsserts.java    From c5-replicator with Apache License 2.0 5 votes vote down vote up
public T waitFor(Matcher<? super T> matcher) {
  ListenableFuture<T> finished = future(matcher);

  try {
    return finished.get(WAIT_TIMEOUT, TimeUnit.SECONDS);
  } catch (Exception e) {
    Description d = new StringDescription();
    matcher.describeTo(d);
    throw new AssertionError("Failed waiting for " + d.toString(), e);
  }
}
 
Example 20
Source File: ARecordTest.java    From octarine with Apache License 2.0 5 votes vote down vote up
@Test public void
lists_expected_properties_in_self_description() {
    Matcher<Record> matcher = ARecord.validAgainst(Person.schema)
            .with(Person.name, "Hubert Cumberdale")
            .with(Person.age, greaterThan(23))
            .with(Person.address.join(Address.postcode), "HR9 5BH");

    StringDescription description = new StringDescription();
    matcher.describeTo(description);

    assertThat(description.toString(), containsString("name: present and \"Hubert Cumberdale\""));
    assertThat(description.toString(), containsString("age: present and a value greater than <23>"));
    assertThat(description.toString(), containsString("address.postcode: present and \"HR9 5BH\""));
}