Skip to content

Commit

Permalink
add init props tests
Browse files Browse the repository at this point in the history
  • Loading branch information
lofcz committed Jan 8, 2025
1 parent 57a8de1 commit 2041a22
Showing 1 changed file with 128 additions and 0 deletions.
128 changes: 128 additions & 0 deletions FastCloner.Tests/SpecificScenariosTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,134 @@ public void Test_DeepClone_Marshal()
Assert.That(cloned.internals[i].myUint3, Is.EqualTo(original.internals[i].myUint3));
}
}

[Test]
public void Test_InitOnlyProperties_ObjectInitialization()
{
// Arrange & Act
PersonWithInitProperties person = new PersonWithInitProperties
{
Name = "John Doe",
Age = 30,
BirthDate = new DateTime(1993, 1, 1),
HomeAddress = new Address
{
Street = "123 Main St",
City = "New York",
ZipCode = "10001"
}
};

// Assert
Assert.Multiple(() =>
{
Assert.That(person.Name, Is.EqualTo("John Doe"));
Assert.That(person.Age, Is.EqualTo(30));
Assert.That(person.BirthDate, Is.EqualTo(new DateTime(1993, 1, 1)));
Assert.That(person.HomeAddress.Street, Is.EqualTo("123 Main St"));
});
}

[Test]
public void Test_InitOnlyProperties_WithCloning()
{
// Arrange
PersonWithInitProperties original = new PersonWithInitProperties
{
Name = "John Doe",
Age = 30,
HomeAddress = new Address
{
Street = "123 Main St",
City = "New York",
ZipCode = "10001"
}
};

// Act
PersonWithInitProperties modified = original with { Age = 31 };

// Assert
Assert.Multiple(() =>
{
Assert.That(modified.Name, Is.EqualTo(original.Name));
Assert.That(modified.Age, Is.EqualTo(31));
Assert.That(modified.HomeAddress, Is.EqualTo(original.HomeAddress));
Assert.That(modified, Is.Not.SameAs(original));
});
}

[Test]
public void Test_InitOnlyProperties_RecordEquality()
{
// Arrange
PersonWithInitProperties person1 = new PersonWithInitProperties
{
Name = "John Doe",
Age = 30,
HomeAddress = new Address
{
Street = "123 Main St",
City = "New York",
ZipCode = "10001"
}
};

PersonWithInitProperties person2 = new PersonWithInitProperties
{
Name = "John Doe",
Age = 30,
HomeAddress = new Address
{
Street = "123 Main St",
City = "New York",
ZipCode = "10001"
}
};

// Act & Assert
Assert.Multiple(() =>
{
Assert.That(person1, Is.EqualTo(person2));
Assert.That(person1.GetHashCode(), Is.EqualTo(person2.GetHashCode()));
Assert.That(person1, Is.EqualTo(person2));
});
}

public record PersonWithInitProperties
{
public string Name { get; init; }
public int Age { get; init; }
public DateTime BirthDate { get; init; }
public Address HomeAddress { get; init; }
}

public record Address
{
public string Street { get; init; }
public string City { get; init; }
public string ZipCode { get; init; }
}

[Test]
public void Test_InitOnlyProperties_WithNullValues()
{
// Arrange & Act
PersonWithInitProperties person = new PersonWithInitProperties
{
Name = null,
Age = 30,
HomeAddress = null
};

// Assert
Assert.Multiple(() =>
{
Assert.That(person.Name, Is.Null);
Assert.That(person.Age, Is.EqualTo(30));
Assert.That(person.HomeAddress, Is.Null);
});
}

public class CBase<TKey>
{
Expand Down

0 comments on commit 2041a22

Please sign in to comment.