|
1 |
| -import 'package:test/test.dart'; |
| 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'; |
2 | 5 | import 'package:flutter_form_builder/flutter_form_builder.dart';
|
3 | 6 |
|
4 | 7 | void main() {
|
5 |
| - test('FormBuilderValidators.required', () { |
6 |
| - expect(FormBuilderValidators.required()('something long'), isNull); |
7 |
| - expect(FormBuilderValidators.required()(DateTime.now()), isNull); |
8 |
| - expect(FormBuilderValidators.required()(''), isNotNull); |
9 |
| - expect(FormBuilderValidators.required()([]), isNotNull); |
10 |
| - // expect(FormBuilderValidators.maxLength(5)(5), equals(null)); |
| 8 | + testWidgets('FormBuilderValidators.required', (WidgetTester tester) async { |
| 9 | + await tester.pumpWidget( |
| 10 | + MaterialApp( |
| 11 | + localizationsDelegates: [ |
| 12 | + FormBuilderLocalizationsDelegate(), |
| 13 | + GlobalMaterialLocalizations.delegate, |
| 14 | + GlobalWidgetsLocalizations.delegate, |
| 15 | + ], |
| 16 | + home: Builder( |
| 17 | + builder: (BuildContext context) { |
| 18 | + expect(FormBuilderValidators.required(context)('something long'), isNull); |
| 19 | + expect(FormBuilderValidators.required(context)(DateTime.now()), isNull); |
| 20 | + expect(FormBuilderValidators.required(context)(''), isNotNull); |
| 21 | + expect(FormBuilderValidators.required(context)([]), isNotNull); |
| 22 | + |
| 23 | + // The builder function must return a widget. |
| 24 | + return Placeholder(); |
| 25 | + }, |
| 26 | + ), |
| 27 | + ), |
| 28 | + ); |
11 | 29 | });
|
12 | 30 |
|
13 |
| - test('FormBuilderValidators.maxLength', () { |
14 |
| - expect(FormBuilderValidators.maxLength(5)('something long'), equals('Value must have a length less than or equal to 5')); |
15 |
| - expect(FormBuilderValidators.maxLength(5)('two'), equals(null)); |
16 |
| - // expect(FormBuilderValidators.maxLength(5)(5), equals(null)); |
| 31 | + testWidgets('FormBuilderValidators.maxLength', (WidgetTester tester) async { |
| 32 | + await tester.pumpWidget( |
| 33 | + MaterialApp( |
| 34 | + localizationsDelegates: [ |
| 35 | + FormBuilderLocalizationsDelegate(), |
| 36 | + GlobalMaterialLocalizations.delegate, |
| 37 | + GlobalWidgetsLocalizations.delegate, |
| 38 | + ], |
| 39 | + home: Builder( |
| 40 | + builder: (BuildContext context) { |
| 41 | + expect(FormBuilderValidators.maxLength(context, 5)('something long'), equals('Value must have a length less than or equal to 5')); |
| 42 | + expect(FormBuilderValidators.maxLength(context, 5)('two'), equals(null)); |
| 43 | + // expect(FormBuilderValidators.maxLength(context, 5)(5), equals(null)); |
| 44 | + |
| 45 | + // The builder function must return a widget. |
| 46 | + return Placeholder(); |
| 47 | + }, |
| 48 | + ), |
| 49 | + ), |
| 50 | + ); |
17 | 51 | });
|
18 | 52 |
|
19 |
| - test('FormBuilderValidators.email', () { |
20 |
| - expect(FormBuilderValidators.email()('john@flutter'), isNotNull); |
21 |
| - expect( FormBuilderValidators. email()( '[email protected]'), isNull); |
22 |
| - expect( FormBuilderValidators. email()( ' [email protected] '), isNull); |
23 |
| - expect( FormBuilderValidators. email()( '[email protected] '), isNull); |
24 |
| - expect( FormBuilderValidators. email()( ' [email protected]'), isNull); |
25 |
| - expect(FormBuilderValidators.email()(null), isNull); |
26 |
| - expect(FormBuilderValidators.email()(''),isNull); |
| 53 | + testWidgets('FormBuilderValidators.email', (WidgetTester tester) async { |
| 54 | + await tester.pumpWidget( |
| 55 | + MaterialApp( |
| 56 | + localizationsDelegates: [ |
| 57 | + FormBuilderLocalizationsDelegate(), |
| 58 | + GlobalMaterialLocalizations.delegate, |
| 59 | + GlobalWidgetsLocalizations.delegate, |
| 60 | + ], |
| 61 | + home: Builder( |
| 62 | + builder: (BuildContext context) { |
| 63 | + expect(FormBuilderValidators.email(context)('john@flutter'), isNotNull); |
| 64 | + expect( FormBuilderValidators. email(context)( '[email protected]'), isNull); |
| 65 | + expect( FormBuilderValidators. email(context)( ' [email protected] '), isNull); |
| 66 | + expect( FormBuilderValidators. email(context)( '[email protected] '), isNull); |
| 67 | + expect( FormBuilderValidators. email(context)( ' [email protected]'), isNull); |
| 68 | + expect(FormBuilderValidators.email(context)(null), isNull); |
| 69 | + expect(FormBuilderValidators.email(context)(''),isNull); |
| 70 | + |
| 71 | + // The builder function must return a widget. |
| 72 | + return Placeholder(); |
| 73 | + }, |
| 74 | + ), |
| 75 | + ), |
| 76 | + ); |
27 | 77 | });
|
28 | 78 |
|
29 |
| - test('FormBuilderValidators.max', () { |
30 |
| - expect(FormBuilderValidators.max(20)('70'), isNotNull); |
31 |
| - expect(FormBuilderValidators.max(30)(70), isNotNull); |
32 |
| - expect(FormBuilderValidators.max(30)(20), isNull); |
| 79 | + testWidgets('FormBuilderValidators.max', (WidgetTester tester) async { |
| 80 | + await tester.pumpWidget( |
| 81 | + MaterialApp( |
| 82 | + localizationsDelegates: [ |
| 83 | + FormBuilderLocalizationsDelegate(), |
| 84 | + GlobalMaterialLocalizations.delegate, |
| 85 | + GlobalWidgetsLocalizations.delegate, |
| 86 | + ], |
| 87 | + home: Builder( |
| 88 | + builder: (BuildContext context) { |
| 89 | + expect(FormBuilderValidators.max(context, 20)('70'), isNotNull); |
| 90 | + expect(FormBuilderValidators.max(context,30)(70), isNotNull); |
| 91 | + expect(FormBuilderValidators.max(context,30)(20), isNull); |
| 92 | + |
| 93 | + // The builder function must return a widget. |
| 94 | + return Placeholder(); |
| 95 | + }, |
| 96 | + ), |
| 97 | + ), |
| 98 | + ); |
33 | 99 | });
|
34 | 100 |
|
35 |
| - test('FormBuilderValidators.min', () { |
36 |
| - expect(FormBuilderValidators.min(30)('10'), isNotNull); |
37 |
| - expect(FormBuilderValidators.min(30)(10), isNotNull); |
38 |
| - expect(FormBuilderValidators.min(30)(70), isNull); |
| 101 | + testWidgets('FormBuilderValidators.min', (WidgetTester tester) async { |
| 102 | + await tester.pumpWidget( |
| 103 | + MaterialApp( |
| 104 | + localizationsDelegates: [ |
| 105 | + FormBuilderLocalizationsDelegate(), |
| 106 | + GlobalMaterialLocalizations.delegate, |
| 107 | + GlobalWidgetsLocalizations.delegate, |
| 108 | + ], |
| 109 | + home: Builder( |
| 110 | + builder: (BuildContext context) { |
| 111 | + expect(FormBuilderValidators.min(context,30)('10'), isNotNull); |
| 112 | + expect(FormBuilderValidators.min(context,30)(10), isNotNull); |
| 113 | + expect(FormBuilderValidators.min(context,30)(70), isNull); |
| 114 | + |
| 115 | + // The builder function must return a widget. |
| 116 | + return Placeholder(); |
| 117 | + }, |
| 118 | + ), |
| 119 | + ), |
| 120 | + ); |
39 | 121 | });
|
40 | 122 |
|
41 |
| - test('FormBuilderValidators.url', () { |
42 |
| - expect(FormBuilderValidators.url()(null), isNull); |
43 |
| - expect(FormBuilderValidators.url()('https://www.google.com'), isNull); |
44 |
| - expect(FormBuilderValidators.url()('www.google.com'), isNull); |
45 |
| - expect(FormBuilderValidators.url()('google.com'), isNull); |
46 |
| - expect(FormBuilderValidators.url()('http://google.com'), isNull); |
47 |
| - expect(FormBuilderValidators.url()('.com'), isNotNull); |
48 |
| - expect(FormBuilderValidators.url(protocols: ['https', 'http'], errorText: 'Only HTTP and HTTPS allowed')('ftp://www.google.com'), isNotNull); |
| 123 | + testWidgets('FormBuilderValidators.url', (WidgetTester tester) async { |
| 124 | + await tester.pumpWidget( |
| 125 | + MaterialApp( |
| 126 | + localizationsDelegates: [ |
| 127 | + FormBuilderLocalizationsDelegate(), |
| 128 | + GlobalMaterialLocalizations.delegate, |
| 129 | + GlobalWidgetsLocalizations.delegate, |
| 130 | + ], |
| 131 | + home: Builder( |
| 132 | + builder: (BuildContext context) { |
| 133 | + expect(FormBuilderValidators.url(context)(null), isNull); |
| 134 | + expect(FormBuilderValidators.url(context)('https://www.google.com'), isNull); |
| 135 | + expect(FormBuilderValidators.url(context)('www.google.com'), isNull); |
| 136 | + expect(FormBuilderValidators.url(context)('google.com'), isNull); |
| 137 | + expect(FormBuilderValidators.url(context)('http://google.com'), isNull); |
| 138 | + expect(FormBuilderValidators.url(context)('.com'), isNotNull); |
| 139 | + expect(FormBuilderValidators.url(context, protocols: ['https', 'http'], errorText: 'Only HTTP and HTTPS allowed')('ftp://www.google.com'), isNotNull); |
| 140 | + |
| 141 | + // The builder function must return a widget. |
| 142 | + return Placeholder(); |
| 143 | + }, |
| 144 | + ), |
| 145 | + ), |
| 146 | + ); |
49 | 147 | });
|
50 | 148 |
|
51 |
| - test('FormBuilderValidators.IP', () { |
52 |
| - expect(FormBuilderValidators.IP()(null), isNull); |
53 |
| - expect(FormBuilderValidators.IP()('192.168.0.1'), isNull); |
54 |
| - expect(FormBuilderValidators.IP()('256.168.0.1'), isNotNull); |
| 149 | + testWidgets('FormBuilderValidators.IP', (WidgetTester tester) async { |
| 150 | + await tester.pumpWidget( |
| 151 | + MaterialApp( |
| 152 | + localizationsDelegates: [ |
| 153 | + FormBuilderLocalizationsDelegate(), |
| 154 | + GlobalMaterialLocalizations.delegate, |
| 155 | + GlobalWidgetsLocalizations.delegate, |
| 156 | + ], |
| 157 | + home: Builder( |
| 158 | + builder: (BuildContext context) { |
| 159 | + expect(FormBuilderValidators.IP(context)(null), isNull); |
| 160 | + expect(FormBuilderValidators.IP(context)('192.168.0.1'), isNull); |
| 161 | + expect(FormBuilderValidators.IP(context)('256.168.0.1'), isNotNull); |
| 162 | + |
| 163 | + // The builder function must return a widget. |
| 164 | + return Placeholder(); |
| 165 | + }, |
| 166 | + ), |
| 167 | + ), |
| 168 | + ); |
55 | 169 | });
|
56 | 170 | }
|
0 commit comments