本文整理汇总了Java中org.hamcrest.StringDescription类的典型用法代码示例。如果您正苦于以下问题:Java StringDescription类的具体用法?Java StringDescription怎么用?Java StringDescription使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
StringDescription类属于org.hamcrest包,在下文中一共展示了StringDescription类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: assertThatClassIsImmutable
import org.hamcrest.StringDescription; //导入依赖的package包/类
/**
* Assert that the given class adheres to the immutable class rules.
*
* @param clazz the class to check
*
* @throws java.lang.AssertionError if the class is not an
* immutable class
*/
public static void assertThatClassIsImmutable(Class<?> clazz) {
final ImmutableClassChecker checker = new ImmutableClassChecker();
if (!checker.isImmutableClass(clazz, false)) {
final Description toDescription = new StringDescription();
final Description mismatchDescription = new StringDescription();
checker.describeTo(toDescription);
checker.describeMismatch(mismatchDescription);
final String reason =
"\n" +
"Expected: is \"" + toDescription.toString() + "\"\n" +
" but : was \"" + mismatchDescription.toString() + "\"";
throw new AssertionError(reason);
}
}
示例2: assertThatClassIsImmutableBaseClass
import org.hamcrest.StringDescription; //导入依赖的package包/类
/**
* Assert that the given class adheres to the immutable class rules, but
* is not declared final. Classes that need to be inherited from cannot be
* declared final.
*
* @param clazz the class to check
*
* @throws java.lang.AssertionError if the class is not an
* immutable class
*/
public static void assertThatClassIsImmutableBaseClass(Class<?> clazz) {
final ImmutableClassChecker checker = new ImmutableClassChecker();
if (!checker.isImmutableClass(clazz, true)) {
final Description toDescription = new StringDescription();
final Description mismatchDescription = new StringDescription();
checker.describeTo(toDescription);
checker.describeMismatch(mismatchDescription);
final String reason =
"\n" +
"Expected: is \"" + toDescription.toString() + "\"\n" +
" but : was \"" + mismatchDescription.toString() + "\"";
throw new AssertionError(reason);
}
}
示例3: assertThatClassIsUtility
import org.hamcrest.StringDescription; //导入依赖的package包/类
/**
* Assert that the given class adheres to the utility class rules.
*
* @param clazz the class to check
*
* @throws java.lang.AssertionError if the class is not a valid
* utility class
*/
public static void assertThatClassIsUtility(Class<?> clazz) {
final UtilityClassChecker checker = new UtilityClassChecker();
if (!checker.isProperlyDefinedUtilityClass(clazz)) {
final Description toDescription = new StringDescription();
final Description mismatchDescription = new StringDescription();
checker.describeTo(toDescription);
checker.describeMismatch(mismatchDescription);
final String reason =
"\n" +
"Expected: is \"" + toDescription.toString() + "\"\n" +
" but : was \"" + mismatchDescription.toString() + "\"";
throw new AssertionError(reason);
}
}
示例4: describeTo
import org.hamcrest.StringDescription; //导入依赖的package包/类
@Override
public void describeTo(Description description) {
description.appendText("{\n");
_entryMatchers.forEach(
(key, matcher) -> {
description.appendText(" ");
description.appendText(key);
description.appendText(": ");
Description innerDescription = new StringDescription();
matcher.describeTo(innerDescription);
indentDescription(description, innerDescription);
});
description.appendText("}");
}
示例5: testInvokingDescribesToUpdatesDescription
import org.hamcrest.StringDescription; //导入依赖的package包/类
@Test
public void testInvokingDescribesToUpdatesDescription() {
Conditions.Builder builder = new Conditions.Builder();
Conditions conditions = builder.where(
"apio", is(aJsonString(equalTo("Live long and prosper")))
).where(
"geek", is(aJsonBoolean(true))
).build();
Description description = new StringDescription();
conditions.describeTo(description);
String expected =
"a JSON object where {\n apio: is a string element with a value " +
"that is \"Live long and prosper\"\n geek: is a boolean " +
"element with a value that is <true>\n}";
assertThat(description.toString(), is(expected));
}
示例6: testInvokingMatchesElementInSoftModeValidates
import org.hamcrest.StringDescription; //导入依赖的package包/类
@Test
public void testInvokingMatchesElementInSoftModeValidates() {
Conditions.Builder builder = new Conditions.Builder();
Conditions conditions = builder.where(
"geek", is(aJsonBoolean(true))
).where(
"apio", is(aJsonString(equalTo("Live long and prosper")))
).withStrictModeDeactivated(
).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);
assertThat(matchesElement, is(true));
}
示例7: testInvalidJsonObjectWithMultiConditionUpdatesDescription
import org.hamcrest.StringDescription; //导入依赖的package包/类
@Test
public void testInvalidJsonObjectWithMultiConditionUpdatesDescription() {
Conditions conditions = _builder.where(
"apio", is(aJsonString(equalTo("Live long and prosper")))
).where(
"geek", is(aJsonBoolean(true))
).build();
Matcher<String> stringMatcher = aJsonObjectStringWith(conditions);
Description description = new StringDescription();
stringMatcher.describeMismatch("{}", description);
String expected =
"was a JSON object {\n apio: was null\n geek: was null\n}";
assertThat(description.toString(), is(expected));
}
示例8: verifyThat
import org.hamcrest.StringDescription; //导入依赖的package包/类
public static <T> boolean verifyThat(String reason, T actual, Matcher<? super T> matcher) {
boolean result = matcher.matches(actual);
Description description = new StringDescription();
description.appendText(reason).appendText("\nExpected: ").appendDescriptionOf(matcher)
.appendText("\n Actual: ");
matcher.describeMismatch(actual, description);
String msg = description.toString();
if (msg.endsWith("Actual: ")) {
msg = String.format(msg + "%s", actual);
}
msg = msg.replaceAll("<", "<").replaceAll(">", ">");
Reporter.log(msg, result ? MessageTypes.Pass : MessageTypes.Fail);
return result;
}
示例9: assertTokenStream
import org.hamcrest.StringDescription; //导入依赖的package包/类
/**
* Matches that the given <code>TokenStream</code> produces the expected sequence of tokens. Fails if a token does
* not match its respective matcher, or if the number of tokens in the token stream does not match the number of
* given matchers.
*
* @param stream
* the token stream.
* @param expectedTokens
* the matchers for the expected tokens.
*/
@SafeVarargs
protected final void assertTokenStream(TokenStream stream, Matcher<TokenStream>... expectedTokens) throws Exception {
final int expectedTokenCount = expectedTokens.length;
int tokenCount = 0;
while (stream.incrementToken()) {
assertTrue("Too many tokens", tokenCount < expectedTokens.length);
Matcher<TokenStream> tokenMatcher = expectedTokens[tokenCount];
boolean matches = tokenMatcher.matches(stream);
if (!matches) {
Description description = new StringDescription();
description.appendText("Unexpected token at position ").appendValue(tokenCount).appendText("\n");
tokenMatcher.describeMismatch(stream, description);
fail(description.toString());
}
tokenCount++;
}
assertEquals("Unexpected number of tokens", expectedTokenCount, tokenCount);
}
示例10: describeNestedMismatchesNoEllipsisBeforeFirstValue
import org.hamcrest.StringDescription; //导入依赖的package包/类
@Test
public void describeNestedMismatchesNoEllipsisBeforeFirstValue() throws Exception {
Set<String> allKeys = new LinkedHashSet<>(asList("first", "second", "third"));
StringDescription description = new StringDescription();
Map<String, Consumer<Description>> mismatchedKeys =
ImmutableMap.of("first", desc -> desc.appendText("mismatch!"));
BiConsumer<String, Description> describeKey = (str, desc) -> desc.appendText(str);
DescriptionUtils.describeNestedMismatches(allKeys, description, mismatchedKeys, describeKey);
assertThat(description.toString(), is(
"{\n"
+ " first: mismatch!\n"
+ " ...\n"
+ "}"
));
}
示例11: testOffset
import org.hamcrest.StringDescription; //导入依赖的package包/类
@Test
public void testOffset() throws Exception {
Description description = mock(Description.class);
when(description.appendText(anyString())).thenReturn(description);
when(description.appendValue(anyInt())).thenReturn(description);
Matcher<TokenStream> offsetsMatcher = test.offsets(0, 1);
offsetsMatcher.describeTo(description);
verify(description, times(1)).appendText("startOffset=");
verify(description, times(1)).appendValue(0);
verify(description, times(1)).appendText(",endOffset=");
verify(description, times(1)).appendValue(1);
// Not a real test, but good for coverage
assertFalse(test.offsets(42, 4711).matches(stream));
test.offsets(42, 4711).describeMismatch(stream, new StringDescription());
}
示例12: testDescriptionFormatting
import org.hamcrest.StringDescription; //导入依赖的package包/类
@Test
public void testDescriptionFormatting() throws Exception {
final IsPojo<SomeClass> sut = pojo(SomeClass.class)
.where("baz", is(pojo(SomeClass.class).where("foo", is(42))))
.where("foo", is(42))
.withProperty("bar", is("bar"));
final StringDescription description = new StringDescription();
sut.describeTo(description);
assertThat(description.toString(), is(
"SomeClass {\n"
+ " baz(): is SomeClass {\n"
+ " foo(): is <42>\n"
+ " }\n"
+ " foo(): is <42>\n"
+ " getBar(): is \"bar\"\n"
+ "}"
));
}
示例13: testMismatchFormatting
import org.hamcrest.StringDescription; //导入依赖的package包/类
@Test
public void testMismatchFormatting() throws Exception {
final IsPojo<SomeClass> sut = pojo(SomeClass.class)
.where("baz", is(
pojo(SomeClass.class)
.where("foo", is(43))
))
.where("foo", is(42))
.withProperty("bar", is("bar"));
final StringDescription description = new StringDescription();
sut.describeMismatch(new SomeClass(), description);
assertThat(description.toString(), is(
"SomeClass {\n"
+ " baz(): SomeClass {\n"
+ " foo(): was <42>\n"
+ " }\n"
+ " ...\n"
+ "}"
));
}
示例14: describeNestMismatchesNoEllipsisBetweenConsecutiveMismatches
import org.hamcrest.StringDescription; //导入依赖的package包/类
@Test
public void describeNestMismatchesNoEllipsisBetweenConsecutiveMismatches() throws Exception {
Set<String> allKeys = new LinkedHashSet<>(asList("first", "second", "third", "forth"));
StringDescription description = new StringDescription();
Map<String, Consumer<Description>> mismatchedKeys = ImmutableMap.of(
"second", desc -> desc.appendText("mismatch!"),
"third", desc -> desc.appendText("mismatch!"));
BiConsumer<String, Description> describeKey = (str, desc) -> desc.appendText(str);
DescriptionUtils.describeNestedMismatches(allKeys, description, mismatchedKeys, describeKey);
assertThat(description.toString(), is(
"{\n"
+ " ...\n"
+ " second: mismatch!\n"
+ " third: mismatch!\n"
+ " ...\n"
+ "}"
));
}
示例15: testTypeSafeMismatch
import org.hamcrest.StringDescription; //导入依赖的package包/类
@Test
public void testTypeSafeMismatch() throws Exception {
final IsPojo<SomeClass> sut = pojo(SomeClass.class)
.where(SomeClass::foo, is(41))
.where(SomeClass::baz, is(
pojo(SomeClass.class)
.where(SomeClass::foo, is(43))
));
final StringDescription description = new StringDescription();
sut.describeMismatch(new SomeClass(), description);
assertThat(description.toString(), is(
"SomeClass {\n"
+ " foo(): was <42>\n"
+ " baz(): SomeClass {\n"
+ " foo(): was <42>\n"
+ " }\n"
+ "}"
));
}