Skip to content

Commit

Permalink
Merge pull request #256 from dodexahedron/migrate-to-nunit-4
Browse files Browse the repository at this point in the history
Migrate to nunit 4
  • Loading branch information
tznind authored Dec 2, 2023
2 parents 164e65c + efa86fc commit 6258e77
Show file tree
Hide file tree
Showing 43 changed files with 1,002 additions and 991 deletions.
22 changes: 13 additions & 9 deletions tests/AddViewTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ namespace UnitTests;
/// <summary>
/// Tests for adding Views to other Views either with <see cref="AddViewOperation"/> or directly.
/// </summary>
[TestFixture]
internal class AddViewTests : Tests
{
[Test]
Expand All @@ -29,17 +30,17 @@ public void TestAdd_Undo()
var op = new AddViewOperation(lbl, designOut, "label1");

OperationManager.Instance.Do(op);
Assert.AreEqual(1, designOut.View.GetActualSubviews().OfType<Label>().Count());
Assert.That( designOut.View.GetActualSubviews().OfType<Label>().Count(), Is.EqualTo( 1 ) );

OperationManager.Instance.Undo();
Assert.AreEqual(0, designOut.View.GetActualSubviews().OfType<Label>().Count());
Assert.That( designOut.View.GetActualSubviews().OfType<Label>().Count(), Is.Zero );

viewToCode.GenerateDesignerCs(designOut, typeof(Dialog));

var codeToView = new CodeToView(designOut.SourceCode);
var designBackIn = codeToView.CreateInstance();

Assert.AreEqual(0, designBackIn.View.GetActualSubviews().OfType<Label>().Count());
Assert.That( designBackIn.View.GetActualSubviews().OfType<Label>().Count(), Is.Zero );
}

[Test]
Expand Down Expand Up @@ -67,7 +68,7 @@ public void TestAddUndoRedo_RoundTrip()

var lblIn = designBackIn.View.GetActualSubviews().OfType<Label>().Single();

Assert.AreEqual(lblOut.Text, lblIn.Text);
Assert.That(lblOut.Text, Is.EqualTo(lblIn.Text));
}

/// <summary>
Expand All @@ -89,15 +90,18 @@ public void Test60Percent_RoundTrip(bool? offset)
lbl.X = offset == null ? Pos.Percent(60) : offset.Value ? Pos.Percent(60) + 1 : Pos.Percent(60) - 1;
}, out var lblOut);

Assert.AreEqual(lblOut.Text, lblIn.Text);
Assert.That(lblOut.Text, Is.EqualTo(lblIn.Text));

lblIn.Width.GetDimType(out var outDimType, out var outDimValue, out var outDimOffset);
lblIn.X.GetPosType(new List<Design>(), out var outPosType, out var outPosValue, out var outPosOffset, out _, out _);

Assert.AreEqual(DimType.Percent, outDimType);
Assert.Less(Math.Abs(60f - outDimValue), 0.0001);
Assert.Multiple( ( ) =>
{
Assert.That(outDimType, Is.EqualTo( DimType.Percent));
Assert.That(60f - outDimValue, Is.Zero.Within( 0.0001f ));
Assert.AreEqual(PosType.Percent, outPosType);
Assert.Less(Math.Abs(60f - outPosValue), 0.0001);
Assert.That(outPosType, Is.EqualTo(PosType.Percent));
Assert.That(60f - outPosValue, Is.Zero.Within( 0.0001f ));
} );
}
}
63 changes: 34 additions & 29 deletions tests/BorderColorTests.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using NUnit.Framework;
using NUnit.Framework;
using System.Linq;
using System.Reflection;
using Terminal.Gui;
Expand All @@ -7,6 +7,7 @@

