Browse Source

Merge pull request #650 from tig/responder_unit_tests

Added more unit tests. Now up to 22 tests.
Charlie Kindel 5 years ago
parent
commit
0ed5f0e50d
3 changed files with 277 additions and 0 deletions
  1. 127 0
      UnitTests/DimPosTests.cs
  2. 39 0
      UnitTests/ResponderTests.cs
  3. 111 0
      UnitTests/ViewTests.cs

+ 127 - 0
UnitTests/DimPosTests.cs

@@ -0,0 +1,127 @@
+using System;
+using System.Collections.Generic;
+using System.ComponentModel;
+using System.IO;
+using System.Linq;
+using Terminal.Gui;
+using Xunit;
+
+// Alais Console to MockConsole so we don't accidentally use Console
+using Console = Terminal.Gui.MockConsole;
+
+namespace Terminal.Gui {
+	public class DimPosTests {
+		[Fact]
+		public void TestNew ()
+		{
+			var pos = new Pos ();
+			Assert.Equal ("Terminal.Gui.Pos", pos.ToString ());
+		}
+
+		[Fact]
+		public void TestAnchorEnd ()
+		{
+			var pos = Pos.AnchorEnd ();
+			Assert.Equal ("Pos.AnchorEnd(margin=0)", pos.ToString ());
+
+			pos = Pos.AnchorEnd (5);
+			Assert.Equal ("Pos.AnchorEnd(margin=5)", pos.ToString ());
+		}
+
+		[Fact]
+		public void TestAt ()
+		{
+			var pos = Pos.At (0);
+			Assert.Equal ("Pos.Absolute(0)", pos.ToString ());
+
+			pos = Pos.At (5);
+			Assert.Equal ("Pos.Absolute(5)", pos.ToString ());
+
+			//Assert.Throws<ArgumentException> (() => pos = Pos.At (-1));
+		}
+
+		[Fact]
+		public void TestLeft ()
+		{
+			var pos = Pos.Left (null);
+			Assert.Throws<NullReferenceException> (() => pos.ToString ());
+
+			pos = Pos.Left (new View ());
+			Assert.Equal ("Pos.View(side=x, target=View()({X=0,Y=0,Width=0,Height=0})", pos.ToString ());
+
+			pos = Pos.Left (new View (new Rect (1, 2, 3, 4)));
+			Assert.Equal ("Pos.View(side=x, target=View()({X=1,Y=2,Width=3,Height=4})", pos.ToString ());
+		}
+
+		[Fact]
+		public void TestTop ()
+		{
+			var pos = Pos.Top (null);
+			Assert.Throws<NullReferenceException> (() => pos.ToString ());
+
+			pos = Pos.Top (new View ());
+			Assert.Equal ("Pos.View(side=y, target=View()({X=0,Y=0,Width=0,Height=0})", pos.ToString ());
+
+			pos = Pos.Top (new View (new Rect (1, 2, 3, 4)));
+			Assert.Equal ("Pos.View(side=y, target=View()({X=1,Y=2,Width=3,Height=4})", pos.ToString ());
+		}
+
+		[Fact]
+		public void TestRight ()
+		{
+			var pos = Pos.Right (null);
+			Assert.Throws<NullReferenceException> (() => pos.ToString ());
+
+			pos = Pos.Right (new View ());
+			Assert.Equal ("Pos.View(side=right, target=View()({X=0,Y=0,Width=0,Height=0})", pos.ToString ());
+
+			pos = Pos.Right (new View (new Rect (1, 2, 3, 4)));
+			Assert.Equal ("Pos.View(side=right, target=View()({X=1,Y=2,Width=3,Height=4})", pos.ToString ());
+		}
+
+		[Fact]
+		public void TestBottom ()
+		{
+			var pos = Pos.Bottom (null);
+			Assert.Throws<NullReferenceException> (() => pos.ToString ());
+
+			pos = Pos.Bottom (new View ());
+			Assert.Equal ("Pos.View(side=bottom, target=View()({X=0,Y=0,Width=0,Height=0})", pos.ToString ());
+
+			pos = Pos.Bottom (new View (new Rect (1, 2, 3, 4)));
+			Assert.Equal ("Pos.View(side=bottom, target=View()({X=1,Y=2,Width=3,Height=4})", pos.ToString ());
+
+			//Assert.Throws<ArgumentException> (() => pos = Pos.Bottom (new View (new Rect (0, 0, -3, -4))));
+
+		}
+
+		[Fact]
+		public void TestCenter ()
+		{
+			var pos = Pos.Center ();
+			Assert.Equal ("Pos.Center", pos.ToString ());
+		}
+
+		[Fact]
+		public void TestPercent ()
+		{
+			var pos = Pos.Percent (0);
+			Assert.Equal ("Pos.Factor(0)", pos.ToString ());
+
+			pos = Pos.Percent (0.5F);
+			Assert.Equal ("Pos.Factor(0.005)", pos.ToString ());
+
+			pos = Pos.Percent (100);
+			Assert.Equal ("Pos.Factor(1)", pos.ToString ());
+
+			Assert.Throws<ArgumentException> (() => pos = Pos.Percent (-1));
+			Assert.Throws<ArgumentException> (() => pos = Pos.Percent (101));
+			Assert.Throws<ArgumentException> (() => pos = Pos.Percent (100.0001F));
+			Assert.Throws<ArgumentException> (() => pos = Pos.Percent (1000001));
+		}
+
+		// TODO: Test operators
+
+		// TODO: Test Dim
+	}
+}

