Skip to content

Commit

Permalink
Split MergeWrapperFields test in two to communicate the different nul…
Browse files Browse the repository at this point in the history
…l vs. non-null use-cases as it relates to the source field values
  • Loading branch information
vassilvk committed May 16, 2022
1 parent c4e7df1 commit 9baab1f
Showing 1 changed file with 49 additions and 13 deletions.
62 changes: 49 additions & 13 deletions csharp/src/Google.Protobuf.Test/FieldMaskTreeTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -433,13 +433,46 @@ public void Merge(bool useDynamicMessage)
}

[Test]
[TestCase("Hi", 240, true, "Hi", 240)]
[TestCase("Hi", 240, false, "Hi", 240)]
[TestCase(null, null, true, null, null)]
[TestCase(null, null, false, "Hello", 24)]
public void MergeWrapperFields(
string sourceStringValue,
long? sourceInt64Value,
public void MergeWrapperFieldsWithNonNullFieldsInSource()
{
// Instantiate a destination with wrapper-based field types.
var destination = new TestWellKnownTypes()
{
StringField = "Hello",
Int32Field = 12,
Int64Field = 24,
BoolField = true,
};

// Set up a targeted update.
var source = new TestWellKnownTypes()
{
StringField = "Hi",
Int64Field = 240
};

Merge(new FieldMaskTree().AddFieldPath("string_field").AddFieldPath("int64_field"),
source,
destination,
new FieldMask.MergeOptions(),
false);

// Make sure the targeted fields changed.
Assert.AreEqual("Hi", destination.StringField);
Assert.AreEqual(240, destination.Int64Field);

// Prove that non-targeted fields stay intact...
Assert.AreEqual(12, destination.Int32Field);
Assert.IsTrue(destination.BoolField);

// ...including default values which were not explicitly set in the destination object.
Assert.IsNull(destination.FloatField);
}

[Test]
[TestCase(false, "Hello", 24)]
[TestCase(true, null, null)]
public void MergeWrapperFieldsWithNullFieldsInSource(
bool replaceMessageFields,
string expectedStringValue,
long? expectedInt64Value)
Expand All @@ -453,11 +486,11 @@ public void Merge(bool useDynamicMessage)
BoolField = true,
};

// Set up a targeted update.
// Set up a targeted update with null valued fields.
var source = new TestWellKnownTypes()
{
StringField = sourceStringValue,
Int64Field = sourceInt64Value
StringField = null,
Int64Field = null
};

Merge(new FieldMaskTree().AddFieldPath("string_field").AddFieldPath("int64_field"),
Expand All @@ -469,15 +502,18 @@ public void Merge(bool useDynamicMessage)
},
false);

// Make sure the targeted fields changed according to our expectations.
// Make sure the targeted fields changed according to our expectations, depending on the value of ReplaceMessageFields.
// When ReplaceMessageFields is false, the null values are not applied to the destination, because, although wrapped types
// are semantically primitives, FieldMaskTree.Merge still treats them as message types in order to maintain consistency with other Protobuf
// libraries such as Java and C++.
Assert.AreEqual(expectedStringValue, destination.StringField);
Assert.AreEqual(expectedInt64Value, destination.Int64Field);

// Prove that non-targeted fields stay intact.
// Prove that non-targeted fields stay intact...
Assert.AreEqual(12, destination.Int32Field);
Assert.IsTrue(destination.BoolField);

// Including default values which were not explicitly set in the destination object.
// ...including default values which were not explicitly set in the destination object.
Assert.IsNull(destination.FloatField);
}
}
Expand Down

0 comments on commit 9baab1f

Please sign in to comment.