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

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

Rev | Line | |
---|---|---|

[37] | 1 | |

2 | import unittest | |

3 | import parser2 | |

4 | ||

[40] | 5 | EX99 = r""" |

6 | key: | |

7 | value | |

8 | """ | |

9 | ||

10 | TOKENS99 = """ | |

11 | Should produce error. | |

12 | """ | |

13 | ||

[37] | 14 | EX1 = r""" |

15 | - Mark McGwire | |

16 | - Sammy Sosa | |

17 | - Ken Griffey | |

18 | """ | |

19 | ||

20 | TOKENS1 = """ | |

21 | BLOCK_SEQ_START | |

22 | ENTRY SCALAR | |

23 | ENTRY SCALAR | |

24 | ENTRY SCALAR | |

25 | BLOCK_END | |

26 | """ | |

27 | ||

28 | NODES1 = [True, True, True] | |

29 | ||

30 | EX2 = r""" | |

31 | hr: 65 # Home runs | |

32 | avg: 0.278 # Batting average | |

33 | rbi: 147 # Runs Batted In | |

34 | """ | |

35 | ||

36 | TOKENS2 = """ | |

37 | BLOCK_MAP_START | |

38 | KEY SCALAR VALUE SCALAR | |

39 | KEY SCALAR VALUE SCALAR | |

40 | KEY SCALAR VALUE SCALAR | |

41 | BLOCK_END | |

42 | """ | |

43 | ||

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

45 | ||

46 | EX3 = r""" | |

47 | american: | |

48 | - Boston Red Sox | |

49 | - Detroit Tigers | |

50 | - New York Yankees | |

51 | national: | |

52 | - New York Mets | |

53 | - Chicago Cubs | |

54 | - Atlanta Braves | |

55 | """ | |

56 | ||

57 | TOKENS3 = """ | |

58 | BLOCK_MAP_START | |

59 | KEY SCALAR VALUE | |

60 | BLOCK_SEQ_START | |

61 | ENTRY SCALAR | |

62 | ENTRY SCALAR | |

63 | ENTRY SCALAR | |

64 | BLOCK_END | |

65 | KEY SCALAR VALUE | |

66 | BLOCK_SEQ_START | |

67 | ENTRY SCALAR | |

68 | ENTRY SCALAR | |

69 | ENTRY SCALAR | |

70 | BLOCK_END | |

71 | BLOCK_END | |

72 | """ | |

73 | ||

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

75 | ||

76 | EX4 = r""" | |

77 | - | |

78 | name: Mark McGwire | |

79 | hr: 65 | |

80 | avg: 0.278 | |

81 | - | |

82 | name: Sammy Sosa | |

83 | hr: 63 | |

84 | avg: 0.288 | |

85 | """ | |

86 | ||

87 | TOKENS4 = """ | |

88 | BLOCK_SEQ_START | |

89 | ENTRY | |

90 | BLOCK_MAP_START | |

91 | KEY SCALAR VALUE SCALAR | |

92 | KEY SCALAR VALUE SCALAR | |

93 | KEY SCALAR VALUE SCALAR | |

94 | BLOCK_END | |

95 | ENTRY | |

96 | BLOCK_MAP_START | |

97 | KEY SCALAR VALUE SCALAR | |

98 | KEY SCALAR VALUE SCALAR | |

99 | KEY SCALAR VALUE SCALAR | |

100 | BLOCK_END | |

101 | BLOCK_END | |

102 | """ | |

103 | ||

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

105 | ||

106 | EX5 = r""" | |

107 | - [name , hr, avg ] | |

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

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

110 | """ | |

111 | ||

112 | TOKENS5 = """ | |

113 | BLOCK_SEQ_START | |

114 | ENTRY FLOW_SEQ_START SCALAR ENTRY SCALAR ENTRY SCALAR FLOW_SEQ_END | |

115 | ENTRY FLOW_SEQ_START SCALAR ENTRY SCALAR ENTRY SCALAR FLOW_SEQ_END | |

116 | ENTRY FLOW_SEQ_START SCALAR ENTRY SCALAR ENTRY SCALAR FLOW_SEQ_END | |

117 | BLOCK_END | |

118 | """ | |

