77
88import java .util .regex .Pattern ;
99
10+ import static net .datafaker .providers .base .Text .DEFAULT_SPECIAL ;
1011import static net .datafaker .providers .base .Text .DIGITS ;
1112import static net .datafaker .providers .base .Text .EN_LOWERCASE ;
1213import static net .datafaker .providers .base .Text .EN_UPPERCASE ;
1314import static org .assertj .core .api .Assertions .assertThat ;
1415import static org .assertj .core .api .Assertions .assertThatThrownBy ;
1516
1617class TextTest {
18+ private static final Pattern characterPattern = Pattern .compile ("[A-Za-z]" );
1719 private final Faker faker = new Faker ();
1820
1921 @ Test
@@ -57,7 +59,8 @@ void exceptionIfLengthIsShorterThanNumberOfRequiredSymbols() {
5759 .with (DIGITS , 1 )
5860 .throwIfLengthSmall (true )
5961 .build ()))
60- .isInstanceOf (IllegalArgumentException .class );
62+ .isInstanceOf (IllegalArgumentException .class )
63+ .hasMessage ("Min length (1) should be not smaller than number of required characters (3)" );
6164 }
6265
6366 @ Test
@@ -99,11 +102,8 @@ void everyTextShouldContainLowerCaseUpperCaseAndDigit() {
99102
100103 @ Test
101104 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 );
107107 }
108108
109109 @ RepeatedTest ((100 ))
@@ -122,16 +122,83 @@ void testLowercaseCharacter() {
122122 void testFixedLengthText () {
123123 for (int i = 0 ; i < 100 ; i ++) {
124124 String text = faker .text ().text (i );
125- assertThat (text ).hasSize (i );
125+ assertThat (text ).hasSize (i ). matches ( "[a-z]*" ) ;
126126 }
127127 }
128128
129- @ Test
129+ @ RepeatedTest ( 10 )
130130 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]" );
135156 }
136157
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 not be 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 not be 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 not be 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 not be 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+ }
137204}
0 commit comments