Files
Terminal.Gui/Tests/UnitTests/Configuration/ConfigPropertyTests.cs
Tig b0f32811eb Fixes #3930 - Splits tests to Tests/UnitTests, Tests/IntegrationTests, Tests/StressTests (#3954)
* Tons of API doc updates

* Removed stale test

* Removed stale tests

* Fixed Skipped Shadow test 1

* Fixed Skipped Shadow test 2

* Fixed Skipped Shadow test 3

* Removed stale test

* Removed stale test2

* Explicit unregister of event handler on Application.Driver!.ClearedContents

* Added Toplevels to dict

* code cleanup

* spelling error

* Removed stale test3

* Removed stale test4

* Removed stale test5

* added script

* tweaked script

* tweaked script

* Created StressTests project; moved some tests

* Created IntegrationTests project; moved some tests

* New yml

* made old yml just unit tests

* Tweaked Button_IsDefault_Raises_Accepted_Correctly

* tweaked script

* cleaned up ymls

* tweakled up ymls

* stress tests...

* stress tests on ubuntu only

* Fixed WindowsDriver in InvokeLeakTest

* Fixed WindowsDriver in InvokeLeakTest2

* Added Directory.Packages.props.
Added Directory.Build.props

* Shortened StressTest time

* Removed dupe file.

* DemoFiles

* Moved all tests to ./Tests dir.

* Fixed release build issue

* Fixed .sln file

* Fixed .sl* files

* Fixing ymls

* Fixing interation tests

* Create link to the file TestHelpers.

* Created Tests/UnitTestsParallelizable.
Moved all obviously parallelizable tests.
Updated yml.

* fixing logs

* fixing logs2

* fixing logs3

* don't require stress to pass for PRs

* Fix a failure?

* tweaked script

* Coudl this be it?

* Moved tons of tests to parallelizable

* Fixed some stuff

* Script to find duplicate tests

* Testing workflows

* Updated to v4

* Fix RelativeBasePath issue

* Replace powershell to pwsh

* Add ignore projects.

* Removed dupe unit tests

* Code cleanup of tests

* Cleaned up test warnings

* yml tweak

* Moved setter

* tweak ymls

* just randomly throwing spaghetti at a wall

* Enable runing 5 test runners in par

* Turned off DEBUG_DISPOSABLE for par tests

* RunningUnitTests=true

* code cleanup (forcing more Action runs)

* DISABLE_DEBUG_IDISPOSABLE

* Added View.DebugIDisposable. False by default.

* Remobed bogus tareet

* Remobed bogus tareet2

* fixed warning

* added api doc

* fixed warning

* fixed warning

* fixed warning2

* fixed warning3

* fixed warning4

---------

Co-authored-by: BDisp <bd.bdisp@gmail.com>
2025-03-05 23:44:27 -07:00

177 lines
5.2 KiB
C#

