#
source:
trunk/sandbox/my-parser/parser2_test.py
@
37

Revision 37, 15.1 KB checked in by xi, 10 years ago (diff) |
---|

Line | |
---|---|

1 | |

2 | import unittest |

3 | import parser2 |

4 | |

5 | EX1 = r""" |

6 | - Mark McGwire |

7 | - Sammy Sosa |

8 | - Ken Griffey |

9 | """ |

10 | |

11 | TOKENS1 = """ |

12 | BLOCK_SEQ_START |

13 | ENTRY SCALAR |

14 | ENTRY SCALAR |

15 | ENTRY SCALAR |

16 | BLOCK_END |

17 | """ |

18 | |

19 | NODES1 = [True, True, True] |

20 | |

21 | EX2 = r""" |

22 | hr: 65 # Home runs |

23 | avg: 0.278 # Batting average |

24 | rbi: 147 # Runs Batted In |

25 | """ |

26 | |

27 | TOKENS2 = """ |

28 | BLOCK_MAP_START |

29 | KEY SCALAR VALUE SCALAR |

30 | KEY SCALAR VALUE SCALAR |

31 | KEY SCALAR VALUE SCALAR |

32 | BLOCK_END |

33 | """ |

34 | |

35 | NODES2 = [(True, True), (True, True), (True, True)] |

36 | |

37 | EX3 = r""" |

38 | american: |

39 | - Boston Red Sox |

40 | - Detroit Tigers |

41 | - New York Yankees |

42 | national: |

43 | - New York Mets |

44 | - Chicago Cubs |

45 | - Atlanta Braves |

46 | """ |

47 | |

48 | TOKENS3 = """ |

49 | BLOCK_MAP_START |

50 | KEY SCALAR VALUE |

51 | BLOCK_SEQ_START |

52 | ENTRY SCALAR |

53 | ENTRY SCALAR |

54 | ENTRY SCALAR |

55 | BLOCK_END |

56 | KEY SCALAR VALUE |

57 | BLOCK_SEQ_START |

58 | ENTRY SCALAR |

59 | ENTRY SCALAR |

60 | ENTRY SCALAR |

61 | BLOCK_END |

62 | BLOCK_END |

63 | """ |

64 | |

65 | NODES3 = [(True, [True, True, True]), (True, [True, True, True])] |

66 | |

67 | EX4 = r""" |

68 | - |

69 | name: Mark McGwire |

70 | hr: 65 |

71 | avg: 0.278 |

72 | - |

73 | name: Sammy Sosa |

74 | hr: 63 |

75 | avg: 0.288 |

76 | """ |

77 | |

78 | TOKENS4 = """ |

79 | BLOCK_SEQ_START |

80 | ENTRY |

81 | BLOCK_MAP_START |

82 | KEY SCALAR VALUE SCALAR |

83 | KEY SCALAR VALUE SCALAR |

84 | KEY SCALAR VALUE SCALAR |

85 | BLOCK_END |

86 | ENTRY |

87 | BLOCK_MAP_START |

88 | KEY SCALAR VALUE SCALAR |

89 | KEY SCALAR VALUE SCALAR |

90 | KEY SCALAR VALUE SCALAR |

91 | BLOCK_END |

92 | BLOCK_END |

93 | """ |

94 | |

95 | NODES4 = [[(True, True), (True, True), (True, True)], [(True, True), (True, True), (True, True)]] |

96 | |

97 | EX5 = r""" |

98 | - [name , hr, avg ] |

99 | - [Mark McGwire, 65, 0.278] |

100 | - [Sammy Sosa , 63, 0.288] |

101 | """ |

102 | |

103 | TOKENS5 = """ |

104 | BLOCK_SEQ_START |

105 | ENTRY FLOW_SEQ_START SCALAR ENTRY SCALAR ENTRY SCALAR FLOW_SEQ_END |

106 | ENTRY FLOW_SEQ_START SCALAR ENTRY SCALAR ENTRY SCALAR FLOW_SEQ_END |

107 | ENTRY FLOW_SEQ_START SCALAR ENTRY SCALAR ENTRY SCALAR FLOW_SEQ_END |

108 | BLOCK_END |

109 | """ |

110 | |

111 | NODES5 = [[True, True, True], [True, True, True], [True, True, True]] |

112 | |

113 | EX6 = r""" |

114 | Mark McGwire: {hr: 65, avg: 0.278} |

115 | Sammy Sosa: { |

116 | hr: 63, |

117 | avg: 0.288 |

118 | } |

119 | """ |

120 | |

121 | TOKENS6 = """ |

122 | BLOCK_MAP_START |

123 | KEY SCALAR VALUE |

124 | FLOW_MAP_START KEY SCALAR VALUE SCALAR ENTRY KEY SCALAR VALUE SCALAR FLOW_MAP_END |

125 | KEY SCALAR VALUE |

126 | FLOW_MAP_START KEY SCALAR VALUE SCALAR ENTRY KEY SCALAR VALUE SCALAR FLOW_MAP_END |

127 | BLOCK_END |

128 | """ |

129 | |

130 | NODES6 = [(True, [(True, True), (True, True)]), (True, [(True, True), (True, True)])] |

131 | |

132 | EX7 = r""" |

133 | # Ranking of 1998 home runs |

134 | --- |

135 | - Mark McGwire |

136 | - Sammy Sosa |

137 | - Ken Griffey |

138 | |

139 | # Team ranking |

140 | --- |

141 | - Chicago Cubs |

142 | - St Louis Cardinals |

143 | """ |

144 | |

145 | TOKENS7 = """ |

146 | DOCUMENT_START |

147 | BLOCK_SEQ_START |

148 | ENTRY SCALAR |

149 | ENTRY SCALAR |

150 | ENTRY SCALAR |

151 | BLOCK_END |

152 | |

153 | DOCUMENT_START |

154 | BLOCK_SEQ_START |

155 | ENTRY SCALAR |

156 | ENTRY SCALAR |

157 | BLOCK_END |

158 | """ |

159 | |

160 | NODES7 = ([True, True, True], [True, True]) |

161 | |

162 | EX8 = r""" |

163 | --- |

164 | time: 20:03:20 |

165 | player: Sammy Sosa |

166 | action: strike (miss) |

167 | ... |

168 | --- |

169 | time: 20:03:47 |

170 | player: Sammy Sosa |

171 | action: grand slam |

172 | ... |

173 | """ |

174 | |

175 | TOKENS8 = """ |

176 | DOCUMENT_START |

177 | BLOCK_MAP_START |

178 | KEY SCALAR VALUE SCALAR |

179 | KEY SCALAR VALUE SCALAR |

180 | KEY SCALAR VALUE SCALAR |

181 | BLOCK_END |

182 | DOCUMENT_END |

183 | |

184 | DOCUMENT_START |

185 | BLOCK_MAP_START |

186 | KEY SCALAR VALUE SCALAR |

187 | KEY SCALAR VALUE SCALAR |

188 | KEY SCALAR VALUE SCALAR |

189 | BLOCK_END |

190 | DOCUMENT_END |

191 | """ |

192 | |

193 | NODES8 = ([(True, True), (True, True), (True, True)], [(True, True), (True, True), (True, True)]) |

194 | |

195 | EX9 = r""" |

196 | --- |

197 | hr: # 1998 hr ranking |

198 | - Mark McGwire |

199 | - Sammy Sosa |

200 | rbi: |

201 | # 1998 rbi ranking |

202 | - Sammy Sosa |

203 | - Ken Griffey |

204 | """ |

205 | |

206 | TOKENS9 = """ |

207 | DOCUMENT_START |

208 | BLOCK_MAP_START |

209 | KEY SCALAR VALUE |

210 | BLOCK_SEQ_START |

211 | ENTRY SCALAR |

212 | ENTRY SCALAR |

213 | BLOCK_END |

214 | KEY SCALAR VALUE |

215 | BLOCK_SEQ_START |

216 | ENTRY SCALAR |

217 | ENTRY SCALAR |

218 | BLOCK_END |

219 | BLOCK_END |

220 | """ |

221 | |

222 | NODES9 = [(True, [True, True]), (True, [True, True])] |

223 | |

224 | EX10 = r""" |

225 | --- |

226 | hr: |

227 | - Mark McGwire |

228 | # Following node labeled SS |

229 | - &SS Sammy Sosa |

230 | rbi: |

231 | - *SS # Subsequent occurrence |

232 | - Ken Griffey |

233 | """ |

234 | |

