PosTests.cs 27 KB

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