Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

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

2#@+leo-ver=5-thin 

3#@+node:ekr.20210902092024.1: * @file ../unittests/core/test_leoShadow.py 

4#@@first 

5"""Tests of leoShadow.py""" 

6 

7import glob 

8import os 

9import textwrap 

10from leo.core import leoGlobals as g 

11from leo.core.leoShadow import ShadowController 

12from leo.core.leoTest2 import LeoUnitTest 

13 

14#@+others 

15#@+node:ekr.20080709062932.2: ** class TestAtShadow (LeoUnitTest) 

16class TestAtShadow(LeoUnitTest): 

17 #@+others 

18 #@+node:ekr.20080709062932.8: *3* TestShadow.setUp & helpers 

19 def setUp(self): 

20 """AtShadowTestCase.setup.""" 

21 super().setUp() 

22 delims = '#', '', '' 

23 c = self.c 

24 base_dir = os.path.dirname(__file__) 

25 c.mFileName = g.os_path_finalize_join(base_dir, '..', '..', 'test666.leo') 

26 self.shadow_controller = ShadowController(c) 

27 self.marker = self.shadow_controller.Marker(delims) 

28 

29 #@+node:ekr.20210902210953.1: *4* TestShadow.deleteShadowDir (was a function) 

30 def deleteShadowDir(self, shadow_dir): 

31 if not g.os_path_exists(shadow_dir): 

32 return 

33 files = g.os_path_abspath(g.os_path_join(shadow_dir, "*.*")) 

34 files = glob.glob(files) 

35 for z in files: 

36 if z != shadow_dir: 

37 os.unlink(z) 

38 os.rmdir(shadow_dir) 

39 self.assertFalse(os.path.exists(shadow_dir), msg=shadow_dir) 

40 #@+node:ekr.20210908053444.1: *4* TestShadow.make_lines 

41 def make_lines(self, old, new): 

42 """Make all lines and return the result of propagating changed lines.""" 

43 c = self.c 

44 # Calculate all required lines. 

45 old_private_lines = self.makePrivateLines(old) 

46 new_private_lines = self.makePrivateLines(new) 

47 old_public_lines = self.makePublicLines(old_private_lines) 

48 new_public_lines = self.makePublicLines(new_private_lines) 

49 expected_private_lines = self.mungePrivateLines( 

50 new_private_lines, 'node:new', 'node:old') 

51 # Return the propagated results. 

52 results = self.shadow_controller.propagate_changed_lines( 

53 new_public_lines, old_private_lines, self.marker, p=c.p) 

54 if 0: # To verify that sentinels are as expected. 

55 print('') 

56 print(g.callers(1)) 

57 g.printObj(old_private_lines, tag='old_private_lines') 

58 g.printObj(new_private_lines, tag='new_private_lines') 

59 g.printObj(old_public_lines, tag='old_public_lines') 

60 g.printObj(new_public_lines, tag='new_public_lines') 

61 return results, expected_private_lines 

62 #@+node:ekr.20080709062932.21: *4* TestShadow.makePrivateLines 

63 def makePrivateLines(self, p): 

64 """Return a list of the lines of p containing sentinels.""" 

65 at = self.c.atFileCommands 

66 # A hack: we want to suppress gnx's *only* in @+node sentinels, 

67 # but we *do* want sentinels elsewhere. 

68 at.at_shadow_test_hack = True 

69 try: 

70 s = at.atFileToString(p, sentinels=True) 

71 finally: 

72 at.at_shadow_test_hack = False 

73 return g.splitLines(s) 

74 #@+node:ekr.20080709062932.22: *4* TestShadow.makePublicLines 

75 def makePublicLines(self, lines): 

76 """Return the public lines in lines.""" 

77 lines, junk = self.shadow_controller.separate_sentinels(lines, self.marker) 

78 return lines 

79 #@+node:ekr.20080709062932.23: *4* TestShadow.mungePrivateLines 

80 def mungePrivateLines(self, lines, find, replace): 

81 """Change the 'find' the 'replace' pattern in sentinel lines.""" 

82 marker = self.marker 

83 i, results = 0, [] 

84 while i < len(lines): 

85 line = lines[i] 

86 if marker.isSentinel(line): 

87 new_line = line.replace(find, replace) 

88 results.append(new_line) 

89 if marker.isVerbatimSentinel(line): 

90 i += 1 

91 if i < len(lines): 

92 line = lines[i] 

93 results.append(line) 

94 else: 

95 self.shadow_controller.verbatim_error() 

96 else: 

97 results.append(line) 

98 i += 1 

99 return results 

100 #@+node:ekr.20210908160006.1: *3* test update algorithm... 

101 #@+node:ekr.20210908134131.16: *4* TestShadow.test_change_end_of_prev_node 

102 def test_change_end_of_prev_node(self): 

103 p = self.c.p 

104 # Create the 'old' node. 

105 old = p.insertAsLastChild() 

106 old.h = 'old' 

107 old.b = textwrap.dedent("""\ 

108 ATothers 

109 node 1 line 1 

110 node 1 line 2 

111 node 2 line 1 

112 node 2 line 2 

113 """).replace('AT', '@') 

