Source

moin-2.0 / MoinMoin / util / _tests / test_paramparser.py

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
# Copyright: 2010 MoinMoin:ThomasWaldmann
# License: GNU GPL v2 (or any later version), see LICENSE.txt for details.

"""
    MoinMoin - MoinMoin.util.paramparser Tests
"""


import pytest

from MoinMoin.util import paramparser


class TestParamParsing(object):
    def testMacroArgs(self):
        abcd = [u'a', u'b', u'c', u'd']
        abcd_dict = {u'a': u'1', u'b': u'2', u'c': u'3', u'd': u'4'}
        tests = [
            # regular and quoting tests
            (u'd = 4,c=3,b=2,a= 1 ', ([], abcd_dict, [])),
            (u'a,b,c,d', (abcd, {}, [])),
            (u' a , b , c , d ', (abcd, {}, [])),
            (u'   a   ', ([u'a'], {}, [])),
            (u'"  a  "', ([u'  a  '], {}, [])),
            (u'a,b,c,d, "a,b,c,d"', (abcd + [u'a,b,c,d'], {}, [])),
            (u'quote " :), b', ([u'quote " :)', u'b'], {}, [])),
            (u'"quote "" :)", b', ([u'quote " :)', u'b'], {}, [])),
            (u'=7', ([], {u'': u'7'}, [])),
            (u',,', ([None, None, None], {}, [])),
            (u',"",', ([None, u'', None], {}, [])),
            (u',"", ""', ([None, u'', u''], {}, [])),
            (u'  ""  ,"", ""', ([u'', u'', u''], {}, [])),
            # some name=value test
            (u'd = 4,c=3,b=2,a= 1 ', ([], abcd_dict, [])),
            (u'd=d,e="a,b,c,d"', ([], {u'd': u'd', u'e': u'a,b,c,d'}, [])),
            (u'd = d,e = "a,b,c,d"', ([], {u'd': u'd', u'e': u'a,b,c,d'}, [])),
            (u'd = d, e = "a,b,c,d"', ([], {u'd': u'd', u'e': u'a,b,c,d'}, [])),
            (u'd = , e = "a,b,c,d"', ([], {u'd': None, u'e': u'a,b,c,d'}, [])),
            (u'd = "", e = "a,b,c,d"', ([], {u'd': u'', u'e': u'a,b,c,d'}, [])),
            (u'd = "", e = ', ([], {u'd': u'', u'e': None}, [])),
            (u'd=""', ([], {u'd': u''}, [])),
            (u'd = "", e = ""', ([], {u'd': u'', u'e': u''}, [])),
            # no, None as key isn't accepted
            (u' = "",  e = ""', ([], {u'': u'', u'e': u''}, [])),
            # can quote both name and value:
            (u'd = d," e "= "a,b,c,d"', ([], {u'd': u'd', u' e ': u'a,b,c,d'}, [])),
            # trailing args
            (u'1,2,a=b,3,4', ([u'1', u'2'], {u'a': u'b'}, [u'3', u'4'])),
            # can quote quotes:
            (u'd = """d"', ([], {u'd': u'"d'}, [])),
            (u'd = """d"""', ([], {u'd': u'"d"'}, [])),
            (u'd = "d"" ", e=7', ([], {u'd': u'd" ', u'e': u'7'}, [])),
            (u'd = "d""", e=8', ([], {u'd': u'd"', u'e': u'8'}, [])),
        ]
        for args, expected in tests:
            result = paramparser.parse_quoted_separated(args)
            assert expected == result
            for val in result[0]:
                assert val is None or isinstance(val, unicode)
            for val in result[1].keys():
                assert val is None or isinstance(val, unicode)
            for val in result[1].values():
                assert val is None or isinstance(val, unicode)
            for val in result[2]:
                assert val is None or isinstance(val, unicode)

    def testLimited(self):
        tests = [
            # regular and quoting tests
            (u'd = 4,c=3,b=2,a= 1 ', ([], {u'd': u'4', u'c': u'3,b=2,a= 1'}, [])),
            (u'a,b,c,d', ([u'a', u'b,c,d'], {}, [])),
            (u'a=b,b,c,d', ([], {u'a': u'b'}, [u'b,c,d'])),
        ]
        for args, expected in tests:
            result = paramparser.parse_quoted_separated(args, seplimit=1)
            assert expected == result
            for val in result[0]:
                assert val is None or isinstance(val, unicode)
            for val in result[1].keys():
                assert val is None or isinstance(val, unicode)
            for val in result[1].values():
                assert val is None or isinstance(val, unicode)
            for val in result[2]:
                assert val is None or isinstance(val, unicode)

    def testDoubleNameValueSeparator(self):
        tests = [
            # regular and quoting tests
            (u'd==4,=3 ', ([], {u'd': u'=4', u'': u'3'}, [])),
            (u'===a,b,c,d', ([], {u'': u'==a'}, [u'b', u'c', u'd'])),
            (u'a,b,===,c,d', ([u'a', u'b'], {u'': u'=='}, [u'c', u'd'])),
        ]

        def _check(a, e):
            r = paramparser.parse_quoted_separated(a)
            assert r == e

        for args, expected in tests:
            yield _check, args, expected

    def testNoNameValue(self):
        abcd = [u'a', u'b', u'c', u'd']
        tests = [
            # regular and quoting tests
            (u'd = 4,c=3,b=2,a= 1 ', [u'd = 4', u'c=3', u'b=2', u'a= 1']),
            (u'a,b,c,d', abcd),
            (u' a , b , c , d ', abcd),
            (u'   a   ', [u'a']),
            (u'"  a  "', [u'  a  ']),
            (u'a,b,c,d, "a,b,c,d"', abcd + [u'a,b,c,d']),
            (u'quote " :), b', [u'quote " :)', u'b']),
            (u'"quote "" :)", b', [u'quote " :)', u'b']),
            (u'"unended quote', [u'"unended quote']),
            (u'"', [u'"']),
            (u'd=d,e="a,b,c,d"', [u'd=d', u'e="a', u'b', u'c', u'd"']),
        ]
        for args, expected in tests:
            result = paramparser.parse_quoted_separated(args, name_value=False)
            assert expected == result
            for val in result:
                assert val is None or isinstance(val, unicode)

    def testUnitArgument(self):
        result = paramparser.UnitArgument('7mm', float, ['%', 'mm'])
        assert result.get_default() == (7.0, 'mm')
        assert result.parse_argument('8%') == (8.0, '%')
        pytest.raises(ValueError, result.parse_argument, u'7m')
        pytest.raises(ValueError, result.parse_argument, u'7')
        pytest.raises(ValueError, result.parse_argument, u'mm')

    def testExtendedParser(self):
        tests = [
            (u'"a", "b", "c"', u',', None, [u'a', u'b', u'c']),
            (u'a:b, b:c, c:d', u',', u':', [(u'a', u'b'), (u'b', u'c'), (u'c', u'd')]),
            (u'a:b, b:c, c:d', u',', None, [u'a:b', u'b:c', u'c:d']),
            (u'a=b, b=c, c=d', u',', None, [u'a=b', u'b=c', u'c=d']),
            (u'a=b, b=c, c=d', u',', u'=', [(u'a', u'b'), (u'b', u'c'), (u'c', u'd')]),
            (u'"a"; "b"; "c"', u';', None, [u'a', u'b', u'c']),
            (u'a:b; b:c; c:d', u';', u':', [(u'a', u'b'), (u'b', u'c'), (u'c', u'd')]),
            (u'a:b; b:c; c:d', u';', None, [u'a:b', u'b:c', u'c:d']),
            (u'a=b; b=c; c=d', u';', None, [u'a=b', u'b=c', u'c=d']),
            (u'a=b; b=c; c=d', u';', u'=', [(u'a', u'b'), (u'b', u'c'), (u'c', u'd')]),
            (u'"a" "b" "c"', None, None, [u'a', u'b', u'c']),
            (u'" a " "b" "c"', None, None, [u' a ', u'b', u'c']),
            (u'"a  " "b" "c"', None, None, [u'a  ', u'b', u'c']),
            (u'"  a" "b" "c"', None, None, [u'  a', u'b', u'c']),
            (u'"  a" "b" "c"', None, u':', [u'  a', u'b', u'c']),
            (u'"a:a" "b:b" "c:b"', None, u':', [u'a:a', u'b:b', u'c:b']),
            (u'   a:a  ', None, u':', [None, None, None, (u'a', u'a'), None, None]),
            (u'a a: a', None, u':', [u'a', (u'a', None), u'a']),
            (u'a a:"b c d" a', None, u':', [u'a', (u'a', u'b c d'), u'a']),
            (u'a a:"b "" d" a', None, u':', [u'a', (u'a', u'b " d'), u'a']),
            (u'title:Help* dog cat', None, u':', [(u'title', u'Help*'), u'dog', u'cat']),
            (u'title:Help* "dog cat"', None, u':', [(u'title', u'Help*'), u'dog cat']),
            (u'a:b:c d:e:f', None, u':', [(u'a', u'b:c'), (u'd', 'e:f')]),
            (u'a:b:c:d', None, u':', [(u'a', u'b:c:d')]),
        ]

        def _check(args, sep, kwsep, expected):
            res = paramparser.parse_quoted_separated_ext(args, sep, kwsep)
            assert res == expected

        for test in tests:
            yield [_check] + list(test)

    def testExtendedParserBracketing(self):
        tests = [
            (u'"a", "b", "c"', u',', None, [u'a', u'b', u'c']),
            (u'("a", "b", "c")', u',', None, [[u'(', u'a', u'b', u'c']]),
            (u'("a"("b", "c"))', u',', None, [[u'(', u'a', [u'(', u'b', u'c']]]),
            (u'("a"("b)))", "c"))', u',', None, [[u'(', u'a', [u'(', u'b)))', u'c']]]),
            (u'("a"("b>>> ( ab )>", "c"))', u',', None, [[u'(', u'a', [u'(', u'b>>> ( ab )>', u'c']]]),
            (u'("a" ("b" "c"))', None, None, [[u'(', u'a', [u'(', u'b', u'c']]]),
            (u'("a"("b", "c") ) ', u',', None, [[u'(', u'a', [u'(', u'b', u'c']]]),
            (u'("a", <"b", ("c")>)', u',', None, [[u'(', u'a', [u'<', u'b', [u'(', u'c']]]]),
            (u',,,(a, b, c)', u',', None, [None, None, None, [u'(', u'a', u'b', u'c']]),
        ]

        def _check(args, sep, kwsep, expected):
            res = paramparser.parse_quoted_separated_ext(args, sep, kwsep, brackets=(u'<>', u'()'))
            assert res == expected

        for test in tests:
            yield [_check] + list(test)

    def testExtendedParserQuoting(self):
        tests = [
            (u'"a b" -a b-', u'"', [u'a b', u'-a', u'b-']),
            (u'"a b" -a b-', u"-", [u'"a', u'b"', u'a b']),
            (u'"a b" -a b-', u'"-', [u'a b', u'a b']),
            (u'"a- b" -a b-', u'"-', [u'a- b', u'a b']),
            (u'"a- b" -a" b-', u'"-', [u'a- b', u'a" b']),
        ]

        def _check(args, quotes, expected):
            res = paramparser.parse_quoted_separated_ext(args, quotes=quotes)
            assert res == expected

        for test in tests:
            yield [_check] + list(test)

    def testExtendedParserMultikey(self):
        tests = [
            (u'"a", "b", "c"', u',', None, [u'a', u'b', u'c']),
            (u'a:b, b:c, c:d', u',', u':', [(u'a', u'b'), (u'b', u'c'), (u'c', u'd')]),
            (u'a:b, b:c, c:d', u',', None, [u'a:b', u'b:c', u'c:d']),
            (u'a=b, b=c, c=d', u',', None, [u'a=b', u'b=c', u'c=d']),
            (u'a=b, b=c, c=d', u',', u'=', [(u'a', u'b'), (u'b', u'c'), (u'c', u'd')]),
            (u'"a"; "b"; "c"', u';', None, [u'a', u'b', u'c']),
            (u'a:b; b:c; c:d', u';', u':', [(u'a', u'b'), (u'b', u'c'), (u'c', u'd')]),
            (u'a:b; b:c; c:d', u';', None, [u'a:b', u'b:c', u'c:d']),
            (u'a=b; b=c; c=d', u';', None, [u'a=b', u'b=c', u'c=d']),
            (u'a=b; b=c; c=d', u';', u'=', [(u'a', u'b'), (u'b', u'c'), (u'c', u'd')]),
            (u'"a" "b" "c"', None, None, [u'a', u'b', u'c']),
            (u'" a " "b" "c"', None, None, [u' a ', u'b', u'c']),
            (u'"a  " "b" "c"', None, None, [u'a  ', u'b', u'c']),
            (u'"  a" "b" "c"', None, None, [u'  a', u'b', u'c']),
            (u'"  a" "b" "c"', None, u':', [u'  a', u'b', u'c']),
            (u'"a:a" "b:b" "c:b"', None, u':', [u'a:a', u'b:b', u'c:b']),
            (u'   a:a  ', None, u':', [None, None, None, (u'a', u'a'), None, None]),
            (u'a a: a', None, u':', [u'a', (u'a', None), u'a']),
            (u'a a:"b c d" a', None, u':', [u'a', (u'a', u'b c d'), u'a']),
            (u'a a:"b "" d" a', None, u':', [u'a', (u'a', u'b " d'), u'a']),
            (u'title:Help* dog cat', None, u':', [(u'title', u'Help*'), u'dog', u'cat']),
            (u'title:Help* "dog cat"', None, u':', [(u'title', u'Help*'), u'dog cat']),
            (u'a:b:c d:e:f', None, u':', [(u'a', u'b', u'c'), (u'd', 'e', u'f')]),
            (u'a:b:c:d', None, u':', [(u'a', u'b', u'c', u'd')]),
            (u'a:"b:c":d', None, u':', [(u'a', u'b:c', u'd')]),
        ]

        def _check(args, sep, kwsep, expected):
            res = paramparser.parse_quoted_separated_ext(args, sep, kwsep, multikey=True)
            assert res == expected

        for test in tests:
            yield [_check] + list(test)

    def testExtendedParserPrefix(self):
        P = paramparser.ParserPrefix('+')
        M = paramparser.ParserPrefix('-')
        tests = [
            (u'"a", "b", "c"', u',', None, [u'a', u'b', u'c']),
            (u'a:b, b:c, c:d', u',', u':', [(u'a', u'b'), (u'b', u'c'), (u'c', u'd')]),
            (u'a:b, b:c, c:d', u',', None, [u'a:b', u'b:c', u'c:d']),
            (u'a=b, b=c, c=d', u',', None, [u'a=b', u'b=c', u'c=d']),
            (u'a=b, b=c, c=d', u',', u'=', [(u'a', u'b'), (u'b', u'c'), (u'c', u'd')]),
            (u'"a"; "b"; "c"', u';', None, [u'a', u'b', u'c']),
            (u'a:b; b:c; c:d', u';', u':', [(u'a', u'b'), (u'b', u'c'), (u'c', u'd')]),
            (u'a:b; b:c; c:d', u';', None, [u'a:b', u'b:c', u'c:d']),
            (u'a=b; b=c; c=d', u';', None, [u'a=b', u'b=c', u'c=d']),
            (u'a=b; b=c; c=d', u';', u'=', [(u'a', u'b'), (u'b', u'c'), (u'c', u'd')]),
            (u'"a" "b" "c"', None, None, [u'a', u'b', u'c']),
            (u'" a " "b" "c"', None, None, [u' a ', u'b', u'c']),
            (u'"a  " "b" "c"', None, None, [u'a  ', u'b', u'c']),
            (u'"  a" "b" "c"', None, None, [u'  a', u'b', u'c']),
            (u'"  a" "b" "c"', None, u':', [u'  a', u'b', u'c']),
            (u'"a:a" "b:b" "c:b"', None, u':', [u'a:a', u'b:b', u'c:b']),
            (u'   a:a  ', None, u':', [None, None, None, (u'a', u'a'), None, None]),
            (u'a a: a', None, u':', [u'a', (u'a', None), u'a']),
            (u'a a:"b c d" a', None, u':', [u'a', (u'a', u'b c d'), u'a']),
            (u'a a:"b "" d" a', None, u':', [u'a', (u'a', u'b " d'), u'a']),
            (u'title:Help* dog cat', None, u':', [(u'title', u'Help*'), u'dog', u'cat']),
            (u'title:Help* "dog cat"', None, u':', [(u'title', u'Help*'), u'dog cat']),
            (u'a:b:c d:e:f', None, u':', [(u'a', u'b', u'c'), (u'd', 'e', u'f')]),
            (u'a:b:c:d', None, u':', [(u'a', u'b', u'c', u'd')]),
            (u'a:"b:c":d', None, u':', [(u'a', u'b:c', u'd')]),
            (u'-a:b:d', None, u':', [(M, u'a', u'b', u'd')]),
            (u'"-a:b:d"', None, u':', [(u'-a:b:d')]),
            (u'-"a:b:d"', None, u':', [(M, u'a:b:d')]),
            (u'-a:"b:c":"d e f g"', None, u':', [(M, u'a', u'b:c', u'd e f g')]),
            (u'+-a:b:d', None, u':', [(P, u'-a', u'b', u'd')]),
            (u'-"+a:b:d"', None, u':', [(M, u'+a:b:d')]),
            # bit of a weird case...
            (u'-+"a:b:d"', None, u':', [(M, u'+"a', u'b', u'd"')]),
            (u'-a:"b:c" a +b', None, u':', [(M, u'a', u'b:c'), u'a', (P, u'b')]),
        ]

        def _check(args, sep, kwsep, expected):
            res = paramparser.parse_quoted_separated_ext(args, sep, kwsep, multikey=True, prefixes='-+')
            assert res == expected

        for test in tests:
            yield [_check] + list(test)

    def testExtendedParserBracketingErrors(self):
        UCE = paramparser.BracketUnexpectedCloseError
        MCE = paramparser.BracketMissingCloseError
        tests = [
            (u'("a", "b", "c"', u',', None, MCE),
            (u'("a"("b", "c")', u',', None, MCE),
            (u'("a"<"b", "c")>', u',', None, UCE),
            (u')("a" ("b" "c"))', None, None, UCE),
            (u'("a", ("b", "c">))', u',', None, UCE),
            (u'("a", ("b", <"c">>))', u',', None, UCE),
            (u'(<(<)>)>', u',', None, UCE),
        ]

        def _check(args, sep, kwsep, err):
            pytest.raises(err,
                          paramparser.parse_quoted_separated_ext,
                          args, sep, kwsep,
                          brackets=(u'<>', u'()'))

        for test in tests:
            yield [_check] + list(test)


