Java Code Examples for org.assertj.core.api.Condition#matches()

The following examples show how to use org.assertj.core.api.Condition#matches() . 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: LithoViewSubComponentDeepExtractor.java    From litho with Apache License 2.0 6 votes vote down vote up
public static Condition<LithoView> deepSubComponentWith(
    final Condition<InspectableComponent> inner) {
  return new Condition<LithoView>() {
    @Override
    public boolean matches(LithoView lithoView) {
      as("deep sub component with <%s>", inner);
      for (InspectableComponent component : subComponentsDeeply().extract(lithoView)) {
        if (inner.matches(component)) {
          return true;
        }
      }

      return false;
    }
  };
}
 
Example 2
Source File: LithoViewSubComponentExtractor.java    From litho with Apache License 2.0 6 votes vote down vote up
public static Condition<? super LithoView> subComponentWith(
    final Condition<InspectableComponent> inner) {
  return new Condition<LithoView>() {
    @Override
    public boolean matches(LithoView value) {
      as("sub component with <%s>", inner);
      for (InspectableComponent component : subComponents().extract(value)) {
        if (inner.matches(component)) {
          return true;
        }
      }

      return false;
    }
  };
}
 
Example 3
Source File: BaseMatcherTest.java    From litho with Apache License 2.0 5 votes vote down vote up
@Test
public void testMatcherFailureMessage() {
  final TestBaseMatcher matcher =
      new TestBaseMatcher().clickHandler(IsNull.<EventHandler<ClickEvent>>notNullValue(null));
  final Condition<InspectableComponent> condition =
      BaseMatcherBuilder.buildCommonMatcher(matcher);

  condition.matches(mInspectableComponent);
  assertThat(condition.description().toString())
      .isEqualTo("Click handler <not null> (doesn't match <null>)");
}
 
Example 4
Source File: Assertions.java    From ArchUnit with Apache License 2.0 5 votes vote down vote up
public AccessesAssertion contain(Condition<? super JavaAccess<?>> condition) {
    for (Iterator<JavaAccess<?>> iterator = actualRemaining.iterator(); iterator.hasNext(); ) {
        if (condition.matches(iterator.next())) {
            iterator.remove();
            return this;
        }
    }
    throw new AssertionError("No access matches " + condition);
}
 
Example 5
Source File: SubComponentDeepExtractor.java    From litho with Apache License 2.0 4 votes vote down vote up
/**
 * Verify that a component tree contains a component that matches the provided condition at any
 * level in its tree.
 *
 * <p><em>Note that asserting on indirect children breaks encapsulation and can lead to Change
 * Detector Tests[0]. Prefer {@link SubComponentExtractor#subComponentWith(ComponentContext,
 * Condition)} over this whenever possible.</em>
 *
 * <p>
 *
 * <h2>Example Use</h2>
 *
 * Suppose you have a <code>MyWrapperComponentSpec</code> that creates a {@link
 * com.facebook.litho.widget.Card} which contains a {@link com.facebook.litho.widget.Text} for a
 * <code>text</code> prop that your component accepts.
 *
 * <p>You want to ensure that the text truncation logic correctly applies to the text that sits
 * inside the card. Using {@link SubComponentExtractor#subComponentWith(ComponentContext,
 * Condition)} won't work here as it will only be able to make assertions on the {@link
 * com.facebook.litho.widget.Card} but not the {@link com.facebook.litho.widget.Text} sitting
 * inside.
 *
 * <p>
 *
 * <pre><code>
 * mComponent = MyWrapperComponent.create(c).text("I'm Mr. Meeseeks! Look at me!").build();
 * assertThat(c, mComponent)
 *   .has(deepSubComponentWith(c, textEquals("I'm Mr. Meeseeks!")))
 *   .doesNotHave(deepSubComponentWith(c, text(containsString("wubba lubba dub dub"))));
 * </code></pre>
 *
 * For more applicable combinators, see below:
 *
 * @see org.hamcrest.CoreMatchers
 * @see ComponentConditions
 * @param c The ComponentContext used to create the tree.
 * @param inner The condition that at least one sub component needs to match.
 *     <p>[0] https://testing.googleblog.com/2015/01/testing-on-toilet-change-detector-tests.html
 */
public static Condition<? super Component> deepSubComponentWith(
    final ComponentContext c, final Condition<InspectableComponent> inner) {
  return new Condition<Component>(new TextDescription("Deep subcomponent with <%s>", inner)) {
    @Override
    public boolean matches(Component value) {
      for (InspectableComponent component : subComponentsDeeply(c).extract(value)) {
        if (inner.matches(component)) {
          return true;
        }
      }

      return false;
    }
  };
}
 
Example 6
Source File: SubComponentExtractor.java    From litho with Apache License 2.0 4 votes vote down vote up
/**
 * This combinator allows you to make an assertion that applies to at least one sub-component
 * directly spun up by the component under test.
 *
 * <p><em>Note that this combinator only works on direct child-components, i.e. sub-components not
 * further than one level deep.</em>
 *
 * <p>If you want to make assertions over deeply nested sub-components, check out {@link
 * SubComponentDeepExtractor#deepSubComponentWith(ComponentContext, Condition)}.
 *
 * <p>
 *
 * <h2>Example Use</h2>
 *
 * Suppose you've got a <code>MyComponentSpec</code> which takes a <code>text</code> prop and
 * creates multiple children; one of them a {@link com.facebook.litho.widget.Text} with a
 * truncated version of your text. You want to verify that there is a direct child present that
 * contains the <code>text</code> that you have passed in:
 *
 * <p>
 *
 * <pre><code>
 * mComponent = MyComponent.create(c).text("Cells interlinked within cells interlinked").build();
 * assertThat(c, mComponent)
 *   .has(subComponentWith(c, textEquals("Cells interlink...")))
 *   .doesNotHave(subComponentWith(c, text(containsString("within cells"))));
 * </code></pre>
 *
 * For more applicable combinators, see below:
 *
 * @see org.hamcrest.CoreMatchers
 * @see ComponentConditions
 * @param c The ComponentContext used to create the tree.
 * @param inner The condition that at least one sub component needs to match.
 */
public static Condition<? super Component> subComponentWith(
    final ComponentContext c, final Condition<InspectableComponent> inner) {
  return new Condition<Component>() {
    @Override
    public boolean matches(Component value) {
      as("sub component with <%s>", inner);
      for (InspectableComponent component : subComponents(c).extract(value)) {
        if (inner.matches(component)) {
          return true;
        }
      }

      return false;
    }
  };
}
 
Example 7
Source File: ComponentConditions.java    From litho with Apache License 2.0 3 votes vote down vote up
/**
 * Matcher that succeeds if a {@link InspectableComponent} has text content that matches the
 * provided condition.
 *
 * <p>N.B. We are implicitly casting the {@link CharSequence} to a {@link String} when matching so
 * that more powerful matchers can be applied like sub-string matching.
 *
 * <p>
 *
 * <h2>Example Use</h2>
 *
 * <pre><code>
 * mComponent = MyComponent.create(c).text("Cells interlinked within cells interlinked").build();
 * assertThat(c, mComponent)
 *   .has(subComponentWith(c, startsWith("Cells")))
 *   .doesNotHave(subComponentWith(c, text(containsString("A Tall White Fountain Played."))));
 * </code></pre>
 */
public static Condition<InspectableComponent> text(final Condition<String> condition) {
  return new Condition<InspectableComponent>() {
    @Override
    public boolean matches(InspectableComponent value) {
      as("text <%s>", condition);
      return condition.matches(value.getTextContent());
    }
  };
}