Skip to content

Commit 2041a22

Browse files
committed
add init props tests
1 parent 57a8de1 commit 2041a22

File tree

1 file changed

+128
-0
lines changed

1 file changed

+128
-0
lines changed

FastCloner.Tests/SpecificScenariosTest.cs

Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,134 @@ public void Test_DeepClone_Marshal()
8080
Assert.That(cloned.internals[i].myUint3, Is.EqualTo(original.internals[i].myUint3));
8181
}
8282
}
83+
84+
[Test]
85+
public void Test_InitOnlyProperties_ObjectInitialization()
86+
{
87+
// Arrange & Act
88+
PersonWithInitProperties person = new PersonWithInitProperties
89+
{
90+
Name = "John Doe",
91+
Age = 30,
92+
BirthDate = new DateTime(1993, 1, 1),
93+
HomeAddress = new Address
94+
{
95+
Street = "123 Main St",
96+
City = "New York",
97+
ZipCode = "10001"
98+
}
99+
};
100+
101+
// Assert
102+
Assert.Multiple(() =>
103+
{
104+
Assert.That(person.Name, Is.EqualTo("John Doe"));
105+
Assert.That(person.Age, Is.EqualTo(30));
106+
Assert.That(person.BirthDate, Is.EqualTo(new DateTime(1993, 1, 1)));
107+
Assert.That(person.HomeAddress.Street, Is.EqualTo("123 Main St"));
108+
});
109+
}
110+
111+
[Test]
112+
public void Test_InitOnlyProperties_WithCloning()
113+
{
114+
// Arrange
115+
PersonWithInitProperties original = new PersonWithInitProperties
116+
{
117+
Name = "John Doe",
118+
Age = 30,
119+
HomeAddress = new Address
120+
{
121+
Street = "123 Main St",
122+
City = "New York",
123+
ZipCode = "10001"
124+
}
125+
};
126+
127+
// Act
128+
PersonWithInitProperties modified = original with { Age = 31 };
129+
130+
// Assert
131+
Assert.Multiple(() =>
132+
{
133+
Assert.That(modified.Name, Is.EqualTo(original.Name));
134+
Assert.That(modified.Age, Is.EqualTo(31));
135+
Assert.That(modified.HomeAddress, Is.EqualTo(original.HomeAddress));
136+
Assert.That(modified, Is.Not.SameAs(original));
137+
});
138+
}
139+
140+
[Test]
141+
public void Test_InitOnlyProperties_RecordEquality()
142+
{
143+
// Arrange
144+
PersonWithInitProperties person1 = new PersonWithInitProperties
145+
{
146+
Name = "John Doe",
147+
Age = 30,
148+
HomeAddress = new Address
149+
{
150+
Street = "123 Main St",
151+
City = "New York",
152+
ZipCode = "10001"
153+
}
154+
};
155+
156+
PersonWithInitProperties person2 = new PersonWithInitProperties
157+
{
158+
Name = "John Doe",
159+
Age = 30,
160+
HomeAddress = new Address
161+
{
162+
Street = "123 Main St",
163+
City = "New York",
164+
ZipCode = "10001"
165+
}
166+
};
167+
168+
// Act & Assert
169+
Assert.Multiple(() =>
170+
{
171+
Assert.That(person1, Is.EqualTo(person2));
172+
Assert.That(person1.GetHashCode(), Is.EqualTo(person2.GetHashCode()));
173+
Assert.That(person1, Is.EqualTo(person2));
174+
});
175+
}
176+
177+
public record PersonWithInitProperties
178+
{
179+
public string Name { get; init; }
180+
public int Age { get; init; }
181+
public DateTime BirthDate { get; init; }
182+
public Address HomeAddress { get; init; }
183+
}
184+
185+
public record Address
186+
{
187+
public string Street { get; init; }
188+
public string City { get; init; }
189+
public string ZipCode { get; init; }
190+
}
191+
192+
[Test]
193+
public void Test_InitOnlyProperties_WithNullValues()
194+
{
195+
// Arrange & Act
196+
PersonWithInitProperties person = new PersonWithInitProperties
197+
{
198+
Name = null,
199+
Age = 30,
200+
HomeAddress = null
201+
};
202+
203+
// Assert
204+
Assert.Multiple(() =>
205+
{
206+
Assert.That(person.Name, Is.Null);
207+
Assert.That(person.Age, Is.EqualTo(30));
208+
Assert.That(person.HomeAddress, Is.Null);
209+
});
210+
}
83211

84212
public class CBase<TKey>
85213
{

0 commit comments

Comments
 (0)