7
7
8
8
import java .util .regex .Pattern ;
9
9
10
+ import static net .datafaker .providers .base .Text .DEFAULT_SPECIAL ;
10
11
import static net .datafaker .providers .base .Text .DIGITS ;
11
12
import static net .datafaker .providers .base .Text .EN_LOWERCASE ;
12
13
import static net .datafaker .providers .base .Text .EN_UPPERCASE ;
13
14
import static org .assertj .core .api .Assertions .assertThat ;
14
15
import static org .assertj .core .api .Assertions .assertThatThrownBy ;
15
16
16
17
class TextTest {
18
+ private static final Pattern characterPattern = Pattern .compile ("[A-Za-z]" );
17
19
private final Faker faker = new Faker ();
18
20
19
21
@ Test
@@ -57,7 +59,8 @@ void exceptionIfLengthIsShorterThanNumberOfRequiredSymbols() {
57
59
.with (DIGITS , 1 )
58
60
.throwIfLengthSmall (true )
59
61
.build ()))
60
- .isInstanceOf (IllegalArgumentException .class );
62
+ .isInstanceOf (IllegalArgumentException .class )
63
+ .hasMessage ("Min length (1) should be not smaller than number of required characters (3)" );
61
64
}
62
65
63
66
@ Test
@@ -99,11 +102,8 @@ void everyTextShouldContainLowerCaseUpperCaseAndDigit() {
99
102
100
103
@ Test
101
104
void testCharacter () {
102
- final Pattern characterPattern = Pattern .compile ("[A-Za-z]" );
103
- for (int i = 0 ; i < 100 ; i ++) {
104
- Character character = faker .text ().character ();
105
- assertThat (character .toString ()).matches (characterPattern );
106
- }
105
+ Character character = faker .text ().character ();
106
+ assertThat (character .toString ()).matches (characterPattern );
107
107
}
108
108
109
109
@ RepeatedTest ((100 ))
@@ -122,16 +122,83 @@ void testLowercaseCharacter() {
122
122
void testFixedLengthText () {
123
123
for (int i = 0 ; i < 100 ; i ++) {
124
124
String text = faker .text ().text (i );
125
- assertThat (text ).hasSize (i );
125
+ assertThat (text ).hasSize (i ). matches ( "[a-z]*" ) ;
126
126
}
127
127
}
128
128
129
- @ Test
129
+ @ RepeatedTest ( 10 )
130
130
void testDefaultLengthText () {
131
- for (int i = 0 ; i < 100 ; i ++) {
132
- String text = faker .text ().text ();
133
- assertThat (text ).hasSizeBetween (20 , 80 );
134
- }
131
+ String text = faker .text ().text ();
132
+ assertThat (text ).hasSizeBetween (20 , 80 ).matches ("[a-z]{20,80}" );
133
+ }
134
+
135
+ @ RepeatedTest (10 )
136
+ void upTo64LowerCase () {
137
+ assertThat (faker .text ().text (1 , 64 , false , false , false )).matches ("[a-z]{1,64}" );
138
+ assertThat (faker .text ().text (2 , 64 , false , false , false )).matches ("[a-z]{2,64}" );
139
+ assertThat (faker .text ().text (64 , 64 , false , false , false )).matches ("[a-z]{64}" );
140
+ }
141
+
142
+ @ Test
143
+ void zeroLength () {
144
+ assertThat (faker .text ().text (0 , 0 , false , false , false )).isEqualTo ("" );
145
+ }
146
+
147
+ @ Test
148
+ void oneLowerCase () {
149
+ assertThat (faker .text ().text (1 , 1 , false , false , false )).matches ("[a-z]" );
150
+ assertThat (faker .text ().text (0 , 1 , false , false , false )).matches ("[a-z]?" );
151
+ }
152
+
153
+ @ RepeatedTest (10 )
154
+ void oneWithUpperCase () {
155
+ assertThat (faker .text ().text (1 , 1 , true , false , false )).matches ("[A-Z]" );
135
156
}
136
157
158
+ @ RepeatedTest (10 )
159
+ void oneWithDigit () {
160
+ assertThat (faker .text ().text (1 , 1 , false , false , true )).matches ("[0-9]" );
161
+ }
162
+
163
+ @ RepeatedTest (10 )
164
+ void oneWithSpecialSymbol () {
165
+ assertThat (faker .text ().text (1 , 1 , false , true , false )).matches ("[" + DEFAULT_SPECIAL + "]" );
166
+ }
167
+
168
+ @ RepeatedTest (10 )
169
+ void twoWithUpperCaseAndDigit () {
170
+ assertThat (faker .text ().text (2 , 2 , true , false , true )).matches ("[A-Z0-9]{2}" );
171
+ }
172
+
173
+ @ RepeatedTest (10 )
174
+ void twoWithLowerAndUpperCaseAndDigit () {
175
+ assertThat (faker .text ().text (2 , 2 , true , false , true )).matches ("[a-zA-Z0-9]{2}" );
176
+ assertThat (faker .text ().text (3 , 3 , true , true , true )).matches ("[a-zA-Z0-9" + DEFAULT_SPECIAL + "]{3}" );
177
+ }
178
+
179
+ @ Test
180
+ void minLengthCannotBeGreaterThanMaxLength () {
181
+ assertThatThrownBy (() -> faker .text ().text (22 , 21 , false , false , false ))
182
+ .isInstanceOf (IllegalArgumentException .class )
183
+ .hasMessage ("Min length (22) should be not greater than max length (21)" );
184
+
185
+ assertThatThrownBy (() -> faker .text ().text (3 , 2 , true , true , true ))
186
+ .isInstanceOf (IllegalArgumentException .class )
187
+ .hasMessage ("Min length (3) should be not greater than max length (2)" );
188
+ }
189
+
190
+ @ Test
191
+ void isNotEnoughLengthToContainAllRequiredSymbols () {
192
+ assertThatThrownBy (() -> faker .text ().text (0 , 0 , true , false , false ))
193
+ .isInstanceOf (IllegalArgumentException .class )
194
+ .hasMessage ("Minimum number of required characters (1) should be not greater than max length (0)" );
195
+ assertThatThrownBy (() -> faker .text ().text (1 , 2 , true , true , true ))
196
+ .isInstanceOf (IllegalArgumentException .class )
197
+ .hasMessage ("Minimum number of required characters (3) should be not greater than max length (2)" );
198
+ }
199
+
200
+ @ RepeatedTest (10 )
201
+ void minimumLengthIsNotEnoughToContainAllRequiredSymbols () {
202
+ assertThat (faker .text ().text (1 , 4 , true , false , true )).matches ("[a-zA-Z0-9]{2,4}" );
203
+ }
137
204
}
0 commit comments