Skip to content
Merged
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
57 changes: 51 additions & 6 deletions protoc_plugin/test/generated_message_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -794,12 +794,14 @@ void main() {
test('deepCopy', () {
final value1 = getAllSet();
final value2 = value1.deepCopy();
assertAllFieldsSet(value2);
expect(value2, isNot(same(value1)));
expect(
value2.optionalForeignMessage,
isNot(same(value1.optionalForeignMessage)),
);
testCopy(value1, value2);
});

test('clone', () {
final value1 = getAllSet();
// ignore: deprecated_member_use_from_same_package
final value2 = value1.clone();
testCopy(value1, value2);
});

test('deepCopy extensions', () {
Expand Down Expand Up @@ -832,3 +834,46 @@ void main() {
expect(decoded.sparseOutOfOrderEnums, SparseEnumOutOfOrder.values);
});
}

/// Shared test function to test `clone` and `deepCopy`.
///
/// `value2` should be the clone of `value1`, either with `clone` or `deepCopy`.
void testCopy(TestAllTypes value1, TestAllTypes value2) {
assertAllFieldsSet(value2);
expect(value2, isNot(same(value1)));

// Check that updates to value2 does not update value1.
value2.optionalInt32 += 1;
value2.optionalInt64 += 1;
value2.optionalUint32 += 1;
value2.optionalUint64 += 1;
value2.optionalSint32 += 1;
value2.optionalSint64 += 1;
value2.optionalFixed32 += 1;
value2.optionalFixed64 += 1;
value2.optionalSfixed32 += 1;
value2.optionalSfixed64 += 1;
value2.optionalFloat += 1;
value2.optionalDouble += 1;
value2.optionalBool = !value2.optionalBool;
value2.optionalString = "hi 1";

// Don't test `bytes` fields: the semantics here depends on the field value:
// - If the value is `Uint8List` or a non-growable list, this will throw.
// - Otherwise it will update both messages as the library assumes `bytes`
// fields are immutable and can be shared.
// value2.optionalBytes.add(123);

value2.optionalGroup.a += 1;
value2.optionalNestedMessage.bb += 1;
value2.optionalNestedMessage.i += 1;
value2.optionalImportMessage.d += 1;
value2.optionalNestedEnum = TestAllTypes_NestedEnum.BAR;
value2.optionalForeignEnum = ForeignEnum.FOREIGN_BAR;
value2.optionalImportEnum = ImportEnum.IMPORT_BAR;
value2.optionalStringPiece = "hi 2";
value2.optionalCord = "hi 3";
modifyRepeatedFields(value2);

assertAllFieldsSet(value1);
}
Loading