namespace UnitTests
{
[TestFixture]
internal class BorderColorTests : Tests
{
[Test]
Expand All @@ -15,35 +16,39 @@ public void TestRoundTrip_BorderColors_NeverSet()
var result = RoundTrip<Window, FrameView>((d, v) =>
{
// Our view should not have any border color to start with
Assert.AreEqual(-1, (int)v.Border.BorderBrush);
Assert.AreEqual(-1, (int)v.Border.Background);
Assert.Multiple( ( ) =>
{
Assert.That((int)v.Border.BorderBrush, Is.EqualTo( -1 ));
Assert.That((int)v.Border.Background, Is.EqualTo( -1 ));
} );
}, out _);

// Since there were no changes we would expect them to stay the same
// after reload
Assert.AreEqual(-1, (int)result.Border.BorderBrush);
Assert.AreEqual(-1, (int)result.Border.Background);
Assert.Multiple( ( ) =>
{
Assert.That((int)result.Border.BorderBrush, Is.EqualTo( -1 ));
Assert.That((int)result.Border.Background, Is.EqualTo( -1 ));
} );
}

[TestCase(true)]
[TestCase(false)]
public void TestCopyPasteContainer(bool alsoSelectSubElements)
[Test]
public void TestCopyPasteContainer([Values]bool alsoSelectSubElements)
{
RoundTrip<Window, FrameView>((d, v) =>
{
new AddViewOperation(new Label(), d, "lbl1").Do();
new AddViewOperation(new Label(), d, "lbl2").Do();
Assert.AreEqual(2, v.GetActualSubviews().Count(), "Expected the FrameView to have 2 children (lbl1 and lbl2)");
Assert.That(v.GetActualSubviews().Count, Is.EqualTo(2),"Expected the FrameView to have 2 children (lbl1 and lbl2)");
Design[] toCopy;
if (alsoSelectSubElements)
{
var lbl1Design = (Design)d.View.GetActualSubviews().First().Data;
Assert.AreEqual("lbl1", lbl1Design.FieldName);
Assert.That(lbl1Design.FieldName, Is.EqualTo("lbl1"));
toCopy = new Design[] { d, lbl1Design };
}
else
Expand All @@ -52,31 +57,31 @@ public void TestCopyPasteContainer(bool alsoSelectSubElements)
}
// copy the FrameView
Assert.IsTrue(new CopyOperation(toCopy).Do());
Assert.That(new CopyOperation(toCopy).Do(), Is.True);
var rootDesign = d.GetRootDesign();
// paste the FrameView
Assert.IsTrue(new PasteOperation(rootDesign).Do());
Assert.That(new PasteOperation(rootDesign).Do(), Is.True);
var rootSubviews = rootDesign.View.GetActualSubviews();
Assert.AreEqual(2, rootSubviews.Count, "Expected root to have 2 FrameView now");
Assert.IsTrue(rootSubviews.All(v => v is FrameView));
Assert.IsTrue(
rootSubviews.All(f => f.GetActualSubviews().Count() == 2),
"Expected both FrameView (copied and pasted) to have the full contents of 2 Labels");
// Since there were no changes we would expect them to stay the same
// after reload
foreach (var rsv in rootSubviews)
Assert.Multiple( ( ) =>
{
Assert.AreEqual(-1, (int)rsv.Border.BorderBrush);
Assert.AreEqual(-1, (int)rsv.Border.Background);
Assert.Null(GetFieldValue<Color?>(rsv.Border, "borderBrush"));
Assert.Null(GetFieldValue<Color?>(rsv.Border, "background"));
}
Assert.That(rootSubviews.Count, Is.EqualTo(2), "Expected root to have 2 FrameView now");
Assert.That(rootSubviews, Is.All.TypeOf<FrameView>());
Assert.That( rootSubviews.Select(f=>f.GetActualSubviews( )),
Has.All.Count.EqualTo( 2 ),
"Expected both FrameView (copied and pasted) to have the full contents of 2 Labels");
// Since there were no changes we would expect them to stay the same
// after reload
Assert.That(rootSubviews.Select(rsv=> rsv.Border.BorderBrush).Cast<int>( ), Has.All.EqualTo(-1));
Assert.That(rootSubviews.Select(rsv=> rsv.Border.Background).Cast<int>( ), Has.All.EqualTo(-1));
Assert.That(rootSubviews.Select(rsv=> GetFieldValue<Color?>( rsv.Border, "borderBrush" )), Has.All.Null);
Assert.That(rootSubviews.Select(rsv=> GetFieldValue<Color?>( rsv.Border, "background" )), Has.All.Null);
} );
}
, out _);
}
Expand Down
64 changes: 32 additions & 32 deletions tests/ColorSchemeTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ public void TestHasColorScheme(bool whenMultiSelected)

var state = Application.Begin(window);

Assert.AreSame(Colors.Base, d.View.ColorScheme);
Assert.IsNotNull(d.View.ColorScheme);
Assert.IsFalse(d.HasKnownColorScheme());
ClassicAssert.AreSame(Colors.Base, d.View.ColorScheme);
ClassicAssert.IsNotNull(d.View.ColorScheme);
ClassicAssert.IsFalse(d.HasKnownColorScheme());

var scheme = new ColorScheme();
var prop = new SetPropertyOperation(d, d.GetDesignableProperty(nameof(View.ColorScheme))
Expand All @@ -33,7 +33,7 @@ public void TestHasColorScheme(bool whenMultiSelected)
prop.Do();

// we still don't know about this scheme yet
Assert.IsFalse(d.HasKnownColorScheme());
ClassicAssert.IsFalse(d.HasKnownColorScheme());

ColorSchemeManager.Instance.AddOrUpdateScheme("fff", scheme, d);

Expand All @@ -43,7 +43,7 @@ public void TestHasColorScheme(bool whenMultiSelected)
}

