-
Notifications
You must be signed in to change notification settings - Fork 38
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: 添加注解SpelMax测试用例,添加SpelParser解析器测试用例
- Loading branch information
Showing
5 changed files
with
130 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
47 changes: 47 additions & 0 deletions
47
src/test/java/cn/sticki/validator/spel/SpelParserTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package cn.sticki.validator.spel; | ||
|
||
import cn.sticki.validator.spel.exception.SpelParserException; | ||
import cn.sticki.validator.spel.parse.SpelParser; | ||
import org.junit.jupiter.api.Assertions; | ||
import org.junit.jupiter.api.Test; | ||
|
||
/** | ||
* SpelParser 测试 | ||
* | ||
* @author 阿杆 | ||
* @version 1.0 | ||
* @since 2024/10/12 | ||
*/ | ||
public class SpelParserTest { | ||
|
||
@Test | ||
void testParse() { | ||
Integer num = SpelParser.parse("1+1", null, Integer.class); | ||
Assertions.assertEquals(2, (int) num); | ||
|
||
String str = SpelParser.parse("'hello' + 'world'", null, String.class); | ||
Assertions.assertEquals("helloworld", str); | ||
|
||
Boolean bool = SpelParser.parse("true && false", null, Boolean.class); | ||
Assertions.assertFalse(bool); | ||
|
||
Double decimal = SpelParser.parse("1.1 + 2.2", null, Double.class); | ||
Assertions.assertEquals(3.3, decimal, 0.0001); | ||
|
||
// ----- 异常情况 ------ | ||
|
||
// 表达式结果为空 | ||
Integer nullValue = null; | ||
Assertions.assertThrows(SpelParserException.class, () -> SpelParser.parse("#this", nullValue, Integer.class)); | ||
|
||
// 表达式计算结果类型不匹配 | ||
Assertions.assertThrows(SpelParserException.class, () -> SpelParser.parse("1+1", null, String.class)); | ||
|
||
// 表达式计算异常 | ||
Assertions.assertThrows(SpelParserException.class, () -> SpelParser.parse("1/0", null)); | ||
|
||
// 表达式解析异常 | ||
Assertions.assertThrows(SpelParserException.class, () -> SpelParser.parse("#this.aaa", null)); | ||
} | ||
|
||
} |
73 changes: 73 additions & 0 deletions
73
src/test/java/cn/sticki/validator/spel/bean/SpelMaxTestBean.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
package cn.sticki.validator.spel.bean; | ||
|
||
import cn.sticki.validator.spel.SpelValid; | ||
import cn.sticki.validator.spel.VerifyFailedField; | ||
import cn.sticki.validator.spel.VerifyObject; | ||
import cn.sticki.validator.spel.constrain.SpelMax; | ||
import cn.sticki.validator.spel.util.ID; | ||
import lombok.Builder; | ||
import lombok.Data; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
/** | ||
* SpelMax 测试用例 | ||
* <p> | ||
* 由于 SpelMax 和 SpelMax 的实现方式相同,故SpelMax仅进行简单测试 | ||
* | ||
* @author 阿杆 | ||
* @version 1.0 | ||
* @since 2024/10/12 | ||
*/ | ||
public class SpelMaxTestBean { | ||
|
||
/** | ||
* 参数测试 | ||
*/ | ||
@Data | ||
@Builder | ||
@SpelValid | ||
public static class ParamTestBean implements ID { | ||
|
||
private int id; | ||
|
||
private int max; | ||
|
||
@SpelMax(value = "#this.max") | ||
private Long testLong; | ||
|
||
} | ||
|
||
/** | ||
* 参数测试 | ||
*/ | ||
public static List<VerifyObject> paramTestCase() { | ||
ArrayList<VerifyObject> result = new ArrayList<>(); | ||
|
||
// null | ||
result.add(VerifyObject.of( | ||
ParamTestBean.builder().id(1).max(1).build(), | ||
VerifyFailedField.of() | ||
)); | ||
|
||
// 大于最大值 | ||
result.add(VerifyObject.of( | ||
ParamTestBean.builder().id(2).max(5).testLong(6L).build(), | ||
VerifyFailedField.of(SpelMaxTestBean.ParamTestBean::getTestLong) | ||
)); | ||
|
||
// 等于最大值 | ||
result.add(VerifyObject.of( | ||
ParamTestBean.builder().id(3).max(5).testLong(5L).build() | ||
)); | ||
|
||
// 小于最大值 | ||
result.add(VerifyObject.of( | ||
ParamTestBean.builder().id(4).max(5).testLong(-123L).build() | ||
)); | ||
|
||
return result; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters