RulerTests.cs 9.5 KB

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