114 # Create the 'new' node. 

115 new = p.insertAsLastChild() 

116 new.h = 'new' 

117 new.b = textwrap.dedent("""\ 

118 ATothers 

119 node 1 line 1 

120 node 1 line 1 changed 

121 node 2 line 1 

122 node 2 line 2 

123 """).replace('AT', '@') 

124 # Run the test. 

125 results, expected = self.make_lines(old, new) 

126 self.assertEqual(results, expected) 

127 #@+node:ekr.20210908134131.4: *4* TestShadow.test_change_first_line 

128 def test_change_first_line(self): 

129 p = self.c.p 

130 # Create the 'old' node. 

131 old = p.insertAsLastChild() 

132 old.h = 'old' 

133 old.b = textwrap.dedent("""\ 

134 line 1 

135 line 2 

136 line 3 

137 """) 

138 # Create the 'new' node. 

139 new = p.insertAsLastChild() 

140 new.h = 'new' 

141 new.b = textwrap.dedent("""\ 

142 line 1 changed 

143 line 2 

144 line 3 

145 """) 

146 # Run the test. 

147 results, expected = self.make_lines(old, new) 

148 self.assertEqual(results, expected) 

149 #@+node:ekr.20210908134131.5: *4* TestShadow.test_change_last_line 

150 def test_change_last_line(self): 

151 p = self.c.p 

152 # Create the 'old' node. 

153 old = p.insertAsLastChild() 

154 old.h = 'old' 

155 old.b = textwrap.dedent("""\ 

156 line 1 

157 line 2 

158 line 3 

159 """) 

160 # Create the 'new' node. 

161 new = p.insertAsLastChild() 

162 new.h = 'new' 

163 new.b = textwrap.dedent("""\ 

164 line 1 

165 line 2 

166 line 3 changed 

167 """) 

168 # Run the test. 

169 results, expected = self.make_lines(old, new) 

170 self.assertEqual(results, expected) 

171 #@+node:ekr.20210908134131.3: *4* TestShadow.test_change_middle_line 

172 def test_change_middle_line(self): 

173 p = self.c.p 

174 # Create the 'old' node. 

175 old = p.insertAsLastChild() 

176 old.h = 'old' 

177 old.b = textwrap.dedent("""\ 

178 line 1 

179 line 2 

180 line 3 

181 """) 

182 # Create the 'new' node. 

183 new = p.insertAsLastChild() 

184 new.h = 'new' 

185 new.b = textwrap.dedent("""\ 

186 line 1 

187 line 2 changed 

188 line 3 

189 """) 

190 # Run the test. 

191 results, expected = self.make_lines(old, new) 

192 self.assertEqual(results, expected) 

193 #@+node:ekr.20210908134131.17: *4* TestShadow.test_change_start_of_next_node 

194 def test_change_start_of_next_node(self): 

195 p = self.c.p 

196 # Create the 'old' node. 

197 old = p.insertAsLastChild() 

198 old.h = 'old' 

199 old.b = textwrap.dedent("""\ 

200 at-others 

201 node 1 line 1 

202 node 1 line 2 

203 node 2 line 1 

204 node 2 line 2 

205 """).replace('at-others', '@others') 

206 # Create the 'new' node. 

207 new = p.insertAsLastChild() 

208 new.h = 'new' 

209 new.b = textwrap.dedent("""\ 

210 at-others 

211 node 1 line 1 

212 node 1 line 2 

213 node 2 line 1 changed 

214 node 2 line 2 

215 """).replace('at-others', '@others') 

216 # Run the test. 

217 results, expected = self.make_lines(old, new) 

218 self.assertEqual(results, expected) 

219 #@+node:ekr.20210908134131.14: *4* TestShadow.test_delete_between_nodes_at_end_of_prev_node 

220 def test_delete_between_nodes_at_end_of_prev_node(self): 

221 p = self.c.p 

222 # Create the 'old' node. 

223 old = p.insertAsLastChild() 

224 old.h = 'old' 

225 old.b = textwrap.dedent("""\ 

226 at-others 

227 node 1 line 1 

228 node 1 line 2 

229 node 2 line 1 

230 node 2 line 2 

231 """).replace('at-others', '@others') 

232 # Create the 'new' node. 

233 new = p.insertAsLastChild() 

234 new.h = 'new' 

235 new.b = textwrap.dedent("""\ 

236 at-others 

237 node 1 line 1 

238 node 2 line 1 

239 node 2 line 2 

240 """).replace('at-others', '@others') 

241 # Run the test. 

242 results, expected = self.make_lines(old, new) 

243 self.assertEqual(results, expected) 

244 #@+node:ekr.20210908134131.15: *4* TestShadow.test_delete_between_nodes_at_start_of_next_node 

245 def test_delete_between_nodes_at_start_of_next_node(self): 

246 p = self.c.p 

247 # Create the 'old' node. 

248 old = p.insertAsLastChild() 

249 old.h = 'old' 

250 old.b = textwrap.dedent("""\ 

251 at-others 

252 node 1 line 1 

253 node 2 line 1 

254 node 2 line 2 

255 """).replace('at-others', '@others') 