119 | ||

120 | NODES5 = [[True, True, True], [True, True, True], [True, True, True]] | |

121 | ||

122 | EX6 = r""" | |

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

124 | Sammy Sosa: { | |

125 | hr: 63, | |

126 | avg: 0.288 | |

127 | } | |

128 | """ | |

129 | ||

130 | TOKENS6 = """ | |

131 | BLOCK_MAP_START | |

132 | KEY SCALAR VALUE | |

133 | FLOW_MAP_START KEY SCALAR VALUE SCALAR ENTRY KEY SCALAR VALUE SCALAR FLOW_MAP_END | |

134 | KEY SCALAR VALUE | |

135 | FLOW_MAP_START KEY SCALAR VALUE SCALAR ENTRY KEY SCALAR VALUE SCALAR FLOW_MAP_END | |

136 | BLOCK_END | |

137 | """ | |

138 | ||

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

140 | ||

141 | EX7 = r""" | |

142 | # Ranking of 1998 home runs | |

143 | --- | |

144 | - Mark McGwire | |

145 | - Sammy Sosa | |

146 | - Ken Griffey | |

147 | ||

148 | # Team ranking | |

149 | --- | |

150 | - Chicago Cubs | |

151 | - St Louis Cardinals | |

152 | """ | |

153 | ||

154 | TOKENS7 = """ | |

155 | DOCUMENT_START | |

156 | BLOCK_SEQ_START | |

157 | ENTRY SCALAR | |

158 | ENTRY SCALAR | |

159 | ENTRY SCALAR | |

160 | BLOCK_END | |

161 | ||

162 | DOCUMENT_START | |

163 | BLOCK_SEQ_START | |

164 | ENTRY SCALAR | |

165 | ENTRY SCALAR | |

166 | BLOCK_END | |

167 | """ | |

168 | ||

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

170 | ||

171 | EX8 = r""" | |

172 | --- | |

173 | time: 20:03:20 | |

174 | player: Sammy Sosa | |

175 | action: strike (miss) | |

176 | ... | |

177 | --- | |

178 | time: 20:03:47 | |

179 | player: Sammy Sosa | |

180 | action: grand slam | |

181 | ... | |

182 | """ | |

183 | ||

184 | TOKENS8 = """ | |

185 | DOCUMENT_START | |

186 | BLOCK_MAP_START | |

187 | KEY SCALAR VALUE SCALAR | |

188 | KEY SCALAR VALUE SCALAR | |

189 | KEY SCALAR VALUE SCALAR | |

190 | BLOCK_END | |

191 | DOCUMENT_END | |

192 | ||

193 | DOCUMENT_START | |

194 | BLOCK_MAP_START | |

195 | KEY SCALAR VALUE SCALAR | |

196 | KEY SCALAR VALUE SCALAR | |

197 | KEY SCALAR VALUE SCALAR | |

198 | BLOCK_END | |

199 | DOCUMENT_END | |

200 | """ | |

201 | ||

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

203 | ||

204 | EX9 = r""" | |

205 | --- | |

206 | hr: # 1998 hr ranking | |

207 | - Mark McGwire | |

208 | - Sammy Sosa | |

209 | rbi: | |

210 | # 1998 rbi ranking | |

211 | - Sammy Sosa | |

212 | - Ken Griffey | |

213 | """ | |

214 | ||

215 | TOKENS9 = """ | |

216 | DOCUMENT_START | |

217 | BLOCK_MAP_START | |

218 | KEY SCALAR VALUE | |

219 | BLOCK_SEQ_START | |

220 | ENTRY SCALAR | |

221 | ENTRY SCALAR | |

222 | BLOCK_END | |

223 | KEY SCALAR VALUE | |

224 | BLOCK_SEQ_START | |

225 | ENTRY SCALAR | |

226 | ENTRY SCALAR | |

227 | BLOCK_END | |

228 | BLOCK_END | |

229 | """ | |

230 | ||

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

232 | ||

233 | EX10 = r""" | |

234 | --- | |

235 | hr: | |

236 | - Mark McGwire | |

237 | # Following node labeled SS | |

238 | - &SS Sammy Sosa | |

239 | rbi: | |

240 | - *SS # Subsequent occurrence | |

241 | - Ken Griffey | |

242 | """ | |

243 | ||

244 | TOKENS10 = """ | |

245 | DOCUMENT_START | |

246 | BLOCK_MAP_START | |

247 | KEY SCALAR VALUE | |

248 | BLOCK_SEQ_START | |

249 | ENTRY SCALAR | |

250 | ENTRY ANCHOR SCALAR | |

251 | BLOCK_END | |

252 | KEY SCALAR VALUE | |

253 | BLOCK_SEQ_START | |

254 | ENTRY ALIAS | |

255 | ENTRY SCALAR | |

256 | BLOCK_END | |

257 | BLOCK_END | |

258 | """ | |

259 | ||

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

261 | ||

262 | EX11 = r""" | |

263 | ? - Detroit Tigers | |

264 | - Chicago cubs | |

265 | : | |

266 | - 2001-07-23 | |

267 | ||

268 | ? [ New York Yankees, | |

269 | Atlanta Braves ] | |

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

271 | 2001-08-14 ] | |

272 | """ | |

273 | ||

274 | TOKENS11 = """ | |

275 | BLOCK_MAP_START | |

276 | KEY | |

277 | BLOCK_SEQ_START | |

278 | ENTRY SCALAR | |

279 | ENTRY SCALAR | |

280 | BLOCK_END | |

281 | VALUE | |

282 | BLOCK_SEQ_START | |

283 | ENTRY SCALAR | |

284 | BLOCK_END | |

285 | KEY | |

286 | FLOW_SEQ_START SCALAR ENTRY SCALAR FLOW_SEQ_END | |

287 | VALUE | |

288 | FLOW_SEQ_START SCALAR ENTRY SCALAR ENTRY SCALAR FLOW_SEQ_END | |

289 | BLOCK_END | |

290 | """ | |

291 | ||

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

293 | ||

294 | EX12 = r""" | |

295 | --- | |

296 | # products purchased | |

297 | - item : Super Hoop | |

298 | quantity: 1 | |

299 | - item : Basketball | |

300 | quantity: 4 | |

301 | - item : Big Shoes | |

302 | quantity: 1 | |

303 | """ | |

304 | ||

305 | TOKENS12 = """ | |

306 | DOCUMENT_START | |

307 | BLOCK_SEQ_START | |

308 | ENTRY | |

309 | BLOCK_MAP_START | |

310 | KEY SCALAR VALUE SCALAR | |

311 | KEY SCALAR VALUE SCALAR | |

312 | BLOCK_END | |

313 | ENTRY | |

314 | BLOCK_MAP_START | |

315 | KEY SCALAR VALUE SCALAR | |

316 | KEY SCALAR VALUE SCALAR | |

317 | BLOCK_END | |

318 | ENTRY | |

319 | BLOCK_MAP_START | |

320 | KEY SCALAR VALUE SCALAR | |

321 | KEY SCALAR VALUE SCALAR | |

322 | BLOCK_END | |

323 | BLOCK_END | |

324 | """ | |

325 | ||

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

327 | ||

328 | EX13 = r""" | |

329 | # ASCII Art | |

330 | --- | | |

331 | \//||\/|| | |

332 | // || ||__ | |

333 | """ | |

334 | ||

335 | TOKENS13 = """ | |

336 | DOCUMENT_START SCALAR | |

337 | """ | |

338 | ||

339 | NODES13 = True | |

340 | ||

341 | EX14 = r""" | |

342 | --- | |

343 | Mark McGwire's | |

344 | year was crippled | |

345 | by a knee injury. | |

346 | """ | |

347 | ||

348 | TOKENS14 = """ | |

349 | DOCUMENT_START SCALAR | |

350 | """ | |

351 | ||

352 | NODES14 = True | |

353 | ||

