Files
Terminal.Gui/Tests/UnitTests/Configuration/ConfigPropertyTests.cs
Tig 7422385457 Fixes #4057 - MASSIVE! Fully implements ColorScheme->Scheme + VisualRole + Colors.->SchemeManager. (#4062)
* touching publish.yml

* ColorScheme->Scheme

* ColorScheme->Scheme 2

* Prototype of GetAttributeForRole

* Badly broke CM

* Further Badly broke CM

* Refactored CM big-time. View still broken

* All unit test pass again. Tons added. CM is still WIP, but Schemes is not mostly refactored and working.

* Actually:
All unit test pass again.
Tons added.
CM is still WIP, but Schemes is not mostly refactored and working.

* Bug fixes.
DeepMemberWiseClone cleanup

* Further cleanup of Scope<T>, ConfigProperty, etc.

* Made ConfigManager thread safe.

* WIP: Broken

* WIP: new deep clone impl

* WIP: new deep clone impl is done. Now fixing CM

* WIP:
- config.md
- Working on AOT clean up
- Core CM is broken; but known.

* WIP

* Merged.
Removed CM from Application.Init

* WIP

* More WIP; Less broke

* All CM unit tests pass... Not sure if it actually works though

* All unit tests pass... Themes are broken though in UI Cat

* CM Ready for review?

* Fixed failures due to TextStyles PR

* Working on Scheme/Attribute

* Working on Scheme/Attribute 2

* Working on Scheme/Attribute 3

* Working on Scheme/Attribute 4

* Working on Scheme/Attribute 5

* Working on Scheme/Attribute 6

* Added test to show how awful memory usage is

* Improved schema. Updated config.json

* Nade Scope<T> concurrentdictionary and added test to prove

* Made Themes ConcrurrentDictionary. Added bunches of tests

* Code cleanup

* Code cleanup 2

* Code cleanup 3

* Tweaking Scheme

* ClearJsonErrors

* ClearJsonErrors2

* Updated Attribute API

* It all (mostly) works!

* Skip odd unit test

* Messed with Themes

* Theme tweaks

* Code reorg. New .md stuff

* Fixed Enabled. Added mock driver

* Fixed a bunch of View.Enabled related issues

* Scheme -> Get/SetScheme()

* Cleanup

* Cleanup2

* Broke something

* Fixed everything

* Made CM.Enable better

* Text Style Scenario

* Added comments

* Fixed UI Catalog Theme Changing

* Fixed more dynamic CM update stuff

* Warning cleanup

* New Default Theme

* fixed unit test

* Refactoring Scheme and Attribute to fix inheritance

* more unit tests

* ConfigProperty is not updating schemes correctly

* All unit tests pass.
Code cleanup

* All unit tests pass.
Code cleanup2

* Fixed unit tests

* Upgraded TextField and TextView

* Fixed TextView !Enabled bug

* More updates to TextView. More unit tests for SchemeManager

* Upgraded CharMap

* API docs

* Fixe HexView API

* upgrade HexView

* Fixed shortcut KeyView

* Fixed more bugs. Added new themes

* updated themes

* upgraded Border

* Fixed themes memory usage...mostly

* Fixed themes memory usage...mostly2

* Fixed themes memory usage...2

* Fixed themes memory usage...3

* Added new colors

* Fixed GetHardCodedConfig bug

* Added Themes Scenario - WIP

* Added Themes Scenario

* Tweaked Themes Scenario

* Code cleanup

* Fixed json schmea

* updated deepdives

* updated deepdives

* Tweaked Themes Scenario

* Made Schemes a concurrent dict

* Test cleanup

* Thread safe ConfigProperty tests

* trying to make things more thread safe

* more trying to make things more thread safe

* Fixing bugs in shadowview

* Fixing bugs in shadowview 2

* Refactored GetViewsUnderMouse to GetViewsUnderLocation etc...

* Fixed dupe unit tests?

* Added better description of layout and coordiantes to deep dive

* Added better description of layout and coordiantes to deep dive

* Modified tests that call v2.AddTimeout; they were returning true which means restart the timer!
This was causing mac/linux unit test failures.
I think

* Fixed auto scheme.
Broke TextView/TextField selection

* Realized Attribute.IsExplicitlySet is stupid; just use nullable

* Fixed Attribute. Simplified. MOre theme testing

* Updated themes again

* GetViewsUnderMouse to GetViewsUnderLocation broke TransparentMouse.

