Files
Garry Newman d95169d8fa Unit Test Cleanup (#5064)
* Move all unit tests to Engine/Tests
* Fix Margin.EdgeSubtract
* Fix Capsule.Contains
* EnvironmentVariables.Remove if it's null
* Fixed ray trace never returning startedsolid
* Fix HistoryList.Navigate on empty list
* Fix GameObject.WorldPosition accepting NaNs
* Fix flex: initial expanding to the wrong grow/shrink
* Translation.TryConvert shouldn't throw on invalid enum strings
* Skip sound file tests on machines with no audio device
2026-06-12 13:23:50 +01:00

52 lines
1.8 KiB
C#

namespace ReflectionTests;
[TestClass]
public class PropertiesTest
{
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;
}