class TestArgGetters(object):
    def testGetBoolean(self):
        tests = [
            # default testing for None value
            (None, None, None, None),
            (None, None, False, False),
            (None, None, True, True),

            # some real values
            (u'0', None, None, False),
            (u'1', None, None, True),
            (u'false', None, None, False),
            (u'true', None, None, True),
            (u'FALSE', None, None, False),
            (u'TRUE', None, None, True),
            (u'no', None, None, False),
            (u'yes', None, None, True),
            (u'NO', None, None, False),
            (u'YES', None, None, True),
        ]
        for arg, name, default, expected in tests:
            assert paramparser.get_bool(arg, name, default) == expected

    def testGetBooleanRaising(self):
        # wrong default type
        pytest.raises(AssertionError, paramparser.get_bool, None, None, 42)

        # anything except None or unicode raises TypeError
        pytest.raises(TypeError, paramparser.get_bool, True)
        pytest.raises(TypeError, paramparser.get_bool, 42)
        pytest.raises(TypeError, paramparser.get_bool, 42.0)
        pytest.raises(TypeError, paramparser.get_bool, '')
        pytest.raises(TypeError, paramparser.get_bool, tuple())
        pytest.raises(TypeError, paramparser.get_bool, [])
        pytest.raises(TypeError, paramparser.get_bool, {})

        # any value not convertable to boolean raises ValueError
        pytest.raises(ValueError, paramparser.get_bool, u'')
        pytest.raises(ValueError, paramparser.get_bool, u'42')
        pytest.raises(ValueError, paramparser.get_bool, u'wrong')
        pytest.raises(ValueError, paramparser.get_bool, u'"True"')  # must not be quoted!

    def testGetInt(self):
        tests = [
            # default testing for None value
            (None, None, None, None),
            (None, None, -23, -23),
            (None, None, 42, 42),

            # some real values
            (u'0', None, None, 0),
            (u'42', None, None, 42),
            (u'-23', None, None, -23),
        ]
        for arg, name, default, expected in tests:
            assert paramparser.get_int(arg, name, default) == expected

    def testGetIntRaising(self):
        # wrong default type
        pytest.raises(AssertionError, paramparser.get_int, None, None, 42.23)

        # anything except None or unicode raises TypeError
        pytest.raises(TypeError, paramparser.get_int, True)
        pytest.raises(TypeError, paramparser.get_int, 42)
        pytest.raises(TypeError, paramparser.get_int, 42.0)
        pytest.raises(TypeError, paramparser.get_int, '')
        pytest.raises(TypeError, paramparser.get_int, tuple())
        pytest.raises(TypeError, paramparser.get_int, [])
        pytest.raises(TypeError, paramparser.get_int, {})

        # any value not convertable to int raises ValueError
        pytest.raises(ValueError, paramparser.get_int, u'')
        pytest.raises(ValueError, paramparser.get_int, u'23.42')
        pytest.raises(ValueError, paramparser.get_int, u'wrong')
        pytest.raises(ValueError, paramparser.get_int, u'"4711"')  # must not be quoted!

    def testGetFloat(self):
        tests = [
            # default testing for None value
            (None, None, None, None),
            (None, None, -23.42, -23.42),
            (None, None, 42.23, 42.23),

            # some real values
            (u'0', None, None, 0),
            (u'42.23', None, None, 42.23),
            (u'-23.42', None, None, -23.42),
            (u'-23.42E3', None, None, -23.42E3),
            (u'23.42E-3', None, None, 23.42E-3),
        ]
        for arg, name, default, expected in tests:
            assert paramparser.get_float(arg, name, default) == expected

    def testGetFloatRaising(self):
        # wrong default type
        pytest.raises(AssertionError, paramparser.get_float, None, None, u'42')

        # anything except None or unicode raises TypeError
        pytest.raises(TypeError, paramparser.get_float, True)
        pytest.raises(TypeError, paramparser.get_float, 42)
        pytest.raises(TypeError, paramparser.get_float, 42.0)
        pytest.raises(TypeError, paramparser.get_float, '')
        pytest.raises(TypeError, paramparser.get_float, tuple())
        pytest.raises(TypeError, paramparser.get_float, [])
        pytest.raises(TypeError, paramparser.get_float, {})

        # any value not convertable to int raises ValueError
        pytest.raises(ValueError, paramparser.get_float, u'')
        pytest.raises(ValueError, paramparser.get_float, u'wrong')
        pytest.raises(ValueError, paramparser.get_float, u'"47.11"')  # must not be quoted!

    def testGetComplex(self):
        tests = [
            # default testing for None value
            (None, None, None, None),
            (None, None, -23.42, -23.42),
            (None, None, 42.23, 42.23),

            # some real values
            (u'0', None, None, 0),
            (u'42.23', None, None, 42.23),
            (u'-23.42', None, None, -23.42),
            (u'-23.42E3', None, None, -23.42E3),
            (u'23.42E-3', None, None, 23.42E-3),
            (u'23.42E-3+3.04j', None, None, 23.42E-3 + 3.04j),
            (u'3.04j', None, None, 3.04j),
            (u'-3.04j', None, None, -3.04j),
            (u'23.42E-3+3.04i', None, None, 23.42E-3 + 3.04j),
            (u'3.04i', None, None, 3.04j),
            (u'-3.04i', None, None, -3.04j),
            (u'-3', None, None, -3L),
            (u'-300000000000000000000', None, None, -300000000000000000000L),
        ]
        for arg, name, default, expected in tests:
            assert paramparser.get_complex(arg, name, default) == expected

    def testGetComplexRaising(self):
        # wrong default type
        pytest.raises(AssertionError, paramparser.get_complex, None, None, u'42')

        # anything except None or unicode raises TypeError
        pytest.raises(TypeError, paramparser.get_complex, True)
        pytest.raises(TypeError, paramparser.get_complex, 42)
        pytest.raises(TypeError, paramparser.get_complex, 42.0)
        pytest.raises(TypeError, paramparser.get_complex, 3j)
        pytest.raises(TypeError, paramparser.get_complex, '')
        pytest.raises(TypeError, paramparser.get_complex, tuple())
        pytest.raises(TypeError, paramparser.get_complex, [])
        pytest.raises(TypeError, paramparser.get_complex, {})

        # any value not convertable to int raises ValueError
        pytest.raises(ValueError, paramparser.get_complex, u'')
        pytest.raises(ValueError, paramparser.get_complex, u'3jj')
        pytest.raises(ValueError, paramparser.get_complex, u'3Ij')
        pytest.raises(ValueError, paramparser.get_complex, u'3i-3i')
        pytest.raises(ValueError, paramparser.get_complex, u'wrong')
        pytest.raises(ValueError, paramparser.get_complex, u'"47.11"')  # must not be quoted!

    def testGetUnicode(self):
        tests = [
            # default testing for None value
            (None, None, None, None),
            (None, None, u'', u''),
            (None, None, u'abc', u'abc'),

            # some real values
            (u'', None, None, u''),
            (u'abc', None, None, u'abc'),
            (u'"abc"', None, None, u'"abc"'),
        ]
        for arg, name, default, expected in tests:
            assert paramparser.get_unicode(arg, name, default) == expected

    def testGetUnicodeRaising(self):
        # wrong default type
        pytest.raises(AssertionError, paramparser.get_unicode, None, None, 42)

        # anything except None or unicode raises TypeError
        pytest.raises(TypeError, paramparser.get_unicode, True)
        pytest.raises(TypeError, paramparser.get_unicode, 42)
        pytest.raises(TypeError, paramparser.get_unicode, 42.0)
        pytest.raises(TypeError, paramparser.get_unicode, '')
        pytest.raises(TypeError, paramparser.get_unicode, tuple())
        pytest.raises(TypeError, paramparser.get_unicode, [])
        pytest.raises(TypeError, paramparser.get_unicode, {})


