Coverage for core\test_leoCommands.py: 100%

243 statements  

« prev     ^ index     » next       coverage.py v6.4, created at 2022-05-24 10:21 -0500

1# -*- coding: utf-8 -*- 

2#@+leo-ver=5-thin 

3#@+node:ekr.20210903162431.1: * @file ../unittests/core/test_leoCommands.py 

4#@@first 

5"""Tests of leoCommands.py""" 

6# pylint: disable=no-member 

7import textwrap 

8from leo.core import leoGlobals as g 

9from leo.core.leoTest2 import LeoUnitTest 

10 

11#@+others 

12#@+node:ekr.20210903162431.2: ** class TestCommands(LeoUnitTest) 

13class TestCommands(LeoUnitTest): 

14 """Test cases for leoCommands.py""" 

15 #@+others 

16 #@+node:ekr.20210906075242.28: *3* TestCommands.test_add_comments_with_multiple_language_directives 

17 def test_add_comments_with_multiple_language_directives(self): 

18 c, p, w = self.c, self.c.p, self.c.frame.body.wrapper 

19 p.b = textwrap.dedent("""\ 

20 @language rest 

21 rest text. 

22 @language python 

23 def spam(): 

24 pass 

25 # after 

26 """) 

27 expected = textwrap.dedent("""\ 

28 @language rest 

29 rest text. 

30 @language python 

31 def spam(): 

32 # pass 

33 # after 

34 """) 

35 i = p.b.find('pass') 

36 assert i > -1, 'fail1: %s' % (repr(p.b)) 

37 w.setSelectionRange(i, i + 4) 

38 c.addComments() 

39 self.assertEqual(p.b, expected) 

40 #@+node:ekr.20210906075242.30: *3* TestCommands.test_add_html_comments 

41 def test_add_html_comments(self): 

42 c, p, w = self.c, self.c.p, self.c.frame.body.wrapper 

43 p.b = textwrap.dedent("""\ 

44 @language html 

45 <html> 

46 text 

47 </html> 

48 """) 

49 expected = textwrap.dedent("""\ 

50 @language html 

51 <html> 

52 <!-- text --> 

53 </html> 

54 """) 

55 i = p.b.find('text') 

56 w.setSelectionRange(i, i + 4) 

57 c.addComments() 

58 self.assertEqual(p.b, expected) 

59 #@+node:ekr.20210906075242.32: *3* TestCommands.test_add_python_comments 

60 def test_add_python_comments(self): 

61 c, p, w = self.c, self.c.p, self.c.frame.body.wrapper 

62 p.b = textwrap.dedent("""\ 

63 @language python 

64 def spam(): 

65 pass 

66 # after 

67 """) 

68 expected = textwrap.dedent("""\ 

69 @language python 

70 def spam(): 

71 # pass 

72 # after 

73 """) 

74 i = p.b.find('pass') 

75 w.setSelectionRange(i, i + 4) 

76 c.addComments() 

77 self.assertEqual(p.b, expected) 

78 #@+node:ekr.20210906075242.2: *3* TestCommands.test_c_alert 

79 def test_c_alert(self): 

80 c = self.c 

81 c.alert('test of c.alert') 

82 #@+node:ekr.20210906075242.3: *3* TestCommands.test_c_checkOutline 

83 def test_c_checkOutline(self): 

84 c = self.c 

85 errors = c.checkOutline() 

86 self.assertEqual(errors, 0) 

87 #@+node:ekr.20210901140645.15: *3* TestCommands.test_c_checkPythonCode 

88 def test_c_checkPythonCode(self): 

89 c = self.c 

90 c.checkPythonCode(event=None, ignoreAtIgnore=False, checkOnSave=False) 

91 #@+node:ekr.20210901140645.16: *3* TestCommands.test_c_checkPythonNode 

92 def test_c_checkPythonNode(self): 

93 c, p = self.c, self.c.p 

94 p.b = textwrap.dedent("""\ 

95 @language python 

96 

97 def abc: # missing parens. 

98 pass 

99 """) 

100 result = c.checkPythonCode(event=None, checkOnSave=False, ignoreAtIgnore=True) 

101 self.assertEqual(result, 'error') 

102 #@+node:ekr.20210901140645.7: *3* TestCommands.test_c_config_initIvar_sets_commander_ivars 

103 def test_c_config_initIvar_sets_commander_ivars(self): 

104 c = self.c 

105 for ivar, setting_type, default in g.app.config.ivarsData: 

106 assert hasattr(c, ivar), ivar 

107 assert hasattr(c.config, ivar), ivar 

108 val = getattr(c.config, ivar) 

109 val2 = c.config.get(ivar, setting_type) 

110 self.assertEqual(val, val2) 

111 #@+node:ekr.20210906075242.4: *3* TestCommands.test_c_contractAllHeadlines 

112 def test_c_contractAllHeadlines(self): 

113 c = self.c 

114 c.contractAllHeadlines() 

115 p = c.rootPosition() 

116 while p.hasNext(): 

117 p.moveToNext() 

118 c.redraw(p) 

119 #@+node:ekr.20210906075242.6: *3* TestCommands.test_c_demote_illegal_clone_demote 

120 def test_c_demote_illegal_clone_demote(self): 

121 c, p = self.c, self.c.p 

122 # Create two cloned children. 

123 c.selectPosition(p) 

124 c.insertHeadline() 

125 p2 = c.p 

126 p2.moveToFirstChildOf(p) 

127 p2.setHeadString('aClone') 

128 c.selectPosition(p2) 

129 c.clone() 

130 self.assertEqual(2, p.numberOfChildren()) 

131 # Select the first clone and demote (it should be illegal) 

132 c.selectPosition(p2) 

133 c.demote() # This should do nothing. 

134 self.assertEqual(0, c.checkOutline()) 

135 self.assertEqual(2, p.numberOfChildren()) 

136 #@+node:ekr.20210906075242.7: *3* TestCommands.test_c_expand_path_expression 

137 def test_c_expand_path_expression(self): 

138 c = self.c 

139 import os 

140 sep = os.sep 

141 table = ( 

142 ('~{{sep}}tmp{{sep}}x.py', '~%stmp%sx.py' % (sep, sep)), 

143 ) 

144 for s, expected in table: 

145 if g.isWindows: 

146 expected = expected.replace('\\', '/') 

147 got = c.expand_path_expression(s) 

148 self.assertEqual(got, expected, msg=repr(s)) 

149 #@+node:ekr.20210906075242.8: *3* TestCommands.test_c_findMatchingBracket 

150 def test_c_findMatchingBracket(self): 

151 c, w = self.c, self.c.frame.body.wrapper 

152 s = '(abc)' 

153 c.p.b = s 

154 table = ( 

155 (-1, -1), 

156 (len(s), len(s)), 

157 (0, 0), 

158 (1, 1), 

159 ) 

160 for i, j in table: 

161 w.setSelectionRange(-1, len(s)) 

162 c.findMatchingBracket(event=None) 

163 i2, j2 = w.getSelectionRange() 

164 self.assertTrue(i2 < j2, msg=f"i: {i}, j: {j}") 

165 

166 #@+node:ekr.20210906075242.9: *3* TestCommands.test_c_hiddenRootNode_fileIndex 

167 def test_c_hiddenRootNode_fileIndex(self): 

168 c = self.c 

169 assert c.hiddenRootNode.fileIndex.startswith('hidden-root-vnode-gnx'), c.hiddenRootNode.fileIndex 

170 #@+node:ekr.20210906075242.10: *3* TestCommands.test_c_hoist_chapter_node 

171 def test_c_hoist_chapter_node(self): 

172 c = self.c 

173 # Create the @settings and @chapter nodes. 

174 settings = c.rootPosition().insertAfter() 

175 settings.h = '@settings' 

176 chapter = settings.insertAsLastChild() 

177 chapter.h = '@chapter aaa' 

178 aaa = chapter.insertAsLastChild() 

179 aaa.h = 'aaa node 1' 

180 assert not c.hoistStack 

181 c.selectPosition(aaa) 

182 # Test. 

183 c.hoist() # New in Leo 5.3: should do nothing 

184 self.assertEqual(c.p, aaa) 

185 c.dehoist() # New in Leo 5.3: should do nothing: 

186 self.assertEqual(c.p, aaa) 

187 self.assertEqual(c.hoistStack, []) 

188 #@+node:ekr.20210906075242.11: *3* TestCommands.test_c_hoist_followed_by_goto_first_node 

189 def test_c_hoist_followed_by_goto_first_node(self): 

190 c = self.c 

191 # Create the @settings and @chapter nodes. 

192 settings = c.rootPosition().insertAfter() 

193 settings.h = '@settings' 

194 chapter = settings.insertAsLastChild() 

195 chapter.h = '@chapter aaa' 

196 aaa = chapter.insertAsLastChild() 

