RulerTests.cs 7.3 KB

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