RulerTests.cs 7.2 KB

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