256 # Create the 'new' node. 

257 new = p.insertAsLastChild() 

258 new.h = 'new' 

259 new.b = textwrap.dedent("""\ 

260 at-others 

261 node 1 line 1 

262 node 2 line 2 

263 """).replace('at-others', '@others') 

264 # Run the test. 

265 results, expected = self.make_lines(old, new) 

266 self.assertEqual(results, expected) 

267 #@+node:ekr.20210908134131.6: *4* TestShadow.test_delete_first_line 

268 def test_delete_first_line(self): 

269 p = self.c.p 

270 # Create the 'old' node. 

271 old = p.insertAsLastChild() 

272 old.h = 'old' 

273 old.b = textwrap.dedent("""\ 

274 line 1 

275 line 2 

276 line 3 

277 """) 

278 # Create the 'new' node. 

279 new = p.insertAsLastChild() 

280 new.h = 'new' 

281 new.b = textwrap.dedent("""\ 

282 line 2 

283 line 3 

284 """) 

285 # Run the test. 

286 results, expected = self.make_lines(old, new) 

287 self.assertEqual(results, expected) 

288 #@+node:ekr.20210908134131.8: *4* TestShadow.test_delete_last_line 

289 def test_delete_last_line(self): 

290 p = self.c.p 

291 # Create the 'old' node. 

292 old = p.insertAsLastChild() 

293 old.h = 'old' 

294 old.b = textwrap.dedent("""\ 

295 line 1 

296 line 2 

297 line 3 

298 """) 

299 # Create the 'new' node. 

300 new = p.insertAsLastChild() 

301 new.h = 'new' 

302 new.b = textwrap.dedent("""\ 

303 line 1 

304 line 2 

305 """) 

306 # Run the test. 

307 results, expected = self.make_lines(old, new) 

308 self.assertEqual(results, expected) 

309 #@+node:ekr.20210908134131.7: *4* TestShadow.test_delete_middle_line 

310 def test_delete_middle_line(self): 

311 p = self.c.p 

312 # Create the 'old' node. 

313 old = p.insertAsLastChild() 

314 old.h = 'old' 

315 old.b = textwrap.dedent("""\ 

316 line 1 

317 line 2 

318 line 3 

319 """) 

320 # Create the 'new' node. 

321 new = p.insertAsLastChild() 

322 new.h = 'new' 

323 new.b = textwrap.dedent("""\ 

324 line 1 

325 line 3 

326 """) 

327 # Run the test. 

328 results, expected = self.make_lines(old, new) 

329 self.assertEqual(results, expected) 

330 #@+node:ekr.20210908134131.12: *4* TestShadow.test_insert_after_last_line 

331 def test_insert_after_last_line(self): 

332 p = self.c.p 

333 # Create the 'old' node. 

334 old = p.insertAsLastChild() 

335 old.h = 'old' 

336 old.b = textwrap.dedent("""\ 

337 line 1 

338 line 2 

339 line 3 

340 """) 

341 # Create the 'new' node. 

342 new = p.insertAsLastChild() 

343 new.h = 'new' 

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

345 line 1 

346 line 2 

347 line 3 

348 inserted line 

349 """) 

350 # Run the test. 

351 results, expected = self.make_lines(old, new) 

352 self.assertEqual(results, expected) 

353 #@+node:ekr.20210908134131.9: *4* TestShadow.test_insert_before_first_line 

354 def test_insert_before_first_line(self): 

355 p = self.c.p 

356 # Create the 'old' node. 

357 old = p.insertAsLastChild() 

358 old.h = 'old' 

359 old.b = textwrap.dedent("""\ 

360 line 1 

361 line 2 

362 line 3 

363 """) 

364 # Create the 'new' node. 

365 new = p.insertAsLastChild() 

366 new.h = 'new' 

367 new.b = textwrap.dedent("""\ 

368 inserted line 

369 line 1 

370 line 2 

371 line 3 

372 """) 

373 # Run the test. 

374 results, expected = self.make_lines(old, new) 

375 self.assertEqual(results, expected) 

376 #@+node:ekr.20210908134131.10: *4* TestShadow.test_insert_middle_line_after_first_line_ 

377 def test_insert_middle_line_after_first_line_(self): 

378 p = self.c.p 

379 # Create the 'old' node. 

380 old = p.insertAsLastChild() 

381 old.h = 'old' 

382 old.b = textwrap.dedent("""\ 

383 line 1 

384 line 2 

385 line 3 

386 """) 

387 # Create the 'new' node. 

388 new = p.insertAsLastChild() 

389 new.h = 'new' 

390 new.b = textwrap.dedent("""\ 

391 line 1 

392 inserted line 

393 line 2 

394 line 3 

395 """) 

396 # Run the test. 

397 results, expected = self.make_lines(old, new) 

398 self.assertEqual(results, expected) 

399 #@+node:ekr.20210908134131.11: *4* TestShadow.test_insert_middle_line_before_last_line_ 

400 def test_insert_middle_line_before_last_line_(self): 

401 p = self.c.p 

402 # Create the 'old' node. 

403 old = p.insertAsLastChild() 

404 old.h = 'old' 

405 old.b = textwrap.dedent("""\ 

