Skip to content

Commit

Permalink
Add inBinary to CharSequenceAssert
Browse files Browse the repository at this point in the history
Fix #3600
  • Loading branch information
sunaleed authored and joel-costigliola committed Sep 25, 2024
1 parent 52a6bb4 commit a4bc7ec
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 43 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1533,6 1533,12 @@ public SELF inHexadecimal() {
return super.inHexadecimal();
}

@Override
@CheckReturnValue
public SELF inBinary() {
return super.inBinary();
}

/**
* Use unicode character representation instead of standard representation in error messages.
* <p>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 13,9 @@
package org.assertj.tests.core.api;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import static org.assertj.core.api.BDDAssertions.then;
import static org.assertj.tests.core.testkit.ErrorMessagesForTest.shouldBeEqualMessage;
import static org.assertj.tests.core.util.AssertionsUtil.expectAssertionError;

import org.junit.jupiter.api.Test;

Expand All @@ -27,89 28,107 @@ class Assertions_assertThat_inBinary_Test {

@Test
void should_assert_byte_in_binary() {
assertThatExceptionOfType(AssertionError.class).isThrownBy(() -> assertThat((byte) 2).inBinary()
.isEqualTo((byte) 3))
.withMessage(shouldBeEqualMessage("0b00000010", "0b00000011"));
// WHEN
AssertionError assertionError = expectAssertionError(() -> assertThat((byte) 2).inBinary().isEqualTo((byte) 3));
// THEN
then(assertionError).hasMessage(shouldBeEqualMessage("0b00000010", "0b00000011"));
}

@Test
void should_assert_signed_byte_in_binary() {
assertThatExceptionOfType(AssertionError.class).isThrownBy(() -> assertThat((byte) -2).inBinary()
.isEqualTo((byte) 3))
.withMessage(shouldBeEqualMessage("0b11111110", "0b00000011"));
// WHEN
AssertionError assertionError = expectAssertionError(() -> assertThat((byte) -2).inBinary().isEqualTo((byte) 3));
// THEN
then(assertionError).hasMessage(shouldBeEqualMessage("0b11111110", "0b00000011"));
}

@Test
void should_assert_bytes_in_binary() {
assertThatExceptionOfType(AssertionError.class).isThrownBy(() -> assertThat(new byte[] { 2, 3 }).inBinary()
.isEqualTo(new byte[] { 1 }))
.withMessage(shouldBeEqualMessage("[0b00000010, 0b00000011]", "[0b00000001]"));
// WHEN
AssertionError error = expectAssertionError(() -> assertThat(new byte[] { 2, 3 }).inBinary().isEqualTo(new byte[] { 1 }));
// THEN
then(error).hasMessage(shouldBeEqualMessage("[0b00000010, 0b00000011]", "[0b00000001]"));
}

@Test
void should_assert_short_in_binary() {
assertThatExceptionOfType(AssertionError.class).isThrownBy(() -> assertThat((short) 2).inBinary()
.isEqualTo((short) 3))
.withMessage(shouldBeEqualMessage("0b00000000_00000010",
"0b00000000_00000011"));
// WHEN
AssertionError assertionError = expectAssertionError(() -> assertThat((short) 2).inBinary().isEqualTo((short) 3));
// THEN
then(assertionError).hasMessage(shouldBeEqualMessage("0b00000000_00000010",
"0b00000000_00000011"));
}

@Test
void should_assert_signed_short_in_binary() {
assertThatExceptionOfType(AssertionError.class).isThrownBy(() -> assertThat((short) -1).inBinary()
.isEqualTo((short) 3))
.withMessage(shouldBeEqualMessage("0b11111111_11111111",
"0b00000000_00000011"));
// WHEN
AssertionError assertionError = expectAssertionError(() -> assertThat((short) -1).inBinary().isEqualTo((short) 3));
// THEN
then(assertionError).hasMessage(shouldBeEqualMessage("0b11111111_11111111",
"0b00000000_00000011"));
}

@Test
void should_assert_integer_in_binary() {
assertThatExceptionOfType(AssertionError.class).isThrownBy(() -> assertThat(2).inBinary().isEqualTo(3))
.withMessage(shouldBeEqualMessage("0b00000000_00000000_00000000_00000010",
"0b00000000_00000000_00000000_00000011"));
// WHEN
AssertionError assertionError = expectAssertionError(() -> assertThat(2).inBinary().isEqualTo(3));
// THEN
then(assertionError).hasMessage(shouldBeEqualMessage("0b00000000_00000000_00000000_00000010",
"0b00000000_00000000_00000000_00000011"));
}

@Test
void should_assert_negative_integer_in_binary() {
assertThatExceptionOfType(AssertionError.class).isThrownBy(() -> assertThat(3).inBinary().isEqualTo(-3))
.withMessage(shouldBeEqualMessage("0b00000000_00000000_00000000_00000011",
"0b11111111_11111111_11111111_11111101"));
// WHEN
AssertionError assertionError = expectAssertionError(() -> assertThat(3).inBinary().isEqualTo(-3));
// THEN
then(assertionError).hasMessage(shouldBeEqualMessage("0b00000000_00000000_00000000_00000011",
"0b11111111_11111111_11111111_11111101"));
}

@Test
void should_assert_long_in_binary() {
assertThatExceptionOfType(AssertionError.class).isThrownBy(() -> assertThat((long) 2).inBinary().isEqualTo(3))
.withMessage(shouldBeEqualMessage("0b00000000_00000000_00000000_00000000_00000000_00000000_00000000_00000010",
"0b00000000_00000000_00000000_00000000_00000000_00000000_00000000_00000011"));
// WHEN
AssertionError assertionError = expectAssertionError(() -> assertThat((long) 2).inBinary().isEqualTo(3));
// THEN
then(assertionError).hasMessage(shouldBeEqualMessage("0b00000000_00000000_00000000_00000000_00000000_00000000_00000000_00000010",
"0b00000000_00000000_00000000_00000000_00000000_00000000_00000000_00000011"));
}

@Test
void should_assert_negative_long_in_binary() {
assertThatExceptionOfType(AssertionError.class).isThrownBy(() -> assertThat((long) -2).inBinary().isEqualTo(3))
.withMessage(shouldBeEqualMessage("0b11111111_11111111_11111111_11111111_11111111_11111111_11111111_11111110",
"0b00000000_00000000_00000000_00000000_00000000_00000000_00000000_00000011"));
// WHEN
AssertionError assertionError = expectAssertionError(() -> assertThat((long) -2).inBinary().isEqualTo(3));
// THEN
then(assertionError).hasMessage(shouldBeEqualMessage("0b11111111_11111111_11111111_11111111_11111111_11111111_11111111_11111110",
"0b00000000_00000000_00000000_00000000_00000000_00000000_00000000_00000011"));
}

@Test
void should_assert_float_in_binary() {
assertThatExceptionOfType(AssertionError.class).isThrownBy(() -> assertThat(2.1f).inBinary().isEqualTo(3f))
.withMessage(shouldBeEqualMessage("0b01000000_00000110_01100110_01100110",
"0b01000000_01000000_00000000_00000000"));
// WHEN
AssertionError assertionError = expectAssertionError(() -> assertThat(2.1f).inBinary().isEqualTo(3f));
// THEN
then(assertionError).hasMessage(shouldBeEqualMessage("0b01000000_00000110_01100110_01100110",
"0b01000000_01000000_00000000_00000000"));
}

@Test
void should_assert_double_in_binary() {
assertThatExceptionOfType(AssertionError.class).isThrownBy(() -> assertThat(2.1d).inBinary().isEqualTo(3d))
.withMessage(shouldBeEqualMessage("0b01000000_00000000_11001100_11001100_11001100_11001100_11001100_11001101",
"0b01000000_00001000_00000000_00000000_00000000_00000000_00000000_00000000"));
// WHEN
AssertionError assertionError = expectAssertionError(() -> assertThat(2.1d).inBinary().isEqualTo(3d));
// THEN
then(assertionError).hasMessage(shouldBeEqualMessage("0b01000000_00000000_11001100_11001100_11001100_11001100_11001100_11001101",
"0b01000000_00001000_00000000_00000000_00000000_00000000_00000000_00000000"));
}

// FIXME https://github.com/assertj/assertj/issues/3547
// @Test
// void should_assert_String_in_binary() {
// assertThatExceptionOfType(AssertionError.class).isThrownBy(() -> assertThat("ab").inBinary().isEqualTo("a6"))
// .withMessage(shouldBeEqualMessage("\"['0b00000000_01100001', '0b00000000_01100010']\"",
// "\"['0b00000000_01100001', '0b00000000_00110110']\""));
// }
@Test
void should_assert_String_in_binary() {
// WHEN
AssertionError assertionError = expectAssertionError(() -> assertThat("ab").inBinary().isEqualTo("a6"));
// THEN
then(assertionError).hasMessage(shouldBeEqualMessage("\"['0b00000000_01100001', '0b00000000_01100010']\"",
"\"['0b00000000_01100001', '0b00000000_00110110']\""));
}

}

0 comments on commit a4bc7ec

Please sign in to comment.