Skip to content
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

Add built_value test macro fields. #145

Merged
merged 1 commit into from
Nov 25, 2024
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 @@ -2,24 +2,89 @@ part of 'package:foo/built_value/built_value_test.dart';

import 'package:_test_macros/built_value.dart' as prefix0;
import 'package:foo/built_value/built_value_test.dart' as prefix1;
import 'dart:core' as prefix2;

@prefix0.BuiltValueBuilder()
class EmptyBuilder {}

@prefix0.BuiltValueBuilder()
class PrimitiveFieldsBuilder {}

@prefix0.BuiltValueBuilder()
class NestedFieldsBuilder {}

augment class Empty {
Empty([void Function(prefix1.EmptyBuilder)? updates]) {}
factory Empty([void Function(prefix1.EmptyBuilder)? updates]) =>
(prefix1.EmptyBuilder()..update(updates)).build();
Empty._() {}

prefix1.EmptyBuilder toBuilder() => prefix1.EmptyBuilder()..replace(this);
prefix1.Empty rebuild(void Function(prefix1.EmptyBuilder) updates) =>
(toBuilder()..update(updates)).build();

bool operator==(Object other) => other is prefix1.Empty;
prefix2.int get hashCode => prefix2.Object.hashAll([]);

prefix2.bool operator==(prefix2.Object other) =>
other is prefix1.Empty;

prefix2.String toString() => 'Empty()';

}
augment class PrimitiveFields {
factory PrimitiveFields([void Function(prefix1.PrimitiveFieldsBuilder)? updates]) =>
(prefix1.PrimitiveFieldsBuilder()..update(updates)).build();
PrimitiveFields._({required this.anInt,required this.aString,}) {}

prefix1.PrimitiveFieldsBuilder toBuilder() => prefix1.PrimitiveFieldsBuilder()..replace(this);
prefix1.PrimitiveFields rebuild(void Function(prefix1.PrimitiveFieldsBuilder) updates) =>
(toBuilder()..update(updates)).build();

prefix2.int get hashCode => prefix2.Object.hashAll([anInt,aString,]);

prefix2.bool operator==(prefix2.Object other) =>
other is prefix1.PrimitiveFields&& anInt == other.anInt&& aString == other.aString;

prefix2.String toString() => 'PrimitiveFields(anInt: $anInt, aString: $aString)';

}
augment class NestedFields {
factory NestedFields([void Function(prefix1.NestedFieldsBuilder)? updates]) =>
(prefix1.NestedFieldsBuilder()..update(updates)).build();
NestedFields._({required this.aPrimitiveFields,required this.aString,}) {}

prefix1.NestedFieldsBuilder toBuilder() => prefix1.NestedFieldsBuilder()..replace(this);
prefix1.NestedFields rebuild(void Function(prefix1.NestedFieldsBuilder) updates) =>
(toBuilder()..update(updates)).build();

prefix2.int get hashCode => prefix2.Object.hashAll([aPrimitiveFields,aString,]);

prefix2.bool operator==(prefix2.Object other) =>
other is prefix1.NestedFields&& aPrimitiveFields == other.aPrimitiveFields&& aString == other.aString;

prefix2.String toString() => 'NestedFields(aPrimitiveFields: $aPrimitiveFields, aString: $aString)';

}
augment class NestedFieldsBuilder {
prefix1.PrimitiveFieldsBuilder aPrimitiveFields = prefix1.PrimitiveFieldsBuilder();prefix2.String? aString;

void replace(prefix1.NestedFields other) { this.aPrimitiveFields = other.aPrimitiveFields.toBuilder();this.aString = other.aString; }
void update(void Function(prefix1.NestedFieldsBuilder)? updates) => updates?.call(this);
prefix1.NestedFields build() => prefix1.NestedFields._(aPrimitiveFields: aPrimitiveFields.build(),aString: aString!,);

}
augment class PrimitiveFieldsBuilder {
prefix2.int? anInt;prefix2.String? aString;

void replace(prefix1.PrimitiveFields other) { this.anInt = other.anInt;this.aString = other.aString; }
void update(void Function(prefix1.PrimitiveFieldsBuilder)? updates) => updates?.call(this);
prefix1.PrimitiveFields build() => prefix1.PrimitiveFields._(anInt: anInt!,aString: aString!,);

}
augment class EmptyBuilder {
void replace(prefix1.Empty other) {}
void update(void Function(prefix1.EmptyBuilder) updates) => updates(this);


void replace(prefix1.Empty other) { }
void update(void Function(prefix1.EmptyBuilder)? updates) => updates?.call(this);
prefix1.Empty build() => prefix1.Empty._();

}
66 changes: 60 additions & 6 deletions goldens/foo/lib/built_value/built_value_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,71 @@ import 'package:test/test.dart';
void main() {
group('Empty class', () {
test('instantiation, builder, rebuild, comparison', () {
final empty = Empty();
final empty2 = empty.rebuild((b) {});
expect(empty2, empty);
final value = Empty();
final sameValue = value.rebuild((b) {});
expect(sameValue, value);

// analyzer: The function 'EmptyBuilder' isn't defined.
// final emptyBuilder = EmptyBuilder();
// final empty3 = emptyBuilder.build();
// expect(empty3, empty);
// final valueBuilder = EmptyBuilder();
// final value3 = valueBuilder.build();
// expect(value3, value);
});
});

group('Class with primitive fields', () {
test('instantiation, builder, rebuild, comparison, hash code, '
'toString', () {
final value = PrimitiveFields(
(b) =>
b
..anInt = 3
..aString = 'four',
);
final value2 = value.rebuild(
(b) =>
b
..anInt = 4
..aString = 'five',
);
expect(value2, isNot(value));
expect(value2.hashCode, isNot(value.hashCode));
expect(value2.toString(), 'PrimitiveFields(anInt: 4, aString: five)');

final sameValue = value.rebuild((b) => b);
expect(sameValue, value);
expect(sameValue.hashCode, value.hashCode);
});
});

group('Class with nested fields', () {
test('has nested builder', () {
final value = NestedFields(
(b) =>
b
..aPrimitiveFields.anInt = 3
..aPrimitiveFields.aString = 'four'
..aString = 'five',
);
expect(
value.toString(),
'NestedFields(aPrimitiveFields: PrimitiveFields('
'anInt: 3, aString: four), aString: five)',
);
});
});
}