+ 39 - 0
UnitTests/ResponderTests.cs

@@ -0,0 +1,39 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using Terminal.Gui;
+using Xunit;
+
+// Alais Console to MockConsole so we don't accidentally use Console
+using Console = Terminal.Gui.MockConsole;
+
+namespace Terminal.Gui {
+	public class ResponderTests {
+		[Fact]
+		public void TestNew ()
+		{
+			var r = new Responder ();
+			Assert.NotNull (r);
+			Assert.Equal ("Terminal.Gui.Responder", r.ToString ());
+			Assert.False (r.CanFocus);
+			Assert.False (r.HasFocus);
+		}
+
+		[Fact]
+		public void TestMethods ()
+		{
+			var r = new Responder ();
+
+			Assert.False (r.ProcessKey (new KeyEvent () { Key = Key.Unknown }));
+			Assert.False (r.ProcessHotKey (new KeyEvent () { Key = Key.Unknown }));
+			Assert.False (r.ProcessColdKey (new KeyEvent () { Key = Key.Unknown }));
+			Assert.False (r.OnKeyDown (new KeyEvent () { Key = Key.Unknown }));
+			Assert.False (r.OnKeyUp (new KeyEvent () { Key = Key.Unknown }));
+			Assert.False (r.MouseEvent (new MouseEvent () { Flags = MouseFlags.AllEvents }));
+			Assert.False (r.OnMouseEnter (new MouseEvent () { Flags = MouseFlags.AllEvents }));
+			Assert.False (r.OnMouseLeave (new MouseEvent () { Flags = MouseFlags.AllEvents }));
+			Assert.False (r.OnEnter ());
+			Assert.False (r.OnLeave ());
+		}
+	}
+}

+ 111 - 0
UnitTests/ViewTests.cs