235 | TOKENS10 = """ |

236 | DOCUMENT_START |

237 | BLOCK_MAP_START |

238 | KEY SCALAR VALUE |

239 | BLOCK_SEQ_START |

240 | ENTRY SCALAR |

241 | ENTRY ANCHOR SCALAR |

242 | BLOCK_END |

243 | KEY SCALAR VALUE |

244 | BLOCK_SEQ_START |

245 | ENTRY ALIAS |

246 | ENTRY SCALAR |

247 | BLOCK_END |

248 | BLOCK_END |

249 | """ |

250 | |

251 | NODES10 = [(True, [True, True]), (True, ['*', True])] |

252 | |

253 | EX11 = r""" |

254 | ? - Detroit Tigers |

255 | - Chicago cubs |

256 | : |

257 | - 2001-07-23 |

258 | |

259 | ? [ New York Yankees, |

260 | Atlanta Braves ] |

261 | : [ 2001-07-02, 2001-08-12, |

262 | 2001-08-14 ] |

263 | """ |

264 | |

265 | TOKENS11 = """ |

266 | BLOCK_MAP_START |

267 | KEY |

268 | BLOCK_SEQ_START |

269 | ENTRY SCALAR |

270 | ENTRY SCALAR |

271 | BLOCK_END |

272 | VALUE |

273 | BLOCK_SEQ_START |

274 | ENTRY SCALAR |

275 | BLOCK_END |

276 | KEY |

277 | FLOW_SEQ_START SCALAR ENTRY SCALAR FLOW_SEQ_END |

278 | VALUE |

279 | FLOW_SEQ_START SCALAR ENTRY SCALAR ENTRY SCALAR FLOW_SEQ_END |

280 | BLOCK_END |

281 | """ |

282 | |

283 | NODES11 = [([True, True], [True]), ([True, True], [True, True, True])] |

284 | |

285 | EX12 = r""" |

286 | --- |

287 | # products purchased |

288 | - item : Super Hoop |

289 | quantity: 1 |

290 | - item : Basketball |

291 | quantity: 4 |

292 | - item : Big Shoes |

293 | quantity: 1 |

294 | """ |

295 | |

296 | TOKENS12 = """ |

297 | DOCUMENT_START |

298 | BLOCK_SEQ_START |

299 | ENTRY |

300 | BLOCK_MAP_START |

301 | KEY SCALAR VALUE SCALAR |

302 | KEY SCALAR VALUE SCALAR |

303 | BLOCK_END |

304 | ENTRY |

305 | BLOCK_MAP_START |

306 | KEY SCALAR VALUE SCALAR |

307 | KEY SCALAR VALUE SCALAR |

308 | BLOCK_END |

309 | ENTRY |

310 | BLOCK_MAP_START |

311 | KEY SCALAR VALUE SCALAR |

312 | KEY SCALAR VALUE SCALAR |

313 | BLOCK_END |

314 | BLOCK_END |

315 | """ |

316 | |

317 | NODES12 = [[(True, True), (True, True)], [(True, True), (True, True)], [(True, True), (True, True)]] |

318 | |

319 | EX13 = r""" |

320 | # ASCII Art |

321 | --- | |

322 | \//||\/|| |

323 | // || ||__ |

324 | """ |

325 | |

326 | TOKENS13 = """ |

327 | DOCUMENT_START SCALAR |

328 | """ |

329 | |

330 | NODES13 = True |

331 | |

332 | EX14 = r""" |

333 | --- |

334 | Mark McGwire's |

335 | year was crippled |

336 | by a knee injury. |

337 | """ |

338 | |

339 | TOKENS14 = """ |

340 | DOCUMENT_START SCALAR |

341 | """ |

342 | |

343 | NODES14 = True |

344 | |

345 | EX15 = r""" |

346 | > |

347 | Sammy Sosa completed another |

348 | fine season with great stats. |

349 | |

350 | 63 Home Runs |

351 | 0.288 Batting Average |

352 | |

353 | What a year! |

354 | """ |

355 | |

356 | TOKENS15 = """ |

357 | SCALAR |

358 | """ |

359 | |

360 | NODES15 = True |

361 | |

362 | EX16 = r""" |

363 | name: Mark McGwire |

364 | accomplishment: > |

365 | Mark set a major league |

366 | home run record in 1998. |

367 | stats: | |

368 | 65 Home Runs |

369 | 0.278 Batting Average |

370 | """ |

371 | |

372 | TOKENS16 = """ |

373 | BLOCK_MAP_START |

374 | KEY SCALAR VALUE SCALAR |

375 | KEY SCALAR VALUE SCALAR |

376 | KEY SCALAR VALUE SCALAR |

377 | BLOCK_END |

378 | """ |