406 line 1 

407 line 2 

408 line 3 

409 """) 

410 # Create the 'new' node. 

411 new = p.insertAsLastChild() 

412 new.h = 'new' 

413 new.b = textwrap.dedent("""\ 

414 line 1 

415 line 2 

416 inserted line 

417 line 3 

418 """) 

419 # Run the test. 

420 results, expected = self.make_lines(old, new) 

421 self.assertEqual(results, expected) 

422 #@+node:ekr.20210908134131.13: *4* TestShadow.test_lax_insert_between_nodes_at_end_of_prev_node 

423 def test_lax_insert_between_nodes_at_end_of_prev_node(self): 

424 p = self.c.p 

425 # Create the 'old' node. 

426 old = p.insertAsLastChild() 

427 old.h = 'old' 

428 old.b = textwrap.dedent("""\ 

429 at-others 

430 node 1 line 1 

431 node 2 line 1 

432 """).replace('at-others', '@others') 

433 # Create the 'new' node. 

434 new = p.insertAsLastChild() 

435 new.h = 'new' 

436 new.b = textwrap.dedent("""\ 

437 at-others 

438 node 1 line 1 

439 inserted node at end of node 1 

440 node 2 line 1 

441 """).replace('at-others', '@others') 

442 # Run the test. 

443 results, expected = self.make_lines(old, new) 

444 self.assertEqual(results, expected) 

445 #@+node:ekr.20210908134131.18: *4* TestShadow.test_lax_multiple_line_insert_between_nodes_at_end_of_prev_node 

446 def test_lax_multiple_line_insert_between_nodes_at_end_of_prev_node(self): 

447 p = self.c.p 

448 # Create the 'old' node. 

449 old = p.insertAsLastChild() 

450 old.h = 'old' 

451 old.b = textwrap.dedent("""\ 

452 at-others 

453 node 1 line 1 

454 inserted node 1 at end of node 1 

455 inserted node 2 at end of node 1 

456 node 2 line 1 

457 """).replace('at-others', '@others') 

458 # Create the 'new' node. 

459 new = p.insertAsLastChild() 

460 new.h = 'new' 

461 new.b = textwrap.dedent("""\ 

462 at-others 

463 node 1 line 1 

464 node 2 line 1 

465 """).replace('at-others', '@others') 

466 # Run the test. 

467 results, expected = self.make_lines(old, new) 

468 self.assertEqual(results, expected) 

469 #@+node:ekr.20210908134131.19: *4* TestShadow.test_multiple_line_change_end_of_prev_node 

470 def test_multiple_line_change_end_of_prev_node(self): 

471 p = self.c.p 

472 # Create the 'old' node. 

473 old = p.insertAsLastChild() 

474 old.h = 'old' 

475 old.b = textwrap.dedent("""\ 

476 at-others 

477 node 1 line 1 

478 node 1 line 2 

479 node 1 line 3 

480 node 2 line 1 

481 node 2 line 2 

482 """).replace('at-others', '@others') 

483 # Create the 'new' node. 

484 new = p.insertAsLastChild() 

485 new.h = 'new' 

486 new.b = textwrap.dedent("""\ 

487 at-others 

488 node 1 line 1 

489 node 1 line 2 changed 

490 node 1 line 3 changed 

491 node 2 line 1 

492 node 2 line 2 

493 """).replace('at-others', '@others') 

494 # Run the test. 

495 results, expected = self.make_lines(old, new) 

496 self.assertEqual(results, expected) 

497 #@+node:ekr.20210908134131.20: *4* TestShadow.test_multiple_line_change_start_of_next_node 

498 def test_multiple_line_change_start_of_next_node(self): 

499 p = self.c.p 

500 # Create the 'old' node. 

501 old = p.insertAsLastChild() 

502 old.h = 'old' 

503 old.b = textwrap.dedent("""\ 

504 at-others 

505 node 1 line 1 

506 node 1 line 2 

507 node 2 line 1 

508 node 2 line 2 

509 """).replace('at-others', '@others') 

510 # Create the 'new' node. 

511 new = p.insertAsLastChild() 

512 new.h = 'new' 

513 new.b = textwrap.dedent("""\ 

514 at-others 

515 node 1 line 1 

516 node 1 line 2 

517 node 2 line 1 changed 

518 node 2 line 2 changed 

519 """).replace('at-others', '@others') 

520 # Run the test. 

521 results, expected = self.make_lines(old, new) 

522 self.assertEqual(results, expected) 

523 #@+node:ekr.20210908134131.22: *4* TestShadow.test_multiple_line_delete_between_nodes_at_end_of_prev_node 

524 def test_multiple_line_delete_between_nodes_at_end_of_prev_node(self): 

525 p = self.c.p 

526 # Create the 'old' node. 

527 old = p.insertAsLastChild() 

528 old.h = 'old' 

529 old.b = textwrap.dedent("""\ 

530 at-others 

531 node 1 line 1 

532 node 1 line 2 

533 node 1 line 3 

534 node 2 line 1 

535 node 2 line 2 

