RulerTests.cs 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  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. Application.Refresh ();
  58. r.Draw (new (1, 1));
  59. TestHelpers.AssertDriverContentsWithFrameAre (
  60. @"
  61. ┌──────────────────┐
  62. │|123456789|1234 │
  63. │ │
  64. │ │
  65. └──────────────────┘",
  66. _output
  67. );
  68. // Negative offset
  69. Application.Refresh ();
  70. r.Draw (new (-1, 1));
  71. TestHelpers.AssertDriverContentsWithFrameAre (
  72. @"
  73. ┌──────────────────┐
  74. 123456789|1234 │
  75. │ │
  76. │ │
  77. └──────────────────┘",
  78. _output
  79. );
  80. // Clip
  81. Application.Refresh ();
  82. r.Draw (new (10, 1));
  83. TestHelpers.AssertDriverContentsWithFrameAre (
  84. @"
  85. ┌──────────────────┐
  86. │ |123456789
  87. │ │
  88. │ │
  89. └──────────────────┘",
  90. _output
  91. );
  92. top.Dispose ();
  93. }
  94. [Fact]
  95. [AutoInitShutdown]
  96. public void Draw_Horizontal_Start ()
  97. {
  98. var len = 15;
  99. // Add a frame so we can see the ruler
  100. var f = new FrameView { X = 0, Y = 0, Width = Dim.Fill (), Height = Dim.Fill () };
  101. var top = new Toplevel ();
  102. top.Add (f);
  103. Application.Begin (top);
  104. ((FakeDriver)Application.Driver!).SetBufferSize (len + 5, 5);
  105. Assert.Equal (new (0, 0, len + 5, 5), f.Frame);
  106. var r = new Ruler ();
  107. Assert.Equal (Orientation.Horizontal, r.Orientation);
  108. r.Length = len;
  109. r.Draw (Point.Empty, 1);
  110. TestHelpers.AssertDriverContentsWithFrameAre (
  111. @"
  112. 123456789|12345────┐
  113. │ │
  114. │ │
  115. │ │
  116. └──────────────────┘",
  117. _output
  118. );
  119. Application.Refresh ();
  120. r.Length = len;
  121. r.Draw (new (1, 0), 1);
  122. TestHelpers.AssertDriverContentsWithFrameAre (
  123. @"
  124. ┌123456789|12345───┐
  125. │ │
  126. │ │
  127. │ │
  128. └──────────────────┘",
  129. _output
  130. );
  131. top.Dispose ();
  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 (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 (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 (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 (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. top.Dispose ();
  255. }
  256. [Fact]
  257. [AutoInitShutdown]
  258. public void Draw_Vertical_Start ()
  259. {
  260. var len = 15;
  261. // Add a frame so we can see the ruler
  262. var f = new FrameView { X = 0, Y = 0, Width = Dim.Fill (), Height = Dim.Fill () };
  263. var top = new Toplevel ();
  264. top.Add (f);
  265. Application.Begin (top);
  266. ((FakeDriver)Application.Driver!).SetBufferSize (5, len + 5);
  267. Assert.Equal (new (0, 0, 5, len + 5), f.Frame);
  268. var r = new Ruler ();
  269. r.Orientation = Orientation.Vertical;
  270. r.Length = len;
  271. r.Draw (Point.Empty, 1);
  272. TestHelpers.AssertDriverContentsWithFrameAre (
  273. @"
  274. 1───┐
  275. 2 │
  276. 3 │
  277. 4 │
  278. 5 │
  279. 6 │
  280. 7 │
  281. 8 │
  282. 9 │
  283. - │
  284. 1 │
  285. 2 │
  286. 3 │
  287. 4 │
  288. 5 │
  289. │ │
  290. │ │
  291. │ │
  292. │ │
  293. └───┘",
  294. _output
  295. );
  296. Application.Refresh ();
  297. r.Length = len;
  298. r.Draw (new (0, 1), 1);
  299. TestHelpers.AssertDriverContentsWithFrameAre (
  300. @"
  301. ┌───┐
  302. 1 │
  303. 2 │
  304. 3 │
  305. 4 │
  306. 5 │
  307. 6 │
  308. 7 │
  309. 8 │
  310. 9 │
  311. - │
  312. 1 │
  313. 2 │
  314. 3 │
  315. 4 │
  316. 5 │
  317. │ │
  318. │ │
  319. │ │
  320. └───┘",
  321. _output
  322. );
  323. top.Dispose ();
  324. }
  325. [Fact]
  326. public void Length_set ()
  327. {
  328. var r = new Ruler ();
  329. Assert.Equal (0, r.Length);
  330. r.Length = 42;
  331. Assert.Equal (42, r.Length);
  332. }
  333. [Fact]
  334. public void Orientation_set ()
  335. {
  336. var r = new Ruler ();
  337. Assert.Equal (Orientation.Horizontal, r.Orientation);
  338. r.Orientation = Orientation.Vertical;
  339. Assert.Equal (Orientation.Vertical, r.Orientation);
  340. }
  341. }