@@ -0,0 +1,111 @@
+using System;
+using System.Collections.Generic;
+using System.ComponentModel;
+using System.IO;
+using System.Linq;
+using Terminal.Gui;
+using Xunit;
+
+// Alais Console to MockConsole so we don't accidentally use Console
+using Console = Terminal.Gui.MockConsole;
+
+namespace Terminal.Gui {
+	public class ViewTests {
+		[Fact]
+		public void TestNew ()
+		{
+			// Parameterless
+			var r = new View ();
+			Assert.NotNull (r);
+			Assert.Equal (LayoutStyle.Computed, r.LayoutStyle);
+			Assert.Equal ("View()({X=0,Y=0,Width=0,Height=0})", r.ToString ());
+			Assert.False (r.CanFocus);
+			Assert.False (r.HasFocus);
+			Assert.Equal (new Rect (0, 0, 0, 0), r.Bounds);
+			Assert.Equal (new Rect (0, 0, 0, 0), r.Frame);
+			Assert.Null (r.Focused);
+			Assert.Null (r.ColorScheme);
+			Assert.Equal (Dim.Sized (0), r.Height);
+			Assert.Equal (Dim.Sized (0), r.Height);
+			// BUGBUG: Pos needs eqality implemented
+			//Assert.Equal (Pos.At (0), r.X);
+			//Assert.Equal (Pos.At (0), r.Y);
+			Assert.False (r.IsCurrentTop);
+			Assert.Empty (r.Id);
+			Assert.Empty (r.Subviews);
+			Assert.False (r.WantContinuousButtonPressed);
+			Assert.False (r.WantMousePositionReports);
+			Assert.Null (r.GetEnumerator().Current);
+			Assert.Null (r.SuperView);
+			Assert.Null (r.MostFocused);
+
+			// Empty Rect
+			r = new View (Rect.Empty);
+			Assert.NotNull (r);
+			Assert.Equal (LayoutStyle.Absolute, r.LayoutStyle);
+			Assert.Equal ("View()({X=0,Y=0,Width=0,Height=0})", r.ToString ());
+			Assert.False (r.CanFocus);
+			Assert.False (r.HasFocus);
+			Assert.Equal (new Rect (0, 0, 0, 0), r.Bounds);
+			Assert.Equal (new Rect (0, 0, 0, 0), r.Frame);
+			Assert.Null (r.Focused);
+			Assert.Null (r.ColorScheme);
+			Assert.Null (r.Height);
+			Assert.Null (r.Height);
+			Assert.Null (r.X);
+			Assert.Null (r.Y);
+			Assert.False (r.IsCurrentTop);
+			Assert.Empty (r.Id);
+			Assert.Empty (r.Subviews);
+			Assert.False (r.WantContinuousButtonPressed);
+			Assert.False (r.WantMousePositionReports);
+			Assert.Null (r.GetEnumerator ().Current);
+			Assert.Null (r.SuperView);
+			Assert.Null (r.MostFocused);
+
+			// Rect with values
+			r = new View (new Rect(1, 2, 3, 4));
+			Assert.NotNull (r);
+			Assert.Equal (LayoutStyle.Absolute, r.LayoutStyle);
+			Assert.Equal ("View()({X=1,Y=2,Width=3,Height=4})", r.ToString ());
+			Assert.False (r.CanFocus);
+			Assert.False (r.HasFocus);
+			Assert.Equal (new Rect (0, 0, 3, 4), r.Bounds);
+			Assert.Equal (new Rect (1, 2, 3, 4), r.Frame);
+			Assert.Null (r.Focused);
+			Assert.Null (r.ColorScheme);
+			Assert.Null (r.Height);
+			Assert.Null (r.Height);
+			Assert.Null (r.X);
+			Assert.Null (r.Y);
+			Assert.False (r.IsCurrentTop);
+			Assert.Empty (r.Id);
+			Assert.Empty (r.Subviews);
+			Assert.False (r.WantContinuousButtonPressed);
+			Assert.False (r.WantMousePositionReports);
+			Assert.Null (r.GetEnumerator ().Current);
+			Assert.Null (r.SuperView);
+			Assert.Null (r.MostFocused);
+
+		}
+
+		[Fact]
+		public void TestMethods ()
+		{
+			var r = new View ();
+
+			Assert.False (r.ProcessKey (new KeyEvent () { Key = Key.Unknown }));
+			Assert.False (r.ProcessHotKey (new KeyEvent () { Key = Key.Unknown }));
+			Assert.False (r.ProcessColdKey (new KeyEvent () { Key = Key.Unknown }));
+			Assert.False (r.OnKeyDown (new KeyEvent () { Key = Key.Unknown }));
+			Assert.False (r.OnKeyUp (new KeyEvent () { Key = Key.Unknown }));
+			Assert.False (r.MouseEvent (new MouseEvent () { Flags = MouseFlags.AllEvents }));
+			Assert.False (r.OnMouseEnter (new MouseEvent () { Flags = MouseFlags.AllEvents }));
+			Assert.False (r.OnMouseLeave (new MouseEvent () { Flags = MouseFlags.AllEvents }));
+			Assert.False (r.OnEnter ());
+			Assert.False (r.OnLeave ());
+
+			// TODO: Add more
+		}
+	}
+}