RulerTests.cs 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  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.SetNeedsDisplay ();
  58. Application.Refresh ();
  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.SetNeedsDisplay ();
  71. Application.Refresh ();
  72. r.Draw (new (-1, 1));
  73. TestHelpers.AssertDriverContentsWithFrameAre (
  74. @"
  75. ┌──────────────────┐
  76. 123456789|1234 │
  77. │ │
  78. │ │
  79. └──────────────────┘",
  80. _output
  81. );
  82. // Clip
  83. top.SetNeedsDisplay ();
  84. Application.Refresh ();
  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. Application.Refresh (true);
  123. r.Length = len;
  124. r.Draw (new (1, 0), 1);
  125. TestHelpers.AssertDriverContentsWithFrameAre (
  126. @"
  127. ┌123456789|12345───┐
  128. │ │
  129. │ │
  130. │ │
  131. └──────────────────┘",
  132. _output
  133. );
  134. top.Dispose ();
  135. }
  136. [Fact]
  137. [AutoInitShutdown]
  138. public void Draw_Vertical ()
  139. {
  140. var len = 15;
  141. // Add a frame so we can see the ruler
  142. var f = new FrameView { X = 0, Y = 0, Width = Dim.Fill (), Height = Dim.Fill () };
  143. var top = new Toplevel ();
  144. top.Add (f);
  145. Application.Begin (top);
  146. ((FakeDriver)Application.Driver!).SetBufferSize (5, len + 5);
  147. Assert.Equal (new (0, 0, 5, len + 5), f.Frame);
  148. var r = new Ruler ();
  149. r.Orientation = Orientation.Vertical;
  150. r.Length = len;
  151. r.Draw (Point.Empty);
  152. TestHelpers.AssertDriverContentsWithFrameAre (
  153. @"
  154. -───┐
  155. 1 │
  156. 2 │
  157. 3 │
  158. 4 │
  159. 5 │
  160. 6 │
  161. 7 │
  162. 8 │
  163. 9 │
  164. - │
  165. 1 │
  166. 2 │
  167. 3 │
  168. 4 │
  169. │ │
  170. │ │
  171. │ │
  172. │ │
  173. └───┘",
  174. _output
  175. );
  176. // Postive offset
  177. Application.Refresh (true);
  178. r.Draw (new (1, 1));
  179. TestHelpers.AssertDriverContentsWithFrameAre (
  180. @"
  181. ┌───┐
  182. │- │
  183. │1 │
  184. │2 │
  185. │3 │
  186. │4 │
  187. │5 │
  188. │6 │
  189. │7 │
  190. │8 │
  191. │9 │
  192. │- │
  193. │1 │
  194. │2 │
  195. │3 │
  196. │4 │
  197. │ │
  198. │ │
  199. │ │
  200. └───┘",
  201. _output
  202. );
  203. // Negative offset
  204. Application.Refresh (true);
  205. r.Draw (new (1, -1));
  206. TestHelpers.AssertDriverContentsWithFrameAre (
  207. @"
  208. ┌1──┐
  209. │2 │
  210. │3 │
  211. │4 │
  212. │5 │
  213. │6 │
  214. │7 │
  215. │8 │
  216. │9 │
  217. │- │
  218. │1 │
  219. │2 │
  220. │3 │
  221. │4 │
  222. │ │
  223. │ │
  224. │ │
  225. │ │
  226. │ │
  227. └───┘",
  228. _output
  229. );
  230. // Clip
  231. Application.Refresh (true);
  232. r.Draw (new (1, 10));
  233. TestHelpers.AssertDriverContentsWithFrameAre (
  234. @"
  235. ┌───┐
  236. │ │
  237. │ │
  238. │ │
  239. │ │
  240. │ │
  241. │ │
  242. │ │
  243. │ │
  244. │ │
  245. │- │
  246. │1 │
  247. │2 │
  248. │3 │
  249. │4 │
  250. │5 │
  251. │6 │
  252. │7 │
  253. │8 │
  254. └9──┘",
  255. _output
  256. );
  257. top.Dispose ();
  258. }
  259. [Fact]
  260. [AutoInitShutdown]
  261. public void Draw_Vertical_Start ()
  262. {
  263. var len = 15;
  264. // Add a frame so we can see the ruler
  265. var f = new FrameView { X = 0, Y = 0, Width = Dim.Fill (), Height = Dim.Fill () };
  266. var top = new Toplevel ();
  267. top.Add (f);
  268. Application.Begin (top);
  269. ((FakeDriver)Application.Driver!).SetBufferSize (5, len + 5);
  270. Assert.Equal (new (0, 0, 5, len + 5), f.Frame);
  271. var r = new Ruler ();
  272. r.Orientation = Orientation.Vertical;
  273. r.Length = len;
  274. r.Draw (Point.Empty, 1);
  275. TestHelpers.AssertDriverContentsWithFrameAre (
  276. @"
  277. 1───┐
  278. 2 │
  279. 3 │
  280. 4 │
  281. 5 │
  282. 6 │
  283. 7 │
  284. 8 │
  285. 9 │
  286. - │
  287. 1 │
  288. 2 │
  289. 3 │
  290. 4 │
  291. 5 │
  292. │ │
  293. │ │
  294. │ │
  295. │ │
  296. └───┘",
  297. _output
  298. );
  299. Application.Refresh (true);
  300. r.Length = len;
  301. r.Draw (new (0, 1), 1);
  302. TestHelpers.AssertDriverContentsWithFrameAre (
  303. @"
  304. ┌───┐
  305. 1 │
  306. 2 │
  307. 3 │
  308. 4 │
  309. 5 │
  310. 6 │
  311. 7 │
  312. 8 │
  313. 9 │
  314. - │
  315. 1 │
  316. 2 │
  317. 3 │
  318. 4 │
  319. 5 │
  320. │ │
  321. │ │
  322. │ │
  323. └───┘",
  324. _output
  325. );
  326. top.Dispose ();
  327. }
  328. [Fact]
  329. public void Length_set ()
  330. {
  331. var r = new Ruler ();
  332. Assert.Equal (0, r.Length);
  333. r.Length = 42;
  334. Assert.Equal (42, r.Length);
  335. }
  336. [Fact]
  337. public void Orientation_set ()
  338. {
  339. var r = new Ruler ();
  340. Assert.Equal (Orientation.Horizontal, r.Orientation);
  341. r.Orientation = Orientation.Vertical;
  342. Assert.Equal (Orientation.Vertical, r.Orientation);
  343. }
  344. }