Coverage for core\test_leoGlobals.py: 100%

312 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.20210902164946.1: * @file ../unittests/core/test_leoGlobals.py 

4#@@first 

5"""Tests for leo.core.leoGlobals""" 

6 

7import os 

8import stat 

9import sys 

10import textwrap 

11from leo.core import leoGlobals as g 

12from leo.core.leoTest2 import LeoUnitTest 

13 

14#@+others 

15#@+node:ekr.20210902165045.1: ** class TestGlobals(LeoUnitTest) 

16class TestGlobals(LeoUnitTest): 

17 #@+others 

18 #@+node:ekr.20210901140645.19: *3* TestGlobals.test_getLastTracebackFileAndLineNumber 

19 def test_getLastTracebackFileAndLineNumber(self): 

20 fn = '' 

21 try: 

22 assert False 

23 except AssertionError: 

24 fn, n = g.getLastTracebackFileAndLineNumber() 

25 self.assertEqual(fn.lower(), __file__.lower()) 

26 

27 #@+node:ekr.20210905203541.4: *3* TestGlobals.test_g_checkVersion 

28 def test_g_checkVersion(self): 

29 # for condition in ('<','<=','>','>='): 

30 for v1, condition, v2 in ( 

31 ('8.4.12', '>', '8.4.3'), 

32 ('1', '==', '1.0'), 

33 ('2', '>', '1'), 

34 ('1.2', '>', '1'), 

35 ('2', '>', '1.2.3'), 

36 ('1.2.3', '<', '2'), 

37 ('1', '<', '1.1'), 

38 ): 

39 assert g.CheckVersion(v1, v2, condition=condition, trace=False) 

40 #@+node:ekr.20210905203541.5: *3* TestGlobals.test_g_CheckVersionToInt 

41 def test_g_CheckVersionToInt(self): 

42 self.assertEqual(g.CheckVersionToInt('12'), 12) 

43 self.assertEqual(g.CheckVersionToInt('2a5'), 2) 

44 self.assertEqual(g.CheckVersionToInt('b2'), 0) 

45 #@+node:ekr.20210905203541.6: *3* TestGlobals.test_g_comment_delims_from_extension 

46 def test_g_comment_delims_from_extension(self): 

47 # New in Leo 4.6, set_delims_from_language returns '' instead of None. 

48 table = ( 

49 ('.c', ('//', '/*', '*/')), 

50 ('.html', ('', '<!--', '-->')), 

51 ('.py', ('#', '', '')), 

52 ('.Globals', ('', '', '')), 

53 ) 

54 for ext, expected in table: 

55 result = g.comment_delims_from_extension(ext) 

56 self.assertEqual(result, expected, msg=repr(ext)) 

57 #@+node:ekr.20210905203541.7: *3* TestGlobals.test_g_convertPythonIndexToRowCol 

58 def test_g_convertPythonIndexToRowCol(self): 

59 s1 = 'abc\n\np\nxy' 

60 table1 = ( 

61 (-1, (0, 0)), # One too small. 

62 (0, (0, 0)), 

63 (1, (0, 1)), 

64 (2, (0, 2)), 

65 (3, (0, 3)), # The newline ends a row. 

66 (4, (1, 0)), 

67 (5, (2, 0)), 

68 (6, (2, 1)), 

69 (7, (3, 0)), 

70 (8, (3, 1)), 

71 (9, (3, 2)), # One too many. 

72 (10, (3, 2)), # Two too many. 

73 ) 

74 s2 = 'abc\n\np\nxy\n' 

75 table2 = ( 

76 (9, (3, 2)), 

77 (10, (4, 0)), # One too many. 

78 (11, (4, 0)), # Two too many. 

79 ) 

80 s3 = 'ab' # Test special case. This was the cause of off-by-one problems. 

81 table3 = ( 

82 (-1, (0, 0)), # One too small. 

83 (0, (0, 0)), 

84 (1, (0, 1)), 

85 (2, (0, 2)), # One too many. 

86 (3, (0, 2)), # Two too many. 

87 ) 