536 """).replace('at-others', '@others') 

537 # Create the 'new' node. 

538 new = p.insertAsLastChild() 

539 new.h = 'new' 

540 new.b = textwrap.dedent("""\ 

541 at-others 

542 node 1 line 1 

543 node 2 line 1 

544 node 2 line 2 

545 """).replace('at-others', '@others') 

546 # Run the test. 

547 results, expected = self.make_lines(old, new) 

548 self.assertEqual(results, expected) 

549 #@+node:ekr.20210908134131.23: *4* TestShadow.test_multiple_line_delete_between_nodes_at_start_of_next_node 

550 def test_multiple_line_delete_between_nodes_at_start_of_next_node(self): 

551 p = self.c.p 

552 # Create the 'old' node. 

553 old = p.insertAsLastChild() 

554 old.h = 'old' 

555 old.b = textwrap.dedent("""\ 

556 at-others 

557 node 1 line 1 

558 node 2 line 1 

559 node 2 line 2 

560 node 2 line 3 

561 """).replace('at-others', '@others') 

562 # Create the 'new' node. 

563 new = p.insertAsLastChild() 

564 new.h = 'new' 

565 new.b = textwrap.dedent("""\ 

566 at-others 

567 node 1 line 1 

568 node 2 line 3 

569 """).replace('at-others', '@others') 

570 # Run the test. 

571 results, expected = self.make_lines(old, new) 

572 self.assertEqual(results, expected) 

573 #@+node:ekr.20210908134131.21: *4* TestShadow.test_multiple_node_changes 

574 def test_multiple_node_changes(self): 

575 p = self.c.p 

576 # Create the 'old' node. 

577 old = p.insertAsLastChild() 

578 old.h = 'old' 

579 old.b = textwrap.dedent("""\ 

580 at-others 

581 node 1 line 1 

582 node 1 line 2 

583 node 2 line 1 

584 node 2 line 2 

585 """).replace('at-others', '@others') 

586 # Create the 'new' node. 

587 new = p.insertAsLastChild() 

588 new.h = 'new' 

589 new.b = textwrap.dedent("""\ 

590 at-others 

591 node 1 line 1 

592 node 1 line 2 changed 

593 node 2 line 1 changed 

594 node 2 line 2 changed 

595 """).replace('at-others', '@others') 

596 # Run the test. 

597 results, expected = self.make_lines(old, new) 

598 self.assertEqual(results, expected) 

599 #@+node:ekr.20210908134131.29: *4* TestShadow.test_no_change_no_ending_newline 

600 def test_no_change_no_ending_newline(self): 

601 p = self.c.p 

602 # Create the 'old' node. 

603 old = p.insertAsLastChild() 

604 old.h = 'old' 

605 old.b = textwrap.dedent("""\ 

606 line 

607 """) 

608 # Create the 'new' node. 

609 new = p.insertAsLastChild() 

610 new.h = 'new' 

611 new.b = textwrap.dedent("""\ 

612 line 

613 """) 

614 # Run the test. 

615 results, expected = self.make_lines(old, new) 

616 self.assertEqual(results, expected) 

617 #@+node:ekr.20210907162104.2: *4* TestShadow.test_replace_in_node_new_gt_new_old 

618 def test_replace_in_node_new_gt_new_old(self): 

619 p = self.c.p 

620 old = p.insertAsLastChild() 

621 old.h = 'old' 

622 old.b = '@others\n' 

623 old_node1 = old.insertAsLastChild() 

624 old_node1.h = 'node1' 

625 old_node1.b = textwrap.dedent("""\ 

626 node 1 line 1 

627 node 1 old line 1 

628 node 1 old line 2 

629 node 1 line 2 

630 """) 

631 new = p.insertAsLastChild() 

632 new.h = 'new' 

633 new.b = '@others\n' 

634 new_node1 = new.insertAsLastChild() 

635 new_node1.h = 'node1' 

636 new_node1.b = textwrap.dedent("""\ 

637 node 1 line 1 

638 node 1 new line 1 

639 node 1 new line 2 

640 node 1 new line 3 

641 node 1 line 2 

642 """) 

643 results, expected = self.make_lines(old, new) 

644 self.assertEqual(results, expected) 

645 #@+node:ekr.20210908134131.2: *4* TestShadow.test_replace_in_node_new_lt_old 

646 def test_replace_in_node_new_lt_old(self): 

647 p = self.c.p 

648 # Create the 'old' node. 

649 old = p.insertAsLastChild() 

650 old.h = 'old' 

651 old.b = '@others\n' 

652 old_node1 = old.insertAsLastChild() 

653 old_node1.h = 'node1' 

654 old_node1.b = textwrap.dedent("""\ 

655 node 1 line 1 

656 node 1 old line 1 

657 node 1 old line 2 

658 node 1 old line 3 

659 node 1 old line 4 

660 node 1 line 2 

661 """) 

662 # Create the 'new' node. 

663 new = p.insertAsLastChild() 

664 new.h = 'new' 

665 new.b = '@others\n' 

666 new_node1 = new.insertAsLastChild() 

667 new_node1.h = 'node1' 

668 new_node1.b = textwrap.dedent("""\ 

