10using Microsoft.VisualStudio.TestTools.UnitTesting;
32 _testDIContext = TestDIContextFactory.CreateDefaultContext();
51 mobileObject.
GetState(serializationInfo);
52 actual = serializationInfo.GetValue<
int>(
"PersonId");
55 Assert.AreEqual(expected, actual);
66 string expected =
"Joe";
73 mobileObject.
GetState(serializationInfo);
74 actual = serializationInfo.GetValue<
string>(
"FirstName");
77 Assert.AreEqual(expected, actual);
88 string expected =
"Smith";
95 mobileObject.
GetState(serializationInfo);
96 actual = serializationInfo.GetValue<
string>(
"LastName");
99 Assert.AreEqual(expected, actual);
110 bool expected =
false;
117 mobileObject.
GetState(serializationInfo);
118 actual = serializationInfo.
Values.ContainsKey(
"DateOfBirth");
121 Assert.AreEqual(expected, actual);
132 bool expected =
false;
139 mobileObject.
GetState(serializationInfo);
140 actual = serializationInfo.
Values.ContainsKey(
"MiddleName");
143 Assert.AreEqual(expected, actual);
154 string expected =
"Mid";
161 mobileObject.
GetState(serializationInfo);
162 actual = serializationInfo.GetValue<
string>(
"_middleName");
165 Assert.AreEqual(expected, actual);
178 SerializationInfo serializationInfo = PersonSerializationInfoFactory.GetDefaultSerializationInfo();
185 serializationInfo.
Values[
"PersonId"].Value = 5;
187 mobileObject.
SetState(serializationInfo);
191 Assert.AreEqual(expected, actual);
200 SerializationInfo serializationInfo = PersonSerializationInfoFactory.GetDefaultSerializationInfo();
202 string expected =
"Joe";
207 serializationInfo.
Values[
"FirstName"].Value =
"Joe";
209 mobileObject.
SetState(serializationInfo);
213 Assert.AreEqual(expected, actual);
222 SerializationInfo serializationInfo = PersonSerializationInfoFactory.GetDefaultSerializationInfo();
224 string expected =
"Smith";
229 serializationInfo.
Values[
"LastName"].Value =
"Smith";
231 mobileObject.
SetState(serializationInfo);
235 Assert.AreEqual(expected, actual);
244 SerializationInfo serializationInfo = PersonSerializationInfoFactory.GetDefaultSerializationInfo();
251 serializationInfo.
Values[
"DateOfBirth"].Value =
new DateTime(2021, 04, 12, 18, 27, 43);
253 mobileObject.
SetState(serializationInfo);
257 Assert.AreEqual(expected, actual);
266 SerializationInfo serializationInfo = PersonSerializationInfoFactory.GetDefaultSerializationInfo();
268 string expected =
string.Empty;
273 serializationInfo.
Values[
"NonSerializedText"].Value =
"Fred";
275 mobileObject.
SetState(serializationInfo);
279 Assert.AreEqual(expected, actual);
288 SerializationInfo serializationInfo = PersonSerializationInfoFactory.GetDefaultSerializationInfo();
290 string expected =
string.Empty;
295 serializationInfo.
Values[
"PrivateText"].Value =
"Fred";
297 mobileObject.
SetState(serializationInfo);
301 Assert.AreEqual(expected, actual);
310 SerializationInfo serializationInfo = PersonSerializationInfoFactory.GetDefaultSerializationInfo();
312 string expected =
"Fred";
317 serializationInfo.
Values[
"PrivateSerializedText"].Value =
"Fred";
319 mobileObject.
SetState(serializationInfo);
323 Assert.AreEqual(expected, actual);
332 SerializationInfo serializationInfo = PersonSerializationInfoFactory.GetDefaultSerializationInfo();
334 string expected =
"Mid";
339 serializationInfo.
Values[
"_middleName"].Value =
"Mid";
341 mobileObject.
SetState(serializationInfo);
345 Assert.AreEqual(expected, actual);
356 var applicationContext = _testDIContext.CreateTestApplicationContext();
360 bool expected =
true;
369 mobileObject.
GetChildren(serializationInfo, formatter);
370 actual = serializationInfo.
Children.ContainsKey(
"Address");
373 Assert.AreEqual(expected, actual);
380 var applicationContext = _testDIContext.CreateTestApplicationContext();
384 bool expected =
true;
393 mobileObject.
GetChildren(serializationInfo, formatter);
394 actual = serializationInfo.
Children.ContainsKey(
"EmailAddress");
397 Assert.AreEqual(expected, actual);
407 #region Serialize Then Deserialize
421 deserializedPerson = SerializeThenDeserialisePersonPOCO(person);
422 actual = deserializedPerson.
PersonId;
425 Assert.AreEqual(expected, actual);
435 string expected =
"Joe";
441 deserializedPerson = SerializeThenDeserialisePersonPOCO(person);
445 Assert.AreEqual(expected, actual);
455 string expected =
"Smith";
461 deserializedPerson = SerializeThenDeserialisePersonPOCO(person);
462 actual = deserializedPerson.
LastName;
465 Assert.AreEqual(expected, actual);
475 string expected =
"Mid";
481 deserializedPerson = SerializeThenDeserialisePersonPOCO(person);
485 Assert.AreEqual(expected, actual);
495 string expected =
"";
501 deserializedPerson = SerializeThenDeserialisePersonPOCO(person);
505 Assert.AreEqual(expected, actual);
515 string expected =
"Pri";
521 deserializedPerson = SerializeThenDeserialisePersonPOCO(person);
525 Assert.AreEqual(expected, actual);
535 string expected =
"";
541 deserializedPerson = SerializeThenDeserialisePersonPOCO(person);
545 Assert.AreEqual(expected, actual);
561 deserializedPerson = SerializeThenDeserialisePersonPOCO(person);
565 Assert.AreEqual(expected, actual);
575 string expected =
"1 High Street";
581 deserializedPerson = SerializeThenDeserialisePersonPOCO(person);
585 Assert.AreEqual(expected, actual);
599 deserializedPerson = SerializeThenDeserialisePersonPOCO(person);
600 actual = deserializedPerson.
Address;
603 Assert.IsNull(actual);
613 string expected =
"Townsville";
619 deserializedPerson = SerializeThenDeserialisePersonPOCO(person);
623 Assert.AreEqual(expected, actual);
637 deserializedPerson = SerializeThenDeserialisePersonPOCO(person);
641 Assert.IsNull(actual);
651 string expected =
"a@b.com";
657 deserializedPerson = SerializeThenDeserialisePersonPOCO(person);
661 Assert.AreEqual(expected, actual);
667 #region Private Helper Methods
677 var applicationContext = _testDIContext.CreateTestApplicationContext();
679 System.IO.MemoryStream serializationStream;
684 using (serializationStream =
new System.IO.MemoryStream())
686 formatter.
Serialize(serializationStream, valueToSerialize);
687 serializationStream.Seek(0, System.IO.SeekOrigin.Begin);
691 return deserializedValue;
Object already implementing IMobileObject that can be used for testing serialization behaviour
A class for which automatic serialization code is to be generated Includes children of various types ...
void SetPrivateSerializedText(string newValue)
EmailAddress EmailAddress
string GetPrivateSerializedText()
string GetUnderlyingPrivateText()
void SetUnderlyingPrivateText(string value)
void SetDateOfBirth(DateTime newDateOfBirth)
void SetMiddleName(string middleName)
DateTime GetDateOfBirth()
Tests of serialization on the PersonPOCO class and its children
void GetState_WithInternalDateOfBirth210412_ReturnsInfoWithoutDateOfBirth()
void SerializeThenDeserialize_WithInternalAutoImpPropertyDateOfBirth20210412165753_HasMinDateOfBirth()
void SerializeThenDeserialize_WithIncludedPrivateFieldMiddleNameMid_HasMiddleNameMid()
void SetState_WithFirstNameJoe_ReturnsPersonWithFirstNameJoe()
void SetState_WithIncludedMiddleNameFieldMid_ReturnsMiddleNamePropertyMid()
void GetState_WithFirstNameJoe_ReturnsInfoContainingJoe()
void GetChildren_WithAddress1HighStreet_IncludesAddressKey()
void GetState_WithFieldMiddleNameMid_ReturnsInfoContainingMid()
void SerializeThenDeserialize_WithIMobileObjectEmailAddressAatBdotCom_HasEmailAddressAatBdotCom()
void SerializeThenDeserialize_WithAutoSerializableAddress1HighStreet_HasAddressOf1HighStreet()
void SetState_WithPrivateSerializedTextFred_ReturnsFred()
void SetState_WithLastNameSmith_ReturnsPersonWithLastNameSmith()
void GetState_WithPersonId5_ReturnsInfoContaining5()
void SetState_WithNonSerializedTextFred_ReturnsEmptyString()
static void ClassInitialize(TestContext testContext)
void SerializeThenDeserialize_WithPublicAutoImpPropertyFirstNameJoe_HasFirstNameJoe()
void SetState_WithPersonId5_ReturnsPersonWithId5()
void SerializeThenDeserialize_WithAutoSerializableAddressNull_HasNullAddress()
void SetState_WithPrivateTextFred_ReturnsEmptyString()
void SerializeThenDeserialize_WithExcludedPublicAutoImpPropertyNonSerializedTextNon_HasEmptyNonSerializedText()
void GetState_WithMiddleNameMid_ReturnsInfoWithoutMiddleName()
void GetState_WithLastNameSmith_ReturnsInfoContainingSmith()
void GetChildren_WithEmailAddress_IncludesEmailAddressKey()
void SerializeThenDeserialize_WithIncludedPrivateAutoImpPropertyPrivateSerializedTextPri_HasPrivateSerializedTextPri()
void SerializeThenDeserialize_WithPublicPropertyLastNameSmith_HasLastNameSmith()
void SerializeThenDeserialize_WithPrivateAutoImpPropertyPrivateTextPriv_HasEmptyPrivateText()
void SetState_WithInternalDateOfBirth210412_ReturnsPersonWithNoDateOfBirth()
void SerializeThenDeserialize_WithIMobileObjectEmailAddressNull_HasEmailAddressNull()
void SerializeThenDeserialize_WithPublicAutoImpPropertyPersonId5_HasPersonId5()
void SerializeThenDeserialize_WithAutoSerializableAddressTownsville_HasAddressOfTownsville()
Object containing the serialization data for a specific object.
Dictionary< string, FieldData > Values
Dictionary containg field data.
Dictionary< string, ChildData > Children
Dictionary containing child reference data.
Type to carry context information for DI in unit tests
Interface to be implemented by any object that supports serialization by the SerializationFormatterFa...
void GetChildren(SerializationInfo info, MobileFormatter formatter)
Method called by MobileFormatter when an object should serialize its child references.
void GetState(SerializationInfo info)
Method called by MobileFormatter when an object should serialize its data.
void SetState(SerializationInfo info)
Method called by MobileFormatter when an object should be deserialized.