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

View File

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