669 node 1 line 1 

670 node 1 new line 1 

671 node 1 new line 2 

672 node 1 line 2 

673 """) 

674 # Run the test. 

675 results, expected = self.make_lines(old, new) 

676 self.assertEqual(results, expected) 

677 #@+node:ekr.20210908140242.6: *4* TestShadow.test_verbatim_sentinels_add_verbatim_line 

678 def test_verbatim_sentinels_add_verbatim_line(self): 

679 p = self.c.p 

680 # Create the 'old' node. 

681 old = p.insertAsLastChild() 

682 old.h = 'old' 

683 old.b = textwrap.dedent("""\ 

684 at-others 

685 node 1 line 1 

686 node 1 line 2 

687 node 2 line 1 

688 node 2 line 2 

689 """).replace('at-', '@') 

690 # Create the 'new' node. 

691 new = p.insertAsLastChild() 

692 new.h = 'new' 

693 new.b = textwrap.dedent("""\ 

694 at-others 

695 node 1 line 1 

696 at-verbatim 

697 # at- should be handled by verbatim 

698 node 1 line 2 

699 node 2 line 1 

700 node 2 line 2 

701 """).replace('at-', '@') 

702 # Run the test. 

703 results, expected = self.make_lines(old, new) 

704 self.assertEqual(results, expected) 

705 #@+node:ekr.20210908140242.2: *4* TestShadow.test_verbatim_sentinels_delete_verbatim_line 

706 def test_verbatim_sentinels_delete_verbatim_line(self): 

707 p = self.c.p 

708 # Create the 'old' node. 

709 old = p.insertAsLastChild() 

710 old.h = 'old' 

711 old.b = textwrap.dedent("""\ 

712 at-others 

713 node 1 line 1 

714 at-verbatim 

715 # at- should be handled by verbatim 

716 line 1 line 3 

717 node 2 line 1 

718 node 2 line 2 

719 node 2 line 3 

720 """).replace('at-', '@') 

721 # Create the 'new' node. 

722 new = p.insertAsLastChild() 

723 new.h = 'new' 

724 new.b = textwrap.dedent("""\ 

725 at-others 

726 node 1 line 1 

727 line 1 line 3 

728 node 2 line 1 

729 node 2 line 2 

730 node 2 line 3 

731 """).replace('at-', '@') 

732 # Run the test. 

733 results, expected = self.make_lines(old, new) 

734 self.assertEqual(results, expected) 

735 #@+node:ekr.20210908140242.5: *4* TestShadow.test_verbatim_sentinels_delete_verbatim_line_at_end_of_node 

736 def test_verbatim_sentinels_delete_verbatim_line_at_end_of_node(self): 

737 p = self.c.p 

738 # Create the 'old' node. 

739 old = p.insertAsLastChild() 

740 old.h = 'old' 

741 old.b = textwrap.dedent("""\ 

742 at-others 

743 node 1 line 1 

744 at-verbatim 

745 # at- should be handled by verbatim 

746 node 2 line 1 

747 node 2 line 2 

748 """).replace('at-', '@') 

749 # Create the 'new' node. 

750 new = p.insertAsLastChild() 

751 new.h = 'new' 

752 new.b = textwrap.dedent("""\ 

753 at-others 

754 node 1 line 1 

755 node 2 line 1 

756 node 2 line 2 

757 """).replace('at-', '@') 

758 # Run the test. 

759 results, expected = self.make_lines(old, new) 

760 self.assertEqual(results, expected) 

761 #@+node:ekr.20210908140242.3: *4* TestShadow.test_verbatim_sentinels_delete_verbatim_line_at_start_of_node 

762 def test_verbatim_sentinels_delete_verbatim_line_at_start_of_node(self): 

763 p = self.c.p 

764 # Create the 'old' node. 

765 old = p.insertAsLastChild() 

766 old.h = 'old' 

767 old.b = textwrap.dedent("""\ 

768 at-others 

769 node 1 line 1 

770 at-verbatim 

771 # at- should be handled by verbatim 

772 node 2 line 2 

773 """).replace('at-', '@') 

774 # Create the 'new' node. 

775 new = p.insertAsLastChild() 

776 new.h = 'new' 

777 new.b = textwrap.dedent("""\ 

778 at-others 

779 node 1 line 1 

780 node 2 line 2 

781 """).replace('at-', '@') 

782 # Run the test. 

783 results, expected = self.make_lines(old, new) 

784 self.assertEqual(results, expected) 

785 #@+node:ekr.20210908140242.4: *4* TestShadow.test_verbatim_sentinels_no_change 

786 def test_verbatim_sentinels_no_change(self): 

787 p = self.c.p 

788 # Create the 'old' node. 

789 old = p.insertAsLastChild() 

790 old.h = 'old' 

791 old.b = textwrap.dedent("""\ 

792 at-others 

793 node 1 line 1 

794 at-verbatim 

795 #at- should be handled by verbatim 

796 line 1 line 3 

797 node 2 line 1 

798 node 2 line 2 

799 node 2 line 3 