// now we know about it
Assert.IsTrue(d.HasKnownColorScheme());
ClassicAssert.IsTrue(d.HasKnownColorScheme());

ColorSchemeManager.Instance.Clear();

Expand All @@ -60,18 +60,18 @@ public void TestTrackingColorSchemes()

var d = new Design(new SourceCodeFile(new FileInfo("TestTrackingColorSchemes.cs")), Design.RootDesignName, view);

Assert.AreEqual(0, mgr.Schemes.Count);
ClassicAssert.AreEqual(0, mgr.Schemes.Count);
mgr.FindDeclaredColorSchemes(d);
Assert.AreEqual(2, mgr.Schemes.Count);
ClassicAssert.AreEqual(2, mgr.Schemes.Count);

var found = mgr.GetNameForColorScheme(new ColorScheme
{
Normal = new Attribute(Color.Magenta, Color.Black),
Focus = new Attribute(Color.Cyan, Color.Black),
});

Assert.IsNotNull(found);
Assert.AreEqual("aaa", found);
ClassicAssert.IsNotNull(found);
ClassicAssert.AreEqual("aaa", found);
mgr.Clear();
}

Expand All @@ -90,7 +90,7 @@ public void TestColorSchemeProperty_ToString(bool testMultiSelectingSeveralTimes

var p = (ColorSchemeProperty)(btnDesign.GetDesignableProperty(nameof(View.ColorScheme)) ?? throw new Exception("Expected this not to be null"));

Assert.AreEqual("ColorScheme:(Inherited)", p.ToString());
ClassicAssert.AreEqual("ColorScheme:(Inherited)", p.ToString());

// Define a new color scheme
var mgr = ColorSchemeManager.Instance;
Expand All @@ -105,7 +105,7 @@ public void TestColorSchemeProperty_ToString(bool testMultiSelectingSeveralTimes
mgr.AddOrUpdateScheme("pink", pink, btnDesign);

p.SetValue(pink);
Assert.AreEqual("ColorScheme:pink", p.ToString());
ClassicAssert.AreEqual("ColorScheme:pink", p.ToString());

// when multiselecting (with a selection box) a bunch of views
// all the views turn to green. But we shouldn't loose track
Expand All @@ -121,15 +121,15 @@ public void TestColorSchemeProperty_ToString(bool testMultiSelectingSeveralTimes
selection.SetSelection(p.Design);
selection.Clear();

Assert.AreEqual(pink, p.Design.View.ColorScheme);
ClassicAssert.AreEqual(pink, p.Design.View.ColorScheme);
}

selection.SetSelection(p.Design);
Assert.AreNotEqual(pink, p.Design.View.ColorScheme, "Expected view to be selected so be green not pink");
Assert.AreEqual("ColorScheme:pink", p.ToString(), "Expected us to know it was pink under the hood even while selected");
ClassicAssert.AreNotEqual(pink, p.Design.View.ColorScheme, "Expected view to be selected so be green not pink");
ClassicAssert.AreEqual("ColorScheme:pink", p.ToString(), "Expected us to know it was pink under the hood even while selected");
selection.Clear();

Assert.AreEqual(pink, p.Design.View.ColorScheme);
ClassicAssert.AreEqual(pink, p.Design.View.ColorScheme);
}

[Test]
Expand All @@ -140,7 +140,7 @@ public void TestColorSchemeProperty_ToString_SelectThenSetScheme()
var v = this.Get10By10View();
var p = (ColorSchemeProperty)(v.GetDesignableProperty(nameof(View.ColorScheme)) ?? throw new Exception("Expected this not to be null"));

Assert.AreEqual("ColorScheme:(Inherited)", p.ToString());
ClassicAssert.AreEqual("ColorScheme:(Inherited)", p.ToString());

// Define a new color scheme
var mgr = ColorSchemeManager.Instance;
Expand All @@ -158,10 +158,10 @@ public void TestColorSchemeProperty_ToString_SelectThenSetScheme()
SelectionManager.Instance.SetSelection(p.Design);

p.SetValue(pink);
Assert.AreEqual("ColorScheme:pink", p.ToString());
ClassicAssert.AreEqual("ColorScheme:pink", p.ToString());

SelectionManager.Instance.Clear();
Assert.AreEqual("ColorScheme:pink", p.ToString(), "Expected clearing selection not to reset an old scheme");
ClassicAssert.AreEqual("ColorScheme:pink", p.ToString(), "Expected clearing selection not to reset an old scheme");
}

/// <summary>
Expand Down Expand Up @@ -196,19 +196,19 @@ public void TestColorScheme_RoundTrip(bool multiSelectBeforeSaving)
if (multiSelectBeforeSaving)
{
Assert.AreEqual(mgr.Schemes.Single().Scheme, l.ColorScheme);
ClassicAssert.AreEqual(mgr.Schemes.Single().Scheme, l.ColorScheme);
SelectionManager.Instance.SetSelection((Design)l.Data);
Assert.AreNotEqual(mgr.Schemes.Single().Scheme, l.ColorScheme, "Expected multi selecting the view to change its color to the selected color");
ClassicAssert.AreNotEqual(mgr.Schemes.Single().Scheme, l.ColorScheme, "Expected multi selecting the view to change its color to the selected color");
}
}, out _);