88 for n, s, table in ((1, s1, table1), (2, s2, table2), (3, s3, table3)): 

89 for i, result in table: 

90 row, col = g.convertPythonIndexToRowCol(s, i) 

91 self.assertEqual(row, result[0], msg=f"n: {n}, i: {i}") 

92 self.assertEqual(col, result[1], msg=f"n: {n}, i: {i}") 

93 #@+node:ekr.20210905203541.8: *3* TestGlobals.test_g_convertRowColToPythonIndex 

94 def test_g_convertRowColToPythonIndex(self): 

95 s1 = 'abc\n\np\nxy' 

96 s2 = 'abc\n\np\nxy\n' 

97 table1 = ( 

98 (0, (-1, 0)), # One too small. 

99 (0, (0, 0)), 

100 (1, (0, 1)), 

101 (2, (0, 2)), 

102 (3, (0, 3)), # The newline ends a row. 

103 (4, (1, 0)), 

104 (5, (2, 0)), 

105 (6, (2, 1)), 

106 (7, (3, 0)), 

107 (8, (3, 1)), 

108 (9, (3, 2)), # One too large. 

109 ) 

110 table2 = ( 

111 (9, (3, 2)), 

112 (10, (4, 0)), # One two many. 

113 ) 

114 for s, table in ((s1, table1), (s2, table2)): 

115 for i, data in table: 

116 row, col = data 

117 result = g.convertRowColToPythonIndex(s, row, col) 

118 self.assertEqual(i, result, msg=f"row: {row}, col: {col}, i: {i}") 

119 #@+node:ekr.20210905203541.9: *3* TestGlobals.test_g_create_temp_file 

120 def test_g_create_temp_file(self): 

121 theFile = None 

122 try: 

123 theFile, fn = g.create_temp_file() 

124 assert theFile 

125 assert isinstance(fn, str) 

126 finally: 

127 if theFile: 

128 theFile.close() 

129 #@+node:ekr.20210905203541.10: *3* TestGlobals.test_g_ensureLeadingNewlines 

130 def test_g_ensureLeadingNewlines(self): 

131 s = ' \n \n\t\naa bc' 

132 s2 = 'aa bc' 

133 for i in range(3): 

134 result = g.ensureLeadingNewlines(s, i) 

135 val = ('\n' * i) + s2 

136 self.assertEqual(result, val) 

137 #@+node:ekr.20210905203541.11: *3* TestGlobals.test_g_ensureTrailingNewlines 

138 def test_g_ensureTrailingNewlines(self): 

139 s = 'aa bc \n \n\t\n' 

140 s2 = 'aa bc' 

141 for i in range(3): 

142 result = g.ensureTrailingNewlines(s, i) 

143 val = s2 + ('\n' * i) 

144 self.assertEqual(result, val) 

145 #@+node:ekr.20210905203541.12: *3* TestGlobals.test_g_find_word 

146 def test_g_find_word(self): 

147 table = ( 

148 ('abc a bc x', 'bc', 0, 6), 

149 ('abc a bc x', 'bc', 1, 6), 

150 ('abc a x', 'bc', 0, -1), 

151 ) 

152 for s, word, i, expected in table: 

153 actual = g.find_word(s, word, i) 

154 self.assertEqual(actual, expected) 

155 #@+node:ekr.20210905203541.14: *3* TestGlobals.test_g_fullPath 

156 def test_g_fullPath(self): 

157 c = self.c 

158 child = c.rootPosition().insertAfter() 

159 child.h = '@path abc' 

160 grand = child.insertAsLastChild() 

161 grand.h = 'xyz' 

162 path = g.fullPath(c, grand, simulate=True) 

163 end = g.os_path_normpath('abc/xyz') 

164 assert path.endswith(end), repr(path) 

165 #@+node:ekr.20210905203541.16: *3* TestGlobals.test_g_get_directives_dict 

166 def test_g_get_directives_dict(self): 

167 c = self.c 

168 p = c.p 

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

170 @language python 

171 @comment a b c 

