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

Revision 40, 15.1 KB checked in by xi, 8 years ago (diff)

Add test that breaks the scanner.

Line 
1
2import unittest
3import parser2
4
5EX99 = r"""
6key:
7value
8"""
9
10TOKENS99 = """
11Should produce error.
12"""
13
14EX1 = r"""
15- Mark McGwire
16- Sammy Sosa
17- Ken Griffey
18"""
19
20TOKENS1 = """
21BLOCK_SEQ_START
22ENTRY SCALAR
23ENTRY SCALAR
24ENTRY SCALAR
25BLOCK_END
26"""
27
28NODES1 = [True, True, True]
29
30EX2 = r"""
31hr:  65    # Home runs
32avg: 0.278 # Batting average
33rbi: 147   # Runs Batted In
34"""
35
36TOKENS2 = """
37BLOCK_MAP_START
38KEY SCALAR VALUE SCALAR
39KEY SCALAR VALUE SCALAR
40KEY SCALAR VALUE SCALAR
41BLOCK_END
42"""
43
44NODES2 = [(True, True), (True, True), (True, True)]
45
46EX3 = r"""
47american:
48  - Boston Red Sox
49  - Detroit Tigers
50  - New York Yankees
51national:
52  - New York Mets
53  - Chicago Cubs
54  - Atlanta Braves
55"""
56
57TOKENS3 = """
58BLOCK_MAP_START
59KEY SCALAR VALUE
60    BLOCK_SEQ_START
61    ENTRY SCALAR
62    ENTRY SCALAR
63    ENTRY SCALAR
64    BLOCK_END
65KEY SCALAR VALUE
66    BLOCK_SEQ_START
67    ENTRY SCALAR
68    ENTRY SCALAR
69    ENTRY SCALAR
70    BLOCK_END
71BLOCK_END
72"""
73
74NODES3 = [(True, [True, True, True]), (True, [True, True, True])]
75
76EX4 = 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
87TOKENS4 = """
88BLOCK_SEQ_START
89ENTRY
90    BLOCK_MAP_START
91    KEY SCALAR VALUE SCALAR
92    KEY SCALAR VALUE SCALAR
93    KEY SCALAR VALUE SCALAR
94    BLOCK_END
95ENTRY
96    BLOCK_MAP_START
97    KEY SCALAR VALUE SCALAR
98    KEY SCALAR VALUE SCALAR
99    KEY SCALAR VALUE SCALAR
100    BLOCK_END
101BLOCK_END
102"""
103
104NODES4 = [[(True, True), (True, True), (True, True)], [(True, True), (True, True), (True, True)]]
105
106EX5 = r"""
107- [name        , hr, avg  ]
108- [Mark McGwire, 65, 0.278]
109- [Sammy Sosa  , 63, 0.288]
110"""
111
112TOKENS5 = """
113BLOCK_SEQ_START
114ENTRY FLOW_SEQ_START SCALAR ENTRY SCALAR ENTRY SCALAR FLOW_SEQ_END
115ENTRY FLOW_SEQ_START SCALAR ENTRY SCALAR ENTRY SCALAR FLOW_SEQ_END
116ENTRY FLOW_SEQ_START SCALAR ENTRY SCALAR ENTRY SCALAR FLOW_SEQ_END
117BLOCK_END
118"""
119
120NODES5 = [[True, True, True], [True, True, True], [True, True, True]]
121
122EX6 = r"""
123Mark McGwire: {hr: 65, avg: 0.278}
124Sammy Sosa: {
125    hr: 63,
126    avg: 0.288
127  }
128"""
129
130TOKENS6 = """
131BLOCK_MAP_START
132KEY SCALAR VALUE
133    FLOW_MAP_START KEY SCALAR VALUE SCALAR ENTRY KEY SCALAR VALUE SCALAR FLOW_MAP_END
134KEY SCALAR VALUE
135    FLOW_MAP_START KEY SCALAR VALUE SCALAR ENTRY KEY SCALAR VALUE SCALAR FLOW_MAP_END
136BLOCK_END   
137"""
138
139NODES6 = [(True, [(True, True), (True, True)]), (True, [(True, True), (True, True)])]
140
141EX7 = 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
154TOKENS7 = """
155DOCUMENT_START
156BLOCK_SEQ_START
157ENTRY SCALAR
158ENTRY SCALAR
159ENTRY SCALAR
160BLOCK_END
161
162DOCUMENT_START
163BLOCK_SEQ_START
164ENTRY SCALAR
165ENTRY SCALAR
166BLOCK_END
167"""
168
169NODES7 = ([True, True, True], [True, True])
170
171EX8 = r"""
172---
173time: 20:03:20
174player: Sammy Sosa
175action: strike (miss)
176...
177---
178time: 20:03:47
179player: Sammy Sosa
180action: grand slam
181...
182"""
183
184TOKENS8 = """
185DOCUMENT_START
186BLOCK_MAP_START
187KEY SCALAR VALUE SCALAR
188KEY SCALAR VALUE SCALAR
189KEY SCALAR VALUE SCALAR
190BLOCK_END
191DOCUMENT_END
192
193DOCUMENT_START
194BLOCK_MAP_START
195KEY SCALAR VALUE SCALAR
196KEY SCALAR VALUE SCALAR
197KEY SCALAR VALUE SCALAR
198BLOCK_END
199DOCUMENT_END
200"""
201
202NODES8 = ([(True, True), (True, True), (True, True)], [(True, True), (True, True), (True, True)])
203
204EX9 = r"""
205---
206hr: # 1998 hr ranking
207  - Mark McGwire
208  - Sammy Sosa
209rbi:
210  # 1998 rbi ranking
211  - Sammy Sosa
212  - Ken Griffey
213"""
214
215TOKENS9 = """
216DOCUMENT_START
217BLOCK_MAP_START
218KEY SCALAR VALUE
219    BLOCK_SEQ_START
220    ENTRY SCALAR
221    ENTRY SCALAR
222    BLOCK_END
223KEY SCALAR VALUE
224    BLOCK_SEQ_START
225    ENTRY SCALAR
226    ENTRY SCALAR
227    BLOCK_END
228BLOCK_END
229"""
230
231NODES9 = [(True, [True, True]), (True, [True, True])]
232
233EX10 = r"""
234---
235hr:
236  - Mark McGwire
237  # Following node labeled SS
238  - &SS Sammy Sosa
239rbi:
240  - *SS # Subsequent occurrence
241  - Ken Griffey
242"""
243
244TOKENS10 = """
245DOCUMENT_START
246BLOCK_MAP_START
247KEY SCALAR VALUE
248    BLOCK_SEQ_START
249    ENTRY SCALAR
250    ENTRY ANCHOR SCALAR
251    BLOCK_END
252KEY SCALAR VALUE
253    BLOCK_SEQ_START
254    ENTRY ALIAS
255    ENTRY SCALAR
256    BLOCK_END
257BLOCK_END
258"""
259
260NODES10 = [(True, [True, True]), (True, ['*', True])]
261
262EX11 = 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
274TOKENS11 = """
275BLOCK_MAP_START
276KEY
277    BLOCK_SEQ_START
278    ENTRY SCALAR
279    ENTRY SCALAR
280    BLOCK_END
281VALUE
282    BLOCK_SEQ_START
283    ENTRY SCALAR
284    BLOCK_END
285KEY
286    FLOW_SEQ_START SCALAR ENTRY SCALAR FLOW_SEQ_END
287VALUE
288    FLOW_SEQ_START SCALAR ENTRY SCALAR ENTRY SCALAR FLOW_SEQ_END
289BLOCK_END
290"""
291
292NODES11 = [([True, True], [True]), ([True, True], [True, True, True])]
293
294EX12 = 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
305TOKENS12 = """
306DOCUMENT_START
307BLOCK_SEQ_START
308ENTRY
309    BLOCK_MAP_START
310    KEY SCALAR VALUE SCALAR
311    KEY SCALAR VALUE SCALAR
312    BLOCK_END
313ENTRY
314    BLOCK_MAP_START
315    KEY SCALAR VALUE SCALAR
316    KEY SCALAR VALUE SCALAR
317    BLOCK_END
318ENTRY
319    BLOCK_MAP_START
320    KEY SCALAR VALUE SCALAR
321    KEY SCALAR VALUE SCALAR
322    BLOCK_END
323BLOCK_END
324"""
325
326NODES12 = [[(True, True), (True, True)], [(True, True), (True, True)], [(True, True), (True, True)]]
327
328EX13 = r"""
329# ASCII Art
330--- |
331  \//||\/||
332  // ||  ||__
333"""
334
335TOKENS13 = """
336DOCUMENT_START SCALAR
337"""
338
339NODES13 = True
340
341EX14 = r"""
342---
343  Mark McGwire's
344  year was crippled
345  by a knee injury.
346"""
347
348TOKENS14 = """
349DOCUMENT_START SCALAR
350"""
351
352NODES14 = True
353
354EX15 = 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
365TOKENS15 = """
366SCALAR
367"""
368
369NODES15 = True
370
371EX16 = r"""
372name: Mark McGwire
373accomplishment: >
374  Mark set a major league
375  home run record in 1998.
376stats: |
377  65 Home Runs
378  0.278 Batting Average
379"""
380
381TOKENS16 = """
382BLOCK_MAP_START
383KEY SCALAR VALUE SCALAR
384KEY SCALAR VALUE SCALAR
385KEY SCALAR VALUE SCALAR
386BLOCK_END
387"""
388
389NODES16 = [(True, True), (True, True), (True, True)]
390
391EX17 = r"""
392unicode: "Sosa did fine.\u263A"
393control: "\b1998\t1999\t2000\n"
394hexesc:  "\x13\x10 is \r\n"
395
396single: '"Howdy!" he cried.'
397quoted: ' # not a ''comment''.'
398tie-fighter: '|\-*-/|'
399"""
400
401TOKENS17 = """
402BLOCK_MAP_START
403KEY SCALAR VALUE SCALAR
404KEY SCALAR VALUE SCALAR
405KEY SCALAR VALUE SCALAR
406KEY SCALAR VALUE SCALAR
407KEY SCALAR VALUE SCALAR
408KEY SCALAR VALUE SCALAR
409BLOCK_END
410"""
411
412NODES17 = [(True, True), (True, True), (True, True), (True, True), (True, True), (True, True)]
413
414EX18 = r"""
415plain:
416  This unquoted scalar
417  spans many lines.
418
419quoted: "So does this
420  quoted scalar.\n"
421"""
422
423TOKENS18 = """
424BLOCK_MAP_START
425KEY SCALAR VALUE SCALAR
426KEY SCALAR VALUE SCALAR
427BLOCK_END
428"""
429
430NODES18 = [(True, True), (True, True)]
431
432EX19 = r"""
433canonical: 12345
434decimal: +12,345
435sexagesimal: 3:25:45
436octal: 014
437hexadecimal: 0xC
438"""
439
440TOKENS19 = """
441BLOCK_MAP_START
442KEY SCALAR VALUE SCALAR
443KEY SCALAR VALUE SCALAR
444KEY SCALAR VALUE SCALAR
445KEY SCALAR VALUE SCALAR
446KEY SCALAR VALUE SCALAR
447BLOCK_END
448"""
449
450NODES19 = [(True, True), (True, True), (True, True), (True, True), (True, True)]
451
452EX20 = r"""
453canonical: 1.23015e+3
454exponential: 12.3015e+02
455sexagesimal: 20:30.15
456fixed: 1,230.15
457negative infinity: -.inf
458not a number: .NaN
459"""
460
461TOKENS20 = """
462BLOCK_MAP_START
463KEY SCALAR VALUE SCALAR
464KEY SCALAR VALUE SCALAR
465KEY SCALAR VALUE SCALAR
466KEY SCALAR VALUE SCALAR
467KEY SCALAR VALUE SCALAR
468KEY SCALAR VALUE SCALAR
469BLOCK_END
470"""
471
472NODES20 = [(True, True), (True, True), (True, True), (True, True), (True, True), (True, True)]
473
474EX21 = r"""
475null: ~
476true: y
477false: n
478string: '12345'
479"""
480
481TOKENS21 = """
482BLOCK_MAP_START
483KEY SCALAR VALUE SCALAR
484KEY SCALAR VALUE SCALAR
485KEY SCALAR VALUE SCALAR
486KEY SCALAR VALUE SCALAR
487BLOCK_END
488"""
489
490NODES21 = [(True, True), (True, True), (True, True), (True, True)]
491
492EX22 = r"""
493canonical: 2001-12-15T02:59:43.1Z
494iso8601: 2001-12-14t21:59:43.10-05:00
495spaced: 2001-12-14 21:59:43.10 -5
496date: 2002-12-14
497"""
498
499TOKENS22 = """
500BLOCK_MAP_START
501KEY SCALAR VALUE SCALAR
502KEY SCALAR VALUE SCALAR
503KEY SCALAR VALUE SCALAR
504KEY SCALAR VALUE SCALAR
505BLOCK_END
506"""
507
508NODES22 = [(True, True), (True, True), (True, True), (True, True)]
509
510EX23 = r"""
511---
512not-date: !!str 2002-04-28
513
514picture: !!binary |
515 R0lGODlhDAAMAIQAAP//9/X
516 17unp5WZmZgAAAOfn515eXv
517 Pz7Y6OjuDg4J+fn5OTk6enp
518 56enmleECcgggoBADs=
519
520application specific tag: !something |
521 The semantics of the tag
522 above may be different for
523 different documents.
524"""
525
526TOKENS23 = """
527DOCUMENT_START
528BLOCK_MAP_START
529KEY SCALAR VALUE TAG SCALAR
530KEY SCALAR VALUE TAG SCALAR
531KEY SCALAR VALUE TAG SCALAR
532BLOCK_END
533"""
534
535NODES23 = [(True, True), (True, True), (True, True)]
536
537EX24 = 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
554TOKENS24 = """
555DIRECTIVE
556DOCUMENT_START TAG
557BLOCK_SEQ_START
558ENTRY 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
564ENTRY 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
570ENTRY TAG
571    BLOCK_MAP_START
572    KEY SCALAR VALUE ALIAS
573    KEY SCALAR VALUE SCALAR
574    KEY SCALAR VALUE SCALAR
575    BLOCK_END
576BLOCK_END
577"""
578
579NODES24 = [[(True, [(True, True), (True, True)]), (True, True)],
580    [(True, '*'), (True, [(True, True), (True, True)])],
581    [(True, '*'), (True, True), (True, True)]]
582
583EX25 = 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
593TOKENS25 = """
594DOCUMENT_START TAG
595BLOCK_MAP_START
596KEY SCALAR
597KEY SCALAR
598KEY SCALAR
599BLOCK_END
600"""
601
602NODES25 = [(True, None), (True, None), (True, None)]
603
604EX26 = 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
614TOKENS26 = """
615DOCUMENT_START TAG
616BLOCK_SEQ_START
617ENTRY
618    BLOCK_MAP_START
619    KEY SCALAR VALUE SCALAR
620    BLOCK_END
621ENTRY
622    BLOCK_MAP_START
623    KEY SCALAR VALUE SCALAR
624    BLOCK_END
625ENTRY
626    BLOCK_MAP_START
627    KEY SCALAR VALUE SCALAR
628    BLOCK_END
629BLOCK_END
630"""
631
632NODES26 = [[(True, True)], [(True, True)], [(True, True)]]
633
634EX27 = r"""
635--- !<tag:clarkevans.com,2002:invoice>
636invoice: 34843
637date   : 2001-01-23
638bill-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
648ship-to: *id001
649product:
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
658tax  : 251.42
659total: 4443.52
660comments:
661    Late afternoon is best.
662    Backup contact is Nancy
663    Billsmer @ 338-4338.
664"""
665
666TOKENS27 = """
667DOCUMENT_START TAG
668BLOCK_MAP_START
669KEY SCALAR VALUE SCALAR
670KEY SCALAR VALUE SCALAR
671KEY 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
683KEY SCALAR VALUE ALIAS
684KEY 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
701KEY SCALAR VALUE SCALAR
702KEY SCALAR VALUE SCALAR
703KEY SCALAR VALUE SCALAR
704BLOCK_END
705"""
706
707NODES27 = [
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
712EX28 = r"""
713---
714Time: 2001-11-23 15:01:42 -5
715User: ed
716Warning:
717  This is an error message
718  for the log file
719---
720Time: 2001-11-23 15:02:31 -5
721User: ed
722Warning:
723  A slightly different error
724  message.
725---
726Date: 2001-11-23 15:03:17 -5
727User: ed
728Fatal:
729  Unknown variable "bar"
730Stack:
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
741TOKENS28 = """
742DOCUMENT_START
743BLOCK_MAP_START
744KEY SCALAR VALUE SCALAR
745KEY SCALAR VALUE SCALAR
746KEY SCALAR VALUE SCALAR
747BLOCK_END
748
749DOCUMENT_START
750BLOCK_MAP_START
751KEY SCALAR VALUE SCALAR
752KEY SCALAR VALUE SCALAR
753KEY SCALAR VALUE SCALAR
754BLOCK_END
755
756DOCUMENT_START
757BLOCK_MAP_START
758KEY SCALAR VALUE SCALAR
759KEY SCALAR VALUE SCALAR
760KEY SCALAR VALUE SCALAR
761KEY 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
776BLOCK_END
777"""
778
779NODES28 = (
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
784MAX_TESTS = 100
785
786class 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
833TestParser2.add_tests('testTokens', 'EX', 'TOKENS')
834TestParser2.add_tests('testNodes', 'EX', 'NODES')
835
836if __name__ == '__main__':
837    unittest.main()
838
Note: See TracBrowser for help on using the repository browser.