using System;
using System.Reflection;
using System.Text.Json;
using System.Text.Json.Serialization;
using Terminal.Gui;
using Xunit;
namespace Terminal.Gui.ConfigurationTests;
public class ConfigPropertyTests
{
[Fact]
public void Apply_PropertyValueIsAppliedToStatic_String_Property()
{
// Arrange
TestConfiguration.Reset ();
var propertyInfo = typeof(TestConfiguration).GetProperty(nameof(TestConfiguration.TestStringProperty));
var configProperty = new ConfigProperty
{
PropertyInfo = propertyInfo,
PropertyValue = "UpdatedValue"
};
// Act
var result = configProperty.Apply();
// Assert
Assert.Equal (1, TestConfiguration.TestStringPropertySetCount);
Assert.True(result);
Assert.Equal("UpdatedValue", TestConfiguration.TestStringProperty);
TestConfiguration.Reset ();
}
[Fact]
public void Apply_PropertyValueIsAppliedToStatic_Key_Property ()
{
// Arrange
TestConfiguration.Reset ();
var propertyInfo = typeof (TestConfiguration).GetProperty (nameof (TestConfiguration.TestKeyProperty));
var configProperty = new ConfigProperty
{
PropertyInfo = propertyInfo,
PropertyValue = Key.Q.WithCtrl
};
// Act
var result = configProperty.Apply ();
// Assert
Assert.Equal(1, TestConfiguration.TestKeyPropertySetCount);
Assert.True (result);
Assert.Equal (Key.Q.WithCtrl, TestConfiguration.TestKeyProperty);
TestConfiguration.Reset ();
}
[Fact]
public void RetrieveValue_GetsCurrentValueOfStaticProperty()
{
// Arrange
TestConfiguration.TestStringProperty = "CurrentValue";
var propertyInfo = typeof(TestConfiguration).GetProperty(nameof(TestConfiguration.TestStringProperty));
var configProperty = new ConfigProperty
{
PropertyInfo = propertyInfo
};
// Act
var value = configProperty.RetrieveValue();
// Assert
Assert.Equal("CurrentValue", value);
Assert.Equal("CurrentValue", configProperty.PropertyValue);
}
[Fact]
public void UpdateValueFrom_Updates_String_Property_Value ()
{
// Arrange
TestConfiguration.Reset ();
var propertyInfo = typeof(TestConfiguration).GetProperty(nameof(TestConfiguration.TestStringProperty));
var configProperty = new ConfigProperty
{
PropertyInfo = propertyInfo,
PropertyValue = "InitialValue"
};
// Act
var updatedValue = configProperty.UpdateValueFrom("NewValue");
// Assert
Assert.Equal (0, TestConfiguration.TestStringPropertySetCount);
Assert.Equal("NewValue", updatedValue);
Assert.Equal("NewValue", configProperty.PropertyValue);
TestConfiguration.Reset ();
}
//[Fact]
//public void UpdateValueFrom_InvalidType_ThrowsArgumentException()
//{
// // Arrange
// var propertyInfo = typeof(TestConfiguration).GetProperty(nameof(TestConfiguration.TestStringProperty));
// var configProperty = new ConfigProperty
// {
// PropertyInfo = propertyInfo
// };
// // Act & Assert
// Assert.Throws<ArgumentException>(() => configProperty.UpdateValueFrom(123));
//}
[Fact]
public void Apply_TargetInvocationException_ThrowsJsonException()
{
// Arrange
var propertyInfo = typeof(TestConfiguration).GetProperty(nameof(TestConfiguration.TestStringProperty));
var configProperty = new ConfigProperty
{
PropertyInfo = propertyInfo,
PropertyValue = null // This will cause ArgumentNullException in the set accessor
};
// Act & Assert
var exception = Assert.Throws<JsonException> (() => configProperty.Apply());
}
[Fact]
public void GetJsonPropertyName_ReturnsJsonPropertyNameAttributeValue()
{
// Arrange
var propertyInfo = typeof(TestConfiguration).GetProperty(nameof(TestConfiguration.TestStringProperty));
// Act
var jsonPropertyName = ConfigProperty.GetJsonPropertyName(propertyInfo);
// Assert
Assert.Equal("TestStringProperty", jsonPropertyName);
}
}
public class TestConfiguration
{
private static string _testStringProperty = "Default";
public static int TestStringPropertySetCount { get; set; }
[SerializableConfigurationProperty]
public static string TestStringProperty
{
get => _testStringProperty;
set
{
TestStringPropertySetCount++;
_testStringProperty = value ?? throw new ArgumentNullException (nameof (value));
}
}
private static Key _testKeyProperty = Key.Esc;
public static int TestKeyPropertySetCount { get; set; }
[SerializableConfigurationProperty]
public static Key TestKeyProperty
{
get => _testKeyProperty;
set
{
TestKeyPropertySetCount++;
_testKeyProperty = value ?? throw new ArgumentNullException (nameof (value));
}
}
public static void Reset ()
{
TestStringPropertySetCount = 0;
TestKeyPropertySetCount = 0;
}
}