25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

canvas.py 12 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  1. # -*- coding: utf8 -*-
  2. #
  3. # libcaca Colour ASCII Art library
  4. # Python language bindings
  5. # Copyright (c) 2012 Alex Foulon <alxf@lavabit.com>
  6. # All Rights Reserved
  7. #
  8. # This program is free software. It comes without any warranty, to
  9. # the extent permitted by applicable law. You can redistribute it
  10. # and/or modify it under the terms of the Do What the Fuck You Want
  11. # to Public License, Version 2, as published by Sam Hocevar. See
  12. # http://www.wtfpl.net/ for more details.
  13. #
  14. """ Libcaca canvas test suite """
  15. import unittest
  16. from caca.canvas import Canvas, CanvasError
  17. class CanvasTestCase(unittest.TestCase):
  18. """ Class to test canvas functions.
  19. """
  20. def test_create(self):
  21. """ module canvas: Canvas.__init__()
  22. """
  23. self.assertTrue(isinstance(Canvas(), Canvas))
  24. self.assertTrue(isinstance(Canvas(10, 10), Canvas))
  25. self.assertRaises(CanvasError, Canvas, "a", 12)
  26. self.assertRaises(CanvasError, Canvas, 12, "a")
  27. def test_get_width(self):
  28. """ module canvas: Canvas.get_width()
  29. """
  30. cv = Canvas(5, 10)
  31. self.assertEqual(5, cv.get_width())
  32. def test_get_height(self):
  33. """ module canvas: Canvas.get_width()
  34. """
  35. cv = Canvas(5, 10)
  36. self.assertEqual(10, cv.get_height())
  37. def test_set_size(self):
  38. """ module canvas: Canvas.set_size()
  39. """
  40. cv = Canvas(0, 0)
  41. cv.set_size(1234, 1001)
  42. self.assertEqual((1234, 1001), (cv.get_width(), cv.get_height()))
  43. self.assertRaises(CanvasError, cv.set_size, "a", 12)
  44. self.assertRaises(CanvasError, cv.set_size, 12, "a")
  45. def test_get_char(self):
  46. """ module canvas: Canvas.get_char()
  47. """
  48. cv = Canvas(1, 1)
  49. self.assertEqual(" ", cv.get_char(0, 0))
  50. def test_put_char(self):
  51. """ module canvas: Canvas.put_char()
  52. """
  53. cv = Canvas(10, 1)
  54. self.assertEqual(1, cv.put_char(0, 0, "z"))
  55. self.assertEqual(1, cv.put_char(1, 0, "é"))
  56. self.assertEqual("z", cv.get_char(0, 0))
  57. self.assertEqual("é", cv.get_char(1, 0))
  58. self.assertRaises(CanvasError, cv.put_char, "a", 1, 2)
  59. self.assertRaises(CanvasError, cv.put_char, "a", 1, "b")
  60. def test_put_str(self):
  61. """ module canvas: Canvas.put_str()
  62. """
  63. cv = Canvas(10, 1)
  64. self.assertEqual(10, cv.put_str(0, 0, "teststring"))
  65. liststring = []
  66. for i in range(0, 10):
  67. liststring.append(cv.get_char(i, 0))
  68. self.assertEqual("teststring", "".join(liststring))
  69. self.assertRaises(CanvasError, cv.put_str, 0, 0, 111)
  70. self.assertRaises(CanvasError, cv.put_str, 0, "z", "abc")
  71. def test_printf(self):
  72. """ module canvas: Canvas.printf()
  73. """
  74. word1, word2 = "test", "string"
  75. cv = Canvas(10, 1)
  76. self.assertEqual(10, cv.printf(0, 0, "%s%s", word1, word2))
  77. liststring = []
  78. for i in range(0, 10):
  79. liststring.append(cv.get_char(i, 0))
  80. self.assertEqual("teststring", "".join(liststring))
  81. self.assertRaises(CanvasError, cv.printf, 0, 0, 111)
  82. self.assertRaises(CanvasError, cv.printf, 0, "z", "abc %s", "def")
  83. def test_wherex(self):
  84. """ module canvas: Canvas.wherex()
  85. """
  86. cv = Canvas(10, 10)
  87. self.assertEqual(0, cv.wherex())
  88. def test_wherey(self):
  89. """ module canvas: Canvas.wherey()
  90. """
  91. cv = Canvas(10, 10)
  92. self.assertEqual(0, cv.wherey())
  93. def test_gotoxy(self):
  94. """ module canvas: Canvas.gotoxy()
  95. """
  96. cv = Canvas(10, 10)
  97. cv.gotoxy(5, 5)
  98. self.assertEqual((5, 5), (cv.wherex(), cv.wherey()))
  99. self.assertRaises(CanvasError, cv.gotoxy, "a", 9)
  100. self.assertRaises(CanvasError, cv.gotoxy, 9, "a")
  101. def test_clear(self):
  102. """ module canvas: Canvas.clear()
  103. """
  104. cv = Canvas(5, 1)
  105. cv.put_str(0, 0, "test!")
  106. cv.clear()
  107. data = cv.export_to_memory("utf8").strip('\n')
  108. self.assertEqual(" ", data)
  109. def test_get_handle_x(self):
  110. """ module canvas: Canvas.get_handle_x()
  111. """
  112. cv = Canvas()
  113. self.assertEqual(0, cv.get_handle_x())
  114. def test_get_handle_y(self):
  115. """ module canvas: Canvas.get_handle_y()
  116. """
  117. cv = Canvas()
  118. self.assertEqual(0, cv.get_handle_y())
  119. def test_set_handle(self):
  120. """ module canvas: Canvas.set_handle()
  121. """
  122. cv = Canvas(10, 5)
  123. self.assertEqual(0, cv.set_handle(3, 3))
  124. self.assertEqual((3, 3), (cv.get_handle_x(), cv.get_handle_y()))
  125. self.assertRaises(CanvasError, cv.set_handle, "a", 1)
  126. self.assertRaises(CanvasError, cv.set_handle, 1, "a")
  127. def test_blit(self):
  128. """ module canvas: Canvas.blit()
  129. """
  130. cv1 = Canvas(10, 1)
  131. cv2 = Canvas(10, 1)
  132. cv2.put_str(0, 0, "teststring")
  133. self.assertEqual(0, cv1.blit(0, 0, cv2))
  134. self.assertEqual("teststring", cv1.export_to_memory("utf8").strip('\n'))
  135. self.assertRaises(CanvasError, cv1.blit, 0, 0, "abc")
  136. self.assertRaises(CanvasError, cv1.blit, "a", 0, cv2)
  137. def test_blit_mask(self):
  138. """ module canvas: Canvas.blit() (with mask)
  139. """
  140. cv1 = Canvas(10, 1)
  141. cv2 = Canvas(10, 1)
  142. mask = Canvas(10, 1)
  143. badm = Canvas()
  144. cv2.put_str(0, 0, "teststring")
  145. mask.put_str(0, 0, "####")
  146. self.assertEqual(0, cv1.blit(0, 0, cv2, mask))
  147. self.assertEqual("test ", cv1.export_to_memory("utf8").strip("\n"))
  148. self.assertRaises(CanvasError, cv1.blit, 0, 0, cv2, badm)
  149. def test_set_boundaries(self):
  150. """ module canvas: Canvas.set_boundaries()
  151. """
  152. cv = Canvas()
  153. self.assertEqual(0, cv.set_boundaries(0, 0, 10, 10))
  154. self.assertEqual((10, 10), (cv.get_width(), cv.get_height()))
  155. self.assertEqual(0, cv.set_boundaries(0, 0, 5, 5))
  156. self.assertEqual((5, 5), (cv.get_width(), cv.get_height()))
  157. self.assertRaises(CanvasError, cv.set_boundaries, "a", 0, 10, 10)
  158. self.assertRaises(CanvasError, cv.set_boundaries, 0, 0, "abc", 10)
  159. def test_enable_dirty_rect(self):
  160. """ module canvas: Canvas.enable_dirty_rect()
  161. """
  162. cv = Canvas()
  163. self.assertRaises(CanvasError, cv.enable_dirty_rect)
  164. cv.disable_dirty_rect()
  165. self.assertEqual(0, cv.enable_dirty_rect())
  166. def test_add_dirty_rect(self):
  167. """ module canvas: Canvas.add_dirty_rect()
  168. """
  169. cv = Canvas(10, 10)
  170. self.assertEqual(0, cv.add_dirty_rect(4, 4, 2, 2))
  171. self.assertRaises(CanvasError, cv.add_dirty_rect, 11, 11, 20, 20)
  172. def test_remove_dirty_rect(self):
  173. """ module canvas: Canvas.remove_dirty_rect()
  174. """
  175. cv = Canvas(10, 10)
  176. cv.add_dirty_rect(4, 4, 2, 2)
  177. self.assertEqual(0, cv.remove_dirty_rect(4, 4, 2, 2))
  178. self.assertRaises(CanvasError, cv.remove_dirty_rect, 11, 11, 20, 20)
  179. def test_draw_line(self):
  180. """ module canvas: Canvas.draw_line()
  181. """
  182. cv = Canvas(10, 1)
  183. cv.draw_line(0, 0, 10, 1, "#")
  184. self.assertEqual("#" * 10, cv.export_to_memory("utf8").strip('\n'))
  185. self.assertRaises(CanvasError, cv.draw_line, 0, 0, 10, 1, 7)
  186. self.assertRaises(CanvasError, cv.draw_line, 0, "a", 10, 1, "#")
  187. def test_draw_thin_line(self):
  188. """ module canvas: Canvas.draw_thin_line()
  189. """
  190. cv = Canvas(10, 1)
  191. cv.draw_thin_line(0, 0, 10, 1)
  192. self.assertEqual("-" * 10, cv.export_to_memory("utf8").strip('\n'))
  193. self.assertRaises(CanvasError, cv.draw_thin_line, 0, "a", 10, 1)
  194. def test_draw_polyline(self):
  195. """ module canvas: Canvas.draw_polyline()
  196. """
  197. cv = Canvas(10, 10)
  198. self.assertEqual(0, cv.draw_polyline([(0, 0), (2, 2), (5, 2), (0, 0)],
  199. '#'))
  200. self.assertRaises(CanvasError, cv.draw_polyline, [], '#')
  201. self.assertRaises(CanvasError, cv.draw_polyline, [0, 1, 2, 4], '#')
  202. def test_draw_thin_polyline(self):
  203. """ module canvas: Canvas.draw_thin_polyline()
  204. """
  205. cv = Canvas(10, 10)
  206. self.assertEqual(0, cv.draw_thin_polyline([(0, 0), (2, 2),
  207. (5, 2), (0, 0)]))
  208. self.assertRaises(CanvasError, cv.draw_thin_polyline, [])
  209. self.assertRaises(CanvasError, cv.draw_thin_polyline, [0, 1, 2, 4])
  210. def test_draw_circle(self):
  211. """ module canvas: Canvas.draw_circle()
  212. """
  213. cv = Canvas(10, 10)
  214. self.assertEqual(0, cv.draw_circle(5, 5, 3, '#'))
  215. self.assertRaises(CanvasError, cv.draw_circle, 0, 0, '#', '#')
  216. self.assertRaises(CanvasError, cv.draw_circle, 0, 0, 3, 0)
  217. def test_draw_ellipse(self):
  218. """ module canvas: Canvas.draw_ellipse()
  219. """
  220. cv = Canvas(10, 10)
  221. self.assertEqual(0, cv.draw_ellipse(5, 5, 3, 4, '#'))
  222. self.assertRaises(CanvasError, cv.draw_ellipse, 0, 0, 3, '#', '#')
  223. self.assertRaises(CanvasError, cv.draw_ellipse, 0, 0, 3, 5, 0)
  224. def test_draw_thin_ellipse(self):
  225. """ module canvas: Canvas.draw_thin_ellipse()
  226. """
  227. cv = Canvas(10, 10)
  228. self.assertEqual(0, cv.draw_thin_ellipse(5, 5, 3, 4))
  229. self.assertRaises(CanvasError, cv.draw_thin_ellipse, 0, 0, 3, '#')
  230. def test_fill_ellipse(self):
  231. """ module canvas: Canvas.fill_ellipse()
  232. """
  233. cv = Canvas(10, 10)
  234. self.assertEqual(0, cv.fill_ellipse(5, 5, 3, 3, '#'))
  235. self.assertRaises(CanvasError, cv.fill_ellipse, 0, 0, 3, '#', '#')
  236. self.assertRaises(CanvasError, cv.fill_ellipse, 0, 0, 3, 3, 0)
  237. def test_draw_box(self):
  238. """ module canvas: Canvas.draw_box()
  239. """
  240. cv = Canvas(10, 10)
  241. self.assertEqual(0, cv.draw_box(0, 0, 2, 2, '#'))
  242. self.assertRaises(CanvasError, cv.draw_box, 0, 0, 2, '#', '#')
  243. self.assertRaises(CanvasError, cv.draw_box, 0, 0, 2, 2, 0)
  244. def test_draw_thin_box(self):
  245. """ module canvas: Canvas.draw_thin_box()
  246. """
  247. cv = Canvas(10, 10)
  248. self.assertEqual(0, cv.draw_thin_box(0, 0, 2, 2))
  249. self.assertRaises(CanvasError, cv.draw_thin_box, 0, 0, 2, '#')
  250. def test_draw_cp437_box(self):
  251. """ module canvas: Canvas.draw_cp437_box()
  252. """
  253. cv = Canvas(10, 10)
  254. self.assertEqual(0, cv.draw_cp437_box(0, 0, 2, 2))
  255. self.assertRaises(CanvasError, cv.draw_cp437_box, 0, 0, 2, '#')
  256. def test_fill_box(self):
  257. """ module canvas: Canvas.fill_box()
  258. """
  259. cv = Canvas(10, 10)
  260. self.assertEqual(0, cv.fill_box(0, 0, 2, 2, '#'))
  261. self.assertRaises(CanvasError, cv.fill_box, 0, 0, 2, '#', '#')
  262. self.assertRaises(CanvasError, cv.fill_box, 0, 0, 2, 2, 0)
  263. def test_draw_triangle(self):
  264. """ module canvas: Canvas.draw_triangle()
  265. """
  266. cv = Canvas(10, 10)
  267. self.assertEqual(0, cv.draw_triangle(0, 0, 2, 2, 4, 0, '#'))
  268. self.assertRaises(CanvasError, cv.draw_triangle,
  269. 0, 0, 2, 2, 4, '#', '#')
  270. self.assertRaises(CanvasError, cv.draw_triangle,
  271. 0, 0, 2, 2, 4, 0, 0)
  272. def test_draw_thin_triangle(self):
  273. """ module canvas: Canvas.draw_thin_triangle()
  274. """
  275. cv = Canvas(10, 10)
  276. self.assertEqual(0, cv.draw_thin_triangle(0, 0, 2, 2, 4, 0))
  277. self.assertRaises(CanvasError, cv.draw_thin_triangle,
  278. 0, 0, 2, 2, 4, '#')
  279. def test_fill_triangle(self):
  280. """ module canvas: Canvas.fill_triangle()
  281. """
  282. cv = Canvas(10, 10)
  283. self.assertEqual(0, cv.fill_triangle(0, 0, 2, 2, 4, 0, '#'))
  284. self.assertRaises(CanvasError, cv.fill_triangle,
  285. 0, 0, 2, 2, 4, '#', '#')
  286. self.assertRaises(CanvasError, cv.fill_triangle,
  287. 0, 0, 2, 2, 4, 0, 0)