Skip to content

Commit

Permalink
Generated operator == now takes in an Object instead of dynamic when …
Browse files Browse the repository at this point in the history
…making the comparison. (#724)
  • Loading branch information
diegotori authored Feb 12, 2024
1 parent 3c162e2 commit 2a5d673
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/src/code_generators/swagger_models_generator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -1551,7 +1551,7 @@ $copyWithMethod

return '''
@override
bool operator ==(dynamic other) {
bool operator ==(Object other) {
return identical(this, other) ||
(other is $validatedClassName &&
$checks);
Expand Down
67 changes: 67 additions & 0 deletions test/generator_tests/models_generator_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,14 @@ void main() {
),
);

final generator3 = SwaggerModelsGeneratorV2(
GeneratorOptions(
inputFolder: '',
outputFolder: '',
overrideEqualsAndHashcode: false,
),
);

group('generateDefaultValueFromMap', () {
test('Should return default value', () {
const defaultValue = 'true';
Expand Down Expand Up @@ -514,6 +522,65 @@ void main() {
});
});

group('generateEqualsOverride', () {
test(
"Should not return generated equals due to overrideEqualsAndHashcode set to false",
() {
const className = 'Animals';
final result = generator3.generateEqualsOverride(
"final String foo;",
className,
generator3.options,
);

expect(result, isEmpty);
});

test("Should not return generated equals due to empty properties", () {
const className = 'Animals';
final result = generator3.generateEqualsOverride(
"",
className,
generator3.options,
);

expect(result, isEmpty);
});

test("Should not return generated equals due to non-formatted properties",
() {
const className = 'Animals';
final result = generator3.generateEqualsOverride(
"not_a_property",
className,
generator3.options,
);

expect(result, isEmpty);
});

test("Should return generated equals", () {
const className = 'Animals';
final expected = '''
@override
bool operator ==(Object other) {
return identical(this, other) ||
(other is Animals &&
(identical(other.foo, foo) ||
const DeepCollectionEquality().equals(other.foo, foo))
);
}
''';
final result = generator.generateEqualsOverride(
"final String foo;",
className,
generator.options,
);

expect(result, expected);
});
});

group('Tests for getValidatedClassName', () {
test('Should', () {
final result = generator.getValidatedClassName('Request');
Expand Down

0 comments on commit 2a5d673

Please sign in to comment.