add examples of error messages using binary, unicode and hexadecimal representation

This commit is contained in:
Joel Costigliola 2014-01-26 22:59:07 +01:00
parent 455baf514f
commit 2bbf69df24
5 changed files with 73 additions and 3 deletions

View File

@ -30,6 +30,7 @@ import static org.assertj.examples.data.Ring.oneRing;
import static org.assertj.examples.data.Ring.vilya;
import java.awt.Rectangle;
import java.io.UnsupportedEncodingException;
import java.util.Arrays;
import java.util.Comparator;
@ -184,7 +185,6 @@ public class ArrayAssertionsExamples extends AbstractAssertionsExamples {
public void filter_then_extract_assertion_example() {
Iterable<TolkienCharacter> badBadGuys = filter(orcsWithHobbitPrisoners).with("race.alignment", EVIL).get();
assertThat(badBadGuys).extracting("name").containsOnly("Guruk");
}
@Test
@ -318,4 +318,18 @@ public class ArrayAssertionsExamples extends AbstractAssertionsExamples {
assertThat(array(vilya, nenya, narya)).hasSameSizeAs(array(nenya, nenya, nenya));
}
@Test
public void use_hexadecimal_representation_in_error_messages() throws UnsupportedEncodingException {
try {
assertThat(new Byte[]{0x10,0x20}).asHexadecimal().contains(new Byte[]{0x30});
} catch (AssertionError e) {
logAssertionErrorMessage("asHexadecimal for byte array", e);
}
try {
assertThat("zólc".getBytes()).asHexadecimal().contains("żółć".getBytes("ISO-8859-2"));
} catch (AssertionError e) {
logAssertionErrorMessage("asHexadecimal for byte array", e);
}
}
}

View File

@ -44,4 +44,13 @@ public class CharAssertionsExamples extends AbstractAssertionsExamples {
assertThat(new Character('a')).usingComparator(caseInsensitiveComparator).isEqualTo(new Character('A'));
}
@Test
public void use_unicode_representation_in_error_messages() {
try {
assertThat('µ').asUnicode().isEqualTo('μ');
} catch (AssertionError e) {
logAssertionErrorMessage("asUnicode() for Char", e);
}
}
}

View File

@ -340,4 +340,24 @@ public class IterableAssertionsExamples extends AbstractAssertionsExamples {
}
}
@Test
public void use_hexadecimal_representation_in_error_messages() {
final List<Byte> bytes = newArrayList((byte)0x10, (byte) 0x20);
try {
assertThat(bytes).asHexadecimal().contains((byte)0x30);
} catch (AssertionError e) {
logAssertionErrorMessage("asHexadecimal for byte list", e);
}
}
@Test
public void use_binary_representation_in_error_messages() {
final List<Byte> bytes = newArrayList((byte)0x10, (byte) 0x20);
try {
assertThat(bytes).asBinary().contains((byte)0x30);
} catch (AssertionError e) {
logAssertionErrorMessage("asBinary for byte list", e);
}
}
}

View File

@ -100,7 +100,7 @@ public class NumberAssertionsExamples extends AbstractAssertionsExamples {
// With BigDecimal, 8.0 is not equals to 8.00 but it is if you use compareTo()
assertThat(new BigDecimal("8.0")).isEqualByComparingTo(new BigDecimal("8.00"));
// The following won't work because it relies on equals methos
// The following won't work because it relies on equals methods
// assertThat(new BigDecimal("8.0")).isGreaterThanOrEqualTo(new BigDecimal("8.00"));
// To have a consistent comparison ignoring BigDecimal scale, switch of comparison strategy :
Comparator<BigDecimal> bigDecimalComparator = new Comparator<BigDecimal>() {
@ -124,6 +124,16 @@ public class NumberAssertionsExamples extends AbstractAssertionsExamples {
}
}
@Test
public void number_assertions_with_binary_representation_examples() {
assertThat(1).asBinary().isEqualTo(1);
try {
assertThat(1).asBinary().isEqualTo(2);
} catch (AssertionError e) {
logAssertionErrorMessage("isEqualTo with binary representation_", e);
}
}
}

View File

@ -28,7 +28,6 @@ public class StringAssertionsExamples extends AbstractAssertionsExamples {
@Test
public void string_assertions_examples() {
assertThat("Frodo").startsWith("Fro").endsWith("do").hasSize(5);
assertThat("Frodo").contains("rod").doesNotContain("fro");
assertThat("Frodo").containsOnlyOnce("do");
@ -168,5 +167,23 @@ public class StringAssertionsExamples extends AbstractAssertionsExamples {
}
@Test
public void use_hexadecimal_representation_in_error_messages() {
try {
assertThat("µµµ").asHexadecimal().contains("μμμ");
} catch (AssertionError e) {
logAssertionErrorMessage("asHexadecimal() for String", e);
}
}
@Test
public void use_unicode_representation_in_error_messages() {
try {
assertThat("µµµ").asUnicode().contains("μμμ");
} catch (AssertionError e) {
logAssertionErrorMessage("asUnicode() for String", e);
}
}
}