Compare commits

..

4 Commits

12 changed files with 576 additions and 11 deletions

View File

@ -144,6 +144,15 @@ class WyattFormImpl extends WyattForm {
WyattForm operationWith(FormOperation operation, WyattForm other) => WyattForm operationWith(FormOperation operation, WyattForm other) =>
operation.call(this, other); operation.call(this, other);
@override
bool isPureInput(String key) => inputOf(key).validator.pure;
@override
bool isValidInput(String key) => inputOf(key).validator.valid;
@override
bool isInvalidInput(String key) => inputOf(key).validator.invalid;
@override @override
List<Object?> get props => [_inputs, _name, _validator]; List<Object?> get props => [_inputs, _name, _validator];

View File

@ -30,12 +30,6 @@ class FormDifference extends FormOperation {
FormInput<dynamic, FormInputValidator<dynamic, ValidationError>, FormInput<dynamic, FormInputValidator<dynamic, ValidationError>,
dynamic>>[]; dynamic>>[];
for (final i in b.inputs) {
if (!a.containsKey(i.key)) {
inputs.add(i);
}
}
for (final i in a.inputs) { for (final i in a.inputs) {
if (!b.containsKey(i.key)) { if (!b.containsKey(i.key)) {
inputs.add(i); inputs.add(i);

View File

@ -32,7 +32,11 @@ class FormIntersection extends FormOperation {
for (final i in a.inputs) { for (final i in a.inputs) {
if (b.containsKey(i.key)) { if (b.containsKey(i.key)) {
inputs.add(i); if (i.validator.pure) {
inputs.add(b.inputOf(i.key));
} else {
inputs.add(i);
}
} }
} }

View File

@ -37,6 +37,13 @@ class FormUnion extends FormOperation {
for (final i in b.inputs) { for (final i in b.inputs) {
if (!a.containsKey(i.key)) { if (!a.containsKey(i.key)) {
inputs.add(i); inputs.add(i);
} else {
// if a input is pure, but not b, replace a one with b
if (a.isPureInput(i.key)) {
inputs
..remove(a.inputOf(i.key))
..add(i);
}
} }
} }

View File

@ -22,11 +22,11 @@ import 'package:wyatt_form_bloc/src/domain/input_validators/form_input_validator
/// {@endtemplate} /// {@endtemplate}
class Boolean extends NullableValidator<bool, ValidationStandardError> { class Boolean extends NullableValidator<bool, ValidationStandardError> {
/// {@macro boolean} /// {@macro boolean}
const Boolean.pure({bool? defaultValue = false}) const Boolean.pure({bool defaultValue = false})
: super.pure(defaultValue ?? false); : super.pure(defaultValue);
/// {@macro boolean} /// {@macro boolean}
const Boolean.dirty({bool value = false}) : super.dirty(value); const Boolean.dirty({bool? value}) : super.dirty(value);
@override @override
ValidationStandardError get onNull => ValidationStandardError.invalid; ValidationStandardError get onNull => ValidationStandardError.invalid;

View File

@ -35,7 +35,6 @@ class FormInput<
FormInput( FormInput(
this.key, this.key,
this.validator, { this.validator, {
// ignore: avoid_redundant_argument_values
FormInputMetadata<Extra>? metadata, FormInputMetadata<Extra>? metadata,
}) { }) {
this.metadata = metadata ?? FormInputMetadata<Extra>(); this.metadata = metadata ?? FormInputMetadata<Extra>();

View File

@ -58,4 +58,8 @@ abstract class WyattForm extends Equatable {
WyattForm clone(); WyattForm clone();
WyattForm operationWith(FormOperation operation, WyattForm other); WyattForm operationWith(FormOperation operation, WyattForm other);
WyattForm reset(); WyattForm reset();
bool isPureInput(String key);
bool isValidInput(String key);
bool isInvalidInput(String key);
} }

View File

@ -0,0 +1,79 @@
// Copyright (C) 2022 WYATT GROUP
// Please see the AUTHORS file for details.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
import 'package:flutter_test/flutter_test.dart';
import 'package:wyatt_form_bloc/wyatt_form_bloc.dart';
void main() {
group('FormInput', () {
test('contains key', () {
final input = FormInput<String?, Name, void>('key', const Name.pure());
expect(input.key, 'key');
});
test('contains validator', () {
final input = FormInput<String?, Name, void>('key', const Name.pure());
expect(input.validator, const Name.pure());
});
test('contains metadata', () {
final input = FormInput<String?, Name, int>(
'key',
const Name.pure(),
metadata: const FormInputMetadata<int>(
extra: 42,
),
);
expect(input.metadata.extra, 42);
});
test('== to another form input', () {
final input1 = FormInput<String?, Name, void>(
'key',
const Name.pure(),
metadata: const FormInputMetadata<int>(
extra: 42,
),
);
final input2 = FormInput<String?, Name, void>(
'key',
const Name.pure(),
metadata: const FormInputMetadata<int>(
extra: 42,
),
);
expect(input1 == input2, isTrue);
});
test('!= to a different form input', () {
final input1 = FormInput<String?, Name, void>(
'key',
const Name.pure(),
metadata: const FormInputMetadata<int>(
extra: 43, // little difference here
),
);
final input2 = FormInput<String?, Name, void>(
'key',
const Name.pure(),
metadata: const FormInputMetadata<int>(
extra: 42,
),
);
expect(input1 == input2, isFalse);
});
});
}

View File

@ -0,0 +1,158 @@
// Copyright (C) 2022 WYATT GROUP
// Please see the AUTHORS file for details.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
import 'package:flutter_test/flutter_test.dart';
import 'package:wyatt_form_bloc/wyatt_form_bloc.dart';
void main() {
group('FormInputValidator', () {
group('Boolean', () {
test('is pure at the beginning', () {
const validator = Boolean.pure();
expect(validator.pure, true);
});
test('contains `false` at the beginning', () {
const validator = Boolean.pure();
expect(validator.pure, true);
expect(validator.value, false);
});
test('contains `true` when intialized with', () {
const validator = Boolean.pure(defaultValue: true);
expect(validator.pure, true);
expect(validator.value, true);
});
test('returns ValidationStandardError.invalid on null', () {
const validator = Boolean.dirty();
expect(validator.error, ValidationStandardError.invalid);
});
test('is invalid and contains `null` when update with `null`', () {
const validator = Boolean.dirty();
expect(validator.invalid, true);
expect(validator.value, null);
});
test('is valid and contains `true` when update with `true`', () {
const validator = Boolean.dirty(value: true);
expect(validator.valid, true);
expect(validator.value, true);
});
test('is valid and contains `false` when update with `false`', () {
const validator = Boolean.dirty(value: false);
expect(validator.valid, true);
expect(validator.value, false);
});
test('== to another boolean validator with the same attributes', () {
const validator1 = Boolean.dirty(value: false);
const validator2 = Boolean.dirty(value: false);
expect(validator1 == validator2, isTrue);
});
test('!= to another boolean validator with different attributes', () {
const validator1 = Boolean.dirty(value: false);
const validator2 = Boolean.dirty(value: true);
expect(validator1 == validator2, isFalse);
});
});
group('ConfirmedPassword', () {
test('is pure at the beginning', () {
const validator = ConfirmedPassword.pure();
expect(validator.pure, true);
});
test('contains `null` at the beginning', () {
const validator = ConfirmedPassword.pure();
expect(validator.pure, true);
expect(validator.value, null);
});
test('contains `pwd` when intialized with', () {
const validator = ConfirmedPassword.pure(defaultValue: 'pwd');
expect(validator.pure, true);
expect(validator.value, 'pwd');
});
test('returns ValidationStandardError.invalid on null', () {
const validator = ConfirmedPassword.dirty(password: 'pwd');
expect(validator.error, ValidationStandardError.invalid);
});
test('returns ValidationStandardError.notEqual on different values', () {
const validator =
ConfirmedPassword.dirty(password: 'pwd', value: 'incorrect');
expect(validator.error, ValidationStandardError.notEqual);
});
test('is invalid and contains `null` when update with `null`', () {
const validator = ConfirmedPassword.dirty(password: 'pwd');
expect(validator.invalid, true);
expect(validator.value, null);
});
test('is invalid and contains `incorrect` when update with `incorrect`',
() {
const validator =
ConfirmedPassword.dirty(password: 'pwd', value: 'incorrect');
expect(validator.invalid, true);
expect(validator.value, 'incorrect');
});
test('is valid and contains `pwd` when update with `pwd`', () {
const validator =
ConfirmedPassword.dirty(password: 'pwd', value: 'pwd');
expect(validator.valid, true);
expect(validator.value, 'pwd');
});
test(
'== to another confirmed password validator with the same attributes',
() {
const validator1 =
ConfirmedPassword.dirty(password: 'pwd', value: 'pwd');
const validator2 =
ConfirmedPassword.dirty(password: 'pwd', value: 'pwd');
expect(validator1 == validator2, isTrue);
});
test(
'!= to another confirmed password validator '
'with different attributes', () {
const validator1 =
ConfirmedPassword.dirty(password: 'pwd', value: 'pwd1');
const validator2 =
ConfirmedPassword.dirty(password: 'pwd', value: 'pwd2');
expect(validator1 == validator2, isFalse);
});
test('!= to another confirmed password validator with different password',
() {
const validator1 =
ConfirmedPassword.dirty(password: 'pwd1', value: 'pwd');
const validator2 =
ConfirmedPassword.dirty(password: 'pwd2', value: 'pwd');
expect(validator1 == validator2, isFalse);
});
});
});
// TODO(hpcl): add tests for all validators...
}

View File

@ -0,0 +1,144 @@
// Copyright (C) 2022 WYATT GROUP
// Please see the AUTHORS file for details.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
import 'package:flutter_test/flutter_test.dart';
import 'package:wyatt_form_bloc/wyatt_form_bloc.dart';
void main() {
final WyattForm formA = WyattFormImpl(
[
FormInput(
'email',
const Email.dirty('test@test.fr'),
),
FormInput(
'password',
const Password.pure(),
),
],
name: 'A',
);
final WyattForm formB = WyattFormImpl(
[
FormInput(
'email',
const Email.dirty('test2@test.fr'),
),
FormInput(
'password',
const Password.dirty('password1234'),
),
FormInput(
'bool',
const Boolean.pure(),
),
FormInput(
'phone',
const Phone.dirty('0000000000'),
),
],
name: 'B',
);
group('FormReplace', () {
test('returns B when called on (A,B)', () {
final result = const FormReplace().call(formA, formB);
expect(result, formB);
});
});
group('FormUnion', () {
final WyattForm union = WyattFormImpl(
[
FormInput(
'email',
const Email.dirty('test@test.fr'),
),
FormInput(
'password',
const Password.dirty('password1234'),
),
FormInput(
'bool',
const Boolean.pure(),
),
FormInput(
'phone',
const Phone.dirty('0000000000'),
),
],
name: 'A',
);
test('returns A union B when called on (A,B), with A name', () {
final result = const FormUnion().call(formA, formB);
expect(result, union);
});
});
group('FormIntersection', () {
final WyattForm intersection = WyattFormImpl(
[
FormInput(
'email',
const Email.dirty('test@test.fr'),
),
FormInput(
'password',
const Password.dirty('password1234'),
),
],
name: 'A',
);
test('returns A intersection B when called on (A,B), with A name', () {
final result = const FormIntersection().call(formA, formB);
expect(result, intersection);
});
});
group('FormDifference', () {
test('returns A - B when called on (A,B), with A name', () {
final result = const FormDifference().call(formA, formB);
expect(
result,
WyattFormImpl(
const [],
name: 'A',
),
);
});
final WyattForm difference = WyattFormImpl(
[
FormInput(
'bool',
const Boolean.pure(),
),
FormInput(
'phone',
const Phone.dirty('0000000000'),
),
],
name: 'B',
);
test('returns B - A when called on (B,A), with B name', () {
final result = const FormDifference().call(formB, formA);
expect(result, difference);
});
});
}

View File

@ -0,0 +1,167 @@
// Copyright (C) 2022 WYATT GROUP
// Please see the AUTHORS file for details.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
import 'package:flutter_test/flutter_test.dart';
import 'package:wyatt_form_bloc/wyatt_form_bloc.dart';
void main() {
group('WyattForm', () {
final WyattForm form = WyattFormImpl(
[
FormInput(
'email',
const Email.pure(),
),
FormInput(
'password',
const Password.pure(),
),
],
name: 'signInForm',
);
test('returns inputs', () {
final inputs = <
FormInput<dynamic, FormInputValidator<dynamic, ValidationError>,
dynamic>>[
FormInput(
'email',
const Email.pure(),
),
FormInput(
'password',
const Password.pure(),
),
];
expect(form.inputs, inputs);
});
test('returns validation strategy', () {
final strategy = form.formValidationStrategy;
expect(strategy, const EveryInputValidator());
});
test('returns validator list', () {
final validators = [
const Email.pure(),
const Password.pure(),
];
expect(form.asValidatorList<dynamic, ValidationError>(), validators);
});
test('returns name', () {
final name = form.name;
expect(name, 'signInForm');
});
group('containsKey', () {
test('returns `true` on key `email`', () {
expect(form.containsKey('email'), true);
});
test('returns `true` on key `password`', () {
expect(form.containsKey('password'), true);
});
test('return `false` on key `incorrect`', () {
expect(form.containsKey('incorrect'), false);
});
});
group('inputOf', () {
test('returns email input', () {
final input = FormInput<dynamic,
FormInputValidator<dynamic, ValidationError>, dynamic>(
'email',
const Email.pure(),
);
expect(form.inputOf('email'), input);
});
test('returns password input', () {
final input = FormInput<dynamic,
FormInputValidator<dynamic, ValidationError>, dynamic>(
'password',
const Password.pure(),
);
expect(form.inputOf('password'), input);
});
test('throw on unknown key', () {
expect(() => form.inputOf('incorrect'), throwsException);
});
});
group('errorOf', () {
final WyattForm errorForm = WyattFormImpl(
[
FormInput(
'email',
const Email.dirty('incorrect'),
),
FormInput(
'password',
const Password.dirty('toto1234'),
),
],
name: 'signInForm',
);
test('returns `invalid` on invalid input', () {
expect(errorForm.errorOf('email'), ValidationStandardError.invalid);
});
test('returns `null` on valid input', () {
expect(errorForm.errorOf('password'), isNull);
});
});
group('errorOf', () {
final WyattForm metadataForm = WyattFormImpl(
[
FormInput(
'email',
const Email.pure(),
metadata: const FormInputMetadata<int>(
extra: 1,
),
),
FormInput(
'password',
const Password.pure(),
metadata: const FormInputMetadata<int>(
extra: 2,
),
),
],
name: 'signInForm',
);
test('returns `1` on email extra', () {
expect(metadataForm.metadataOf<int>('email').extra, 1);
});
test('returns `2` on password extra', () {
expect(metadataForm.metadataOf<int>('password').extra, 2);
});
});
});
// TODO(hpcl): add more tests on WyattForm
}