* Fixing mouseunder bugs

* rewriting...

* All working again.
Shadows are now slick as snot.
GetViewsUnderLocation is rewritten to actually work and be readable.
Tons more low-level unit tests.
Margin is now actually ViewportSettings.Transparent.

* Code cleanup

* Code cleanup

* Code cleanup of color apis

* Fixed Hover/Highlight

* Update Examples/UICatalog/Scenarios/AllViewsTester.cs

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>

* Update Examples/UICatalog/Scenarios/CharacterMap/CharacterMap.cs

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>

* Update Examples/UICatalog/Scenarios/Clipping.cs

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>

* Fixed race condition?

* reverted

* Simplified Attribute API by removing events from SetAttributeForRole

* Removed recursion from GetViewsAtLocation

* Removed unneeded code

* Code clean up.
Fixed Scheme bug.

* reverted temporary disable

* Adjusted scheme algo

* Upgraded TextValidateField

* Fixed TextValidate bugs

* Tweaks

* Frameview rounded border by default

* API doc cleanup

* Readme fix

* Addressed tznind feeback

* Fixed more unit test issues by protecting Application statics from being set if Application.Initialized is not true

* Fixed more unit test issues by protecting Application statics from being set if Application.Initialized is not true 2

* cleanup

---------

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
2025-05-29 13:55:54 -06:00

469 lines
16 KiB
C#

