@@ -191,5 +191,230 @@ void main() {
191
191
// Assert
192
192
expect (result, equals (customErrorMessage));
193
193
});
194
+
195
+ group ('FileSizeValidator with base1024Conversion set to false -' , () {
196
+ test ('should return null when the file size is equal to the maximum size' ,
197
+ () {
198
+ // Arrange
199
+ const int maxSize = 1000 ;
200
+ const FileSizeValidator validator = FileSizeValidator (
201
+ maxSize,
202
+ base1024Conversion: false ,
203
+ );
204
+ const String value = '1000' ;
205
+
206
+ // Act
207
+ final String ? result = validator.validate (value);
208
+
209
+ // Assert
210
+ expect (result, isNull);
211
+ });
212
+
213
+ test (
214
+ 'should return null when the file size is less than the maximum size' ,
215
+ () {
216
+ // Arrange
217
+ const int maxSize = 1000 ;
218
+ const FileSizeValidator validator = FileSizeValidator (
219
+ maxSize,
220
+ base1024Conversion: false ,
221
+ );
222
+ const String value = '512' ;
223
+
224
+ // Act
225
+ final String ? result = validator.validate (value);
226
+
227
+ // Assert
228
+ expect (result, isNull);
229
+ });
230
+
231
+ test (
232
+ 'should return the default error message when the file size is greater than the maximum size' ,
233
+ () {
234
+ // Arrange
235
+ const int maxSize = 1000 ;
236
+ const FileSizeValidator validator = FileSizeValidator (
237
+ maxSize,
238
+ base1024Conversion: false ,
239
+ );
240
+ const String value = '2000' ;
241
+
242
+ // Act
243
+ final String ? result = validator.validate (value);
244
+
245
+ // Assert
246
+ expect (result, isNotNull);
247
+ expect (
248
+ result,
249
+ equals (
250
+ FormBuilderLocalizations .current.fileSizeErrorText ('0 B' , '1 kB' ),
251
+ ),
252
+ );
253
+ });
254
+
255
+ test (
256
+ 'should return the custom error message when the file size is greater than the maximum size' ,
257
+ () {
258
+ // Arrange
259
+ const int maxSize = 1000 ;
260
+ final FileSizeValidator validator = FileSizeValidator (
261
+ maxSize,
262
+ base1024Conversion: false ,
263
+ errorText: customErrorMessage,
264
+ );
265
+ const String value = '2000' ;
266
+
267
+ // Act
268
+ final String ? result = validator.validate (value);
269
+
270
+ // Assert
271
+ expect (result, equals (customErrorMessage));
272
+ });
273
+
274
+ test (
275
+ 'should return null when the value is null and null check is disabled' ,
276
+ () {
277
+ // Arrange
278
+ const int maxSize = 1000 ;
279
+ const FileSizeValidator validator = FileSizeValidator (
280
+ maxSize,
281
+ base1024Conversion: false ,
282
+ checkNullOrEmpty: false ,
283
+ );
284
+ const String ? value = null ;
285
+
286
+ // Act
287
+ final String ? result = validator.validate (value);
288
+
289
+ // Assert
290
+ expect (result, isNull);
291
+ });
292
+
293
+ test ('should return the default error message when the value is null' ,
294
+ () {
295
+ // Arrange
296
+ const int maxSize = 1000 ;
297
+ const FileSizeValidator validator = FileSizeValidator (
298
+ maxSize,
299
+ base1024Conversion: false ,
300
+ );
301
+ const String ? value = null ;
302
+
303
+ // Act
304
+ final String ? result = validator.validate (value);
305
+
306
+ // Assert
307
+ expect (result, isNotNull);
308
+ expect (
309
+ result,
310
+ equals (
311
+ FormBuilderLocalizations .current.fileSizeErrorText ('0 B' , '1 kB' ),
312
+ ),
313
+ );
314
+ });
315
+
316
+ test (
317
+ 'should return null when the value is an empty string and null check is disabled' ,
318
+ () {
319
+ // Arrange
320
+ const int maxSize = 1000 ;
321
+ const FileSizeValidator validator = FileSizeValidator (
322
+ maxSize,
323
+ base1024Conversion: false ,
324
+ checkNullOrEmpty: false ,
325
+ );
326
+ const String value = '' ;
327
+
328
+ // Act
329
+ final String ? result = validator.validate (value);
330
+
331
+ // Assert
332
+ expect (result, isNull);
333
+ });
334
+
335
+ test (
336
+ 'should return the default error message when the value is an empty string' ,
337
+ () {
338
+ // Arrange
339
+ const int maxSize = 1000 ;
340
+ const FileSizeValidator validator = FileSizeValidator (
341
+ maxSize,
342
+ base1024Conversion: false ,
343
+ );
344
+ const String value = '' ;
345
+
346
+ // Act
347
+ final String ? result = validator.validate (value);
348
+
349
+ // Assert
350
+ expect (result, isNotNull);
351
+ expect (
352
+ result,
353
+ equals (
354
+ FormBuilderLocalizations .current.fileSizeErrorText ('0 B' , '1 kB' ),
355
+ ),
356
+ );
357
+ });
358
+
359
+ test ('should return null for a valid file size within the allowed range' ,
360
+ () {
361
+ // Arrange
362
+ const int maxSize = 1000 ;
363
+ const FileSizeValidator validator = FileSizeValidator (
364
+ maxSize,
365
+ base1024Conversion: false ,
366
+ );
367
+ const String value = '500' ;
368
+
369
+ // Act
370
+ final String ? result = validator.validate (value);
371
+
372
+ // Assert
373
+ expect (result, isNull);
374
+ });
375
+
376
+ test (
377
+ 'should return the default error message for invalid file size format' ,
378
+ () {
379
+ // Arrange
380
+ const int maxSize = 1000 ;
381
+ const FileSizeValidator validator = FileSizeValidator (
382
+ maxSize,
383
+ base1024Conversion: false ,
384
+ );
385
+ const String value = 'invalid-size' ;
386
+
387
+ // Act
388
+ final String ? result = validator.validate (value);
389
+
390
+ // Assert
391
+ expect (result, isNotNull);
392
+ expect (
393
+ result,
394
+ equals (
395
+ FormBuilderLocalizations .current.fileSizeErrorText ('0 B' , '1 kB' ),
396
+ ),
397
+ );
398
+ });
399
+
400
+ test (
401
+ 'should return the custom error message for invalid file size format' ,
402
+ () {
403
+ // Arrange
404
+ const int maxSize = 1000 ;
405
+ final FileSizeValidator validator = FileSizeValidator (
406
+ maxSize,
407
+ base1024Conversion: false ,
408
+ errorText: customErrorMessage,
409
+ );
410
+ const String value = 'invalid-size' ;
411
+
412
+ // Act
413
+ final String ? result = validator.validate (value);
414
+
415
+ // Assert
416
+ expect (result, equals (customErrorMessage));
417
+ });
418
+ });
194
419
});
195
420
}
0 commit comments