379 | |

380 | NODES16 = [(True, True), (True, True), (True, True)] |

381 | |

382 | EX17 = r""" |

383 | unicode: "Sosa did fine.\u263A" |

384 | control: "\b1998\t1999\t2000\n" |

385 | hexesc: "\x13\x10 is \r\n" |

386 | |

387 | single: '"Howdy!" he cried.' |

388 | quoted: ' # not a ''comment''.' |

389 | tie-fighter: '|\-*-/|' |

390 | """ |

391 | |

392 | TOKENS17 = """ |

393 | BLOCK_MAP_START |

394 | KEY SCALAR VALUE SCALAR |

395 | KEY SCALAR VALUE SCALAR |

396 | KEY SCALAR VALUE SCALAR |

397 | KEY SCALAR VALUE SCALAR |

398 | KEY SCALAR VALUE SCALAR |

399 | KEY SCALAR VALUE SCALAR |

400 | BLOCK_END |

401 | """ |

402 | |

403 | NODES17 = [(True, True), (True, True), (True, True), (True, True), (True, True), (True, True)] |

404 | |

405 | EX18 = r""" |

406 | plain: |

407 | This unquoted scalar |

408 | spans many lines. |

409 | |

410 | quoted: "So does this |

411 | quoted scalar.\n" |

412 | """ |

413 | |

414 | TOKENS18 = """ |

415 | BLOCK_MAP_START |

416 | KEY SCALAR VALUE SCALAR |

417 | KEY SCALAR VALUE SCALAR |

418 | BLOCK_END |

419 | """ |

420 | |

421 | NODES18 = [(True, True), (True, True)] |

422 | |

423 | EX19 = r""" |

424 | canonical: 12345 |

425 | decimal: +12,345 |

426 | sexagesimal: 3:25:45 |

427 | octal: 014 |

428 | hexadecimal: 0xC |

429 | """ |

430 | |

431 | TOKENS19 = """ |

432 | BLOCK_MAP_START |

433 | KEY SCALAR VALUE SCALAR |

434 | KEY SCALAR VALUE SCALAR |

435 | KEY SCALAR VALUE SCALAR |

436 | KEY SCALAR VALUE SCALAR |

437 | KEY SCALAR VALUE SCALAR |

438 | BLOCK_END |

439 | """ |

440 | |

441 | NODES19 = [(True, True), (True, True), (True, True), (True, True), (True, True)] |

442 | |

443 | EX20 = r""" |

444 | canonical: 1.23015e+3 |

445 | exponential: 12.3015e+02 |

446 | sexagesimal: 20:30.15 |

447 | fixed: 1,230.15 |

448 | negative infinity: -.inf |

449 | not a number: .NaN |

450 | """ |

451 | |

452 | TOKENS20 = """ |

453 | BLOCK_MAP_START |

454 | KEY SCALAR VALUE SCALAR |

455 | KEY SCALAR VALUE SCALAR |

456 | KEY SCALAR VALUE SCALAR |

457 | KEY SCALAR VALUE SCALAR |

458 | KEY SCALAR VALUE SCALAR |

459 | KEY SCALAR VALUE SCALAR |

460 | BLOCK_END |

461 | """ |

462 | |

463 | NODES20 = [(True, True), (True, True), (True, True), (True, True), (True, True), (True, True)] |

464 | |

465 | EX21 = r""" |

466 | null: ~ |

467 | true: y |

468 | false: n |

469 | string: '12345' |

470 | """ |

471 | |

472 | TOKENS21 = """ |

473 | BLOCK_MAP_START |

474 | KEY SCALAR VALUE SCALAR |

475 | KEY SCALAR VALUE SCALAR |

476 | KEY SCALAR VALUE SCALAR |

477 | KEY SCALAR VALUE SCALAR |

478 | BLOCK_END |

479 | """ |

480 | |

481 | NODES21 = [(True, True), (True, True), (True, True), (True, True)] |

482 | |

483 | EX22 = r""" |

484 | canonical: 2001-12-15T02:59:43.1Z |

485 | iso8601: 2001-12-14t21:59:43.10-05:00 |

486 | spaced: 2001-12-14 21:59:43.10 -5 |

487 | date: 2002-12-14 |

488 | """ |

489 | |