@BuiltValue()
class Empty {}

@BuiltValue()
class PrimitiveFields {
final int anInt;
final String aString;
}

@BuiltValue()
class NestedFields {
final PrimitiveFields aPrimitiveFields;
final String aString;
}
26 changes: 16 additions & 10 deletions goldens/foo/lib/foo.analyzer.json
Original file line number Diff line number Diff line change
Expand Up @@ -204,11 +204,14 @@
"type": "ConstructorInvocation",
"value": {
"type": {
"reference": {
"type": "ClassReference",
"value": {}
},
"typeArguments": []
"type": "NamedTypeAnnotation",
"value": {
"reference": {
"type": "ClassReference",
"value": {}
},
"typeArguments": []
}
},
"constructor": {
"type": "ConstructorReference",
Expand Down Expand Up @@ -265,11 +268,14 @@
"type": "ConstructorInvocation",
"value": {
"type": {
"reference": {
"type": "ClassReference",
"value": {}
},
"typeArguments": []
"type": "NamedTypeAnnotation",
"value": {
"reference": {
"type": "ClassReference",
"value": {}
},
"typeArguments": []
}
},
"constructor": {
"type": "ConstructorReference",
Expand Down
4 changes: 2 additions & 2 deletions goldens/foo/lib/literal_params.analyzer.augmentations
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ augment class Foo {
// aNum: 8.0, double
// aDouble: 9.0, double
// aString: 10, String
// anObject: {type: {reference: {type: ClassReference, value: {}}, typeArguments: []}, constructor: {type: ConstructorReference, value: {}}, arguments: [{type: NamedArgument, value: {name: a, expression: {type: BooleanLiteral, value: {text: true}}}}, {type: NamedArgument, value: {name: b, expression: {type: BooleanLiteral, value: {text: false}}}}]}, String
// anObject: {type: {type: NamedTypeAnnotation, value: {reference: {type: ClassReference, value: {}}, typeArguments: []}}, constructor: {type: ConstructorReference, value: {}}, arguments: [{type: NamedArgument, value: {name: a, expression: {type: BooleanLiteral, value: {text: true}}}}, {type: NamedArgument, value: {name: b, expression: {type: BooleanLiteral, value: {text: false}}}}]}, String
// ints: [11, 12], List<Object>
// nums: [13.0, 14], List<Object>
// doubles: [15.0, 16], List<Object>
// strings: [17, eighteen], List<Object>
// objects: [19, {type: {reference: {type: ClassReference, value: {}}, typeArguments: []}, constructor: {type: ConstructorReference, value: {}}, arguments: [{type: NamedArgument, value: {name: a, expression: {type: BooleanLiteral, value: {text: true}}}}, {type: NamedArgument, value: {name: b, expression: {type: BooleanLiteral, value: {text: false}}}}]}], List<Object>
// objects: [19, {type: {type: NamedTypeAnnotation, value: {reference: {type: ClassReference, value: {}}, typeArguments: []}}, constructor: {type: ConstructorReference, value: {}}, arguments: [{type: NamedArgument, value: {name: a, expression: {type: BooleanLiteral, value: {text: true}}}}, {type: NamedArgument, value: {name: b, expression: {type: BooleanLiteral, value: {text: false}}}}]}], List<Object>
}
26 changes: 16 additions & 10 deletions goldens/foo/lib/metadata.analyzer.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,14 @@
"type": "ConstructorInvocation",
"value": {
"type": {
"reference": {
"type": "ClassReference",
"value": {}
},
"typeArguments": []
"type": "NamedTypeAnnotation",
"value": {
"reference": {
"type": "ClassReference",
"value": {}
},
"typeArguments": []
}
},
"constructor": {
"type": "ConstructorReference",
Expand All @@ -43,11 +46,14 @@
"type": "ConstructorInvocation",
"value": {
"type": {
"reference": {
"type": "ClassReference",
"value": {}
},
"typeArguments": []
"type": "NamedTypeAnnotation",
"value": {
"reference": {
"type": "ClassReference",
"value": {}
},
"typeArguments": []
}
},
"constructor": {
"type": "ConstructorReference",
Expand Down
2 changes: 1 addition & 1 deletion pkgs/_analyzer_cfe_macros/lib/metadata_converter.dart
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,7 @@ T? convert<T>(Object? object) => switch (object) {
as T,
front_end.ConstructorInvocation o =>
dart_model.ConstructorInvocation(
type: convert(o.type),
type: convertToTypeAnnotation(o.type),
constructor: convertToReference(o.constructor),
arguments: convert(o.arguments),
)
Expand Down
Loading