m2m模型翻译
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

391 lines
11 KiB

6 months ago
  1. # Copyright (C) 2006 Michael Graz. <mgraz@plan10.com>
  2. from __future__ import print_function, unicode_literals, absolute_import
  3. import sys, unittest
  4. sys.path.append ('../..')
  5. #from pyreadline.modes.vi import *
  6. #from pyreadline import keysyms
  7. from pyreadline.lineeditor import lineobj
  8. #----------------------------------------------------------------------
  9. #----------------------------------------------------------------------
  10. class Test_copy (unittest.TestCase):
  11. def test_copy1 (self):
  12. l=lineobj.ReadLineTextBuffer("first second")
  13. q=l.copy()
  14. self.assertEqual(q.get_line_text(),l.get_line_text())
  15. self.assertEqual(q.point,l.point)
  16. self.assertEqual(q.mark,l.mark)
  17. def test_copy2 (self):
  18. l=lineobj.ReadLineTextBuffer("first second",point=5)
  19. q=l.copy()
  20. self.assertEqual(q.get_line_text(),l.get_line_text())
  21. self.assertEqual(q.point,l.point)
  22. self.assertEqual(q.mark,l.mark)
  23. class Test_linepos (unittest.TestCase):
  24. t="test text"
  25. def test_NextChar (self):
  26. t=self.t
  27. l=lineobj.ReadLineTextBuffer(t)
  28. for i in range(len(t)):
  29. self.assertEqual(i,l.point)
  30. l.point=lineobj.NextChar
  31. #advance past end of buffer
  32. l.point=lineobj.NextChar
  33. self.assertEqual(len(t),l.point)
  34. def test_PrevChar (self):
  35. t=self.t
  36. l=lineobj.ReadLineTextBuffer(t,point=len(t))
  37. for i in range(len(t)):
  38. self.assertEqual(len(t)-i,l.point)
  39. l.point=lineobj.PrevChar
  40. #advance past beginning of buffer
  41. l.point=lineobj.PrevChar
  42. self.assertEqual(0,l.point)
  43. def test_EndOfLine (self):
  44. t=self.t
  45. l=lineobj.ReadLineTextBuffer(t,point=len(t))
  46. for i in range(len(t)):
  47. l.point=i
  48. l.point=lineobj.EndOfLine
  49. self.assertEqual(len(t),l.point)
  50. def test_StartOfLine (self):
  51. t=self.t
  52. l=lineobj.ReadLineTextBuffer(t,point=len(t))
  53. for i in range(len(t)):
  54. l.point=i
  55. l.point=lineobj.StartOfLine
  56. self.assertEqual(0,l.point)
  57. class Tests_linepos2(Test_linepos):
  58. t="kajkj"
  59. class Tests_linepos3(Test_linepos):
  60. t=""
  61. class Test_movement (unittest.TestCase):
  62. def test_NextChar (self):
  63. cmd=lineobj.NextChar
  64. tests=[
  65. # "First"
  66. (cmd,
  67. "First",
  68. "# ",
  69. " # "),
  70. (cmd,
  71. "First",
  72. " # ",
  73. " #"),
  74. (cmd,
  75. "First",
  76. " #",
  77. " #"),
  78. ]
  79. for cmd,text,init_point,expected_point in tests:
  80. l=lineobj.ReadLineTextBuffer(text,get_point_pos(init_point))
  81. l.point=cmd
  82. self.assertEqual(get_point_pos(expected_point),l.point)
  83. def test_PrevChar (self):
  84. cmd=lineobj.PrevChar
  85. tests=[
  86. # "First"
  87. (cmd,
  88. "First",
  89. " #",
  90. " # "),
  91. (cmd,
  92. "First",
  93. " # ",
  94. "# "),
  95. (cmd,
  96. "First",
  97. "# ",
  98. "# "),
  99. ]
  100. for cmd,text,init_point,expected_point in tests:
  101. l=lineobj.ReadLineTextBuffer(text,get_point_pos(init_point))
  102. l.point=cmd
  103. self.assertEqual(get_point_pos(expected_point),l.point)
  104. def test_PrevWordStart (self):
  105. cmd=lineobj.PrevWordStart
  106. tests=[
  107. # "First Second Third"
  108. (cmd,
  109. "First Second Third",
  110. " #",
  111. " # "),
  112. (cmd,
  113. "First Second Third",
  114. " # ",
  115. " # "),
  116. (cmd,
  117. "First Second Third",
  118. " # ",
  119. "# "),
  120. (cmd,
  121. "First Second Third",
  122. "# ",
  123. "# "),
  124. ]
  125. for cmd,text,init_point,expected_point in tests:
  126. l=lineobj.ReadLineTextBuffer(text,get_point_pos(init_point))
  127. l.point=cmd
  128. self.assertEqual(get_point_pos(expected_point),l.point)
  129. def test_NextWordStart (self):
  130. cmd=lineobj.NextWordStart
  131. tests=[
  132. # "First Second Third"
  133. (cmd,
  134. "First Second Third",
  135. "# ",
  136. " # "),
  137. (cmd,
  138. "First Second Third",
  139. " # ",
  140. " # "),
  141. (cmd,
  142. "First Second Third",
  143. " # ",
  144. " # "),
  145. (cmd,
  146. "First Second Third",
  147. " # ",
  148. " #"),
  149. ]
  150. for cmd,text,init_point,expected_point in tests:
  151. l=lineobj.ReadLineTextBuffer(text,get_point_pos(init_point))
  152. l.point=cmd
  153. self.assertEqual(get_point_pos(expected_point),l.point)
  154. def test_NextWordEnd (self):
  155. cmd=lineobj.NextWordEnd
  156. tests=[
  157. # "First Second Third"
  158. (cmd,
  159. "First Second Third",
  160. "# ",
  161. " # "),
  162. (cmd,
  163. "First Second Third",
  164. " # ",
  165. " # "),
  166. (cmd,
  167. "First Second Third",
  168. " # ",
  169. " # "),
  170. (cmd,
  171. "First Second Third",
  172. " # ",
  173. " #"),
  174. ]
  175. for cmd,text,init_point,expected_point in tests:
  176. l=lineobj.ReadLineTextBuffer(text,get_point_pos(init_point))
  177. l.point=cmd
  178. self.assertEqual(get_point_pos(expected_point),l.point)
  179. def test_PrevWordEnd (self):
  180. cmd=lineobj.PrevWordEnd
  181. tests=[
  182. # "First Second Third"
  183. (cmd,
  184. "First Second Third",
  185. " #",
  186. " # "),
  187. (cmd,
  188. "First Second Third",
  189. " # ",
  190. " # "),
  191. (cmd,
  192. "First Second Third",
  193. " # ",
  194. "# "),
  195. (cmd,
  196. "First Second Third",
  197. "# ",
  198. "# "),
  199. ]
  200. for cmd,text,init_point,expected_point in tests:
  201. l=lineobj.ReadLineTextBuffer(text,get_point_pos(init_point))
  202. l.point=cmd
  203. self.assertEqual(get_point_pos(expected_point),l.point)
  204. def test_WordEnd_1 (self):
  205. cmd=lineobj.WordEnd
  206. tests=[
  207. # "First Second Third"
  208. (cmd,
  209. "First Second Third",
  210. "# ",
  211. " # "),
  212. (cmd,
  213. "First Second Third",
  214. " # ",
  215. " # "),
  216. (cmd,
  217. "First Second Third",
  218. " # ",
  219. " #"),
  220. ]
  221. for cmd,text,init_point,expected_point in tests:
  222. l=lineobj.ReadLineTextBuffer(text,get_point_pos(init_point))
  223. l.point=cmd
  224. self.assertEqual(get_point_pos(expected_point),l.point)
  225. def test_WordEnd_2 (self):
  226. cmd=lineobj.WordEnd
  227. tests=[
  228. # "First Second Third"
  229. (cmd,
  230. "First Second Third",
  231. " # "),
  232. (cmd,
  233. "First Second Third",
  234. " # "),
  235. (cmd,
  236. "First Second Third",
  237. " #"),
  238. ]
  239. for cmd,text,init_point in tests:
  240. l=lineobj.ReadLineTextBuffer(text,get_point_pos(init_point))
  241. self.assertRaises(lineobj.NotAWordError,cmd,l)
  242. def test_WordStart_1 (self):
  243. cmd=lineobj.WordStart
  244. tests=[
  245. # "First Second Third"
  246. (cmd,
  247. "First Second Third",
  248. "# ",
  249. "# "),
  250. (cmd,
  251. "First Second Third",
  252. " # ",
  253. "# "),
  254. (cmd,
  255. "First Second Third",
  256. " # ",
  257. " # "),
  258. ]
  259. for cmd,text,init_point,expected_point in tests:
  260. l=lineobj.ReadLineTextBuffer(text,get_point_pos(init_point))
  261. l.point=cmd
  262. self.assertEqual(get_point_pos(expected_point),l.point)
  263. def test_WordStart_2 (self):
  264. cmd=lineobj.WordStart
  265. tests=[
  266. # "First Second Third"
  267. (cmd,
  268. "First Second Third",
  269. " # "),
  270. (cmd,
  271. "First Second Third",
  272. " # "),
  273. (cmd,
  274. "First Second Third",
  275. " #"),
  276. ]
  277. for cmd,text,init_point in tests:
  278. l=lineobj.ReadLineTextBuffer(text,get_point_pos(init_point))
  279. self.assertRaises(lineobj.NotAWordError,cmd,l)
  280. def test_StartOfLine (self):
  281. cmd=lineobj.StartOfLine
  282. tests=[
  283. # "First Second Third"
  284. (cmd,
  285. "First Second Third",
  286. "# ",
  287. "# "),
  288. (cmd,
  289. "First Second Third",
  290. " # ",
  291. "# "),
  292. (cmd,
  293. "First Second Third",
  294. " #",
  295. "# "),
  296. ]
  297. for cmd,text,init_point,expected_point in tests:
  298. l=lineobj.ReadLineTextBuffer(text,get_point_pos(init_point))
  299. l.point=cmd
  300. self.assertEqual(get_point_pos(expected_point),l.point)
  301. def test_EndOfLine (self):
  302. cmd=lineobj.EndOfLine
  303. tests=[
  304. # "First Second Third"
  305. (cmd,
  306. "First Second Third",
  307. "# ",
  308. " #"),
  309. (cmd,
  310. "First Second Third",
  311. " # ",
  312. " #"),
  313. (cmd,
  314. "First Second Third",
  315. " #",
  316. " #"),
  317. ]
  318. for cmd,text,init_point,expected_point in tests:
  319. l=lineobj.ReadLineTextBuffer(text,get_point_pos(init_point))
  320. l.point=cmd
  321. self.assertEqual(get_point_pos(expected_point),l.point)
  322. def test_Point(self):
  323. cmd=lineobj.Point
  324. tests=[
  325. # "First Second Third"
  326. (cmd,
  327. "First Second Third",
  328. 0),
  329. (cmd,
  330. "First Second Third",
  331. 12),
  332. (cmd,
  333. "First Second Third",
  334. 18),
  335. ]
  336. for cmd,text,p in tests:
  337. l=lineobj.ReadLineTextBuffer(text,p)
  338. self.assertEqual(p,cmd(l))
  339. #----------------------------------------------------------------------
  340. # utility functions
  341. def get_point_pos(pstr):
  342. return pstr.index("#")
  343. def get_mark_pos(mstr):
  344. try:
  345. return mstr.index("#")
  346. except ValueError:
  347. return -1
  348. #----------------------------------------------------------------------
  349. if __name__ == '__main__':
  350. unittest.main()
  351. l=lineobj.ReadLineTextBuffer("First Second Third")