Files
sbox-public/engine/Sandbox.Test.Unit/Reflection/Properties.cs
Lorenz Junglas 91f8fcf183 Speed up / parallelize tests (#3587)
- Added Sandbox.Test.Unit project (contains independent tests that can run in parallel) 
- Modify some slow/stress tests (e.g. instead of doing a million iterations settle for 10k).

Tests run almost twice as fast now.
2025-12-10 14:23:00 +01:00

52 lines
1.8 KiB
C#

namespace Reflection;
[TestClass]
public class Properties
{
System.Reflection.Assembly ThisAssembly => this.GetType().Assembly;
[TestMethod]
public void SetPropertiesInDynamicAssembly()
{
var tl = new Sandbox.Internal.TypeLibrary();
tl.AddAssembly( ThisAssembly, false );
var instance = new ClassWithProps();
var type = tl.GetType<ClassWithProps>();
// We should only see the public properties, not the private or internal
Assert.AreEqual( 3, type.Properties.Count() );
// We should be able to set public properties
var publicNameProp = type.GetProperty( nameof( ClassWithProps.PublicName ) );
publicNameProp.SetValue( instance, "Homer" );
Assert.AreEqual( publicNameProp.GetValue( instance ), "Homer" );
// We should not be able to set private setter properties
var privateSetNameProp = type.GetProperty( nameof( ClassWithProps.PrivateSetName ) );
privateSetNameProp.SetValue( instance, "Simpson" );
Assert.AreNotEqual( privateSetNameProp.GetValue( instance ), "Simpson" );
// We should not be able to set init properties
var initNameProp = type.GetProperty( nameof( ClassWithProps.InitName ) );
initNameProp.SetValue( instance, "Simpsons" );
Assert.AreNotEqual( initNameProp.GetValue( instance ), "Simpsons" );
// This other route shouldn't work either
tl.SetProperty( instance, nameof( ClassWithProps.InitName ), "Flintstones" );
Assert.AreNotEqual( tl.GetPropertyValue( instance, nameof( ClassWithProps.InitName ) ), "Flintstones" );
tl.RemoveAssembly( ThisAssembly );
}
}
[Expose]
public class ClassWithProps
{
public string PublicName { get; set; } = "Peter";
public string PrivateSetName { get; private set; } = "Griffin";
public string InitName { get; init; } = "Family Guy";
private int PrivateAge { get; set; } = 42;
internal int InternalSize { get; set; } = 3;
}