完成 IWithCode 相关接口的单元测试。

pull/1/head
ZhouXY108 2024-12-24 10:44:05 +08:00
parent c78a2ab380
commit f6b2509b12
3 changed files with 96 additions and 67 deletions

View File

@ -1,7 +1,7 @@
[x] 无需测试 - 6
[ ] 未开始测试 - 34
[-] 测试未完成 - 17
[Y] 测试完成 - 12
[-] 测试未完成 - 15
[Y] 测试完成 - 15
xyz.zhouxy.plusone.commons
├───annotation
@ -17,9 +17,9 @@ xyz.zhouxy.plusone.commons
│ CharRef.java [Y]
│ DoubleRef.java [Y]
│ IntRef.java [Y]
│ IWithCode.java [-]
│ IWithIntCode.java [-]
│ IWithLongCode.java [ ]
│ IWithCode.java [Y]
│ IWithIntCode.java [Y]
│ IWithLongCode.java [Y]
│ JRE.java [ ]
│ LongRef.java [Y]
│ Ref.java [Y]

View File

@ -1,22 +1,80 @@
package xyz.zhouxy.plusone.commons.base;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.*;
import javax.annotation.Nonnull;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import xyz.zhouxy.plusone.commons.util.AssertTools;
class IWithCodeTests {
private static class WithCodeImpl implements IWithCode<String> {
@Test
void equalsCode_SameCode_ReturnsTrue() {
assertTrue(WithCode.INSTANCE.equalsCode("testCode"));
Integer intCode = 0;
Long longCode = 0L;
assertTrue(WithIntCode.INSTANCE.equalsCode(intCode));
assertTrue(WithLongCode.INSTANCE.equalsCode(intCode));
assertTrue(WithLongCode.INSTANCE.equalsCode(longCode));
assertTrue(WithCode.INSTANCE.equalsCode(WithCode.SAME_CODE_INSTANCE));
assertTrue(WithIntCode.INSTANCE.equalsCode(WithIntCode.SAME_CODE_INSTANCE));
assertTrue(WithIntCode.INSTANCE.equalsCode(WithLongCode.SAME_CODE_INSTANCE));
assertTrue(WithLongCode.INSTANCE.equalsCode(WithLongCode.SAME_CODE_INSTANCE));
assertTrue(WithLongCode.INSTANCE.equalsCode(WithIntCode.SAME_CODE_INSTANCE));
}
@Test
void equalsCode_DifferentCode_ReturnsFalse() {
assertFalse(WithCode.INSTANCE.equalsCode("wrongCode"));
Integer intCode = 108;
Long longCode = 108L;
assertFalse(WithIntCode.INSTANCE.equalsCode(intCode));
assertFalse(WithLongCode.INSTANCE.equalsCode(intCode));
assertFalse(WithLongCode.INSTANCE.equalsCode(longCode));
assertFalse(WithCode.INSTANCE.equalsCode(WithCode.WRONG_CODE_INSTANCE));
assertFalse(WithIntCode.INSTANCE.equalsCode(WithIntCode.WRONG_CODE_INSTANCE));
assertFalse(WithIntCode.INSTANCE.equalsCode(WithLongCode.WRONG_CODE_INSTANCE));
assertFalse(WithLongCode.INSTANCE.equalsCode(WithLongCode.WRONG_CODE_INSTANCE));
assertFalse(WithLongCode.INSTANCE.equalsCode(WithIntCode.WRONG_CODE_INSTANCE));
}
@Test
@SuppressWarnings("null")
void equalsCode_NullCode_ReturnsFalse() {
assertFalse(WithCode.INSTANCE.equalsCode((WithCode) null));
assertFalse(WithCode.INSTANCE.equalsCode((WithIntCode) null));
assertFalse(WithCode.INSTANCE.equalsCode((WithLongCode) null));
assertFalse(WithIntCode.INSTANCE.equalsCode((WithCode) null));
assertFalse(WithIntCode.INSTANCE.equalsCode((WithIntCode) null));
assertFalse(WithIntCode.INSTANCE.equalsCode((WithLongCode) null));
assertFalse(WithLongCode.INSTANCE.equalsCode((WithCode) null));
assertFalse(WithLongCode.INSTANCE.equalsCode((WithIntCode) null));
assertFalse(WithLongCode.INSTANCE.equalsCode((WithLongCode) null));
assertFalse(WithCode.INSTANCE.equalsCode((String) null));
Integer intCode = null;
Long longCode = null;
assertThrows(NullPointerException.class, () -> WithIntCode.INSTANCE.equalsCode(intCode));
assertThrows(NullPointerException.class, () -> WithLongCode.INSTANCE.equalsCode(intCode));
assertThrows(NullPointerException.class, () -> WithLongCode.INSTANCE.equalsCode(longCode));
}
private static enum WithCode implements IWithCode<String> {
INSTANCE("testCode"),
SAME_CODE_INSTANCE("testCode"),
WRONG_CODE_INSTANCE("wrongCode"),
;
@Nonnull
private final String code;
WithCodeImpl(String code) {
WithCode(String code) {
AssertTools.checkNotNull(code);
this.code = code;
}
@ -28,30 +86,39 @@ class IWithCodeTests {
}
}
private WithCodeImpl instance;
private static enum WithIntCode implements IWithIntCode {
INSTANCE(0),
SAME_CODE_INSTANCE(0),
WRONG_CODE_INSTANCE(1),
;
@BeforeEach
void setUp() {
instance = new WithCodeImpl("testCode");
private final int code;
WithIntCode(int code) {
this.code = code;
}
@Override
public int getCode() {
return code;
}
}
@Test
void equalsCode_SameCode_ReturnsTrue() {
assertTrue(instance.equalsCode("testCode"));
}
private static enum WithLongCode implements IWithLongCode {
INSTANCE(0L),
SAME_CODE_INSTANCE(0L),
WRONG_CODE_INSTANCE(108L),
;
@Test
void equalsCode_DifferentCode_ReturnsFalse() {
assertFalse(instance.equalsCode("wrongCode"));
}
private final long code;
@Test
void equalsCode_NullCode_ReturnsFalse() {
assertFalse(instance.equalsCode((String) null));
}
WithLongCode(long code) {
this.code = code;
}
@Test
void equalsCode_NullObject_ReturnsFalse() {
assertFalse(instance.equalsCode((String) null));
@Override
public long getCode() {
return code;
}
}
}

View File

@ -1,38 +0,0 @@
package xyz.zhouxy.plusone.commons.base;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
import org.junit.jupiter.api.Test;
class IWithIntCodeTests {
@Test
void test() {
IWithIntCode instance1 = new WithIntCodeImpl(10);
IWithIntCode instance2 = new WithIntCodeImpl(20);
IWithIntCode instance3 = new WithIntCodeImpl(10);
// Test for equalsCode method
assertTrue(instance1.equalsCode(10));
assertFalse(instance1.equalsCode(20));
assertTrue(instance2.equalsCode(20));
assertTrue(instance3.equalsCode(10));
// Test for distinct instances with same code
assertTrue(instance1.equalsCode(instance3.getCode())); // because they have the same code
assertFalse(instance1.equalsCode(instance2.getCode())); // because they have different codes
}
}
class WithIntCodeImpl implements IWithIntCode {
private int code;
public WithIntCodeImpl(int code) {
this.code = code;
}
@Override
public int getCode() {
return code;
}
}