RulerTests.cs 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  1. using Xunit.Abstractions;
  2. namespace Terminal.Gui.DrawingTests;
  3. public class RulerTests
  4. {
  5. private readonly ITestOutputHelper _output;
  6. public RulerTests (ITestOutputHelper output) { _output = output; }
  7. [Fact]
  8. public void Attribute_set ()
  9. {
  10. var newAttribute = new Attribute (Color.Red, Color.Green);
  11. var r = new Ruler ();
  12. r.Attribute = newAttribute;
  13. Assert.Equal (newAttribute, r.Attribute);
  14. }
  15. [Fact]
  16. public void Constructor_Defaults ()
  17. {
  18. var r = new Ruler ();
  19. Assert.Equal (0, r.Length);
  20. Assert.Equal (Orientation.Horizontal, r.Orientation);
  21. }
  22. [Fact]
  23. [AutoInitShutdown]
  24. public void Draw_Default ()
  25. {
  26. ((FakeDriver)Application.Driver!).SetBufferSize (25, 25);
  27. var r = new Ruler ();
  28. r.Draw (Point.Empty);
  29. TestHelpers.AssertDriverContentsWithFrameAre (@"", _output);
  30. }
  31. [Fact]
  32. [AutoInitShutdown]
  33. public void Draw_Horizontal ()
  34. {
  35. var len = 15;
  36. // Add a frame so we can see the ruler
  37. var f = new FrameView { X = 0, Y = 0, Width = Dim.Fill (), Height = Dim.Fill () };
  38. var top = new Toplevel ();
  39. top.Add (f);
  40. Application.Begin (top);
  41. ((FakeDriver)Application.Driver!).SetBufferSize (len + 5, 5);
  42. Assert.Equal (new (0, 0, len + 5, 5), f.Frame);
  43. var r = new Ruler ();
  44. Assert.Equal (Orientation.Horizontal, r.Orientation);
  45. r.Length = len;
  46. r.Draw (Point.Empty);
  47. TestHelpers.AssertDriverContentsWithFrameAre (
  48. @"
  49. |123456789|1234────┐
  50. │ │
  51. │ │
  52. │ │
  53. └──────────────────┘",
  54. _output
  55. );
  56. // Postive offset
  57. top.SetNeedsDraw ();
  58. Application.LayoutAndDrawToplevels ();
  59. r.Draw (new (1, 1));
  60. TestHelpers.AssertDriverContentsWithFrameAre (
  61. @"
  62. ┌──────────────────┐
  63. │|123456789|1234 │
  64. │ │
  65. │ │
  66. └──────────────────┘",
  67. _output
  68. );
  69. // Negative offset
  70. top.SetNeedsDraw ();
  71. Application.LayoutAndDrawToplevels ();
  72. r.Draw (new (-1, 1));
  73. TestHelpers.AssertDriverContentsWithFrameAre (
  74. @"
  75. ┌──────────────────┐
  76. 123456789|1234 │
  77. │ │
  78. │ │
  79. └──────────────────┘",
  80. _output
  81. );
  82. // Clip
  83. top.SetNeedsDraw ();
  84. Application.LayoutAndDrawToplevels ();
  85. r.Draw (new (10, 1));
  86. TestHelpers.AssertDriverContentsWithFrameAre (
  87. @"
  88. ┌──────────────────┐
  89. │ |123456789
  90. │ │
  91. │ │
  92. └──────────────────┘",
  93. _output
  94. );
  95. top.Dispose ();
  96. }
  97. [Fact]
  98. [AutoInitShutdown]
  99. public void Draw_Horizontal_Start ()
  100. {
  101. var len = 15;
  102. // Add a frame so we can see the ruler
  103. var f = new FrameView { X = 0, Y = 0, Width = Dim.Fill (), Height = Dim.Fill () };
  104. var top = new Toplevel ();
  105. top.Add (f);
  106. Application.Begin (top);
  107. ((FakeDriver)Application.Driver!).SetBufferSize (len + 5, 5);
  108. Assert.Equal (new (0, 0, len + 5, 5), f.Frame);
  109. var r = new Ruler ();
  110. Assert.Equal (Orientation.Horizontal, r.Orientation);
  111. r.Length = len;
  112. r.Draw (Point.Empty, 1);
  113. TestHelpers.AssertDriverContentsWithFrameAre (
  114. @"
  115. 123456789|12345────┐
  116. │ │
  117. │ │
  118. │ │
  119. └──────────────────┘",
  120. _output
  121. );
  122. f.SetNeedsDraw();
  123. Application.LayoutAndDrawToplevels ();
  124. r.Length = len;
  125. r.Draw (new (1, 0), 1);
  126. TestHelpers.AssertDriverContentsWithFrameAre (
  127. @"
  128. ┌123456789|12345───┐
  129. │ │
  130. │ │
  131. │ │
  132. └──────────────────┘",
  133. _output
  134. );
  135. top.Dispose ();
  136. }
  137. [Fact]
  138. [AutoInitShutdown]
  139. public void Draw_Vertical ()
  140. {
  141. var len = 15;
  142. // Add a frame so we can see the ruler
  143. var f = new FrameView { X = 0, Y = 0, Width = Dim.Fill (), Height = Dim.Fill () };
  144. var top = new Toplevel ();
  145. top.Add (f);
  146. Application.Begin (top);
  147. ((FakeDriver)Application.Driver!).SetBufferSize (5, len + 5);
  148. Assert.Equal (new (0, 0, 5, len + 5), f.Frame);
  149. var r = new Ruler ();
  150. r.Orientation = Orientation.Vertical;
  151. r.Length = len;
  152. r.Draw (Point.Empty);
  153. TestHelpers.AssertDriverContentsWithFrameAre (
  154. @"
  155. -───┐
  156. 1 │
  157. 2 │
  158. 3 │
  159. 4 │
  160. 5 │
  161. 6 │
  162. 7 │
  163. 8 │
  164. 9 │
  165. - │
  166. 1 │
  167. 2 │
  168. 3 │
  169. 4 │
  170. │ │
  171. │ │
  172. │ │
  173. │ │
  174. └───┘",
  175. _output
  176. );
  177. // Postive offset
  178. f.SetNeedsDraw ();
  179. Application.LayoutAndDrawToplevels (true);
  180. r.Draw (new (1, 1));
  181. TestHelpers.AssertDriverContentsWithFrameAre (
  182. @"
  183. ┌───┐
  184. │- │
  185. │1 │
  186. │2 │
  187. │3 │
  188. │4 │
  189. │5 │
  190. │6 │
  191. │7 │
  192. │8 │
  193. │9 │
  194. │- │
  195. │1 │
  196. │2 │
  197. │3 │
  198. │4 │
  199. │ │
  200. │ │
  201. │ │
  202. └───┘",
  203. _output
  204. );
  205. // Negative offset
  206. f.SetNeedsDraw ();
  207. Application.LayoutAndDrawToplevels ();
  208. r.Draw (new (1, -1));
  209. TestHelpers.AssertDriverContentsWithFrameAre (
  210. @"
  211. ┌1──┐
  212. │2 │
  213. │3 │
  214. │4 │
  215. │5 │
  216. │6 │
  217. │7 │
  218. │8 │
  219. │9 │
  220. │- │
  221. │1 │
  222. │2 │
  223. │3 │
  224. │4 │
  225. │ │
  226. │ │
  227. │ │
  228. │ │
  229. │ │
  230. └───┘",
  231. _output
  232. );
  233. // Clip
  234. f.SetNeedsDraw ();
  235. Application.LayoutAndDrawToplevels ();
  236. r.Draw (new (1, 10));
  237. TestHelpers.AssertDriverContentsWithFrameAre (
  238. @"
  239. ┌───┐
  240. │ │
  241. │ │
  242. │ │
  243. │ │
  244. │ │
  245. │ │
  246. │ │
  247. │ │
  248. │ │
  249. │- │
  250. │1 │
  251. │2 │
  252. │3 │
  253. │4 │
  254. │5 │
  255. │6 │
  256. │7 │
  257. │8 │
  258. └9──┘",
  259. _output
  260. );
  261. top.Dispose ();
  262. }
  263. [Fact]
  264. [AutoInitShutdown]
  265. public void Draw_Vertical_Start ()
  266. {
  267. var len = 15;
  268. // Add a frame so we can see the ruler
  269. var f = new FrameView { X = 0, Y = 0, Width = Dim.Fill (), Height = Dim.Fill () };
  270. var top = new Toplevel ();
  271. top.Add (f);
  272. Application.Begin (top);
  273. ((FakeDriver)Application.Driver!).SetBufferSize (5, len + 5);
  274. Assert.Equal (new (0, 0, 5, len + 5), f.Frame);
  275. var r = new Ruler ();
  276. r.Orientation = Orientation.Vertical;
  277. r.Length = len;
  278. r.Draw (Point.Empty, 1);
  279. TestHelpers.AssertDriverContentsWithFrameAre (
  280. @"
  281. 1───┐
  282. 2 │
  283. 3 │
  284. 4 │
  285. 5 │
  286. 6 │
  287. 7 │
  288. 8 │
  289. 9 │
  290. - │
  291. 1 │
  292. 2 │
  293. 3 │
  294. 4 │
  295. 5 │
  296. │ │
  297. │ │
  298. │ │
  299. │ │
  300. └───┘",
  301. _output
  302. );
  303. f.SetNeedsDraw ();
  304. Application.LayoutAndDrawToplevels (true);
  305. r.Length = len;
  306. r.Draw (new (0, 1), 1);
  307. TestHelpers.AssertDriverContentsWithFrameAre (
  308. @"
  309. ┌───┐
  310. 1 │
  311. 2 │
  312. 3 │
  313. 4 │
  314. 5 │
  315. 6 │
  316. 7 │
  317. 8 │
  318. 9 │
  319. - │
  320. 1 │
  321. 2 │
  322. 3 │
  323. 4 │
  324. 5 │
  325. │ │
  326. │ │
  327. │ │
  328. └───┘",
  329. _output
  330. );
  331. top.Dispose ();
  332. }
  333. [Fact]
  334. public void Length_set ()
  335. {
  336. var r = new Ruler ();
  337. Assert.Equal (0, r.Length);
  338. r.Length = 42;
  339. Assert.Equal (42, r.Length);
  340. }
  341. [Fact]
  342. public void Orientation_set ()
  343. {
  344. var r = new Ruler ();
  345. Assert.Equal (Orientation.Horizontal, r.Orientation);
  346. r.Orientation = Orientation.Vertical;
  347. Assert.Equal (Orientation.Vertical, r.Orientation);
  348. }
  349. }