490 | TOKENS22 = """ |

491 | BLOCK_MAP_START |

492 | KEY SCALAR VALUE SCALAR |

493 | KEY SCALAR VALUE SCALAR |

494 | KEY SCALAR VALUE SCALAR |

495 | KEY SCALAR VALUE SCALAR |

496 | BLOCK_END |

497 | """ |

498 | |

499 | NODES22 = [(True, True), (True, True), (True, True), (True, True)] |

500 | |

501 | EX23 = r""" |

502 | --- |

503 | not-date: !!str 2002-04-28 |

504 | |

505 | picture: !!binary | |

506 | R0lGODlhDAAMAIQAAP//9/X |

507 | 17unp5WZmZgAAAOfn515eXv |

508 | Pz7Y6OjuDg4J+fn5OTk6enp |

509 | 56enmleECcgggoBADs= |

510 | |

511 | application specific tag: !something | |

512 | The semantics of the tag |

513 | above may be different for |

514 | different documents. |

515 | """ |

516 | |

517 | TOKENS23 = """ |

518 | DOCUMENT_START |

519 | BLOCK_MAP_START |

520 | KEY SCALAR VALUE TAG SCALAR |

521 | KEY SCALAR VALUE TAG SCALAR |

522 | KEY SCALAR VALUE TAG SCALAR |

523 | BLOCK_END |

524 | """ |

525 | |

526 | NODES23 = [(True, True), (True, True), (True, True)] |

527 | |

528 | EX24 = r""" |

529 | %TAG ! tag:clarkevans.com,2002: |

530 | --- !shape |

531 | # Use the ! handle for presenting |

532 | # tag:clarkevans.com,2002:circle |

533 | - !circle |

534 | center: &ORIGIN {x: 73, y: 129} |

535 | radius: 7 |

536 | - !line |

537 | start: *ORIGIN |

538 | finish: { x: 89, y: 102 } |

539 | - !label |

540 | start: *ORIGIN |

541 | color: 0xFFEEBB |

542 | text: Pretty vector drawing. |

543 | """ |

544 | |

545 | TOKENS24 = """ |

546 | DIRECTIVE |

547 | DOCUMENT_START TAG |

548 | BLOCK_SEQ_START |

549 | ENTRY TAG |

550 | BLOCK_MAP_START |

551 | KEY SCALAR VALUE ANCHOR |

552 | FLOW_MAP_START KEY SCALAR VALUE SCALAR ENTRY KEY SCALAR VALUE SCALAR FLOW_MAP_END |

553 | KEY SCALAR VALUE SCALAR |

554 | BLOCK_END |

555 | ENTRY TAG |

556 | BLOCK_MAP_START |

557 | KEY SCALAR VALUE ALIAS |

558 | KEY SCALAR VALUE |

559 | FLOW_MAP_START KEY SCALAR VALUE SCALAR ENTRY KEY SCALAR VALUE SCALAR FLOW_MAP_END |

560 | BLOCK_END |

561 | ENTRY TAG |

562 | BLOCK_MAP_START |

563 | KEY SCALAR VALUE ALIAS |

564 | KEY SCALAR VALUE SCALAR |

565 | KEY SCALAR VALUE SCALAR |

566 | BLOCK_END |

567 | BLOCK_END |

568 | """ |

569 | |

570 | NODES24 = [[(True, [(True, True), (True, True)]), (True, True)], |

571 | [(True, '*'), (True, [(True, True), (True, True)])], |

572 | [(True, '*'), (True, True), (True, True)]] |

573 | |

574 | EX25 = r""" |

575 | # sets are represented as a |

576 | # mapping where each key is |

577 | # associated with the empty string |

578 | --- !!set |

579 | ? Mark McGwire |

580 | ? Sammy Sosa |

581 | ? Ken Griff |

582 | """ |

583 | |

584 | TOKENS25 = """ |

585 | DOCUMENT_START TAG |

586 | BLOCK_MAP_START |

587 | KEY SCALAR |

588 | KEY SCALAR |

589 | KEY SCALAR |

590 | BLOCK_END |

591 | """ |

592 | |

593 | NODES25 = [(True, None), (True, None), (True, None)] |

594 | |

595 | EX26 = r""" |

596 | # ordered maps are represented as |

597 | # a sequence of mappings, with |

598 | # each mapping having one key |

599 | --- !!omap |

600 | - Mark McGwire: 65 |

601 | - Sammy Sosa: 63 |

602 | - Ken Griffy: 58 |

603 | """ |