#nullable enable
using System.Collections.Concurrent;
using System.Reflection;
using System.Text.Json;
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.UpdateToCurrentValue ();
// Assert
Assert.Equal ("CurrentValue", value);
Assert.Equal ("CurrentValue", configProperty.PropertyValue);
}
[Fact]
public void DeepCloneFrom_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.UpdateFrom ("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);
}
[Fact]
public void UpdateFrom_NullSource_ReturnsExistingValue()
{
// Arrange
TestConfiguration.TestStringProperty = "CurrentValue";
var propertyInfo = typeof(TestConfiguration).GetProperty(nameof(TestConfiguration.TestStringProperty));
var configProperty = new ConfigProperty
{
PropertyInfo = propertyInfo,
PropertyValue = "ExistingValue"
};
// Act
var result = configProperty.UpdateFrom(null);
// Assert
Assert.Equal("ExistingValue", result);
Assert.Equal("ExistingValue", configProperty.PropertyValue);
}
[Fact]
public void UpdateFrom_InvalidType_ThrowsArgumentException()
{
// Arrange
var propertyInfo = typeof(TestConfiguration).GetProperty(nameof(TestConfiguration.TestStringProperty));
var configProperty = new ConfigProperty
{
PropertyInfo = propertyInfo,
PropertyValue = "ExistingValue"
};
// Act & Assert
Assert.Throws<ArgumentException>(() => configProperty.UpdateFrom(123));
}
[Fact]
public void UpdateFrom_ConfigPropertySource_CopiesValue()
{
// Arrange
var propertyInfo = typeof(TestConfiguration).GetProperty(nameof(TestConfiguration.TestStringProperty));
var configProperty = new ConfigProperty
{
PropertyInfo = propertyInfo,
PropertyValue = "ExistingValue"
};
var sourceConfigProperty = new ConfigProperty
{
PropertyValue = "SourceValue",
HasValue = true
};
// Act
var result = configProperty.UpdateFrom(sourceConfigProperty);
// Assert
Assert.Equal("SourceValue", result);
Assert.Equal("SourceValue", configProperty.PropertyValue);
}
[Fact]
public void UpdateFrom_ConfigPropertySource_WithoutValue_KeepsExistingValue()
{
// Arrange
var propertyInfo = typeof(TestConfiguration).GetProperty(nameof(TestConfiguration.TestStringProperty));
var configProperty = new ConfigProperty
{
PropertyInfo = propertyInfo,
PropertyValue = "ExistingValue"
};
var sourceConfigProperty = new ConfigProperty
{
HasValue = false
};
// Act
var result = configProperty.UpdateFrom(sourceConfigProperty);
// Assert
Assert.Equal("ExistingValue", result);
Assert.Equal("ExistingValue", configProperty.PropertyValue);
}
[Fact]
public void UpdateFrom_ConcurrentDictionaryOfThemeScopes_UpdatesValues()
{
// Arrange
var propertyInfo = typeof(TestConfiguration).GetProperty(nameof(TestConfiguration.TestDictionaryProperty));
// Create a destination dictionary
var destinationDict = new ConcurrentDictionary<string, ThemeScope>(StringComparer.InvariantCultureIgnoreCase);
destinationDict.TryAdd("theme1", new ThemeScope());
var configProperty = new ConfigProperty
{
PropertyInfo = propertyInfo,
PropertyValue = destinationDict
};
// Create a source dictionary with one matching key and one new key
var sourceDict = new ConcurrentDictionary<string, ThemeScope>(StringComparer.InvariantCultureIgnoreCase);
var sourceTheme1 = new ThemeScope();
var sourceTheme2 = new ThemeScope();
// Add a property to sourceTheme1 to verify it gets updated
var keyProperty = typeof(TestConfiguration).GetProperty(nameof(TestConfiguration.TestKeyProperty));
if (sourceTheme1.TryAdd("TestKey", new ConfigProperty { PropertyInfo = keyProperty, PropertyValue = Key.A.WithCtrl, HasValue = true }))
{
// Successfully added
}
sourceDict.TryAdd("theme1", sourceTheme1);
sourceDict.TryAdd("theme2", sourceTheme2);
// Act
var result = configProperty.UpdateFrom(sourceDict);
// Assert
var resultDict = result as ConcurrentDictionary<string, ThemeScope>;
Assert.NotNull(resultDict);
Assert.Equal(2, resultDict.Count);
Assert.True(resultDict.ContainsKey("theme1"));
Assert.True(resultDict.ContainsKey("theme2"));
// Verify the theme1 was updated with the property
Assert.True(resultDict["theme1"].ContainsKey("TestKey"));
Assert.Equal(Key.A.WithCtrl, resultDict["theme1"]["TestKey"].PropertyValue);
}
[Fact]
public void UpdateFrom_ConcurrentDictionaryOfConfigProperties_UpdatesValues()
{
// Arrange
var propertyInfo = typeof(TestConfiguration).GetProperty(nameof(TestConfiguration.TestConfigDictionaryProperty));
// Create a destination dictionary
var destinationDict = new ConcurrentDictionary<string, ConfigProperty>(StringComparer.InvariantCultureIgnoreCase);
destinationDict.TryAdd("prop1", new ConfigProperty { PropertyValue = "Original", HasValue = true });
var configProperty = new ConfigProperty
{
PropertyInfo = propertyInfo,
PropertyValue = destinationDict
};
// Create a source dictionary with one matching key and one new key
var sourceDict = new ConcurrentDictionary<string, ConfigProperty>(StringComparer.InvariantCultureIgnoreCase);
sourceDict.TryAdd("prop1", new ConfigProperty { PropertyValue = "Updated", HasValue = true });
sourceDict.TryAdd("prop2", new ConfigProperty { PropertyValue = "New", HasValue = true });
// Act
var result = configProperty.UpdateFrom(sourceDict);
// Assert
var resultDict = result as ConcurrentDictionary<string, ConfigProperty>;
Assert.NotNull(resultDict);
Assert.Equal(2, resultDict.Count);
Assert.True(resultDict.ContainsKey("prop1"));
Assert.True(resultDict.ContainsKey("prop2"));
Assert.Equal("Updated", resultDict["prop1"].PropertyValue);
Assert.Equal("New", resultDict["prop2"].PropertyValue);
}
[Fact]
public void UpdateFrom_DictionaryOfConfigProperties_UpdatesValues()
{
// Arrange
var propertyInfo = typeof(TestConfiguration).GetProperty(nameof(TestConfiguration.TestConfigDictionaryProperty));
// Create a destination dictionary
var destinationDict = new Dictionary<string, ConfigProperty>(StringComparer.InvariantCultureIgnoreCase);
destinationDict.Add("prop1", new ConfigProperty { PropertyValue = "Original", HasValue = true });
var configProperty = new ConfigProperty
{
PropertyInfo = propertyInfo,
PropertyValue = destinationDict
};
// Create a source dictionary with one matching key and one new key
var sourceDict = new Dictionary<string, ConfigProperty>(StringComparer.InvariantCultureIgnoreCase);
sourceDict.Add("prop1", new ConfigProperty { PropertyValue = "Updated", HasValue = true });
sourceDict.Add("prop2", new ConfigProperty { PropertyValue = "New", HasValue = true });
// Act
var result = configProperty.UpdateFrom(sourceDict);
// Assert
var resultDict = result as Dictionary<string, ConfigProperty>;
Assert.NotNull(resultDict);
Assert.Equal(2, resultDict.Count);
Assert.True(resultDict.ContainsKey("prop1"));
Assert.True(resultDict.ContainsKey("prop2"));
Assert.Equal("Updated", resultDict["prop1"].PropertyValue);
Assert.Equal("New", resultDict["prop2"].PropertyValue);
}
[Fact]
public void PropertyValue_SetWhenImmutable_ThrowsException()
{
// Arrange
var propertyInfo = typeof(TestConfiguration).GetProperty(nameof(TestConfiguration.TestStringProperty));
var configProperty = new ConfigProperty
{
PropertyInfo = propertyInfo,
Immutable = true
};
// Act & Assert
var exception = Assert.Throws<InvalidOperationException>(() => configProperty.PropertyValue = "New Value");
Assert.Contains("immutable", exception.Message);
}
[Fact]
public void CreateWithAttributeInfo_ReturnsConfigPropertyWithCorrectValues()
{
// Arrange
var propertyInfo = typeof(TestConfiguration).GetProperty(nameof(TestConfiguration.TestStringProperty));
// Act
var configProperty = ConfigProperty.CreateImmutableWithAttributeInfo(propertyInfo!);
// Assert
Assert.Equal(propertyInfo, configProperty.PropertyInfo);
Assert.False(configProperty.OmitClassName);
Assert.True(configProperty.Immutable);
Assert.Null(configProperty.PropertyValue);
Assert.False(configProperty.HasValue);
}
[Fact]
public void HasConfigurationPropertyAttribute_ReturnsTrue_ForDecoratedProperty()
{
// Arrange
var propertyInfo = typeof(TestConfiguration).GetProperty(nameof(TestConfiguration.TestStringProperty));
// Act
var result = ConfigProperty.HasConfigurationPropertyAttribute(propertyInfo!);
// Assert
Assert.True(result);
}
[Fact]
public void HasConfigurationPropertyAttribute_ReturnsFalse_ForNonDecoratedProperty()
{
// Arrange
var propertyInfo = typeof(TestConfiguration).GetProperty(nameof(TestConfiguration.TestStringPropertySetCount));
// Act
var result = ConfigProperty.HasConfigurationPropertyAttribute(propertyInfo!);
// Assert
Assert.False(result);
}
public class TestConfiguration
{
private static string _testStringProperty = "Default";
public static int TestStringPropertySetCount { get; set; }
[ConfigurationProperty]
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; }
[ConfigurationProperty]
public static Key TestKeyProperty
{
get => _testKeyProperty;
set
{
TestKeyPropertySetCount++;
_testKeyProperty = value ?? throw new ArgumentNullException (nameof (value));
}
}
// Add these new properties for testing dictionaries
[ConfigurationProperty]
public static ConcurrentDictionary<string, ThemeScope>? TestDictionaryProperty { get; set; }
[ConfigurationProperty]
public static Dictionary<string, ConfigProperty>? TestRegularDictionaryProperty { get; set; }
[ConfigurationProperty]
public static ConcurrentDictionary<string, ConfigProperty>? TestConfigDictionaryProperty { get; set; }
[ConfigurationProperty]
public static Scheme? TestSchemeProperty { get; set; }
public static void Reset ()
{
TestStringPropertySetCount = 0;
TestKeyPropertySetCount = 0;
TestDictionaryProperty = null;
TestRegularDictionaryProperty = null;
TestConfigDictionaryProperty = null;
TestSchemeProperty = null;
}
}
[Fact]
public void UpdateFrom_SchemeSource_UpdatesValue ()
{
// Arrange
PropertyInfo? propertyInfo = typeof (TestConfiguration).GetProperty (nameof (TestConfiguration.TestSchemeProperty));
Scheme sourceScheme = new (new Attribute (Color.Red, Color.Blue, TextStyle.Bold));
var configProperty = new ConfigProperty
{
PropertyInfo = propertyInfo!,
PropertyValue = new Scheme (new Attribute (Color.White, Color.Black, TextStyle.None))
};
// Act
object? result = configProperty.UpdateFrom (sourceScheme);
// Assert
Assert.Equal (sourceScheme, result);
Assert.Equal (sourceScheme, configProperty.PropertyValue);
Assert.NotSame (sourceScheme, configProperty.PropertyValue); // Prove it's a clone, not a ref
}
}