java类org.hamcrest.StringDescription的实例源码

IsJsonObjectTest.java 文件源码 项目:java-hamcrest 阅读 18 收藏 0 点赞 0 评论 0
@Test
public void testMismatchNested() throws Exception {
  final Matcher<JsonNode> sut = is(
      jsonObject()
          .where("foo", is(jsonInt(1)))
          .where("bar", is(jsonBoolean(true)))
          .where("baz", is(
              jsonObject()
                  .where("foo", is(jsonNull())))));

  final StringDescription description = new StringDescription();
  sut.describeMismatch(NF.objectNode().put("foo", 1).put("bar", true)
                           .set("baz", NF.objectNode().set("foo", NF.booleanNode(false))),
                       description);

  assertThat(description.toString(), is(
      "{\n"
          + "  ...\n"
          + "  \"baz\": {\n"
          + "    \"foo\": was not a null node, but a boolean node\n"
          + "  }\n"
          + "}"
  ));
}
IsJsonObjectTest.java 文件源码 项目:java-hamcrest 阅读 21 收藏 0 点赞 0 评论 0
@Test
public void multipleConsecutiveMismatchesHaveNoEllipsis() throws Exception {
  final Matcher<JsonNode> sut = jsonObject()
      .where("foo", is(jsonInt(1)))
      .where("bar", is(jsonInt(2)))
      .where("baz", is(jsonInt(3)));

  final ObjectNode nestedMismatches = NF.objectNode()
      .put("foo", -1)
      .put("bar", "was string")
      .put("baz", 3);

  final StringDescription description = new StringDescription();
  sut.describeMismatch(nestedMismatches, description);

  assertThat(description.toString(), is(
      "{\n"
          + "  \"foo\": was a number node with value that was <-1>\n"
          + "  \"bar\": was not a number node, but a string node\n"
          + "  ...\n"
          + "}"
  ));
}
IsJsonObjectTest.java 文件源码 项目:java-hamcrest 阅读 18 收藏 0 点赞 0 评论 0
@Test
public void testMultipleMismatchesWithNestingReportsAllMismatches() throws Exception {
  final Matcher<JsonNode> sut = jsonObject()
      .where("foo", is(jsonObject()
                           .where("val", jsonBoolean(true))))
      .where("bar", is(jsonInt(2)))
      .where("baz", is(jsonInt(3)));

  final ObjectNode nestedMismatches = NF.objectNode()
      .put("bar", "was string")
      .put("baz", 3);
  nestedMismatches.set("foo", NF.objectNode().put("val", false));

  final StringDescription description = new StringDescription();
  sut.describeMismatch(nestedMismatches, description);

  assertThat(description.toString(), is(
      "{\n"
          + "  \"foo\": {\n"
          + "    \"val\": was a boolean node with value that was <false>\n"
          + "  }\n"
          + "  \"bar\": was not a number node, but a string node\n"
          + "  ...\n"
          + "}"
  ));
}
ObjectChecker.java 文件源码 项目:GitHub 阅读 19 收藏 0 点赞 0 评论 0
static void fail(@Nullable Object actualValue, Matcher<?> matcher) {
  Description description =
      new StringDescription()
          .appendText("\nExpected: ")
          .appendDescriptionOf(matcher)
          .appendText("\n     but: ");
  matcher.describeMismatch(actualValue, description);
  AssertionError assertionError = new AssertionError(description.toString());
  assertionError.setStackTrace(ObjectChecker.trimStackTrace(assertionError.getStackTrace()));
  throw assertionError;
}
UpdateAssert.java 文件源码 项目:cucumber-framework-java 阅读 17 收藏 0 点赞 0 评论 0
@Step("Matching Actual - {1} and Expected {2}")
public static <T> void assertThat(String reason, T actual, Matcher<? super T> matcher) {
    if (!matcher.matches(actual)) {
        Description description = new StringDescription();
        description.appendText(reason)
                .appendText("\nExpected: ")
                .appendDescriptionOf(matcher)
                .appendText("\n     but: ");
        matcher.describeMismatch(actual, description);

        ScreenShotUtil.takeScreenShot();
        throw new AssertionError(description.toString());
    }
}
ExceptionCauseMessageMatcherShould.java 文件源码 项目:logcapture 阅读 16 收藏 0 点赞 0 评论 0
@Test
public void description_adds_context() {
  Matcher<Exception> matcher = whereCauseMessage(equalTo("message"));
  StringDescription description = new StringDescription();

  matcher.describeTo(description);

  assertThat(description.toString()).isEqualTo("Expecting exception cause to contain \"message\"");
}
ExceptionCauseMatcherShould.java 文件源码 项目:logcapture 阅读 19 收藏 0 点赞 0 评论 0
@Test
public void description_adds_context() {
  Matcher<Exception> matcher = causeOf(RuntimeException.class);
  StringDescription description = new StringDescription();

  matcher.describeTo(description);

  assertThat(description.toString()).isEqualTo("Expecting exception to be instance of class java.lang.RuntimeException");
}
FormConditionsTest.java 文件源码 项目:com-liferay-apio-architect 阅读 21 收藏 0 点赞 0 评论 0
@Test
public void testInvokingDescribesToUpdatesDescription() {
    Builder<Map<String, Object>> builder = new Builder<>();

    FormConditions formConditions = builder.whereBoolean(
        "boolean", isReturnedIn(map -> map.get("boolean"))
    ).whereString(
        "string", isReturnedIn(map -> map.get("string"))
    ).build();

    Description description = new StringDescription();

    formConditions.describeTo(description);

    StringBuilder stringBuilder = new StringBuilder();

    String expected = stringBuilder.append(
        "a Form...\n\t...that should have a non empty title\n\t...that "
    ).append(
        "should have a non empty description\n\t...that should return "
    ).append(
        "something that is <true>\n\t...that should return something that "
    ).append(
        "is \"String\"\n"
    ).toString();

    assertThat(description.toString(), is(expected));
}
FormConditionsTest.java 文件源码 项目:com-liferay-apio-architect 阅读 19 收藏 0 点赞 0 评论 0
@Test
public void testInvokingMatchesUpdatedDescription() {
    Builder<Map<String, Object>> builder = new Builder<>();

    FormConditions formConditions = builder.whereBoolean(
        "boolean", isReturnedIn(__ -> null)
    ).whereString(
        "string", isReturnedIn(__ -> null)
    ).build();

    Description description = new StringDescription();

    Form.Builder<Map<String, Object>> formBuilder = Form.Builder.empty();

    Form<Map<String, Object>> form = formBuilder.title(
        __ -> "title"
    ).description(
        __ -> "description"
    ).constructor(
        HashMap::new
    ).build();

    boolean matchesElement = formConditions.matches(form);
    formConditions.describeMismatch(form, description);

    String expected =
        "was a Form...\n\t...that should have returned something that is " +
            "<true> instead of null\n\t...that should have returned " +
                "something that is \"String\" instead of null\n";

    assertThat(matchesElement, is(false));
    assertThat(description.toString(), is(expected));
}
ConditionsTest.java 文件源码 项目:com-liferay-apio-architect 阅读 21 收藏 0 点赞 0 评论 0
@Test
public void testInvokingMatchesElementInStrictModeUpdatedDescription() {
    Conditions.Builder builder = new Conditions.Builder();

    Conditions conditions = builder.where(
        "geek", is(aJsonBoolean(true))
    ).where(
        "apio", is(aJsonString(equalTo("Live long and prosper")))
    ).build();

    Description description = new StringDescription();

    JsonObject jsonObject = new JsonObject();

    jsonObject.addProperty("geek", true);
    jsonObject.addProperty("number", 42);
    jsonObject.addProperty("other", "apio");
    jsonObject.addProperty("apio", "Live long and prosper");

    boolean matchesElement = conditions.matches(jsonObject);
    conditions.describeMismatch(jsonObject, description);

    String expected =
        "was a JSON object with more fields than validated. Extra keys: " +
            "number, other";

    assertThat(matchesElement, is(false));
    assertThat(description.toString(), is(expected));
}


问题


面经


文章

微信
公众号

扫码关注公众号