354 | EX15 = r""" | |

355 | > | |

356 | Sammy Sosa completed another | |

357 | fine season with great stats. | |

358 | ||

359 | 63 Home Runs | |

360 | 0.288 Batting Average | |

361 | ||

362 | What a year! | |

363 | """ | |

364 | ||

365 | TOKENS15 = """ | |

366 | SCALAR | |

367 | """ | |

368 | ||

369 | NODES15 = True | |

370 | ||

371 | EX16 = r""" | |

372 | name: Mark McGwire | |

373 | accomplishment: > | |

374 | Mark set a major league | |

375 | home run record in 1998. | |

376 | stats: | | |

377 | 65 Home Runs | |

378 | 0.278 Batting Average | |

379 | """ | |

380 | ||

381 | TOKENS16 = """ | |

382 | BLOCK_MAP_START | |

383 | KEY SCALAR VALUE SCALAR | |

384 | KEY SCALAR VALUE SCALAR | |

385 | KEY SCALAR VALUE SCALAR | |

386 | BLOCK_END | |

387 | """ | |

388 | ||

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

390 | ||

391 | EX17 = r""" | |

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

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

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

395 | ||

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

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

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

399 | """ | |

400 | ||

401 | TOKENS17 = """ | |

402 | BLOCK_MAP_START | |

403 | KEY SCALAR VALUE SCALAR | |

404 | KEY SCALAR VALUE SCALAR | |

405 | KEY SCALAR VALUE SCALAR | |

406 | KEY SCALAR VALUE SCALAR | |

407 | KEY SCALAR VALUE SCALAR | |

408 | KEY SCALAR VALUE SCALAR | |

409 | BLOCK_END | |

410 | """ | |

411 | ||

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

413 | ||

414 | EX18 = r""" | |

415 | plain: | |

416 | This unquoted scalar | |

417 | spans many lines. | |

418 | ||

419 | quoted: "So does this | |

420 | quoted scalar.\n" | |

421 | """ | |

422 | ||

423 | TOKENS18 = """ | |

424 | BLOCK_MAP_START | |

425 | KEY SCALAR VALUE SCALAR | |

426 | KEY SCALAR VALUE SCALAR | |

427 | BLOCK_END | |

428 | """ | |

429 | ||

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

431 | ||

432 | EX19 = r""" | |

433 | canonical: 12345 | |

434 | decimal: +12,345 | |

435 | sexagesimal: 3:25:45 | |

436 | octal: 014 | |

437 | hexadecimal: 0xC | |

438 | """ | |

439 | ||

440 | TOKENS19 = """ | |

441 | BLOCK_MAP_START | |

442 | KEY SCALAR VALUE SCALAR | |

443 | KEY SCALAR VALUE SCALAR | |

444 | KEY SCALAR VALUE SCALAR | |

445 | KEY SCALAR VALUE SCALAR | |

446 | KEY SCALAR VALUE SCALAR | |

447 | BLOCK_END | |

448 | """ | |

449 | ||

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

451 | ||

452 | EX20 = r""" | |

453 | canonical: 1.23015e+3 | |

454 | exponential: 12.3015e+02 | |

455 | sexagesimal: 20:30.15 | |

456 | fixed: 1,230.15 | |

457 | negative infinity: -.inf | |

458 | not a number: .NaN | |

459 | """ | |

460 | ||

461 | TOKENS20 = """ | |

462 | BLOCK_MAP_START | |

463 | KEY SCALAR VALUE SCALAR | |

464 | KEY SCALAR VALUE SCALAR | |

465 | KEY SCALAR VALUE SCALAR | |

466 | KEY SCALAR VALUE SCALAR | |

467 | KEY SCALAR VALUE SCALAR | |

468 | KEY SCALAR VALUE SCALAR | |

469 | BLOCK_END | |

470 | """ | |

471 | ||

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

473 | ||

474 | EX21 = r""" | |

475 | null: ~ | |

476 | true: y | |

477 | false: n | |

478 | string: '12345' | |

479 | """ | |

480 | ||

481 | TOKENS21 = """ | |

482 | BLOCK_MAP_START | |

483 | KEY SCALAR VALUE SCALAR | |

484 | KEY SCALAR VALUE SCALAR | |

485 | KEY SCALAR VALUE SCALAR | |

486 | KEY SCALAR VALUE SCALAR | |

487 | BLOCK_END | |

488 | """ | |

489 | ||

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

491 | ||

492 | EX22 = r""" | |

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

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

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

496 | date: 2002-12-14 | |

497 | """ | |

498 | ||

499 | TOKENS22 = """ | |

500 | BLOCK_MAP_START | |

501 | KEY SCALAR VALUE SCALAR | |

502 | KEY SCALAR VALUE SCALAR | |

503 | KEY SCALAR VALUE SCALAR | |

504 | KEY SCALAR VALUE SCALAR | |

505 | BLOCK_END | |

506 | """ | |

507 | ||

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

509 | ||

510 | EX23 = r""" | |

511 | --- | |

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

513 | ||

514 | picture: !!binary | | |

515 | R0lGODlhDAAMAIQAAP//9/X | |

516 | 17unp5WZmZgAAAOfn515eXv | |

517 | Pz7Y6OjuDg4J+fn5OTk6enp | |

518 | 56enmleECcgggoBADs= | |

519 | ||

520 | application specific tag: !something | | |

521 | The semantics of the tag | |

522 | above may be different for | |

523 | different documents. | |

524 | """ | |

525 | ||

526 | TOKENS23 = """ | |

527 | DOCUMENT_START | |

528 | BLOCK_MAP_START | |

529 | KEY SCALAR VALUE TAG SCALAR | |

530 | KEY SCALAR VALUE TAG SCALAR | |

531 | KEY SCALAR VALUE TAG SCALAR | |

532 | BLOCK_END | |

533 | """ | |

534 | ||

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

536 | ||

537 | EX24 = r""" | |

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

539 | --- !shape | |

540 | # Use the ! handle for presenting | |

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

542 | - !circle | |

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

544 | radius: 7 | |

545 | - !line | |

546 | start: *ORIGIN | |

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

548 | - !label | |

549 | start: *ORIGIN | |

550 | color: 0xFFEEBB | |

551 | text: Pretty vector drawing. | |

552 | """ | |

553 | ||

554 | TOKENS24 = """ | |

555 | DIRECTIVE | |

556 | DOCUMENT_START TAG | |

557 | BLOCK_SEQ_START | |

558 | ENTRY TAG | |

559 | BLOCK_MAP_START | |

560 | KEY SCALAR VALUE ANCHOR | |

561 | FLOW_MAP_START KEY SCALAR VALUE SCALAR ENTRY KEY SCALAR VALUE SCALAR FLOW_MAP_END | |

562 | KEY SCALAR VALUE SCALAR | |

563 | BLOCK_END | |

564 | ENTRY TAG | |

565 | BLOCK_MAP_START | |

566 | KEY SCALAR VALUE ALIAS | |

567 | KEY SCALAR VALUE | |

568 | FLOW_MAP_START KEY SCALAR VALUE SCALAR ENTRY KEY SCALAR VALUE SCALAR FLOW_MAP_END | |

569 | BLOCK_END | |

570 | ENTRY TAG | |

571 | BLOCK_MAP_START | |

572 | KEY SCALAR VALUE ALIAS | |

573 | KEY SCALAR VALUE SCALAR | |

574 | KEY SCALAR VALUE SCALAR | |

575 | BLOCK_END | |

576 | BLOCK_END | |

577 | """ | |

578 | ||

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

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

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

582 | ||

583 | EX25 = r""" | |

584 | # sets are represented as a | |

585 | # mapping where each key is | |

586 | # associated with the empty string | |

587 | --- !!set | |

588 | ? Mark McGwire | |

589 | ? Sammy Sosa | |

590 | ? Ken Griff | |

591 | """ | |

592 | ||

593 | TOKENS25 = """ | |

594 | DOCUMENT_START TAG | |

595 | BLOCK_MAP_START | |

596 | KEY SCALAR | |

597 | KEY SCALAR | |

598 | KEY SCALAR | |

599 | BLOCK_END | |

600 | """ | |