604 | |

605 | TOKENS26 = """ |

606 | DOCUMENT_START TAG |

607 | BLOCK_SEQ_START |

608 | ENTRY |

609 | BLOCK_MAP_START |

610 | KEY SCALAR VALUE SCALAR |

611 | BLOCK_END |

612 | ENTRY |

613 | BLOCK_MAP_START |

614 | KEY SCALAR VALUE SCALAR |

615 | BLOCK_END |

616 | ENTRY |

617 | BLOCK_MAP_START |

618 | KEY SCALAR VALUE SCALAR |

619 | BLOCK_END |

620 | BLOCK_END |

621 | """ |

622 | |

623 | NODES26 = [[(True, True)], [(True, True)], [(True, True)]] |

624 | |

625 | EX27 = r""" |

626 | --- !<tag:clarkevans.com,2002:invoice> |

627 | invoice: 34843 |

628 | date : 2001-01-23 |

629 | bill-to: &id001 |

630 | given : Chris |

631 | family : Dumars |

632 | address: |

633 | lines: | |

634 | 458 Walkman Dr. |

635 | Suite #292 |

636 | city : Royal Oak |

637 | state : MI |

638 | postal : 48046 |

639 | ship-to: *id001 |

640 | product: |

641 | - sku : BL394D |

642 | quantity : 4 |

643 | description : Basketball |

644 | price : 450.00 |

645 | - sku : BL4438H |

646 | quantity : 1 |

647 | description : Super Hoop |

648 | price : 2392.00 |

649 | tax : 251.42 |

650 | total: 4443.52 |

651 | comments: |

652 | Late afternoon is best. |

653 | Backup contact is Nancy |

654 | Billsmer @ 338-4338. |

655 | """ |

656 | |

657 | TOKENS27 = """ |

658 | DOCUMENT_START TAG |

659 | BLOCK_MAP_START |

660 | KEY SCALAR VALUE SCALAR |

661 | KEY SCALAR VALUE SCALAR |

662 | KEY SCALAR VALUE ANCHOR |

663 | BLOCK_MAP_START |

664 | KEY SCALAR VALUE SCALAR |

665 | KEY SCALAR VALUE SCALAR |

666 | KEY SCALAR VALUE |

667 | BLOCK_MAP_START |

668 | KEY SCALAR VALUE SCALAR |

669 | KEY SCALAR VALUE SCALAR |

670 | KEY SCALAR VALUE SCALAR |

671 | KEY SCALAR VALUE SCALAR |

672 | BLOCK_END |

673 | BLOCK_END |

674 | KEY SCALAR VALUE ALIAS |

675 | KEY SCALAR VALUE |

676 | BLOCK_SEQ_START |

677 | ENTRY |

678 | BLOCK_MAP_START |

679 | KEY SCALAR VALUE SCALAR |

680 | KEY SCALAR VALUE SCALAR |

681 | KEY SCALAR VALUE SCALAR |

682 | KEY SCALAR VALUE SCALAR |

683 | BLOCK_END |

684 | ENTRY |

685 | BLOCK_MAP_START |

686 | KEY SCALAR VALUE SCALAR |

687 | KEY SCALAR VALUE SCALAR |

688 | KEY SCALAR VALUE SCALAR |

689 | KEY SCALAR VALUE SCALAR |

690 | BLOCK_END |

691 | BLOCK_END |

692 | KEY SCALAR VALUE SCALAR |

693 | KEY SCALAR VALUE SCALAR |

694 | KEY SCALAR VALUE SCALAR |

695 | BLOCK_END |

696 | """ |

697 | |

698 | NODES27 = [ |

699 | (True, True), (True, True), (True, [(True, True), (True, True), (True, [(True, True), (True, True), (True, True), (True, True)])]), (True, '*'), |

700 | (True, [[(True, True), (True, True), (True, True), (True, True)], [(True, True), (True, True), (True, True), (True, True)]]), (True, True), (True, True), (True, True), |

701 | ] |

702 | |

703 | EX28 = r""" |

704 | --- |

705 | Time: 2001-11-23 15:01:42 -5 |

706 | User: ed |

707 | Warning: |

708 | This is an error message |

709 | for the log file |

710 | --- |

711 | Time: 2001-11-23 15:02:31 -5 |

712 | User: ed |

713 | Warning: |

714 | A slightly different error |

715 | message. |

716 | --- |

717 | Date: 2001-11-23 15:03:17 -5 |

718 | User: ed |

719 | Fatal: |

720 | Unknown variable "bar" |

721 | Stack: |

722 | - file: TopClass.py |

723 | line: 23 |

724 | code: | |

725 | x = MoreObject("345\n") |

726 | - file: MoreClass.py |

727 | line: 58 |

728 | code: |- |

729 | foo = bar |

730 | """ |