172 # @comment must follow @language. 

173 @tabwidth -8 

174 @pagewidth 72 

175 @encoding utf-8 

176 """) 

177 d = g.get_directives_dict(p) 

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

179 self.assertEqual(d.get('tabwidth'), '-8') 

180 self.assertEqual(d.get('pagewidth'), '72') 

181 self.assertEqual(d.get('encoding'), 'utf-8') 

182 self.assertEqual(d.get('comment'), 'a b c') 

183 assert not d.get('path'), d.get('path') 

184 #@+node:ekr.20210905203541.17: *3* TestGlobals.test_g_getDocString 

185 def test_g_getDocString(self): 

186 s1 = 'no docstring' 

187 s2 = textwrap.dedent('''\ 

188 # comment 

189 """docstring2.""" 

190 ''') 

191 s3 = textwrap.dedent('''\ 

192 """docstring3.""" 

193 \'\'\'docstring2.\'\'\' 

194 ''') 

195 table = ( 

196 (s1, ''), 

197 (s2, 'docstring2.'), 

198 (s3, 'docstring3.'), 

199 ) 

200 for s, result in table: 

201 s2 = g.getDocString(s) 

202 self.assertEqual(s2, result) 

203 #@+node:ekr.20210905203541.18: *3* TestGlobals.test_g_getLine 

204 def test_g_getLine(self): 

205 s = 'a\ncd\n\ne' 

206 for i, result in ( 

207 (-1, (0, 2)), # One too few. 

208 (0, (0, 2)), (1, (0, 2)), 

209 (2, (2, 5)), (3, (2, 5)), (4, (2, 5)), 

210 (5, (5, 6)), 

211 (6, (6, 7)), 

212 (7, (6, 7)), # One too many. 

213 ): 

214 j, k = g.getLine(s, i) 

215 self.assertEqual((j, k), result, msg=f"i: {i}, j: {j}, k: {k}") 

216 #@+node:ekr.20210905203541.20: *3* TestGlobals.test_g_getWord 

217 def test_g_getWord(self): 

218 s = 'abc xy_z5 pdq' 

219 i, j = g.getWord(s, 5) 

220 self.assertEqual(s[i:j], 'xy_z5') 

221 #@+node:ekr.20210905203541.21: *3* TestGlobals.test_g_guessExternalEditor 

222 def test_g_guessExternalEditor(self): 

223 c = self.c 

224 val = g.guessExternalEditor(c) 

225 assert val, 'no val' # This can be different on different platforms. 

226 #@+node:ekr.20210905203541.22: *3* TestGlobals.test_g_handleUrl 

227 def test_g_handleUrl(self): 

228 c = self.c 

229 if sys.platform.startswith('win'): 

230 file_, http, unl1 = 'file://', 'http://', 'unl://' 

231 fn1 = 'LeoDocs.leo#' 

232 fn2 = 'doc/LeoDocs.leo#' 

233 unl2 = '@settings-->Plugins-->wikiview plugin' 

234 unl3 = '@settings-->Plugins-->wikiview%20plugin' 

235 table = ( 

236 (http + 'writemonkey.com/index.php', ['browser']), 

237 (file_ + 'x.py', ['os_startfile']), 

238 (file_ + fn1, ['g.findUNL']), 

239 (file_ + fn2, ['g.findUNL']), 

240 (unl1 + fn1 + unl2, ['g.findUNL']), 

241 (unl1 + fn1 + unl3, ['g.findUNL']), 

242 (unl1 + '#' + unl2, ['g.findUNL']), 

243 (unl1 + '#' + unl3, ['g.findUNL']), 

244 (unl1 + unl2, ['g.findUNL']), 

245 (unl1 + unl3, ['g.findUNL']), 

246 ) 

247 for url, aList in table: 

248 g.handleUrl(c=c, p=c.p, url=url) 

249 #@+node:ekr.20210905203541.23: *3* TestGlobals.test_g_import_module 

250 def test_g_import_module(self): 

251 assert g.import_module('leo.core.leoAst') 

252 # Top-level .py file. 

253 #@+node:ekr.20210905203541.24: *3* TestGlobals.test_g_isDirective 

254 def test_g_isDirective(self): 

255 table = ( 

256 (True, '@language python\n'), 

257 (True, '@tabwidth -4 #test\n'), 

258 (True, '@others\n'), 

259 (True, ' @others\n'), 

260 (True, '@encoding\n'), 

261 (False, '@encoding.setter\n'), 

262 (False, '@encoding("abc")\n'), 

263 (False, 'encoding = "abc"\n'), 

264 ) 

265 for expected, s in table: 

266 result = g.isDirective(s) 

267 self.assertEqual(expected, bool(result), msg=s) 

268 #@+node:ekr.20210905203541.25: *3* TestGlobals.test_g_match_word 

269 def test_g_match_word(self): 

270 table = ( 

271 (True, 0, 'a', 'a'), 

272 (False, 0, 'a', 'b'), 

273 (True, 0, 'a', 'a b'), 

274 (False, 1, 'a', 'aa b'), # Tests bug fixed 2017/06/01. 

275 (False, 1, 'a', '_a b'), 

276 (False, 0, 'a', 'aw b'), 

277 (False, 0, 'a', 'a_'), 

278 (True, 2, 'a', 'b a c'), 

279 (False, 0, 'a', 'b a c'), 

280 ) 

281 for data in table: 

282 expected, i, word, line = data 

283 got = g.match_word(line + '\n', i, word) 

284 self.assertEqual(expected, got) 

285 #@+node:ekr.20210905203541.26: *3* TestGlobals.test_g_os_path_finalize_join_with_thumb_drive 

286 def test_g_os_path_finalize_join_with_thumb_drive(self): 

287 path1 = r'C:\Python32\Lib\site-packages\leo-editor\leo\core' 

288 path2 = r'\N:Home\PTC_Creo\Creo.wmv' 

289 path3 = r'N:\Home\PTC_Creo\Creo.wmv' 

290 path12 = os.path.join(path1, path2) 

291 path13 = os.path.join(path1, path3) 

292 if 0: 

293 print(path12, g.os.path.abspath(path12)) 

294 print(path13, g.os.path.abspath(path13)) 

295 #@+node:ekr.20210905203541.28: *3* TestGlobals.test_g_removeBlankLines 

296 def test_g_removeBlankLines(self): 

297 for s, expected in ( 

298 ('a\nb', 'a\nb'), 

299 ('\n \n\nb\n', 'b\n'), 

300 (' \t \n\n \n c\n\t\n', ' c\n'), 

301 ): 

302 result = g.removeBlankLines(s) 

303 self.assertEqual(result, expected, msg=repr(s)) 

304 #@+node:ekr.20210905203541.30: *3* TestGlobals.test_g_removeLeadingBlankLines 

305 def test_g_removeLeadingBlankLines(self): 

306 for s, expected in ( 

307 ('a\nb', 'a\nb'), 

308 ('\n \nb\n', 'b\n'), 

309 (' \t \n\n\n c', ' c'), 

310 ): 

311 result = g.removeLeadingBlankLines(s) 

312 self.assertEqual(result, expected, msg=repr(s)) 

313 #@+node:ekr.20210905203541.31: *3* TestGlobals.test_g_removeTrailing 

314 def test_g_removeTrailing(self): 

315 s = 'aa bc \n \n\t\n' 

316 table = ( 

317 ('\t\n ', 'aa bc'), 

318 ('abc\t\n ', ''), 

319 ('c\t\n ', 'aa b'), 

320 ) 

321 for arg, val in table: 

322 result = g.removeTrailing(s, arg) 

323 self.assertEqual(result, val) 

324 #@+node:ekr.20210905203541.32: *3* TestGlobals.test_g_sanitize_filename 

325 def test_g_sanitize_filename(self): 

326 table = ( 

327 ('A25&()', 'A'), # Non-alpha characters. 

328 ('B\tc', 'B c'), # Tabs. 

329 ('"AB"', "'AB'"), # Double quotes. 

330 ('\\/:|<>*:.', '_'), # Special characters. 

331 ('_____________', '_'), # Combining underscores. 

332 ('A' * 200, 'A' * 128), # Maximum length. 

333 ('abc.', 'abc_'), # Trailing dots. 

334 ) 

335 for s, expected in table: 

336 got = g.sanitize_filename(s) 

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

338 #@+node:ekr.20210905203541.33: *3* TestGlobals.test_g_scanAtHeaderDirectives_header 

339 def test_g_scanAtHeaderDirectives_header(self): 

340 c = self.c 

341 aList = g.get_directives_dict_list(c.p) 

342 g.scanAtHeaderDirectives(aList) 

343 #@+node:ekr.20210905203541.35: *3* TestGlobals.test_g_scanAtHeaderDirectives_noheader 

344 def test_g_scanAtHeaderDirectives_noheader(self): 

345 c = self.c 

346 aList = g.get_directives_dict_list(c.p) 

347 g.scanAtHeaderDirectives(aList) 

348 #@+node:ekr.20210905203541.36: *3* TestGlobals.test_g_scanAtLineendingDirectives_cr 

349 def test_g_scanAtLineendingDirectives_cr(self): 

350 c = self.c 

351 p = c.p 

352 p.b = '@lineending cr\n' 

353 aList = g.get_directives_dict_list(p) 

354 s = g.scanAtLineendingDirectives(aList) 

355 self.assertEqual(s, '\r') 

356 #@+node:ekr.20210905203541.37: *3* TestGlobals.test_g_scanAtLineendingDirectives_crlf 

357 def test_g_scanAtLineendingDirectives_crlf(self): 

358 c = self.c 

359 p = c.p 

360 p.b = '@lineending crlf\n' 

361 aList = g.get_directives_dict_list(p) 

362 s = g.scanAtLineendingDirectives(aList) 

363 self.assertEqual(s, '\r\n') 

364 #@+node:ekr.20210905203541.38: *3* TestGlobals.test_g_scanAtLineendingDirectives_lf 

365 def test_g_scanAtLineendingDirectives_lf(self): 

366 c = self.c 

367 p = c.p 

368 p.b = '@lineending lf\n' 

369 aList = g.get_directives_dict_list(p) 

370 s = g.scanAtLineendingDirectives(aList) 

371 self.assertEqual(s, '\n') 

372 #@+node:ekr.20210905203541.39: *3* TestGlobals.test_g_scanAtLineendingDirectives_nl 

373 def test_g_scanAtLineendingDirectives_nl(self): 

374 c = self.c 

375 p = c.p 

376 p.b = '@lineending nl\n' 

377 aList = g.get_directives_dict_list(p) 

378 s = g.scanAtLineendingDirectives(aList) 

379 self.assertEqual(s, '\n') 

380 #@+node:ekr.20210905203541.40: *3* TestGlobals.test_g_scanAtLineendingDirectives_platform 

381 def test_g_scanAtLineendingDirectives_platform(self): 

382 c = self.c 

383 p = c.p 

384 p.b = '@lineending platform\n' 

385 aList = g.get_directives_dict_list(p) 

386 s = g.scanAtLineendingDirectives(aList) 

387 if sys.platform.startswith('win'): 

388 self.assertEqual(s, '\r\n') # pragma: no cover 

389 else: 

390 self.assertEqual(s, '\n') # pragma: no cover 

391 #@+node:ekr.20210905203541.41: *3* TestGlobals.test_g_scanAtPagewidthDirectives_minus_40 

392 def test_g_scanAtPagewidthDirectives_minus_40(self): 

393 c = self.c 

394 p = c.p 

395 p.b = '@pagewidth -40\n' 

396 aList = g.get_directives_dict_list(p) 

397 n = g.scanAtPagewidthDirectives(aList) 

398 # The @pagewidth directive in the parent should control. 

399 # Depending on how this test is run, the result could be 80 or None. 

400 assert n in (None, 80), repr(n) 

401 #@+node:ekr.20210905203541.42: *3* TestGlobals.test_g_scanAtPagewidthDirectives_40 

402 def test_g_scanAtPagewidthDirectives_40(self): 

403 c = self.c 

404 p = c.p 

405 p.b = '@pagewidth 40\n' 

406 aList = g.get_directives_dict_list(p) 

407 n = g.scanAtPagewidthDirectives(aList) 

408 self.assertEqual(n, 40) 

409 #@+node:ekr.20210905203541.43: *3* TestGlobals.test_g_scanAtTabwidthDirectives_6 

410 def test_g_scanAtTabwidthDirectives_6(self): 

411 c = self.c 

412 p = c.p 

413 p.b = '@tabwidth 6\n' 

414 aList = g.get_directives_dict_list(p) 

415 n = g.scanAtTabwidthDirectives(aList) 

416 self.assertEqual(n, 6) 

417 #@+node:ekr.20210905203541.44: *3* TestGlobals.test_g_scanAtTabwidthDirectives_minus_6 

418 def test_g_scanAtTabwidthDirectives_minus_6(self): 

419 c = self.c 

420 p = c.p 

421 p.b = '@tabwidth -6\n' 

422 aList = g.get_directives_dict_list(p) 

423 n = g.scanAtTabwidthDirectives(aList) 

424 self.assertEqual(n, -6) 

425 #@+node:ekr.20210905203541.45: *3* TestGlobals.test_g_scanAtWrapDirectives_nowrap 

426 def test_g_scanAtWrapDirectives_nowrap(self): 

427 c = self.c 

428 p = c.p 

429 p.b = '@nowrap\n' 

430 aList = g.get_directives_dict_list(p) 

431 s = g.scanAtWrapDirectives(aList) 

432 assert s is False, repr(s) 

433 #@+node:ekr.20210905203541.46: *3* TestGlobals.test_g_scanAtWrapDirectives_wrap_with_wrap_ 

434 def test_g_scanAtWrapDirectives_wrap_with_wrap_(self): 

435 c = self.c 

436 p = c.p 

437 p.b = '@wrap\n' 

438 aList = g.get_directives_dict_list(p) 

439 s = g.scanAtWrapDirectives(aList) 

440 assert s is True, repr(s) 

441 #@+node:ekr.20210905203541.47: *3* TestGlobals.test_g_scanAtWrapDirectives_wrap_without_nowrap_ 

442 def test_g_scanAtWrapDirectives_wrap_without_nowrap_(self): 

443 c = self.c 

444 aList = g.get_directives_dict_list(c.p) 

445 s = g.scanAtWrapDirectives(aList) 

446 assert s is None, repr(s) 

447 #@+node:ekr.20210905203541.48: *3* TestGlobals.test_g_set_delims_from_language 

448 def test_g_set_delims_from_language(self): 

449 table = ( 

450 ('c', ('//', '/*', '*/')), 

451 ('python', ('#', '', '')), 

452 ('xxxyyy', ('', '', '')), 

453 ) 

454 for language, expected in table: 

455 result = g.set_delims_from_language(language) 

456 self.assertEqual(result, expected, msg=language) 

457 #@+node:ekr.20210905203541.49: *3* TestGlobals.test_g_set_delims_from_string 

458 def test_g_set_delims_from_string(self): 

459 table = ( 

460 ('c', '@comment // /* */', ('//', '/*', '*/')), 

461 ('c', '// /* */', ('//', '/*', '*/')), 

462 ('python', '@comment #', ('#', '', '')), 

463 ('python', '#', ('#', '', '')), 

464 ('xxxyyy', '@comment a b c', ('a', 'b', 'c')), 

465 ('xxxyyy', 'a b c', ('a', 'b', 'c')), 

466 ) 

467 for language, s, expected in table: 

468 result = g.set_delims_from_string(s) 

469 self.assertEqual(result, expected, msg=language) 

470 #@+node:ekr.20210905203541.50: *3* TestGlobals.test_g_skip_blank_lines 

471 def test_g_skip_blank_lines(self): 

472 end = g.skip_blank_lines("", 0) 

473 self.assertEqual(end, 0) 

474 end = g.skip_blank_lines(" ", 0) 

475 self.assertEqual(end, 0) 

476 end = g.skip_blank_lines("\n", 0) 

477 self.assertEqual(end, 1) 

478 end = g.skip_blank_lines(" \n", 0) 

479 self.assertEqual(end, 2) 

480 end = g.skip_blank_lines("\n\na\n", 0) 

481 self.assertEqual(end, 2) 

482 end = g.skip_blank_lines("\n\n a\n", 0) 

483 self.assertEqual(end, 2) 

484 #@+node:ekr.20210905203541.51: *3* TestGlobals.test_g_skip_line 

485 def test_g_skip_line(self): 

486 s = 'a\n\nc' 

487 for i, result in ( 

488 (-1, 2), # One too few. 

489 (0, 2), (1, 2), 

490 (2, 3), 

491 (3, 4), 

492 (4, 4), # One too many. 

493 ): 

494 j = g.skip_line(s, i) 

495 self.assertEqual(j, result, msg=i) 

496 #@+node:ekr.20210905203541.52: *3* TestGlobals.test_g_skip_to_end_of_line 

497 def test_g_skip_to_end_of_line(self): 

498 s = 'a\n\nc' 

499 for i, result in ( 

500 (-1, 1), # One too few. 

501 (0, 1), (1, 1), 

502 (2, 2), 

503 (3, 4), 

504 (4, 4), # One too many. 

505 ): 

506 j = g.skip_to_end_of_line(s, i) 

507 self.assertEqual(j, result, msg=i) 

508 #@+node:ekr.20210905203541.53: *3* TestGlobals.test_g_skip_to_start_of_line 

509 def test_g_skip_to_start_of_line(self): 

510 s1 = 'a\n\nc' 

511 table1 = ( 

512 (-1, 0), # One too few. 

513 (0, 0), (1, 0), 

514 (2, 2), 

515 (3, 3), 

516 (4, 4), # One too many. 

517 ) 

518 s2 = 'a\n' 

519 table2 = ( 

520 (1, 0), 

521 (2, 2), 

522 ) # A special case at end. 

523 for s, table in ((s1, table1), (s2, table2)): 

524 for i, result in table: 

525 j = g.skip_to_start_of_line(s, i) 

526 self.assertEqual(j, result, msg=i) 

527 #@+node:ekr.20210905203541.54: *3* TestGlobals.test_g_splitLongFileName 

528 def test_g_splitLongFileName(self): 

529 table = ( 

530 r'abcd/xy\pdqabc/aaa.py', 

531 ) 

532 for s in table: 

533 g.splitLongFileName(s, limit=3) 

534 #@+node:ekr.20210905203541.55: *3* TestGlobals.test_g_stripPathCruft 

535 def test_g_stripPathCruft(self): 

536 table = ( 

537 (None, None), # Retain empty paths for warnings. 

538 ('', ''), 

539 (g.app.loadDir, g.app.loadDir), 

540 ('<abc>', 'abc'), 

541 ('"abc"', 'abc'), 

542 ("'abc'", 'abc'), 

543 ) 

544 for path, expected in table: 

545 result = g.stripPathCruft(path) 

546 self.assertEqual(result, expected) 

547 #@+node:ekr.20210905203541.56: *3* TestGlobals.test_g_warnOnReadOnlyFile 

548 def test_g_warnOnReadOnlyFile(self): 

549 c = self.c 

550 fc = c.fileCommands 

551 path = g.os_path_finalize_join(g.app.loadDir, '..', 'test', 'test-read-only.txt') 

552 if os.path.exists(path): # pragma: no cover 

553 os.chmod(path, stat.S_IREAD) 

554 fc.warnOnReadOnlyFiles(path) 

555 assert fc.read_only 

556 else: # pragma: no cover 

557 fc.warnOnReadOnlyFiles(path) 

558 #@-others 

559#@-others 

560#@-leo