RulerTests.cs 7.3 KB

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