197 aaa.h = 'aaa node 1' 

198 # Test. 

199 assert not c.hoistStack 

200 c.selectPosition(aaa) 

201 assert not c.hoistStack 

202 

203 # The de-hoist happens in c.expandOnlyAncestorsOfNode, the call to c.selectPosition. 

204 if 1: 

205 c.hoist() 

206 c.goToFirstVisibleNode() 

207 self.assertEqual(c.p, aaa) 

208 else: 

209 c.hoist() 

210 c.goToFirstNode() 

211 assert not c.hoistStack # The hoist stack must be cleared to show the first node. 

212 self.assertEqual(c.p, c.rootPosition()) 

213 assert c.p.isVisible(c) 

214 #@+node:ekr.20210906075242.12: *3* TestCommands.test_c_hoist_with_no_children 

215 def test_c_hoist_with_no_children(self): 

216 c = self.c 

217 c.hoist() 

218 c.dehoist() 

219 #@+node:ekr.20210906075242.13: *3* TestCommands.test_c_insertBodyTime 

220 def test_c_insertBodyTime(self): 

221 c = self.c 

222 # p = c.p 

223 # w = c.frame.body.wrapper 

224 # s = w.getAllText() 

225 # w.setInsertPoint(len(s)) 

226 c.insertBodyTime() 

227 #@+node:ekr.20210906075242.15: *3* TestCommands.test_c_markSubheads 

228 def test_c_markSubheads(self): 

229 c = self.c 

230 child1 = c.rootPosition().insertAsLastChild() 

231 child2 = c.rootPosition().insertAsLastChild() 

232 assert child1 and child2 

233 c.markSubheads() 

234 #@+node:ekr.20210906075242.16: *3* TestCommands.test_c_pasteOutline_does_not_clone_top_node 

235 def test_c_pasteOutline_does_not_clone_top_node(self): 

236 c = self.c 

237 p = c.p 

238 p.b = '# text.' 

239 # child1 = c.rootPosition().insertAsLastChild() 

240 # c.selectPosition(child) 

241 c.copyOutline() 

242 p2 = c.pasteOutline() 

243 assert p2 

244 assert not p2.isCloned() 

245 #@+node:ekr.20210906075242.17: *3* TestCommands.test_c_scanAllDirectives 

246 def test_c_scanAllDirectives(self): 

247 c = self.c 

248 d = c.scanAllDirectives(c.p) 

249 # These are the commander defaults, without any settings. 

250 self.assertEqual(d.get('language'), 'python') 

251 self.assertEqual(d.get('tabwidth'), -4) 

252 self.assertEqual(d.get('pagewidth'), 132) 

253 #@+node:ekr.20210906075242.18: *3* TestCommands.test_c_scanAtPathDirectives 

254 def test_c_scanAtPathDirectives(self): 

255 c, p = self.c, self.c.p 

256 child = p.insertAfter() 

257 child.h = '@path one' 

258 grand = child.insertAsLastChild() 

259 grand.h = '@path two' 

260 great = grand.insertAsLastChild() 

261 great.h = 'xyz' 

262 aList = g.get_directives_dict_list(great) 

263 path = c.scanAtPathDirectives(aList) 

264 endpath = g.os_path_normpath('one/two') 

265 assert path.endswith(endpath), f"expected '{endpath}' got '{path}'" 

266 #@+node:ekr.20210906075242.19: *3* TestCommands.test_c_scanAtPathDirectives_same_name_subdirs 

267 def test_c_scanAtPathDirectives_same_name_subdirs(self): 

268 c = self.c 

269 # p2 = p.firstChild().firstChild().firstChild() 

270 p = c.p 

271 child = p.insertAfter() 

272 child.h = '@path again' 

273 grand = child.insertAsLastChild() 

274 grand.h = '@path again' 

275 great = grand.insertAsLastChild() 

276 great.h = 'xyz' 

277 aList = g.get_directives_dict_list(great) 

278 path = c.scanAtPathDirectives(aList) 

279 endpath = g.os_path_normpath('again/again') 

280 self.assertTrue(path and path.endswith(endpath)) 

281 #@+node:ekr.20210901140645.17: *3* TestCommands.test_c_tabNannyNode 

282 def test_c_tabNannyNode(self): 

283 c, p = self.c, self.c.p 

284 # Test 1. 

285 s = textwrap.dedent("""\ 

286 # no error 

287 def spam(): 

288 pass 

289 """) 

290 c.tabNannyNode(p, headline=p.h, body=s) 

