-
Notifications
You must be signed in to change notification settings - Fork 7
test: Add comprehensive tests for luthor_generator #117
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 7 commits
a668a69
3b5b75f
a677edc
0d6cd22
b52e0c9
577c76a
90072b7
b8e1267
93ebb0a
d984f2c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,5 +3,13 @@ builders: | |
| import: "package:luthor_generator/builder.dart" | ||
| builder_factories: ["luthorBuilder"] | ||
| build_extensions: { ".dart": [".luthor.g.part"] } | ||
| auto_apply: dependents | ||
| auto_apply: all_packages | ||
|
||
| build_to: cache | ||
| applies_builders: ["source_gen:combining_builder"] | ||
|
|
||
| targets: | ||
| $default: | ||
| sources: | ||
| - lib/** | ||
| - test/** | ||
| - $package$ | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| targets: | ||
| $default: | ||
| builders: | ||
| luthor_generator:luthor: | ||
| enabled: true |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,97 @@ | ||
| import 'package:luthor/luthor.dart'; | ||
| import 'package:test/test.dart'; | ||
|
|
||
| import 'fixtures/custom_validator_model.dart'; | ||
|
|
||
| void main() { | ||
| group('Custom Validator Tests', () { | ||
| test('should generate schema with custom validators', () { | ||
| expect($CustomValidatorModelSchema, isA<Validator>()); | ||
| }); | ||
|
|
||
| test('should validate @WithCustomValidator - valid case', () { | ||
| final validData = { | ||
| 'customField': 'valid', | ||
| 'matchField': 'test', | ||
| 'confirmField': 'test', | ||
| }; | ||
|
|
||
| final result = $CustomValidatorModelValidate(validData); | ||
|
|
||
| switch (result) { | ||
| case SchemaValidationSuccess(data: final model): | ||
| expect(model.customField, equals('valid')); | ||
| case SchemaValidationError(errors: final errors): | ||
| fail('Should not have validation errors: $errors'); | ||
| } | ||
| }); | ||
|
|
||
| test( | ||
| 'should validate @WithCustomValidator - invalid case with custom message', | ||
| () { | ||
| final invalidData = { | ||
| 'customField': 'invalid', | ||
| 'matchField': 'test', | ||
| 'confirmField': 'test', | ||
| }; | ||
|
|
||
| final result = $CustomValidatorModelValidate(invalidData); | ||
|
|
||
| switch (result) { | ||
| case SchemaValidationSuccess(data: _): | ||
| fail('Should have validation error for custom validator'); | ||
| case SchemaValidationError(errors: final errors): | ||
| expect(errors.keys, contains('customField')); | ||
| expect(errors['customField'], isNotNull); | ||
| expect( | ||
| (errors['customField']! as List).first, | ||
| equals('Value must be "valid"'), | ||
| ); | ||
| } | ||
| }, | ||
| ); | ||
|
|
||
| test('should validate @WithSchemaCustomValidator - matching fields', () { | ||
| final validData = { | ||
| 'customField': 'valid', | ||
| 'matchField': 'password123', | ||
| 'confirmField': 'password123', | ||
| }; | ||
|
|
||
| final result = $CustomValidatorModelValidate(validData); | ||
|
|
||
| switch (result) { | ||
| case SchemaValidationSuccess(data: final model): | ||
| expect(model.matchField, equals('password123')); | ||
| expect(model.confirmField, equals('password123')); | ||
| case SchemaValidationError(errors: final errors): | ||
| fail('Should not have validation errors: $errors'); | ||
| } | ||
| }); | ||
|
|
||
| test( | ||
| 'should validate @WithSchemaCustomValidator - non-matching fields', | ||
| () { | ||
| final invalidData = { | ||
| 'customField': 'valid', | ||
| 'matchField': 'password123', | ||
| 'confirmField': 'different', | ||
| }; | ||
|
|
||
| final result = $CustomValidatorModelValidate(invalidData); | ||
|
|
||
| switch (result) { | ||
| case SchemaValidationSuccess(data: _): | ||
| fail('Should have validation error for non-matching fields'); | ||
| case SchemaValidationError(errors: final errors): | ||
| expect(errors.keys, contains('confirmField')); | ||
| expect(errors['confirmField'], isNotNull); | ||
| expect( | ||
| (errors['confirmField']! as List).first, | ||
| equals('Fields must match'), | ||
| ); | ||
| } | ||
| }, | ||
| ); | ||
| }); | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| import 'package:freezed_annotation/freezed_annotation.dart'; | ||
| import 'package:luthor/luthor.dart'; | ||
|
|
||
| part 'annotated_model.freezed.dart'; | ||
| part 'annotated_model.g.dart'; | ||
|
|
||
| @luthor | ||
| @freezed | ||
| abstract class AnnotatedModel with _$AnnotatedModel { | ||
| const factory AnnotatedModel({ | ||
| @IsEmail() required String email, | ||
| @HasMin(8) @HasMax(100) required String password, | ||
| @HasMin(18) @HasMax(120) required int age, | ||
| @HasMinDouble(0.0) @HasMaxDouble(100.0) required double score, | ||
| @StartsWith('https://') String? website, | ||
| @EndsWith('.com') String? domain, | ||
| @Contains('test') String? testField, | ||
| @IsUuid() required String uuid, | ||
| }) = _AnnotatedModel; | ||
|
|
||
| factory AnnotatedModel.fromJson(Map<String, dynamic> json) => | ||
| _$AnnotatedModelFromJson(json); | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Give this a name
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added name "Build generator test fixtures" in commit 93ebb0a.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@copilot I want a name to the above like
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@copilot See above comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added "Setup project" name to the step in commit d984f2c.