800 """).replace('at-', '@') 

801 # Create the 'new' node. 

802 new = p.insertAsLastChild() 

803 new.h = 'new' 

804 new.b = textwrap.dedent("""\ 

805 at-others 

806 node 1 line 1 

807 at-verbatim 

808 #at- should be handled by verbatim 

809 line 1 line 3 

810 node 2 line 1 

811 node 2 line 2 

812 node 2 line 3 

813 """).replace('at-', '@') 

814 # Run the test. 

815 results, expected = self.make_lines(old, new) 

816 self.assertEqual(results, expected) 

817 #@+node:ekr.20210908160020.1: *3* test utils... 

818 #@+node:ekr.20210902210552.2: *4* TestShadow.test_marker_getDelims 

819 def test_marker_getDelims(self): 

820 c = self.c 

821 x = c.shadowController 

822 table = ( 

823 ('python', '#', ''), 

824 ('c', '//', ''), 

825 ('html', '<!--', '-->'), 

826 ('xxxx', '#--unknown-language--', ''), 

827 ) 

828 for language, delim1, delim2 in table: 

829 delims = g.set_delims_from_language(language) 

830 marker = x.Marker(delims) 

831 result = marker.getDelims() 

832 expected = delim1, delim2 

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

834 #@+node:ekr.20210902210552.3: *4* TestShadow.test_marker_isSentinel 

835 def test_marker_isSentinel(self): 

836 c = self.c 

837 x = c.shadowController 

838 table = ( 

839 ('python', 'abc', False), 

840 ('python', '#abc', False), 

841 ('python', '#@abc', True), 

842 ('python', '@abc#', False), 

843 ('c', 'abc', False), 

844 ('c', '//@', True), 

845 ('c', '// @abc', False), 

846 ('c', '/*@ abc */', True), 

847 ('c', '/*@ abc', False), 

848 ('html', '#@abc', False), 

849 ('html', '<!--abc-->', False), 

850 ('html', '<!--@ abc -->', True), 

851 ('html', '<!--@ abc ->', False), 

852 ('xxxx', '#--unknown-language--@', True) 

853 ) 

854 for language, s, expected in table: 

855 delims = g.set_delims_from_language(language) 

856 marker = x.Marker(delims) 

857 result = marker.isSentinel(s) 

858 self.assertEqual(result, expected) 

859 #@+node:ekr.20210902210552.4: *4* TestShadow.test_marker_isVerbatimSentinel 

860 def test_marker_isVerbatimSentinel(self): 

861 c = self.c 

862 x = c.shadowController 

863 table = ( 

864 ('python', 'abc', False), 

865 ('python', '#abc', False), 

866 ('python', '#verbatim', False), 

867 ('python', '#@verbatim', True), 

868 ('c', 'abc', False), 

869 ('c', '//@', False), 

870 ('c', '//@verbatim', True), 

871 ('html', '#@abc', False), 

872 ('html', '<!--abc-->', False), 

873 ('html', '<!--@verbatim -->', True), 

874 ('xxxx', '#--unknown-language--@verbatim', True) 

875 ) 

876 for language, s, expected in table: 

877 delims = g.set_delims_from_language(language) 

878 marker = x.Marker(delims) 

879 result = marker.isVerbatimSentinel(s) 

880 self.assertEqual(result, expected) 

881 #@+node:ekr.20210902210552.5: *4* TestShadow.test_x_baseDirName 

882 def test_x_baseDirName(self): 

883 c = self.c 

884 x = c.shadowController 

885 path = x.baseDirName() 

886 expected = g.os_path_dirname(g.os_path_abspath(g.os_path_join(c.fileName()))) 

887 self.assertEqual(path, expected) 

888 #@+node:ekr.20210902210552.6: *4* TestShadow.test_x_dirName 

889 def test_x_dirName(self): 

890 c = self.c 

891 x = c.shadowController 

892 filename = 'xyzzy' 

893 path = x.dirName(filename) 

894 expected = g.os_path_dirname(g.os_path_abspath( 

895 g.os_path_join(g.os_path_dirname(c.fileName()), filename))) 

896 self.assertEqual(path, expected) 

897 #@+node:ekr.20210902210552.7: *4* TestShadow.test_x_findAtLeoLine 

898 def test_x_findAtLeoLine(self): 

899 c = self.c 

900 x = c.shadowController 

901 table = ( 

902 ('c', ('//@+leo', 'a'), '//@+leo'), 

903 ('c', ('//@first', '//@+leo', 'b'), '//@+leo'), 

904 ('c', ('/*@+leo*/', 'a'), '/*@+leo*/'), 

905 ('c', ('/*@first*/', '/*@+leo*/', 'b'), '/*@+leo*/'), 

906 ('python', ('#@+leo', 'a'), '#@+leo'), 

907 ('python', ('#@first', '#@+leo', 'b'), '#@+leo'), 

908 ('error', ('',), ''), 

909 ('html', ('<!--@+leo-->', 'a'), '<!--@+leo-->'), 

910 ('html', ('<!--@first-->', '<!--@+leo-->', 'b'), '<!--@+leo-->'), 

911 ) 

912 for language, lines, expected in table: 

913 result = x.findLeoLine(lines) 

914 self.assertEqual(expected, result) 

915 #@+node:ekr.20210902210552.8: *4* TestShadow.test_x_makeShadowDirectory 

916 def test_x_makeShadowDirectory(self): 

917 c = self.c 

918 x = c.shadowController 

919 shadow_fn = x.shadowPathName('unittests/xyzzy/test.py') 

920 shadow_dir = x.shadowDirName('unittests/xyzzy/test.py') 

921 assert not os.path.exists(shadow_fn), shadow_fn 

922 self.deleteShadowDir(shadow_dir) 

923 x.makeShadowDirectory(shadow_dir) 

924 self.assertTrue(os.path.exists(shadow_dir)) 

925 self.deleteShadowDir(shadow_dir) 

926 #@+node:ekr.20210902210552.9: *4* TestShadow.test_x_markerFromFileLines 

927 def test_x_markerFromFileLines(self): 

928 c = self.c 

929 x = c.shadowController 

930 # Add -ver=4 so at.parseLeoSentinel does not complain. 

931 table = ( 

932 ('c', ('//@+leo-ver=4', 'a'), '//', ''), 

933 ('c', ('//@first', '//@+leo-ver=4', 'b'), '//', ''), 

934 ('c', ('/*@+leo-ver=4*/', 'a'), '/*', '*/'), 

935 ('c', ('/*@first*/', '/*@+leo-ver=4*/', 'b'), '/*', '*/'), 

936 ('python', ('#@+leo-ver=4', 'a'), '#', ''), 

937 ('python', ('#@first', '#@+leo-ver=4', 'b'), '#', ''), 

938 ('error', ('',), '#--unknown-language--', ''), 

939 ('html', ('<!--@+leo-ver=4-->', 'a'), '<!--', '-->'), 

940 ('html', ('<!--@first-->', '<!--@+leo-ver=4-->', 'b'), '<!--', '-->'), 

941 ) 

942 

943 for language, lines, delim1, delim2 in table: 

944 lines_s = '\n'.join(lines) 

945 marker = x.markerFromFileLines(lines, 'test-file-name') 

946 result1, result2 = marker.getDelims() 

947 self.assertEqual(delim1, result1, msg=f"language: {language} {lines_s}") 

948 self.assertEqual(delim2, result2, msg=f"language: {language} {lines_s}") 

949 #@+node:ekr.20210902210552.10: *4* TestShadow.test_x_markerFromFileName 

950 def test_x_markerFromFileName(self): 

951 c = self.c 

952 x = c.shadowController 

953 table = ( 

954 ('ini', ';', '',), 

955 ('c', '//', ''), 

956 ('h', '//', ''), 

957 ('py', '#', ''), 

958 ('xyzzy', '#--unknown-language--', ''), 

959 ) 

960 for ext, delim1, delim2 in table: 

961 filename = 'x.%s' % ext 

962 marker = x.markerFromFileName(filename) 

963 result1, result2 = marker.getDelims() 

964 self.assertEqual(delim1, result1) 

965 self.assertEqual(delim2, result2) 

966 #@+node:ekr.20210902210552.11: *4* TestShadow.test_x_pathName 

967 def test_x_pathName(self): 

968 c = self.c 

969 x = c.shadowController 

970 filename = 'xyzzy' 

971 path = x.pathName(filename) 

972 expected = g.os_path_abspath(g.os_path_join(x.baseDirName(), filename)) 

973 self.assertEqual(path, expected) 

974 #@+node:ekr.20210902210552.13: *4* TestShadow.test_x_replaceFileWithString_2 

975 def test_x_replaceFileWithString_2(self): 

976 c = self.c 

977 x = c.shadowController 

978 encoding = 'utf-8' 

979 fn = 'does/not/exist' 

980 assert not g.os_path_exists(fn) 

981 assert not x.replaceFileWithString(encoding, fn, 'abc') 

982 #@+node:ekr.20210902210552.14: *4* TestShadow.test_x_shadowDirName 

983 def test_x_shadowDirName(self): 

984 c = self.c 

985 x = c.shadowController 

986 subdir = c.config.getString('shadow_subdir') or '.leo_shadow' 

987 filename = 'xyzzy' 

988 path = x.shadowDirName(filename) 

989 expected = g.os_path_abspath(g.os_path_join( 

990 g.os_path_dirname(c.fileName()), subdir)) 

991 self.assertEqual(path, expected) 

992 #@+node:ekr.20210902210552.15: *4* TestShadow.test_x_shadowPathName 

993 def test_x_shadowPathName(self): 

994 c = self.c 

995 x = c.shadowController 

996 subdir = c.config.getString('shadow_subdir') or '.leo_shadow' 

997 prefix = c.config.getString('shadow_prefix') or '' 

998 filename = 'xyzzy' 

999 path = x.shadowPathName(filename) 

1000 expected = g.os_path_abspath(g.os_path_join( 

1001 g.os_path_dirname(c.fileName()), subdir, prefix + filename)) 

1002 self.assertEqual(path, expected) 

1003 #@-others 

1004#@-others 

1005#@-leo