|
| 1 | +import 'dart:io'; |
| 2 | +import 'dart:typed_data'; |
| 3 | + |
| 4 | +import 'package:luthor/luthor.dart'; |
| 5 | +import 'package:test/test.dart'; |
| 6 | + |
| 7 | +void main() { |
| 8 | + test('should validate File instances', () { |
| 9 | + final result = l.file().validateValue(File('avatar.png')); |
| 10 | + |
| 11 | + switch (result) { |
| 12 | + case SingleValidationSuccess(data: final data): |
| 13 | + expect(data, isA<File>()); |
| 14 | + case SingleValidationError(data: _, errors: final errors): |
| 15 | + fail('expected success but got errors: $errors'); |
| 16 | + } |
| 17 | + }); |
| 18 | + |
| 19 | + test('should validate byte arrays as file-like values', () { |
| 20 | + final result = l.file().validateValue(Uint8List.fromList([1, 2, 3])); |
| 21 | + |
| 22 | + switch (result) { |
| 23 | + case SingleValidationSuccess(data: final data): |
| 24 | + expect(data, isA<Uint8List>()); |
| 25 | + case SingleValidationError(data: _, errors: final errors): |
| 26 | + fail('expected success but got errors: $errors'); |
| 27 | + } |
| 28 | + }); |
| 29 | + |
| 30 | + test('should fail for null values', () { |
| 31 | + final result = l.file().validateValue(null); |
| 32 | + |
| 33 | + switch (result) { |
| 34 | + case SingleValidationSuccess(data: _): |
| 35 | + fail('should not be a success'); |
| 36 | + case SingleValidationError(data: _, errors: final errors): |
| 37 | + expect(errors, ['value must be a file']); |
| 38 | + } |
| 39 | + }); |
| 40 | + |
| 41 | + test('should fail for non file-like values', () { |
| 42 | + final result = l.file().validateValue('not-a-file'); |
| 43 | + |
| 44 | + switch (result) { |
| 45 | + case SingleValidationSuccess(data: _): |
| 46 | + fail('should not be a success'); |
| 47 | + case SingleValidationError(data: _, errors: final errors): |
| 48 | + expect(errors, ['value must be a file']); |
| 49 | + } |
| 50 | + }); |
| 51 | + |
| 52 | + test('should use custom message when message is provided', () { |
| 53 | + final result = l |
| 54 | + .file(message: 'profileImage must be a file') |
| 55 | + .validateValue('not-a-file'); |
| 56 | + |
| 57 | + switch (result) { |
| 58 | + case SingleValidationSuccess(data: _): |
| 59 | + fail('should not be a success'); |
| 60 | + case SingleValidationError(data: _, errors: final errors): |
| 61 | + expect(errors, ['profileImage must be a file']); |
| 62 | + } |
| 63 | + }); |
| 64 | + |
| 65 | + test('should use messageFn when provided', () { |
| 66 | + final result = l |
| 67 | + .file(messageFn: () => 'dynamic file error') |
| 68 | + .validateValue('not-a-file'); |
| 69 | + |
| 70 | + switch (result) { |
| 71 | + case SingleValidationSuccess(data: _): |
| 72 | + fail('should not be a success'); |
| 73 | + case SingleValidationError(data: _, errors: final errors): |
| 74 | + expect(errors, ['dynamic file error']); |
| 75 | + } |
| 76 | + }); |
| 77 | +} |
0 commit comments