|
| 1 | +import 'package:flutter/cupertino.dart'; |
| 2 | +import 'package:flutter/material.dart'; |
| 3 | +import 'package:flutter_localizations/flutter_localizations.dart'; |
| 4 | +import 'package:flutter_test/flutter_test.dart'; |
| 5 | +import 'package:flutter_form_builder/flutter_form_builder.dart'; |
| 6 | + |
| 7 | +/// Test Harness for running Validations |
| 8 | +Future<void> testValidations( |
| 9 | + WidgetTester tester, void Function(BuildContext) validations) async { |
| 10 | + await tester.pumpWidget(MaterialApp( |
| 11 | + localizationsDelegates: [ |
| 12 | + FormBuilderLocalizations.delegate, |
| 13 | + GlobalMaterialLocalizations.delegate, |
| 14 | + GlobalWidgetsLocalizations.delegate, |
| 15 | + ], |
| 16 | + home: Builder( |
| 17 | + builder: (BuildContext context) { |
| 18 | + // Exercise validations using the provided context |
| 19 | + validations(context); |
| 20 | + // The builder function must return a widget. |
| 21 | + return Placeholder(); |
| 22 | + }, |
| 23 | + ), |
| 24 | + )); |
| 25 | + |
| 26 | + // Critical to pumpAndSettle to let Builder build to exercise validations |
| 27 | + await tester.pumpAndSettle(); |
| 28 | +} |
| 29 | + |
| 30 | +void main() { |
| 31 | + testWidgets( |
| 32 | + 'FormBuilderValidators.required', |
| 33 | + (WidgetTester tester) => testValidations(tester, (context) { |
| 34 | + final validator = FormBuilderValidators.required(context); |
| 35 | + // Pass |
| 36 | + expect(validator(false), isNull); |
| 37 | + expect(validator(0), isNull); |
| 38 | + expect(validator('0'), isNull); |
| 39 | + expect(validator('something long'), isNull); |
| 40 | + expect(validator(DateTime.now()), isNull); |
| 41 | + // Fail |
| 42 | + expect(validator(null), isNotNull); |
| 43 | + expect(validator(''), isNotNull); |
| 44 | + expect(validator([]), isNotNull); |
| 45 | + })); |
| 46 | + |
| 47 | + testWidgets( |
| 48 | + 'FormBuilderValidators.maxLength', |
| 49 | + (WidgetTester tester) => testValidations(tester, (context) { |
| 50 | + final validator = FormBuilderValidators.maxLength(context, 5); |
| 51 | + // Pass |
| 52 | + expect(validator(null), isNull); |
| 53 | + expect(validator(''), isNull); |
| 54 | + expect(validator('two'), isNull); |
| 55 | + expect(validator('12345'), isNull); |
| 56 | + // Fail |
| 57 | + expect(validator('something long'), isNotNull); |
| 58 | + expect(validator('123456'), isNotNull); |
| 59 | + })); |
| 60 | + testWidgets( |
| 61 | + 'FormBuilderValidators.minLength', |
| 62 | + (WidgetTester tester) => testValidations(tester, (context) { |
| 63 | + final validator = FormBuilderValidators.minLength(context, 5); |
| 64 | + // Pass |
| 65 | + expect(validator('12345'), isNull); |
| 66 | + expect(validator('123456'), isNull); |
| 67 | + expect(validator('something long'), isNull); |
| 68 | + // Fail |
| 69 | + expect(validator(null), isNotNull); |
| 70 | + expect(validator(''), isNotNull); |
| 71 | + expect(validator('two'), isNotNull); |
| 72 | + // Advanced |
| 73 | + final validatorAllowEmpty = |
| 74 | + FormBuilderValidators.minLength(context, 5, allowEmpty: true); |
| 75 | + expect(validatorAllowEmpty(null), isNull); |
| 76 | + expect(validatorAllowEmpty(''), isNull); |
| 77 | + })); |
| 78 | + |
| 79 | + testWidgets( |
| 80 | + 'FormBuilderValidators.email', |
| 81 | + (WidgetTester tester) => testValidations(tester, (context) { |
| 82 | + final validator = FormBuilderValidators.email(context); |
| 83 | + // Pass |
| 84 | + expect(validator(null), isNull); |
| 85 | + expect(validator(''), isNull); |
| 86 | + expect( validator( '[email protected]'), isNull); |
| 87 | + expect( validator( ' [email protected] '), isNull); |
| 88 | + expect( validator( '[email protected] '), isNull); |
| 89 | + expect( validator( ' [email protected]'), isNull); |
| 90 | + // Fail |
| 91 | + expect(validator('john@flutter'), isNotNull); |
| 92 | + expect(validator('john@ flutter.dev'), isNotNull); |
| 93 | + expect(validator('john flutter.dev'), isNotNull); |
| 94 | + expect(validator('flutter.dev'), isNotNull); |
| 95 | + })); |
| 96 | + |
| 97 | + testWidgets( |
| 98 | + 'FormBuilderValidators.max', |
| 99 | + (WidgetTester tester) => testValidations(tester, (context) { |
| 100 | + final validator = FormBuilderValidators.max(context, 20); |
| 101 | + // Pass |
| 102 | + expect(validator(null), isNull); |
| 103 | + expect(validator(''), isNull); |
| 104 | + expect(validator(0), isNull); |
| 105 | + expect(validator(-1), isNull); |
| 106 | + expect(validator(-1.1), isNull); |
| 107 | + expect(validator(1.2), isNull); |
| 108 | + expect(validator('19'), isNull); |
| 109 | + expect(validator('20'), isNull); |
| 110 | + expect(validator(20), isNull); |
| 111 | + // Fail |
| 112 | + expect(validator(20.01), isNotNull); |
| 113 | + expect(validator(21), isNotNull); |
| 114 | + expect(validator('21'), isNotNull); |
| 115 | + expect(validator(999), isNotNull); |
| 116 | + })); |
| 117 | + |
| 118 | + testWidgets( |
| 119 | + 'FormBuilderValidators.min', |
| 120 | + (WidgetTester tester) => testValidations(tester, (context) { |
| 121 | + final validator = FormBuilderValidators.min(context, 30); |
| 122 | + // Pass |
| 123 | + expect(validator(null), isNull); |
| 124 | + expect(validator(''), isNull); |
| 125 | + expect(validator(30.01), isNull); |
| 126 | + expect(validator(31), isNull); |
| 127 | + expect(validator('31'), isNull); |
| 128 | + expect(validator(70), isNull); |
| 129 | + // Fail |
| 130 | + expect(validator(-1), isNotNull); |
| 131 | + expect(validator(0), isNotNull); |
| 132 | + expect(validator('10'), isNotNull); |
| 133 | + expect(validator(10), isNotNull); |
| 134 | + expect(validator(29), isNotNull); |
| 135 | + })); |
| 136 | + |
| 137 | + testWidgets( |
| 138 | + 'FormBuilderValidators.numeric', |
| 139 | + (WidgetTester tester) => testValidations(tester, (context) { |
| 140 | + final validator = FormBuilderValidators.numeric(context); |
| 141 | + // Pass |
| 142 | + expect(validator(null), isNull); |
| 143 | + expect(validator(''), isNull); |
| 144 | + expect(validator('0'), isNull); |
| 145 | + expect(validator('31'), isNull); |
| 146 | + expect(validator('-1'), isNull); |
| 147 | + expect(validator('-1.01'), isNull); |
| 148 | + // Fail |
| 149 | + expect(validator('A'), isNotNull); |
| 150 | + expect(validator('XYZ'), isNotNull); |
| 151 | + })); |
| 152 | + |
| 153 | + testWidgets( |
| 154 | + 'FormBuilderValidators.url', |
| 155 | + (WidgetTester tester) => testValidations(tester, (context) { |
| 156 | + final validator = FormBuilderValidators.url(context); |
| 157 | + // Pass |
| 158 | + expect(validator(null), isNull); |
| 159 | + expect(validator(''), isNull); |
| 160 | + expect(validator('https://www.google.com'), isNull); |
| 161 | + expect(validator('www.google.com'), isNull); |
| 162 | + expect(validator('google.com'), isNull); |
| 163 | + expect(validator('http://google.com'), isNull); |
| 164 | + // Fail |
| 165 | + expect(validator('.com'), isNotNull); |
| 166 | + // Advanced overrides |
| 167 | + expect( |
| 168 | + FormBuilderValidators.url(context, |
| 169 | + protocols: ['https', 'http'], |
| 170 | + errorText: |
| 171 | + 'Only HTTP and HTTPS allowed')('ftp://www.google.com'), |
| 172 | + isNotNull); |
| 173 | + })); |
| 174 | + |
| 175 | + testWidgets( |
| 176 | + 'FormBuilderValidators.IP', |
| 177 | + (WidgetTester tester) => testValidations(tester, (context) { |
| 178 | + final validator = FormBuilderValidators.IP(context); |
| 179 | + // Pass |
| 180 | + expect(validator(null), isNull); |
| 181 | + expect(validator(''), isNull); |
| 182 | + expect(validator('192.168.0.1'), isNull); |
| 183 | + // Fail |
| 184 | + expect(validator('256.168.0.1'), isNotNull); |
| 185 | + expect(validator('256.168.0.'), isNotNull); |
| 186 | + expect(validator('255.168.0.'), isNotNull); |
| 187 | + })); |
| 188 | + |
| 189 | + testWidgets( |
| 190 | + 'FormBuilderValidators.compose', |
| 191 | + (WidgetTester tester) => testValidations(tester, (context) { |
| 192 | + final validator = FormBuilderValidators.compose([ |
| 193 | + FormBuilderValidators.required(context), |
| 194 | + FormBuilderValidators.numeric(context), |
| 195 | + FormBuilderValidators.minLength(context, 2), |
| 196 | + FormBuilderValidators.maxLength(context, 3), |
| 197 | + ]); |
| 198 | + // Pass |
| 199 | + expect(validator('12'), isNull); |
| 200 | + expect(validator('123'), isNull); |
| 201 | + // Fail |
| 202 | + expect(validator(null), isNotNull); |
| 203 | + expect(validator(''), isNotNull); |
| 204 | + expect(validator('1'), isNotNull); |
| 205 | + expect(validator('1234'), isNotNull); |
| 206 | + expect(validator('ABC'), isNotNull); |
| 207 | + })); |
| 208 | +} |
0 commit comments