var lblDesignIn = (Design)lblIn.Data;
Assert.IsTrue(lblDesignIn.HasKnownColorScheme());
ClassicAssert.IsTrue(lblDesignIn.HasKnownColorScheme());

// clear the selection before we do the comparison
SelectionManager.Instance.Clear();

Assert.AreEqual("pink", mgr.GetNameForColorScheme(lblDesignIn.View.ColorScheme));
ClassicAssert.AreEqual("pink", mgr.GetNameForColorScheme(lblDesignIn.View.ColorScheme));

mgr.Clear();
}
Expand All @@ -218,9 +218,9 @@ public void TestDefaultColors()
{
var d = new DefaultColorSchemes();

Assert.Contains(d.GreenOnBlack, d.GetDefaultSchemes().ToArray());
Assert.Contains(d.RedOnBlack, d.GetDefaultSchemes().ToArray());
Assert.Contains(d.BlueOnBlack, d.GetDefaultSchemes().ToArray());
ClassicAssert.Contains(d.GreenOnBlack, d.GetDefaultSchemes().ToArray());
ClassicAssert.Contains(d.RedOnBlack, d.GetDefaultSchemes().ToArray());
ClassicAssert.Contains(d.BlueOnBlack, d.GetDefaultSchemes().ToArray());
}

[TestCase(true)]
Expand All @@ -234,11 +234,11 @@ public void TestEditingSchemeAfterLoad(bool withSelection)
{
// Clear known default colors
ColorSchemeManager.Instance.Clear();
Assert.IsEmpty(ColorSchemeManager.Instance.Schemes);
ClassicAssert.IsEmpty(ColorSchemeManager.Instance.Schemes);
// Add a new color for our Label
ColorSchemeManager.Instance.AddOrUpdateScheme("yarg", scheme, d.GetRootDesign());
Assert.AreEqual(1, ColorSchemeManager.Instance.Schemes.Count);
ClassicAssert.AreEqual(1, ColorSchemeManager.Instance.Schemes.Count);
// Assign the new color to the view
var prop = new SetPropertyOperation(d, new ColorSchemeProperty(d), null, scheme);
Expand All @@ -259,9 +259,9 @@ public void TestEditingSchemeAfterLoad(bool withSelection)

ColorSchemeManager.Instance.Clear();
ColorSchemeManager.Instance.FindDeclaredColorSchemes(lblInDesign.GetRootDesign());
Assert.AreEqual(1, ColorSchemeManager.Instance.Schemes.Count, "Reloading the view should find the explicitly declared scheme 'yarg'");
ClassicAssert.AreEqual(1, ColorSchemeManager.Instance.Schemes.Count, "Reloading the view should find the explicitly declared scheme 'yarg'");

Assert.AreEqual(
ClassicAssert.AreEqual(
"yarg",

ColorSchemeManager.Instance.GetNameForColorScheme(
Expand All @@ -272,15 +272,15 @@ public void TestEditingSchemeAfterLoad(bool withSelection)
// make a change to the yarg scheme (e.g. if user opened the color designer and made some changes)
ColorSchemeManager.Instance.AddOrUpdateScheme("yarg", new ColorScheme { Normal = new Attribute(Color.Cyan, Color.BrightBlue) }, lblInDesign.GetRootDesign());

Assert.AreEqual(
ClassicAssert.AreEqual(
"yarg",
ColorSchemeManager.Instance.GetNameForColorScheme(
(withSelection ? lblInDesign.State.OriginalScheme : lblIn.GetExplicitColorScheme())
?? throw new Exception("Expected lblIn to have an explicit ColorScheme")),
"Expected designer to still know the name of lblIn ColorScheme");

Assert.AreEqual(Color.Cyan, lblIn.ColorScheme.Normal.Foreground, "Expected Label to be updated with the new color after being changed in designer");
Assert.AreEqual(Color.Cyan, lblInDesign.State.OriginalScheme?.Normal.Foreground, "Expected Label Design to also be updated with the new color");
ClassicAssert.AreEqual(Color.Cyan, lblIn.ColorScheme.Normal.Foreground, "Expected Label to be updated with the new color after being changed in designer");
ClassicAssert.AreEqual(Color.Cyan, lblInDesign.State.OriginalScheme?.Normal.Foreground, "Expected Label Design to also be updated with the new color");
}

class TestClass : View
Expand Down
Loading

0 comments on commit 6258e77

Please sign in to comment.