601 | ||

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

603 | ||

604 | EX26 = r""" | |

605 | # ordered maps are represented as | |

606 | # a sequence of mappings, with | |

607 | # each mapping having one key | |

608 | --- !!omap | |

609 | - Mark McGwire: 65 | |

610 | - Sammy Sosa: 63 | |

611 | - Ken Griffy: 58 | |

612 | """ | |

613 | ||

614 | TOKENS26 = """ | |

615 | DOCUMENT_START TAG | |

616 | BLOCK_SEQ_START | |

617 | ENTRY | |

618 | BLOCK_MAP_START | |

619 | KEY SCALAR VALUE SCALAR | |

620 | BLOCK_END | |

621 | ENTRY | |

622 | BLOCK_MAP_START | |

623 | KEY SCALAR VALUE SCALAR | |

624 | BLOCK_END | |

625 | ENTRY | |

626 | BLOCK_MAP_START | |

627 | KEY SCALAR VALUE SCALAR | |

628 | BLOCK_END | |

629 | BLOCK_END | |

630 | """ | |

631 | ||

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

633 | ||

634 | EX27 = r""" | |

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

636 | invoice: 34843 | |

637 | date : 2001-01-23 | |

638 | bill-to: &id001 | |

639 | given : Chris | |

640 | family : Dumars | |

641 | address: | |

642 | lines: | | |

643 | 458 Walkman Dr. | |

644 | Suite #292 | |

645 | city : Royal Oak | |

646 | state : MI | |

647 | postal : 48046 | |

648 | ship-to: *id001 | |

649 | product: | |

650 | - sku : BL394D | |

651 | quantity : 4 | |

652 | description : Basketball | |

653 | price : 450.00 | |

654 | - sku : BL4438H | |

655 | quantity : 1 | |

656 | description : Super Hoop | |

657 | price : 2392.00 | |

658 | tax : 251.42 | |

659 | total: 4443.52 | |

660 | comments: | |

661 | Late afternoon is best. | |

662 | Backup contact is Nancy | |

663 | Billsmer @ 338-4338. | |

664 | """ | |

665 | ||

666 | TOKENS27 = """ | |

667 | DOCUMENT_START TAG | |

668 | BLOCK_MAP_START | |

669 | KEY SCALAR VALUE SCALAR | |

670 | KEY SCALAR VALUE SCALAR | |

671 | KEY SCALAR VALUE ANCHOR | |

672 | BLOCK_MAP_START | |

673 | KEY SCALAR VALUE SCALAR | |

674 | KEY SCALAR VALUE SCALAR | |

675 | KEY SCALAR VALUE | |

676 | BLOCK_MAP_START | |

677 | KEY SCALAR VALUE SCALAR | |

678 | KEY SCALAR VALUE SCALAR | |

679 | KEY SCALAR VALUE SCALAR | |

680 | KEY SCALAR VALUE SCALAR | |

681 | BLOCK_END | |

682 | BLOCK_END | |

683 | KEY SCALAR VALUE ALIAS | |

684 | KEY SCALAR VALUE | |

685 | BLOCK_SEQ_START | |

686 | ENTRY | |

687 | BLOCK_MAP_START | |

688 | KEY SCALAR VALUE SCALAR | |

689 | KEY SCALAR VALUE SCALAR | |

690 | KEY SCALAR VALUE SCALAR | |

691 | KEY SCALAR VALUE SCALAR | |

692 | BLOCK_END | |

693 | ENTRY | |

694 | BLOCK_MAP_START | |

695 | KEY SCALAR VALUE SCALAR | |

696 | KEY SCALAR VALUE SCALAR | |

697 | KEY SCALAR VALUE SCALAR | |

698 | KEY SCALAR VALUE SCALAR | |

699 | BLOCK_END | |

700 | BLOCK_END | |

701 | KEY SCALAR VALUE SCALAR | |

702 | KEY SCALAR VALUE SCALAR | |

703 | KEY SCALAR VALUE SCALAR | |

704 | BLOCK_END | |

705 | """ | |