731 | |

732 | TOKENS28 = """ |

733 | DOCUMENT_START |

734 | BLOCK_MAP_START |

735 | KEY SCALAR VALUE SCALAR |

736 | KEY SCALAR VALUE SCALAR |

737 | KEY SCALAR VALUE SCALAR |

738 | BLOCK_END |

739 | |

740 | DOCUMENT_START |

741 | BLOCK_MAP_START |

742 | KEY SCALAR VALUE SCALAR |

743 | KEY SCALAR VALUE SCALAR |

744 | KEY SCALAR VALUE SCALAR |

745 | BLOCK_END |

746 | |

747 | DOCUMENT_START |

748 | BLOCK_MAP_START |

749 | KEY SCALAR VALUE SCALAR |

750 | KEY SCALAR VALUE SCALAR |

751 | KEY SCALAR VALUE SCALAR |

752 | KEY SCALAR VALUE |

753 | BLOCK_SEQ_START |

754 | ENTRY |

755 | BLOCK_MAP_START |

756 | KEY SCALAR VALUE SCALAR |

757 | KEY SCALAR VALUE SCALAR |

758 | KEY SCALAR VALUE SCALAR |

759 | BLOCK_END |

760 | ENTRY |

761 | BLOCK_MAP_START |

762 | KEY SCALAR VALUE SCALAR |

763 | KEY SCALAR VALUE SCALAR |

764 | KEY SCALAR VALUE SCALAR |

765 | BLOCK_END |

766 | BLOCK_END |

767 | BLOCK_END |

768 | """ |

769 | |

770 | NODES28 = ( |

771 | [(True, True), (True, True), (True, True)], [(True, True), (True, True), (True, True)], |

772 | [(True, True), (True, True), (True, True), (True, [[(True, True), (True, True), (True, True)], [(True, True), (True, True), (True, True)]])], |

773 | ) |

774 | |

775 | MAX_TESTS = 100 |

776 | |

777 | class TestParser2(unittest.TestCase): |

778 | |

779 | def _testTokens(self, index, EX, TOKENS): |

780 | try: |

781 | tokens = None |

782 | scanner = parser2.Scanner() |

783 | tokens = scanner.scan('EX'+str(index), EX) |

784 | self.failUnlessEqual(tokens, TOKENS.split()) |

785 | except: |

786 | print "EXAMPLE #%s" % index |

787 | print "EX:" |

788 | print EX |

789 | print "TOKENS:" |

790 | print TOKENS |

791 | print "RESULT:", tokens |

792 | print "EXPECT:", TOKENS.split() |

793 | raise |

794 | |

795 | def _testNodes(self, index, EX, NODES): |

796 | try: |

797 | nodes = None |

798 | parser = parser2.Parser() |

799 | nodes = parser.parse('EX'+str(index), EX) |

800 | self.failUnlessEqual(nodes, NODES) |

801 | except: |

802 | print "EXAMPLE #%s" % index |

803 | print "EX:" |

804 | print EX |

805 | print "RESULT:", nodes |

806 | print "EXPECT:", NODES |

807 | raise |

808 | |

809 | @classmethod |

810 | def add_tests(cls, test_method_name, *tests): |

811 | for index in range(1, MAX_TESTS): |

812 | args = [] |

813 | for name in tests: |

814 | if name+str(index) in globals(): |

815 | args.append(globals()[name+str(index)]) |

816 | else: |

817 | break |

818 | else: |

819 | def test_method(self, index=index, args=args): |

820 | getattr(self, '_'+test_method_name)(index, *args) |

821 | test_method.__name__ = '%s%02d' % (test_method_name, index) |

822 | setattr(cls, test_method.__name__, test_method) |

823 | |

824 | TestParser2.add_tests('testTokens', 'EX', 'TOKENS') |

825 | TestParser2.add_tests('testNodes', 'EX', 'NODES') |

826 | |

827 | if __name__ == '__main__': |

828 | unittest.main() |

829 |

**Note:**See TracBrowser for help on using the repository browser.