PosTests.cs 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799
  1. using Xunit.Abstractions;
  2. namespace Terminal.Gui.ViewTests;
  3. public class PosTests (ITestOutputHelper output)
  4. {
  5. [Fact]
  6. public void At_Equal ()
  7. {
  8. var n1 = 0;
  9. var n2 = 0;
  10. Pos pos1 = Pos.At (n1);
  11. Pos pos2 = Pos.At (n2);
  12. Assert.Equal (pos1, pos2);
  13. }
  14. [Fact]
  15. public void At_SetsValue ()
  16. {
  17. Pos pos = Pos.At (0);
  18. Assert.Equal ("Absolute(0)", pos.ToString ());
  19. pos = Pos.At (5);
  20. Assert.Equal ("Absolute(5)", pos.ToString ());
  21. pos = Pos.At (-1);
  22. Assert.Equal ("Absolute(-1)", pos.ToString ());
  23. }
  24. [Fact]
  25. public void Center_SetsValue ()
  26. {
  27. Pos pos = Pos.Center ();
  28. Assert.Equal ("Center", pos.ToString ());
  29. }
  30. [Fact]
  31. public void DoNotReturnPosCombine ()
  32. {
  33. var v = new View { Id = "V" };
  34. Pos pos = Pos.Left (v);
  35. Assert.Equal (
  36. $"View(side=x,target=View(V){v.Frame})",
  37. pos.ToString ()
  38. );
  39. pos = Pos.X (v);
  40. Assert.Equal (
  41. $"View(side=x,target=View(V){v.Frame})",
  42. pos.ToString ()
  43. );
  44. pos = Pos.Top (v);
  45. Assert.Equal (
  46. $"View(side=y,target=View(V){v.Frame})",
  47. pos.ToString ()
  48. );
  49. pos = Pos.Y (v);
  50. Assert.Equal (
  51. $"View(side=y,target=View(V){v.Frame})",
  52. pos.ToString ()
  53. );
  54. pos = Pos.Right (v);
  55. Assert.Equal (
  56. $"View(side=right,target=View(V){v.Frame})",
  57. pos.ToString ()
  58. );
  59. pos = Pos.Bottom (v);
  60. Assert.Equal (
  61. $"View(side=bottom,target=View(V){v.Frame})",
  62. pos.ToString ()
  63. );
  64. }
  65. [Fact]
  66. public void Function_Equal ()
  67. {
  68. Func<int> f1 = () => 0;
  69. Func<int> f2 = () => 0;
  70. Pos pos1 = Pos.Function (f1);
  71. Pos pos2 = Pos.Function (f2);
  72. Assert.Equal (pos1, pos2);
  73. f2 = () => 1;
  74. pos2 = Pos.Function (f2);
  75. Assert.NotEqual (pos1, pos2);
  76. }
  77. [Fact]
  78. public void Function_SetsValue ()
  79. {
  80. var text = "Test";
  81. Pos pos = Pos.Function (() => text.Length);
  82. Assert.Equal ("PosFunc(4)", pos.ToString ());
  83. text = "New Test";
  84. Assert.Equal ("PosFunc(8)", pos.ToString ());
  85. text = "";
  86. Assert.Equal ("PosFunc(0)", pos.ToString ());
  87. }
  88. [Fact]
  89. [TestRespondersDisposed]
  90. public void Internal_Tests ()
  91. {
  92. var posFactor = new Pos.PosFactor (0.10F);
  93. Assert.Equal (10, posFactor.Anchor (100));
  94. var posAnchorEnd = new Pos.PosAnchorEnd (1);
  95. Assert.Equal (99, posAnchorEnd.Anchor (100));
  96. var posCenter = new Pos.PosCenter ();
  97. Assert.Equal (50, posCenter.Anchor (100));
  98. var posAbsolute = new Pos.PosAbsolute (10);
  99. Assert.Equal (10, posAbsolute.Anchor (0));
  100. var posCombine = new Pos.PosCombine (true, posFactor, posAbsolute);
  101. Assert.Equal (posCombine._left, posFactor);
  102. Assert.Equal (posCombine._right, posAbsolute);
  103. Assert.Equal (20, posCombine.Anchor (100));
  104. posCombine = new (true, posAbsolute, posFactor);
  105. Assert.Equal (posCombine._left, posAbsolute);
  106. Assert.Equal (posCombine._right, posFactor);
  107. Assert.Equal (20, posCombine.Anchor (100));
  108. var view = new View { Frame = new (20, 10, 20, 1) };
  109. var posViewX = new Pos.PosView (view, 0);
  110. Assert.Equal (20, posViewX.Anchor (0));
  111. var posViewY = new Pos.PosView (view, 1);
  112. Assert.Equal (10, posViewY.Anchor (0));
  113. var posRight = new Pos.PosView (view, 2);
  114. Assert.Equal (40, posRight.Anchor (0));
  115. var posViewBottom = new Pos.PosView (view, 3);
  116. Assert.Equal (11, posViewBottom.Anchor (0));
  117. view.Dispose ();
  118. }
  119. // TODO: This actually a SetRelativeLayout/LayoutSubViews test and should be moved
  120. // TODO: A new test that calls SetRelativeLayout directly is needed.
  121. // See: https://github.com/gui-cs/Terminal.Gui/issues/504
  122. [Fact]
  123. [TestRespondersDisposed]
  124. public void LeftTopBottomRight_Win_ShouldNotThrow ()
  125. {
  126. // Setup Fake driver
  127. (Toplevel top, Window win, Button button) setup ()
  128. {
  129. Application.Init (new FakeDriver ());
  130. Application.Iteration += (s, a) => { Application.RequestStop (); };
  131. var win = new Window { X = 0, Y = 0, Width = Dim.Fill (), Height = Dim.Fill () };
  132. var top = new Toplevel ();
  133. top.Add (win);
  134. var button = new Button { X = Pos.Center (), Text = "button" };
  135. win.Add (button);
  136. return (top, win, button);
  137. }
  138. RunState rs;
  139. void cleanup (RunState rs)
  140. {
  141. // Cleanup
  142. Application.End (rs);
  143. Application.Top.Dispose ();
  144. // Shutdown must be called to safely clean up Application if Init has been called
  145. Application.Shutdown ();
  146. }
  147. // Test cases:
  148. (Toplevel top, Window win, Button button) app = setup ();
  149. app.button.Y = Pos.Left (app.win);
  150. rs = Application.Begin (app.top);
  151. // If Application.RunState is used then we must use Application.RunLoop with the rs parameter
  152. Application.RunLoop (rs);
  153. cleanup (rs);
  154. app = setup ();
  155. app.button.Y = Pos.X (app.win);
  156. rs = Application.Begin (app.top);
  157. // If Application.RunState is used then we must use Application.RunLoop with the rs parameter
  158. Application.RunLoop (rs);
  159. cleanup (rs);
  160. app = setup ();
  161. app.button.Y = Pos.Top (app.win);
  162. rs = Application.Begin (app.top);
  163. // If Application.RunState is used then we must use Application.RunLoop with the rs parameter
  164. Application.RunLoop (rs);
  165. cleanup (rs);
  166. app = setup ();
  167. app.button.Y = Pos.Y (app.win);
  168. rs = Application.Begin (app.top);
  169. // If Application.RunState is used then we must use Application.RunLoop with the rs parameter
  170. Application.RunLoop (rs);
  171. cleanup (rs);
  172. app = setup ();
  173. app.button.Y = Pos.Bottom (app.win);
  174. rs = Application.Begin (app.top);
  175. // If Application.RunState is used then we must use Application.RunLoop with the rs parameter
  176. Application.RunLoop (rs);
  177. cleanup (rs);
  178. app = setup ();
  179. app.button.Y = Pos.Right (app.win);
  180. rs = Application.Begin (app.top);
  181. // If Application.RunState is used then we must use Application.RunLoop with the rs parameter
  182. Application.RunLoop (rs);
  183. cleanup (rs);
  184. }
  185. [Fact]
  186. public void New_Works ()
  187. {
  188. var pos = new Pos ();
  189. Assert.Equal ("Terminal.Gui.Pos", pos.ToString ());
  190. }
  191. [Fact]
  192. public void Percent_Equal ()
  193. {
  194. float n1 = 0;
  195. float n2 = 0;
  196. Pos pos1 = Pos.Percent (n1);
  197. Pos pos2 = Pos.Percent (n2);
  198. Assert.Equal (pos1, pos2);
  199. n1 = n2 = 1;
  200. pos1 = Pos.Percent (n1);
  201. pos2 = Pos.Percent (n2);
  202. Assert.Equal (pos1, pos2);
  203. n1 = n2 = 0.5f;
  204. pos1 = Pos.Percent (n1);
  205. pos2 = Pos.Percent (n2);
  206. Assert.Equal (pos1, pos2);
  207. n1 = n2 = 100f;
  208. pos1 = Pos.Percent (n1);
  209. pos2 = Pos.Percent (n2);
  210. Assert.Equal (pos1, pos2);
  211. n1 = 0;
  212. n2 = 1;
  213. pos1 = Pos.Percent (n1);
  214. pos2 = Pos.Percent (n2);
  215. Assert.NotEqual (pos1, pos2);
  216. n1 = 0.5f;
  217. n2 = 1.5f;
  218. pos1 = Pos.Percent (n1);
  219. pos2 = Pos.Percent (n2);
  220. Assert.NotEqual (pos1, pos2);
  221. }
  222. [Fact]
  223. public void Percent_SetsValue ()
  224. {
  225. float f = 0;
  226. Pos pos = Pos.Percent (f);
  227. Assert.Equal ($"Factor({f / 100:0.###})", pos.ToString ());
  228. f = 0.5F;
  229. pos = Pos.Percent (f);
  230. Assert.Equal ($"Factor({f / 100:0.###})", pos.ToString ());
  231. f = 100;
  232. pos = Pos.Percent (f);
  233. Assert.Equal ($"Factor({f / 100:0.###})", pos.ToString ());
  234. }
  235. [Fact]
  236. public void Percent_ThrowsOnIvalid ()
  237. {
  238. Pos pos = Pos.Percent (0);
  239. Assert.Throws<ArgumentException> (() => pos = Pos.Percent (-1));
  240. Assert.Throws<ArgumentException> (() => pos = Pos.Percent (101));
  241. Assert.Throws<ArgumentException> (() => pos = Pos.Percent (100.0001F));
  242. Assert.Throws<ArgumentException> (() => pos = Pos.Percent (1000001));
  243. }
  244. // TODO: This actually a SetRelativeLayout/LayoutSubViews test and should be moved
  245. // TODO: A new test that calls SetRelativeLayout directly is needed.
  246. [Fact]
  247. [TestRespondersDisposed]
  248. public void Pos_Add_Operator ()
  249. {
  250. Application.Init (new FakeDriver ());
  251. Toplevel top = new ();
  252. var view = new View { X = 0, Y = 0, Width = 20, Height = 20 };
  253. var field = new TextField { X = 0, Y = 0, Width = 20 };
  254. var count = 0;
  255. field.KeyDown += (s, k) =>
  256. {
  257. if (k.KeyCode == KeyCode.Enter)
  258. {
  259. field.Text = $"View {count}";
  260. var view2 = new View { X = 0, Y = field.Y, Width = 20, Text = field.Text };
  261. view.Add (view2);
  262. Assert.Equal ($"View {count}", view2.Text);
  263. Assert.Equal ($"Absolute({count})", view2.Y.ToString ());
  264. Assert.Equal ($"Absolute({count})", field.Y.ToString ());
  265. field.Y += 1;
  266. count++;
  267. Assert.Equal ($"Absolute({count})", field.Y.ToString ());
  268. }
  269. };
  270. Application.Iteration += (s, a) =>
  271. {
  272. while (count < 20)
  273. {
  274. field.NewKeyDownEvent (Key.Enter);
  275. }
  276. Application.RequestStop ();
  277. };
  278. var win = new Window ();
  279. win.Add (view);
  280. win.Add (field);
  281. top.Add (win);
  282. Application.Run (top);
  283. Assert.Equal (20, count);
  284. top.Dispose ();
  285. // Shutdown must be called to safely clean up Application if Init has been called
  286. Application.Shutdown ();
  287. }
  288. // TODO: This actually a SetRelativeLayout/LayoutSubViews test and should be moved
  289. // TODO: A new test that calls SetRelativeLayout directly is needed.
  290. [Fact]
  291. [TestRespondersDisposed]
  292. public void Pos_Subtract_Operator ()
  293. {
  294. Application.Init (new FakeDriver ());
  295. Toplevel top = new ();
  296. var view = new View { X = 0, Y = 0, Width = 20, Height = 20 };
  297. var field = new TextField { X = 0, Y = 0, Width = 20 };
  298. var count = 20;
  299. List<View> listViews = new ();
  300. for (var i = 0; i < count; i++)
  301. {
  302. field.Text = $"View {i}";
  303. var view2 = new View { X = 0, Y = field.Y, Width = 20, Text = field.Text };
  304. view.Add (view2);
  305. Assert.Equal ($"View {i}", view2.Text);
  306. Assert.Equal ($"Absolute({i})", field.Y.ToString ());
  307. listViews.Add (view2);
  308. Assert.Equal ($"Absolute({i})", field.Y.ToString ());
  309. field.Y += 1;
  310. Assert.Equal ($"Absolute({i + 1})", field.Y.ToString ());
  311. }
  312. field.KeyDown += (s, k) =>
  313. {
  314. if (k.KeyCode == KeyCode.Enter)
  315. {
  316. Assert.Equal ($"View {count - 1}", listViews [count - 1].Text);
  317. view.Remove (listViews [count - 1]);
  318. listViews [count - 1].Dispose ();
  319. Assert.Equal ($"Absolute({count})", field.Y.ToString ());
  320. field.Y -= 1;
  321. count--;
  322. Assert.Equal ($"Absolute({count})", field.Y.ToString ());
  323. }
  324. };
  325. Application.Iteration += (s, a) =>
  326. {
  327. while (count > 0)
  328. {
  329. field.NewKeyDownEvent (Key.Enter);
  330. }
  331. Application.RequestStop ();
  332. };
  333. var win = new Window ();
  334. win.Add (view);
  335. win.Add (field);
  336. top.Add (win);
  337. Application.Run (top);
  338. Assert.Equal (0, count);
  339. top.Dispose ();
  340. // Shutdown must be called to safely clean up Application if Init has been called
  341. Application.Shutdown ();
  342. }
  343. // TODO: This actually a SetRelativeLayout/LayoutSubViews test and should be moved
  344. // TODO: A new test that calls SetRelativeLayout directly is needed.
  345. [Fact]
  346. public void Pos_Validation_Do_Not_Throws_If_NewValue_Is_PosAbsolute_And_OldValue_Is_Null ()
  347. {
  348. Application.Init (new FakeDriver ());
  349. Toplevel t = new ();
  350. var w = new Window { X = 1, Y = 2, Width = 3, Height = 5 };
  351. t.Add (w);
  352. t.Ready += (s, e) =>
  353. {
  354. Assert.Equal (2, w.X = 2);
  355. Assert.Equal (2, w.Y = 2);
  356. };
  357. Application.Iteration += (s, a) => Application.RequestStop ();
  358. Application.Run (t);
  359. t.Dispose ();
  360. Application.Shutdown ();
  361. }
  362. // TODO: This actually a SetRelativeLayout/LayoutSubViews test and should be moved
  363. // TODO: A new test that calls SetRelativeLayout directly is needed.
  364. [Fact]
  365. public void PosCombine_Referencing_Same_View ()
  366. {
  367. var super = new View { Width = 10, Height = 10, Text = "super" };
  368. var view1 = new View { Width = 2, Height = 2, Text = "view1" };
  369. var view2 = new View { Width = 2, Height = 2, Text = "view2" };
  370. view2.X = Pos.AnchorEnd (0) - (Pos.Right (view2) - Pos.Left (view2));
  371. super.Add (view1, view2);
  372. super.BeginInit ();
  373. super.EndInit ();
  374. Exception exception = Record.Exception (super.LayoutSubviews);
  375. Assert.Null (exception);
  376. Assert.Equal (new (0, 0, 10, 10), super.Frame);
  377. Assert.Equal (new (0, 0, 2, 2), view1.Frame);
  378. Assert.Equal (new (8, 0, 2, 2), view2.Frame);
  379. super.Dispose ();
  380. }
  381. // TODO: This actually a SetRelativeLayout/LayoutSubViews test and should be moved
  382. // TODO: A new test that calls SetRelativeLayout directly is needed.
  383. [Fact]
  384. [TestRespondersDisposed]
  385. public void PosCombine_Will_Throws ()
  386. {
  387. Application.Init (new FakeDriver ());
  388. Toplevel t = new ();
  389. var w = new Window { X = Pos.Left (t) + 2, Y = Pos.Top (t) + 2 };
  390. var f = new FrameView ();
  391. var v1 = new View { X = Pos.Left (w) + 2, Y = Pos.Top (w) + 2 };
  392. var v2 = new View { X = Pos.Left (v1) + 2, Y = Pos.Top (v1) + 2 };
  393. f.Add (v1); // v2 not added
  394. w.Add (f);
  395. t.Add (w);
  396. f.X = Pos.X (v2) - Pos.X (v1);
  397. f.Y = Pos.Y (v2) - Pos.Y (v1);
  398. Assert.Throws<InvalidOperationException> (() => Application.Run (t));
  399. t.Dispose ();
  400. Application.Shutdown ();
  401. v2.Dispose ();
  402. }
  403. // TODO: This actually a SetRelativeLayout/LayoutSubViews test and should be moved
  404. // TODO: A new test that calls SetRelativeLayout directly is needed.
  405. [Theory]
  406. [AutoInitShutdown]
  407. [InlineData (true)]
  408. [InlineData (false)]
  409. public void PosPercentPlusOne (bool testHorizontal)
  410. {
  411. var container = new View { Width = 100, Height = 100 };
  412. var view = new View
  413. {
  414. X = testHorizontal ? Pos.Percent (50) + Pos.Percent (10) + 1 : 1,
  415. Y = testHorizontal ? 1 : Pos.Percent (50) + Pos.Percent (10) + 1,
  416. Width = 10,
  417. Height = 10
  418. };
  419. container.Add (view);
  420. var top = new Toplevel ();
  421. top.Add (container);
  422. top.LayoutSubviews ();
  423. Assert.Equal (100, container.Frame.Width);
  424. Assert.Equal (100, container.Frame.Height);
  425. if (testHorizontal)
  426. {
  427. Assert.Equal (61, view.Frame.X);
  428. Assert.Equal (1, view.Frame.Y);
  429. }
  430. else
  431. {
  432. Assert.Equal (1, view.Frame.X);
  433. Assert.Equal (61, view.Frame.Y);
  434. }
  435. }
  436. // TODO: Test Left, Top, Right bottom Equal
  437. /// <summary>Tests Pos.Left, Pos.X, Pos.Top, Pos.Y, Pos.Right, and Pos.Bottom set operations</summary>
  438. [Fact]
  439. [TestRespondersDisposed]
  440. public void PosSide_SetsValue ()
  441. {
  442. string side; // used in format string
  443. var testRect = Rectangle.Empty;
  444. var testInt = 0;
  445. Pos pos;
  446. // Pos.Left
  447. side = "x";
  448. testInt = 0;
  449. testRect = Rectangle.Empty;
  450. pos = Pos.Left (new ());
  451. Assert.Equal ($"View(side={side},target=View(){testRect})", pos.ToString ());
  452. pos = Pos.Left (new() { Frame = testRect });
  453. Assert.Equal ($"View(side={side},target=View(){testRect})", pos.ToString ());
  454. testRect = new (1, 2, 3, 4);
  455. pos = Pos.Left (new() { Frame = testRect });
  456. Assert.Equal ($"View(side={side},target=View(){testRect})", pos.ToString ());
  457. // Pos.Left(win) + 0
  458. pos = Pos.Left (new() { Frame = testRect }) + testInt;
  459. Assert.Equal (
  460. $"Combine(View(side={side},target=View(){testRect}){(testInt < 0 ? '-' : '+')}Absolute({testInt}))",
  461. pos.ToString ()
  462. );
  463. testInt = 1;
  464. // Pos.Left(win) +1
  465. pos = Pos.Left (new() { Frame = testRect }) + testInt;
  466. Assert.Equal (
  467. $"Combine(View(side={side},target=View(){testRect}){(testInt < 0 ? '-' : '+')}Absolute({testInt}))",
  468. pos.ToString ()
  469. );
  470. testInt = -1;
  471. // Pos.Left(win) -1
  472. pos = Pos.Left (new() { Frame = testRect }) - testInt;
  473. Assert.Equal (
  474. $"Combine(View(side={side},target=View(){testRect}){(testInt < 0 ? '-' : '+')}Absolute({testInt}))",
  475. pos.ToString ()
  476. );
  477. // Pos.X
  478. side = "x";
  479. testInt = 0;
  480. testRect = Rectangle.Empty;
  481. pos = Pos.X (new ());
  482. Assert.Equal ($"View(side={side},target=View(){testRect})", pos.ToString ());
  483. pos = Pos.X (new() { Frame = testRect });
  484. Assert.Equal ($"View(side={side},target=View(){testRect})", pos.ToString ());
  485. testRect = new (1, 2, 3, 4);
  486. pos = Pos.X (new() { Frame = testRect });
  487. Assert.Equal ($"View(side={side},target=View(){testRect})", pos.ToString ());
  488. // Pos.X(win) + 0
  489. pos = Pos.X (new() { Frame = testRect }) + testInt;
  490. Assert.Equal (
  491. $"Combine(View(side={side},target=View(){testRect}){(testInt < 0 ? '-' : '+')}Absolute({testInt}))",
  492. pos.ToString ()
  493. );
  494. testInt = 1;
  495. // Pos.X(win) +1
  496. pos = Pos.X (new() { Frame = testRect }) + testInt;
  497. Assert.Equal (
  498. $"Combine(View(side={side},target=View(){testRect}){(testInt < 0 ? '-' : '+')}Absolute({testInt}))",
  499. pos.ToString ()
  500. );
  501. testInt = -1;
  502. // Pos.X(win) -1
  503. pos = Pos.X (new() { Frame = testRect }) - testInt;
  504. Assert.Equal (
  505. $"Combine(View(side={side},target=View(){testRect}){(testInt < 0 ? '-' : '+')}Absolute({testInt}))",
  506. pos.ToString ()
  507. );
  508. // Pos.Top
  509. side = "y";
  510. testInt = 0;
  511. testRect = Rectangle.Empty;
  512. pos = Pos.Top (new ());
  513. Assert.Equal ($"View(side={side},target=View(){testRect})", pos.ToString ());
  514. pos = Pos.Top (new() { Frame = testRect });
  515. Assert.Equal ($"View(side={side},target=View(){testRect})", pos.ToString ());
  516. testRect = new (1, 2, 3, 4);
  517. pos = Pos.Top (new() { Frame = testRect });
  518. Assert.Equal ($"View(side={side},target=View(){testRect})", pos.ToString ());
  519. // Pos.Top(win) + 0
  520. pos = Pos.Top (new() { Frame = testRect }) + testInt;
  521. Assert.Equal (
  522. $"Combine(View(side={side},target=View(){testRect}){(testInt < 0 ? '-' : '+')}Absolute({testInt}))",
  523. pos.ToString ()
  524. );
  525. testInt = 1;
  526. // Pos.Top(win) +1
  527. pos = Pos.Top (new() { Frame = testRect }) + testInt;
  528. Assert.Equal (
  529. $"Combine(View(side={side},target=View(){testRect}){(testInt < 0 ? '-' : '+')}Absolute({testInt}))",
  530. pos.ToString ()
  531. );
  532. testInt = -1;
  533. // Pos.Top(win) -1
  534. pos = Pos.Top (new() { Frame = testRect }) - testInt;
  535. Assert.Equal (
  536. $"Combine(View(side={side},target=View(){testRect}){(testInt < 0 ? '-' : '+')}Absolute({testInt}))",
  537. pos.ToString ()
  538. );
  539. // Pos.Y
  540. side = "y";
  541. testInt = 0;
  542. testRect = Rectangle.Empty;
  543. pos = Pos.Y (new ());
  544. Assert.Equal ($"View(side={side},target=View(){testRect})", pos.ToString ());
  545. pos = Pos.Y (new() { Frame = testRect });
  546. Assert.Equal ($"View(side={side},target=View(){testRect})", pos.ToString ());
  547. testRect = new (1, 2, 3, 4);
  548. pos = Pos.Y (new() { Frame = testRect });
  549. Assert.Equal ($"View(side={side},target=View(){testRect})", pos.ToString ());
  550. // Pos.Y(win) + 0
  551. pos = Pos.Y (new() { Frame = testRect }) + testInt;
  552. Assert.Equal (
  553. $"Combine(View(side={side},target=View(){testRect}){(testInt < 0 ? '-' : '+')}Absolute({testInt}))",
  554. pos.ToString ()
  555. );
  556. testInt = 1;
  557. // Pos.Y(win) +1
  558. pos = Pos.Y (new() { Frame = testRect }) + testInt;
  559. Assert.Equal (
  560. $"Combine(View(side={side},target=View(){testRect}){(testInt < 0 ? '-' : '+')}Absolute({testInt}))",
  561. pos.ToString ()
  562. );
  563. testInt = -1;
  564. // Pos.Y(win) -1
  565. pos = Pos.Y (new() { Frame = testRect }) - testInt;
  566. Assert.Equal (
  567. $"Combine(View(side={side},target=View(){testRect}){(testInt < 0 ? '-' : '+')}Absolute({testInt}))",
  568. pos.ToString ()
  569. );
  570. // Pos.Bottom
  571. side = "bottom";
  572. testRect = Rectangle.Empty;
  573. testInt = 0;
  574. pos = Pos.Bottom (new ());
  575. Assert.Equal ($"View(side={side},target=View(){testRect})", pos.ToString ());
  576. pos = Pos.Bottom (new() { Frame = testRect });
  577. Assert.Equal ($"View(side={side},target=View(){testRect})", pos.ToString ());
  578. testRect = new (1, 2, 3, 4);
  579. pos = Pos.Bottom (new() { Frame = testRect });
  580. Assert.Equal ($"View(side={side},target=View(){testRect})", pos.ToString ());
  581. // Pos.Bottom(win) + 0
  582. pos = Pos.Bottom (new() { Frame = testRect }) + testInt;
  583. Assert.Equal (
  584. $"Combine(View(side={side},target=View(){testRect}){(testInt < 0 ? '-' : '+')}Absolute({testInt}))",
  585. pos.ToString ()
  586. );
  587. testInt = 1;
  588. // Pos.Bottom(win) +1
  589. pos = Pos.Bottom (new() { Frame = testRect }) + testInt;
  590. Assert.Equal (
  591. $"Combine(View(side={side},target=View(){testRect}){(testInt < 0 ? '-' : '+')}Absolute({testInt}))",
  592. pos.ToString ()
  593. );
  594. testInt = -1;
  595. // Pos.Bottom(win) -1
  596. pos = Pos.Bottom (new() { Frame = testRect }) - testInt;
  597. Assert.Equal (
  598. $"Combine(View(side={side},target=View(){testRect}){(testInt < 0 ? '-' : '+')}Absolute({testInt}))",
  599. pos.ToString ()
  600. );
  601. #if DEBUG_IDISPOSABLE
  602. // HACK: Force clean up of Responders to avoid having to Dispose all the Views created above.
  603. Responder.Instances.Clear ();
  604. #endif
  605. }
  606. [Fact]
  607. public void SetSide_Null_Throws ()
  608. {
  609. Pos pos = Pos.Left (null);
  610. Assert.Throws<NullReferenceException> (() => pos.ToString ());
  611. pos = Pos.X (null);
  612. Assert.Throws<NullReferenceException> (() => pos.ToString ());
  613. pos = Pos.Top (null);
  614. Assert.Throws<NullReferenceException> (() => pos.ToString ());
  615. pos = Pos.Y (null);
  616. Assert.Throws<NullReferenceException> (() => pos.ToString ());
  617. pos = Pos.Bottom (null);
  618. Assert.Throws<NullReferenceException> (() => pos.ToString ());
  619. pos = Pos.Right (null);
  620. Assert.Throws<NullReferenceException> (() => pos.ToString ());
  621. }
  622. }