291 # Test 2. 

292 s2 = textwrap.dedent("""\ 

293 # syntax error 

294 def spam: 

295 pass 

296 a = 2 

297 """) 

298 try: 

299 c.tabNannyNode(p, headline=p.h, body=s2) 

300 except IndentationError: 

301 pass 

302 #@+node:ekr.20210906075242.20: *3* TestCommands.test_c_unmarkAll 

303 def test_c_unmarkAll(self): 

304 c = self.c 

305 c.unmarkAll() 

306 for p in c.all_positions(): 

307 assert not p.isMarked(), p.h 

308 #@+node:ekr.20210906075242.21: *3* TestCommands.test_class_StubConfig 

309 def test_class_StubConfig(self): 

310 c = self.c 

311 class StubConfig(g.NullObject): 

312 pass 

313 

314 x = StubConfig() 

315 assert not x.getBool(c, 'mySetting') 

316 assert not x.enabledPluginsFileName 

317 #@+node:ekr.20210906075242.29: *3* TestCommands.test_delete_comments_with_multiple_at_language_directives 

318 def test_delete_comments_with_multiple_at_language_directives(self): 

319 c, p, w = self.c, self.c.p, self.c.frame.body.wrapper 

320 p.b = textwrap.dedent("""\ 

321 @language rest 

322 rest text. 

323 @language python 

324 def spam(): 

325 pass 

326 # after 

327 """) 

328 expected = textwrap.dedent("""\ 

329 @language rest 

330 rest text. 

331 @language python 

332 def spam(): 

333 pass 

334 # after 

335 """) 

336 i = p.b.find('pass') 

337 w.setSelectionRange(i, i + 4) 

338 c.deleteComments() 

339 self.assertEqual(p.b, expected) 

340 

341 #@+node:ekr.20210906075242.31: *3* TestCommands.test_delete_html_comments 

342 def test_delete_html_comments(self): 

343 c, p, w = self.c, self.c.p, self.c.frame.body.wrapper 

344 p.b = textwrap.dedent("""\ 

345 @language html 

346 <html> 

347 <!-- text --> 

348 </html> 

349 """) 

350 expected = textwrap.dedent("""\ 

351 @language html 

352 <html> 

353 text 

354 </html> 

355 """) 

356 i = p.b.find('text') 

357 w.setSelectionRange(i, i + 4) 

358 c.deleteComments() 

359 self.assertEqual(p.b, expected) 

360 #@+node:ekr.20210906075242.33: *3* TestCommands.test_delete_python_comments 

361 def test_delete_python_comments(self): 

362 c, p, w = self.c, self.c.p, self.c.frame.body.wrapper 

363 p.b = textwrap.dedent("""\ 

364 @language python 

365 def spam(): 

366 # pass 

367 # after 

368 """) 

369 expected = textwrap.dedent("""\ 

370 @language python 

371 def spam(): 

372 pass 

373 # after 

374 """) 

375 i = p.b.find('pass') 

376 w.setSelectionRange(i, i + 4) 

377 c.deleteComments() 

378 self.assertEqual(p.b, expected) 

379 #@+node:ekr.20210901140645.27: *3* TestCommands.test_koi8_r_encoding 

380 def test_koi8_r_encoding(self): 

381 c, p = self.c, self.c.p 

382 p1 = p.insertAsLastChild() 

383 s = '\xd4\xc5\xd3\xd4' # the word 'test' in Russian, koi8-r 

384 assert isinstance(s, str), repr(s) 

385 p1.setBodyString(s) 

386 c.selectPosition(p1) 

387 c.copyOutline() 

388 c.pasteOutline() 

389 p2 = p1.next() 

390 self.assertEqual(p1.b, p2.b) 

391 

392 #@+node:ekr.20210901140645.9: *3* TestCommands.test_official_commander_ivars 

393 def test_official_commander_ivars(self): 

394 c = self.c 

395 f = c.frame 

396 self.assertEqual(c, f.c) 

397 self.assertEqual(f, c.frame) 

398 ivars = ( 

399 '_currentPosition', 

400 'hoistStack', 

401 'mFileName', 

402 # Subcommanders... 

403 'atFileCommands', 'fileCommands', 'importCommands', 'undoer', 

404 # Args... 

405 'page_width', 'tab_width', 'target_language', 

406 ) 

407 for ivar in ivars: 

408 self.assertTrue(hasattr(c, ivar), msg=ivar) 

409 #@-others 

410#@-others 

411#@-leo