1
+ package dev .ditsche .validator .rule .ruleset ;
2
+
3
+ import org .junit .jupiter .api .Test ;
4
+
5
+ import java .util .LinkedList ;
6
+ import java .util .List ;
7
+ import java .util .stream .Stream ;
8
+
9
+ import static org .assertj .core .api .Assertions .assertThat ;
10
+
11
+ public class LengthRuleTest {
12
+
13
+ private final LengthRule lengthRule = new LengthRule (6 );
14
+
15
+ @ Test
16
+ public void shouldFailIfNoStringIsProvided () {
17
+ Stream .of (null , 1 , new LinkedList <>(), 2.00f ).forEach (value -> {
18
+ assertThat (lengthRule .passes (value ).isPassed ()).isFalse ();
19
+ });
20
+ }
21
+
22
+ @ Test
23
+ public void shouldFailIfValueIsTooLong () {
24
+ Stream .of ("212.35.234.256" , "Testing things" , 7 , List .of (1 ,2 ,3 ,4 ,5 ,6 ,7 )).forEach (value -> {
25
+ assertThat (lengthRule .passes (value ).isPassed ()).isFalse ();
26
+ });
27
+ }
28
+
29
+ @ Test
30
+ public void shouldFailIfValueIsTooShort () {
31
+ Stream .of ("" , "Test" , 5 , List .of (1 ,2 ,3 ,4 )).forEach (value -> {
32
+ assertThat (lengthRule .passes (value ).isPassed ()).isFalse ();
33
+ });
34
+ }
35
+
36
+ @ Test
37
+ public void shouldPassWithValidLengths () {
38
+ Stream .of ("Tobias" , "123456" , 6 , List .of (1 ,2 ,3 ,4 ,5 ,6 )).forEach (value -> {
39
+ assertThat (lengthRule .passes (value ).isPassed ()).isTrue ();
40
+ });
41
+ }
42
+
43
+ @ Test
44
+ public void shouldReturnValidErrorMessage () {
45
+ assertThat (lengthRule .message ("test" )).isEqualTo ("The field \" test\" needs to have a length of 6" );
46
+ }
47
+
48
+ }
0 commit comments