mirror of
https://github.com/gui-cs/Terminal.Gui.git
synced 2026-01-02 01:03:29 +01:00
general messing around
This commit is contained in:
@@ -171,7 +171,7 @@ public class ScenarioTests : TestsAllViews
|
||||
|
||||
var top = new Toplevel ();
|
||||
|
||||
Dictionary<string, Type> viewClasses = GetAllViewClasses().ToDictionary (t => t.Name);
|
||||
Dictionary<string, Type> viewClasses = GetAllViewClasses ().ToDictionary (t => t.Name);
|
||||
|
||||
Window leftPane = new ()
|
||||
{
|
||||
@@ -277,7 +277,7 @@ public class ScenarioTests : TestsAllViews
|
||||
classListView.SelectedItemChanged += (s, args) =>
|
||||
{
|
||||
// Remove existing class, if any
|
||||
if (curView is {})
|
||||
if (curView is { })
|
||||
{
|
||||
curView.SubViewsLaidOut -= LayoutCompleteHandler;
|
||||
hostPane.Remove (curView);
|
||||
@@ -357,10 +357,13 @@ public class ScenarioTests : TestsAllViews
|
||||
{
|
||||
classListView.MoveDown ();
|
||||
|
||||
Assert.Equal (
|
||||
curView!.GetType ().Name,
|
||||
viewClasses.Values.ToArray () [classListView.SelectedItem].Name
|
||||
);
|
||||
if (curView is { })
|
||||
{
|
||||
Assert.Equal (
|
||||
curView.GetType ().Name,
|
||||
viewClasses.Values.ToArray () [classListView.SelectedItem].Name
|
||||
);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
@@ -507,12 +510,26 @@ public class ScenarioTests : TestsAllViews
|
||||
// For each of the <T> arguments
|
||||
List<Type> typeArguments = new ();
|
||||
|
||||
// use <object>
|
||||
// use <object> or the original type if applicable
|
||||
foreach (Type arg in type.GetGenericArguments ())
|
||||
{
|
||||
typeArguments.Add (typeof (object));
|
||||
if (arg.IsValueType && Nullable.GetUnderlyingType (arg) == null)
|
||||
{
|
||||
typeArguments.Add (arg);
|
||||
}
|
||||
else
|
||||
{
|
||||
typeArguments.Add (typeof (object));
|
||||
}
|
||||
}
|
||||
|
||||
// Ensure the type does not contain any generic parameters
|
||||
if (type.ContainsGenericParameters)
|
||||
{
|
||||
Logging.Warning ($"Cannot create an instance of {type} because it contains generic parameters.");
|
||||
//throw new ArgumentException ($"Cannot create an instance of {type} because it contains generic parameters.");
|
||||
return null;
|
||||
}
|
||||
// And change what type we are instantiating from MyClass<T> to MyClass<object>
|
||||
type = type.MakeGenericType (typeArguments.ToArray ());
|
||||
}
|
||||
|
||||
@@ -63,14 +63,30 @@ public class TestsAllViews
|
||||
|
||||
if (type is { IsGenericType: true, IsTypeDefinition: true })
|
||||
{
|
||||
List<Type> gTypes = new ();
|
||||
List<Type> typeArguments = new ();
|
||||
|
||||
foreach (Type args in type.GetGenericArguments ())
|
||||
// use <object> or the original type if applicable
|
||||
foreach (Type arg in type.GetGenericArguments ())
|
||||
{
|
||||
gTypes.Add (typeof (object));
|
||||
if (arg.IsValueType && Nullable.GetUnderlyingType (arg) == null)
|
||||
{
|
||||
typeArguments.Add (arg);
|
||||
}
|
||||
else
|
||||
{
|
||||
typeArguments.Add (typeof (object));
|
||||
}
|
||||
}
|
||||
|
||||
type = type.MakeGenericType (gTypes.ToArray ());
|
||||
type = type.MakeGenericType (typeArguments.ToArray ());
|
||||
|
||||
// Ensure the type does not contain any generic parameters
|
||||
if (type.ContainsGenericParameters)
|
||||
{
|
||||
Logging.Warning ($"Cannot create an instance of {type} because it contains generic parameters.");
|
||||
//throw new ArgumentException ($"Cannot create an instance of {type} because it contains generic parameters.");
|
||||
return null;
|
||||
}
|
||||
|
||||
Assert.IsType (type, (View)Activator.CreateInstance (type)!);
|
||||
}
|
||||
|
||||
@@ -591,4 +591,70 @@ public class SubViewTests
|
||||
Assert.NotEqual (superView, subView.SuperView);
|
||||
Assert.Empty (superView.SubViews);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void RemoveAll_Removes_All_SubViews ()
|
||||
{
|
||||
// Arrange
|
||||
var superView = new View ();
|
||||
var subView1 = new View ();
|
||||
var subView2 = new View ();
|
||||
var subView3 = new View ();
|
||||
|
||||
superView.Add (subView1, subView2, subView3);
|
||||
|
||||
// Act
|
||||
var removedViews = superView.RemoveAll ();
|
||||
|
||||
// Assert
|
||||
Assert.Empty (superView.SubViews);
|
||||
Assert.Equal (3, removedViews.Count);
|
||||
Assert.Contains (subView1, removedViews);
|
||||
Assert.Contains (subView2, removedViews);
|
||||
Assert.Contains (subView3, removedViews);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void RemoveAllTView_Removes_All_SubViews_Of_Specific_Type ()
|
||||
{
|
||||
// Arrange
|
||||
var superView = new View ();
|
||||
var subView1 = new View ();
|
||||
var subView2 = new View ();
|
||||
var subView3 = new View ();
|
||||
var subView4 = new Button ();
|
||||
|
||||
superView.Add (subView1, subView2, subView3, subView4);
|
||||
|
||||
// Act
|
||||
var removedViews = superView.RemoveAll<Button> ();
|
||||
|
||||
// Assert
|
||||
Assert.Equal (3, superView.SubViews.Count);
|
||||
Assert.DoesNotContain (subView4, superView.SubViews);
|
||||
Assert.Single (removedViews);
|
||||
Assert.Contains (subView4, removedViews);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void RemoveAllTView_Does_Not_Remove_Other_Types ()
|
||||
{
|
||||
// Arrange
|
||||
var superView = new View ();
|
||||
var subView1 = new View ();
|
||||
var subView2 = new Button ();
|
||||
var subView3 = new Label ();
|
||||
|
||||
superView.Add (subView1, subView2, subView3);
|
||||
|
||||
// Act
|
||||
var removedViews = superView.RemoveAll<Button> ();
|
||||
|
||||
// Assert
|
||||
Assert.Equal (2, superView.SubViews.Count);
|
||||
Assert.Contains (subView1, superView.SubViews);
|
||||
Assert.Contains (subView3, superView.SubViews);
|
||||
Assert.Single (removedViews);
|
||||
Assert.Contains (subView2, removedViews);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,124 +1,208 @@
|
||||
|
||||
namespace Terminal.Gui.ViewsTests;
|
||||
|
||||
public class FlagSelectorTests
|
||||
{
|
||||
[Fact]
|
||||
public void Initialization_ShouldSetDefaults()
|
||||
public void Initialization_ShouldSetDefaults ()
|
||||
{
|
||||
var flagSelector = new FlagSelector();
|
||||
var flagSelector = new FlagSelector ();
|
||||
|
||||
Assert.True(flagSelector.CanFocus);
|
||||
Assert.Equal(Dim.Auto(DimAutoStyle.Content), flagSelector.Width);
|
||||
Assert.Equal(Dim.Auto(DimAutoStyle.Content), flagSelector.Height);
|
||||
Assert.Equal(Orientation.Vertical, flagSelector.Orientation);
|
||||
Assert.True (flagSelector.CanFocus);
|
||||
Assert.Equal (Dim.Auto (DimAutoStyle.Content), flagSelector.Width);
|
||||
Assert.Equal (Dim.Auto (DimAutoStyle.Content), flagSelector.Height);
|
||||
Assert.Equal (Orientation.Vertical, flagSelector.Orientation);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void SetFlags_WithDictionary_ShouldSetFlags()
|
||||
public void SetFlags_WithDictionary_ShouldSetFlags ()
|
||||
{
|
||||
var flagSelector = new FlagSelector();
|
||||
var flagSelector = new FlagSelector ();
|
||||
var flags = new Dictionary<uint, string>
|
||||
{
|
||||
{ 1, "Flag1" },
|
||||
{ 2, "Flag2" }
|
||||
};
|
||||
|
||||
flagSelector.SetFlags(flags);
|
||||
flagSelector.SetFlags (flags);
|
||||
|
||||
Assert.Equal(flags, flagSelector.Flags);
|
||||
Assert.Equal (flags, flagSelector.Flags);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void SetFlags_WithEnum_ShouldSetFlags()
|
||||
public void SetFlags_WithEnum_ShouldSetFlags ()
|
||||
{
|
||||
var flagSelector = new FlagSelector();
|
||||
var flagSelector = new FlagSelector ();
|
||||
|
||||
flagSelector.SetFlags<FlagSelectorStyles>();
|
||||
flagSelector.SetFlags<FlagSelectorStyles> ();
|
||||
|
||||
var expectedFlags = Enum.GetValues<FlagSelectorStyles>()
|
||||
.ToDictionary(f => Convert.ToUInt32(f), f => f.ToString());
|
||||
var expectedFlags = Enum.GetValues<FlagSelectorStyles> ()
|
||||
.ToDictionary (f => Convert.ToUInt32 (f), f => f.ToString ());
|
||||
|
||||
Assert.Equal(expectedFlags, flagSelector.Flags);
|
||||
Assert.Equal (expectedFlags, flagSelector.Flags);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void SetFlags_WithEnumAndCustomNames_ShouldSetFlags()
|
||||
public void SetFlags_WithEnumAndCustomNames_ShouldSetFlags ()
|
||||
{
|
||||
var flagSelector = new FlagSelector();
|
||||
var flagSelector = new FlagSelector ();
|
||||
|
||||
flagSelector.SetFlags<FlagSelectorStyles>(f => f switch
|
||||
flagSelector.SetFlags<FlagSelectorStyles> (f => f switch
|
||||
{
|
||||
FlagSelectorStyles.ShowNone => "Show None Value",
|
||||
FlagSelectorStyles.ShowValueEdit => "Show Value Editor",
|
||||
FlagSelectorStyles.All => "Everything",
|
||||
_ => f.ToString()
|
||||
_ => f.ToString ()
|
||||
});
|
||||
|
||||
var expectedFlags = Enum.GetValues<FlagSelectorStyles>()
|
||||
.ToDictionary(f => Convert.ToUInt32(f), f => f switch
|
||||
var expectedFlags = Enum.GetValues<FlagSelectorStyles> ()
|
||||
.ToDictionary (f => Convert.ToUInt32 (f), f => f switch
|
||||
{
|
||||
FlagSelectorStyles.ShowNone => "Show None Value",
|
||||
FlagSelectorStyles.ShowValueEdit => "Show Value Editor",
|
||||
FlagSelectorStyles.All => "Everything",
|
||||
_ => f.ToString()
|
||||
_ => f.ToString ()
|
||||
});
|
||||
|
||||
Assert.Equal(expectedFlags, flagSelector.Flags);
|
||||
Assert.Equal (expectedFlags, flagSelector.Flags);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Value_Set_ShouldUpdateCheckedState()
|
||||
public void Value_Set_ShouldUpdateCheckedState ()
|
||||
{
|
||||
var flagSelector = new FlagSelector();
|
||||
var flagSelector = new FlagSelector ();
|
||||
var flags = new Dictionary<uint, string>
|
||||
{
|
||||
{ 1, "Flag1" },
|
||||
{ 2, "Flag2" }
|
||||
};
|
||||
|
||||
flagSelector.SetFlags(flags);
|
||||
flagSelector.SetFlags (flags);
|
||||
flagSelector.Value = 1;
|
||||
|
||||
var checkBox = flagSelector.SubViews.OfType<CheckBox>().First(cb => (uint)cb.Data == 1);
|
||||
Assert.Equal(CheckState.Checked, checkBox.CheckedState);
|
||||
var checkBox = flagSelector.SubViews.OfType<CheckBox> ().First (cb => (uint)cb.Data == 1);
|
||||
Assert.Equal (CheckState.Checked, checkBox.CheckedState);
|
||||
|
||||
checkBox = flagSelector.SubViews.OfType<CheckBox>().First(cb => (uint)cb.Data == 2);
|
||||
Assert.Equal(CheckState.UnChecked, checkBox.CheckedState);
|
||||
checkBox = flagSelector.SubViews.OfType<CheckBox> ().First (cb => (uint)cb.Data == 2);
|
||||
Assert.Equal (CheckState.UnChecked, checkBox.CheckedState);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Styles_Set_ShouldCreateSubViews()
|
||||
public void Styles_Set_ShouldCreateSubViews ()
|
||||
{
|
||||
var flagSelector = new FlagSelector();
|
||||
var flagSelector = new FlagSelector ();
|
||||
var flags = new Dictionary<uint, string>
|
||||
{
|
||||
{ 1, "Flag1" },
|
||||
{ 2, "Flag2" }
|
||||
};
|
||||
|
||||
flagSelector.SetFlags(flags);
|
||||
flagSelector.SetFlags (flags);
|
||||
flagSelector.Styles = FlagSelectorStyles.ShowNone;
|
||||
|
||||
Assert.Contains(flagSelector.SubViews, sv => sv is CheckBox cb && cb.Title == "None");
|
||||
Assert.Contains (flagSelector.SubViews, sv => sv is CheckBox cb && cb.Title == "None");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ValueChanged_Event_ShouldBeRaised()
|
||||
public void ValueChanged_Event_ShouldBeRaised ()
|
||||
{
|
||||
var flagSelector = new FlagSelector();
|
||||
var flagSelector = new FlagSelector ();
|
||||
var flags = new Dictionary<uint, string>
|
||||
{
|
||||
{ 1, "Flag1" },
|
||||
{ 2, "Flag2" }
|
||||
};
|
||||
|
||||
flagSelector.SetFlags(flags);
|
||||
flagSelector.SetFlags (flags);
|
||||
bool eventRaised = false;
|
||||
flagSelector.ValueChanged += (sender, args) => eventRaised = true;
|
||||
|
||||
flagSelector.Value = 1;
|
||||
|
||||
Assert.True(eventRaised);
|
||||
Assert.True (eventRaised);
|
||||
}
|
||||
|
||||
// Tests for FlagSelector<TEnum>
|
||||
[Fact]
|
||||
public void GenericInitialization_ShouldSetDefaults ()
|
||||
{
|
||||
var flagSelector = new FlagSelector<FlagSelectorStyles> ();
|
||||
|
||||
Assert.True (flagSelector.CanFocus);
|
||||
Assert.Equal (Dim.Auto (DimAutoStyle.Content), flagSelector.Width);
|
||||
Assert.Equal (Dim.Auto (DimAutoStyle.Content), flagSelector.Height);
|
||||
Assert.Equal (Orientation.Vertical, flagSelector.Orientation);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Generic_SetFlags_Methods_Throw ()
|
||||
{
|
||||
var flagSelector = new FlagSelector<FlagSelectorStyles> ();
|
||||
|
||||
Assert.Throws<InvalidOperationException> (() => flagSelector.SetFlags (new Dictionary<uint, string> ()));
|
||||
Assert.Throws<InvalidOperationException> (() => flagSelector.SetFlags<FlagSelectorStyles> ());
|
||||
Assert.Throws<InvalidOperationException> (() => flagSelector.SetFlags<FlagSelectorStyles> (styles => null));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Generic_ ()
|
||||
{
|
||||
var flagSelector = new FlagSelector<FlagSelectorStyles> ();
|
||||
|
||||
var flags = flagSelector.Flags;
|
||||
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void GenericSetFlagNames_ShouldSetFlagNames ()
|
||||
{
|
||||
var flagSelector = new FlagSelector<FlagSelectorStyles> ();
|
||||
|
||||
flagSelector.SetFlagNames (f => f switch
|
||||
{
|
||||
FlagSelectorStyles.ShowNone => "Show None Value",
|
||||
FlagSelectorStyles.ShowValueEdit => "Show Value Editor",
|
||||
FlagSelectorStyles.All => "Everything",
|
||||
_ => f.ToString ()
|
||||
});
|
||||
|
||||
var expectedFlags = Enum.GetValues<FlagSelectorStyles> ()
|
||||
.ToDictionary (f => Convert.ToUInt32 (f), f => f switch
|
||||
{
|
||||
FlagSelectorStyles.ShowNone => "Show None Value",
|
||||
FlagSelectorStyles.ShowValueEdit => "Show Value Editor",
|
||||
FlagSelectorStyles.All => "Everything",
|
||||
_ => f.ToString ()
|
||||
});
|
||||
|
||||
Assert.Equal (expectedFlags, flagSelector.Flags);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void GenericValue_Set_ShouldUpdateCheckedState ()
|
||||
{
|
||||
var flagSelector = new FlagSelector<FlagSelectorStyles> ();
|
||||
|
||||
flagSelector.SetFlagNames (f => f.ToString ());
|
||||
flagSelector.Value = FlagSelectorStyles.ShowNone;
|
||||
|
||||
var checkBox = flagSelector.SubViews.OfType<CheckBox> ().First (cb => (uint)cb.Data == Convert.ToUInt32 (FlagSelectorStyles.ShowNone));
|
||||
Assert.Equal (CheckState.Checked, checkBox.CheckedState);
|
||||
|
||||
checkBox = flagSelector.SubViews.OfType<CheckBox> ().First (cb => (uint)cb.Data == Convert.ToUInt32 (FlagSelectorStyles.ShowValueEdit));
|
||||
Assert.Equal (CheckState.UnChecked, checkBox.CheckedState);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void GenericValueChanged_Event_ShouldBeRaised ()
|
||||
{
|
||||
var flagSelector = new FlagSelector<FlagSelectorStyles> ();
|
||||
|
||||
flagSelector.SetFlagNames (f => f.ToString ());
|
||||
bool eventRaised = false;
|
||||
flagSelector.ValueChanged += (sender, args) => eventRaised = true;
|
||||
|
||||
flagSelector.Value = FlagSelectorStyles.ShowNone;
|
||||
|
||||
Assert.True (eventRaised);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user