这是indexloc提供的服务,不要输入任何密码
Skip to content

Proper hash code for classes with >20 properties #17

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

Merged
merged 1 commit into from
Jun 2, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ Method generateHashCodeMethod({
refer(
'Object',
'dart:core',
).property('hash').call(hashArgs, {}, []).returned.statement,
).property('hashAll').call([literalList(hashArgs)]).returned.statement,
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -327,7 +327,7 @@ void main() {
const expectedHashCode = '''
@override
int get hashCode {
return Object.hash(base, mixin);
return Object.hashAll([base, mixin]);
}
''';

Expand Down Expand Up @@ -472,7 +472,7 @@ void main() {
const expectedHashCode = '''
@override
int get hashCode {
return Object.hash(status, string);
return Object.hashAll([status, string]);
}
''';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ void main() {

const expectedMethod = '''
@override
int get hashCode { return Object.hash(name, age); }
int get hashCode { return Object.hashAll([name, age]); }
''';

final generatedClass = generator.generateClass(model);
Expand Down Expand Up @@ -102,7 +102,7 @@ void main() {

const expectedMethod = '''
@override
int get hashCode { return Object.hash(id, name, email, age); }
int get hashCode { return Object.hashAll([id, name, email, age]); }
''';

final generatedClass = generator.generateClass(model);
Expand Down Expand Up @@ -136,7 +136,7 @@ void main() {

const expectedMethod = '''
@override
int get hashCode { return Object.hash(name, bio); }
int get hashCode { return Object.hashAll([name, bio]); }
''';

final generatedClass = generator.generateClass(model);
Expand Down Expand Up @@ -170,7 +170,7 @@ void main() {

const expectedMethod = '''
@override
int get hashCode { return Object.hash(firstName, lastName); }
int get hashCode { return Object.hashAll([firstName, lastName]); }
''';

final generatedClass = generator.generateClass(model);
Expand Down Expand Up @@ -209,7 +209,7 @@ void main() {
@override
int get hashCode {
const deepEquals = DeepCollectionEquality();
return Object.hash(name, deepEquals.hash(tags));
return Object.hashAll([name, deepEquals.hash(tags)]);
}
''';

Expand Down Expand Up @@ -252,7 +252,7 @@ void main() {
@override
int get hashCode {
const deepEquals = DeepCollectionEquality();
return Object.hash(name, deepEquals.hash(nestedList));
return Object.hashAll([name, deepEquals.hash(nestedList)]);
}
''';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -311,7 +311,7 @@ void main() {
const expectedMethod = '''
@override
int get hashCode {
return Object.hash(xTest, body);
return Object.hashAll([xTest, body]);
}
''';

Expand Down Expand Up @@ -361,7 +361,7 @@ void main() {
const expectedMethod = '''
@override
int get hashCode {
return Object.hash(xTest, body, xOther);
return Object.hashAll([xTest, body, xOther]);
}
''';

Expand Down Expand Up @@ -406,7 +406,7 @@ void main() {
@override
int get hashCode {
const deepEquals = DeepCollectionEquality();
return Object.hash(deepEquals.hash(xList), body);
return Object.hashAll([deepEquals.hash(xList), body]);
}
''';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,58 @@ void main() {
@override
int get hashCode {
const deepEquals = DeepCollectionEquality();
return Object.hash(id, deepEquals.hash(items), name);
return Object.hashAll([id, deepEquals.hash(items), name]);
}
''';

expect(
collapseWhitespace(formatMethod(method)),
contains(collapseWhitespace(expectedMethod)),
);
});

test('generates hash code method for class with many properties', () {
final method = generateHashCodeMethod(
properties: List.generate(
25,
(i) => (
normalizedName: 'prop$i',
hasCollectionValue: i.isEven,
),
),
);

const expectedMethod = '''
@override
int get hashCode {
const deepEquals = DeepCollectionEquality();
return Object.hashAll([
deepEquals.hash(prop0),
prop1,
deepEquals.hash(prop2),
prop3,
deepEquals.hash(prop4),
prop5,
deepEquals.hash(prop6),
prop7,
deepEquals.hash(prop8),
prop9,
deepEquals.hash(prop10),
prop11,
deepEquals.hash(prop12),
prop13,
deepEquals.hash(prop14),
prop15,
deepEquals.hash(prop16),
prop17,
deepEquals.hash(prop18),
prop19,
deepEquals.hash(prop20),
prop21,
deepEquals.hash(prop22),
prop23,
deepEquals.hash(prop24),
]);
}
''';

Expand Down