summaryrefslogtreecommitdiff
path: root/Xamarin.Forms.Core.UnitTests/PointTests.cs
blob: 40a31b81b9f6fcb3942a2888ba5fb0e7279bc9b5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
using System;
using NUnit.Framework;


namespace Xamarin.Forms.Core.UnitTests
{
	[TestFixture]
	public class PointTests : BaseTestFixture
	{
		[Test]
		public void TestPointEquality ()
		{
			Assert.True (new Point (0, 1) != new Point (1, 0));
			Assert.True (new Point (5, 5) == new Point (5, 5));
		}

		[Test]
		public void TestPointDistance ()
		{
			Assert.That (new Point (2, 2).Distance (new Point (5, 6)), Is.EqualTo (5).Within (0.001));
		}

		[Test]
		public void TestPointMath ()
		{
			var point = new Point (2, 3) + new Size (3, 2);
			Assert.AreEqual (new Point (5, 5), point);

			point = new Point (3, 4) - new Size (2, 3);
			Assert.AreEqual (new Point (1, 1), point);
		}

		[Test]
		public void TestPointFromSize ()
		{
			var point = new Point (new Size (10, 20));

			Assert.AreEqual (10, point.X);
			Assert.AreEqual (20, point.Y);
		}

		[Test]
		public void TestPointOffset ()
		{
			var point = new Point (2, 2);

			point = point.Offset (10, 20);

			Assert.AreEqual (new Point (12, 22), point);
		}

		[Test]
		public void TestPointRound ()
		{
			var point = new Point (2.4, 2.7);
			point = point.Round ();

			Assert.AreEqual (Math.Round (2.4), point.X);
			Assert.AreEqual (Math.Round (2.7), point.Y);
		}

		[Test]
		public void TestPointEmpty ()
		{
			var point = new Point ();

			Assert.True (point.IsEmpty);
		}

		[Test]
		public void TestPointHashCode ([Range (3, 6)] double x1, [Range (3, 6)] double y1, [Range (3, 6)] double x2,
		                               [Range (3, 6)] double y2)
		{
			bool result = new Point (x1, y1).GetHashCode () == new Point (x2, y2).GetHashCode ();
			if (x1 == x2 && y1 == y2)
				Assert.True (result);
			else
				Assert.False (result);
		}

		[Test]
		public void TestSizeFromPoint ()
		{
			var point = new Point (2, 4);
			var size = (Size) point;

			Assert.AreEqual (new Size (2, 4), size);
		}

		[Test]
		public void TestPointEquals ()
		{
			var point = new Point (2, 4);

			Assert.True (point.Equals (new Point (2, 4)));
			Assert.False (point.Equals (new Point (3, 4)));
			Assert.False (point.Equals ("Point"));
		}

		[Test]
		[TestCase (0, 0, ExpectedResult = "{X=0 Y=0}")]
		[TestCase (5, 2, ExpectedResult = "{X=5 Y=2}")]
		public string TestPointToString (double x, double y)
		{
			return new Point (x, y).ToString ();
		}

		[Test]
		public void TestPointTypeConverter ()
		{
			var converter = new PointTypeConverter ();
			Assert.True (converter.CanConvertFrom (typeof(string)));
			Assert.AreEqual (new Point (1, 2), converter.ConvertFromInvariantString ("1,2"));
			Assert.AreEqual (new Point (1, 2), converter.ConvertFromInvariantString ("1, 2"));
			Assert.AreEqual (new Point (1, 2), converter.ConvertFromInvariantString (" 1 , 2 "));
			Assert.AreEqual (new Point (1.1, 2), converter.ConvertFromInvariantString ("1.1,2"));
			Assert.Throws<InvalidOperationException> (()=>converter.ConvertFromInvariantString (""));
		}

	}
}