706 | ||

707 | NODES27 = [ | |

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

709 | (True, [[(True, True), (True, True), (True, True), (True, True)], [(True, True), (True, True), (True, True), (True, True)]]), (True, True), (True, True), (True, True), | |

710 | ] | |

711 | ||

712 | EX28 = r""" | |

713 | --- | |

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

715 | User: ed | |

716 | Warning: | |

717 | This is an error message | |

718 | for the log file | |

719 | --- | |

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

721 | User: ed | |

722 | Warning: | |

723 | A slightly different error | |

724 | message. | |

725 | --- | |

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

727 | User: ed | |

728 | Fatal: | |

729 | Unknown variable "bar" | |

730 | Stack: | |

731 | - file: TopClass.py | |

732 | line: 23 | |

733 | code: | | |

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

735 | - file: MoreClass.py | |

736 | line: 58 | |

737 | code: |- | |

738 | foo = bar | |

739 | """ | |

740 | ||

741 | TOKENS28 = """ | |

742 | DOCUMENT_START | |

743 | BLOCK_MAP_START | |

744 | KEY SCALAR VALUE SCALAR | |

745 | KEY SCALAR VALUE SCALAR | |

746 | KEY SCALAR VALUE SCALAR | |

747 | BLOCK_END | |

748 | ||

749 | DOCUMENT_START | |

750 | BLOCK_MAP_START | |

751 | KEY SCALAR VALUE SCALAR | |

752 | KEY SCALAR VALUE SCALAR | |

753 | KEY SCALAR VALUE SCALAR | |

754 | BLOCK_END | |

755 | ||

756 | DOCUMENT_START | |

757 | BLOCK_MAP_START | |

758 | KEY SCALAR VALUE SCALAR | |

759 | KEY SCALAR VALUE SCALAR | |

760 | KEY SCALAR VALUE SCALAR | |

761 | KEY SCALAR VALUE | |

762 | BLOCK_SEQ_START | |

763 | ENTRY | |

764 | BLOCK_MAP_START | |

765 | KEY SCALAR VALUE SCALAR | |

766 | KEY SCALAR VALUE SCALAR | |

767 | KEY SCALAR VALUE SCALAR | |

768 | BLOCK_END | |

769 | ENTRY | |

770 | BLOCK_MAP_START | |

771 | KEY SCALAR VALUE SCALAR | |

772 | KEY SCALAR VALUE SCALAR | |

773 | KEY SCALAR VALUE SCALAR | |

774 | BLOCK_END | |

775 | BLOCK_END | |

776 | BLOCK_END | |

777 | """ | |

778 | ||

779 | NODES28 = ( | |

780 | [(True, True), (True, True), (True, True)], [(True, True), (True, True), (True, True)], | |

781 | [(True, True), (True, True), (True, True), (True, [[(True, True), (True, True), (True, True)], [(True, True), (True, True), (True, True)]])], | |

782 | ) | |

783 | ||

784 | MAX_TESTS = 100 | |

785 | ||

786 | class TestParser2(unittest.TestCase): | |

787 | ||

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

789 | try: | |

790 | tokens = None | |

791 | scanner = parser2.Scanner() | |

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

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

794 | except: | |

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

796 | print "EX:" | |

797 | print EX | |

798 | print "TOKENS:" | |

799 | print TOKENS | |

800 | print "RESULT:", tokens | |

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

802 | raise | |

803 | ||

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

805 | try: | |

806 | nodes = None | |

807 | parser = parser2.Parser() | |

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

809 | self.failUnlessEqual(nodes, NODES) | |

810 | except: | |

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

812 | print "EX:" | |

813 | print EX | |

814 | print "RESULT:", nodes | |

815 | print "EXPECT:", NODES | |

816 | raise | |

817 | ||

818 | @classmethod | |

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

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

821 | args = [] | |

822 | for name in tests: | |

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

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

825 | else: | |

826 | break | |

827 | else: | |

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

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

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

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

832 | ||

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

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

835 | ||

836 | if __name__ == '__main__': | |

837 | unittest.main() | |

838 |

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