class TestExtensionInvoking(object):
    def _test_invoke_bool(self, b=bool):
        assert b is False

    def _test_invoke_bool_def(self, v=bool, b=False):
        assert b == v
        assert isinstance(b, bool)
        assert isinstance(v, bool)

    def _test_invoke_int_None(self, i=int):
        assert i == 1 or i is None

    def _test_invoke_float_None(self, i=float):
        assert i == 1.4 or i is None

    def _test_invoke_float_required(self, i=paramparser.required_arg(float)):
        assert i == 1.4

    def _test_invoke_choice(self, a, choice=[u'a', u'b', u'c']):
        assert a == 7
        assert choice == u'a'

    def _test_invoke_choicet(self, a, choice=(u'a', u'b', u'c')):
        assert a == 7
        assert choice == u'a'

    def _test_invoke_choice_required(self, i=paramparser.required_arg((u'b', u'a'))):
        assert i == u'a'

    def _test_trailing(self, a, _trailing_args=[]):
        assert _trailing_args == [u'a']

    def _test_arbitrary_kw(self, expect, _kwargs={}):
        assert _kwargs == expect

    def testInvoke(self):
        def _test_invoke_int(i=int):
            assert i == 1

        def _test_invoke_int_fixed(a, b, i=int):
            assert a == 7
            assert b == 8
            assert i == 1 or i is None

        ief = paramparser.invoke_extension_function
        ief(self._test_invoke_bool, u'False')
        ief(self._test_invoke_bool, u'b=False')
        ief(_test_invoke_int, u'1')
        ief(_test_invoke_int, u'i=1')
        ief(self._test_invoke_bool_def, u'False, False')
        ief(self._test_invoke_bool_def, u'b=False, v=False')
        ief(self._test_invoke_bool_def, u'False')
        ief(self._test_invoke_int_None, u'i=1')
        ief(self._test_invoke_int_None, u'i=')
        ief(self._test_invoke_int_None, u'')
        pytest.raises(ValueError, ief,
                      self._test_invoke_int_None, u'x')
        pytest.raises(ValueError, ief,
                      self._test_invoke_int_None, u'""')
        pytest.raises(ValueError, ief,
                      self._test_invoke_int_None, u'i=""')
        pytest.raises(ValueError, ief,
                      _test_invoke_int_fixed, u'a=7', [7, 8])
        ief(_test_invoke_int_fixed, u'i=1', [7, 8])
        pytest.raises(ValueError, ief,
                      _test_invoke_int_fixed, u'i=""', [7, 8])
        ief(_test_invoke_int_fixed, u'i=', [7, 8])

        for choicefn in (self._test_invoke_choice, self._test_invoke_choicet):
            ief(choicefn, u'', [7])
            ief(choicefn, u'choice=a', [7])
            ief(choicefn, u'choice=', [7])
            ief(choicefn, u'choice="a"', [7])
            pytest.raises(ValueError, ief,
                          choicefn, u'x', [7])
            pytest.raises(ValueError, ief,
                          choicefn, u'choice=x', [7])

        ief(self._test_invoke_float_None, u'i=1.4')
        ief(self._test_invoke_float_None, u'i=')
        ief(self._test_invoke_float_None, u'')
        ief(self._test_invoke_float_None, u'1.4')
        pytest.raises(ValueError, ief,
                      self._test_invoke_float_None, u'x')
        pytest.raises(ValueError, ief,
                      self._test_invoke_float_None, u'""')
        pytest.raises(ValueError, ief,
                      self._test_invoke_float_None, u'i=""')
        ief(self._test_trailing, u'a=7, a')
        ief(self._test_trailing, u'7, a')
        ief(self._test_arbitrary_kw, u'test=x, \xc3=test',
            [{u'\xc3': 'test', 'test': u'x'}])
        ief(self._test_arbitrary_kw, u'test=x, "\xc3"=test',
            [{u'\xc3': 'test', 'test': u'x'}])
        ief(self._test_arbitrary_kw, u'test=x, "7 \xc3"=test',
            [{u'7 \xc3': 'test', 'test': u'x'}])
        ief(self._test_arbitrary_kw, u'test=x, 7 \xc3=test',
            [{u'7 \xc3': 'test', 'test': u'x'}])
        ief(self._test_arbitrary_kw, u'7 \xc3=test, test= x ',
            [{u'7 \xc3': 'test', 'test': u'x'}])
        pytest.raises(ValueError, ief,
                      self._test_invoke_float_required, u'')
        ief(self._test_invoke_float_required, u'1.4')
        ief(self._test_invoke_float_required, u'i=1.4')
        pytest.raises(ValueError, ief,
                      self._test_invoke_choice_required, u'')
        ief(self._test_invoke_choice_required, u'a')
        ief(self._test_invoke_choice_required, u'i=a')
        pytest.raises(ValueError, ief,
                      self._test_invoke_float_required, u',')

    def testConstructors(self):
        ief = paramparser.invoke_extension_function

        # new style class
        class TEST1(object):
            def __init__(self, a=int):
                self.constructed = True
                assert a == 7

        class TEST2(TEST1):
            pass

        obj = ief(TEST1, u'a=7')
        assert isinstance(obj, TEST1)
        assert obj.constructed
        pytest.raises(ValueError, ief, TEST1, u'b')

        obj = ief(TEST2, u'a=7')
        assert isinstance(obj, TEST1)
        assert isinstance(obj, TEST2)
        assert obj.constructed
        pytest.raises(ValueError, ief, TEST2, u'b')

        # old style class
        class TEST3:
            def __init__(self, a=int):
                self.constructed = True
                assert a == 7

        class TEST4(TEST3):
            pass

        obj = ief(TEST3, u'a=7')
        assert isinstance(obj, TEST3)
        assert obj.constructed
        pytest.raises(ValueError, ief, TEST3, u'b')

        obj = ief(TEST4, u'a=7')
        assert isinstance(obj, TEST3)
        assert isinstance(obj, TEST4)
        assert obj.constructed
        pytest.raises(ValueError, ief, TEST4, u'b')

    def testFailing(self):
        ief = paramparser.invoke_extension_function

        pytest.raises(TypeError, ief, hex, u'15')
        pytest.raises(TypeError, ief, cmp, u'15')
        pytest.raises(AttributeError, ief, unicode, u'15')

    def testAllDefault(self):
        ief = paramparser.invoke_extension_function

        def has_many_defaults(a=1, b=2, c=3, d=4):
            assert a == 1
            assert b == 2
            assert c == 3
            assert d == 4
            return True

        assert ief(has_many_defaults, u'1, 2, 3, 4')
        assert ief(has_many_defaults, u'2, 3, 4', [1])
        assert ief(has_many_defaults, u'3, 4', [1, 2])
        assert ief(has_many_defaults, u'4', [1, 2, 3])
        assert ief(has_many_defaults, u'', [1, 2, 3, 4])
        assert ief(has_many_defaults, u'd=4,c=3,b=2,a=1')
        assert ief(has_many_defaults, u'd=4,c=3,b=2', [1])
        assert ief(has_many_defaults, u'd=4,c=3', [1, 2])
        assert ief(has_many_defaults, u'd=4', [1, 2, 3])

    def testInvokeComplex(self):
        ief = paramparser.invoke_extension_function

        def has_complex(a=complex, b=complex):
            assert a == b
            return True

        assert ief(has_complex, u'3-3i, 3-3j')
        assert ief(has_complex, u'2i, 2j')
        assert ief(has_complex, u'b=2i, a=2j')
        assert ief(has_complex, u'2.007, 2.007')
        assert ief(has_complex, u'2.007', [2.007])
        assert ief(has_complex, u'b=2.007', [2.007])
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.