RulerTests.cs 9.5 KB

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