aboutsummaryrefslogtreecommitdiffstats
path: root/docs/sphinx/hieroglyph/test/test_comments.py
blob: d1a1453ee1dc18f7e0a2044e76661d971873f72d (plain) (blame)
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
import unittest

from hieroglyph.hieroglyph import parse_hieroglyph_text
from hieroglyph.errors import HieroglyphError

class CommentTests(unittest.TestCase):

    def test_comment1(self):
        source = """Fetches rows from a Bigtable.
        This is a continuation of the opening paragraph.

        Retrieves rows pertaining to the given keys from the Table instance
        represented by big_table.  Silly things may happen if
        other_silly_variable is not None.

        Args:
            big_table: An open Bigtable Table instance.
            keys: A sequence of strings representing the key of each table row
                to fetch.
            other_silly_variable (str): Another optional variable, that has a much
                longer name than the other args, and which does nothing.

        Returns:
            A dict mapping keys to the corresponding table row data
            fetched. Each row is represented as a tuple of strings. For
            example:

              {'Serak': ('Rigel VII', 'Preparer'),
               'Zim': ('Irk', 'Invader'),
               'Lrrr': ('Omicron Persei 8', 'Emperor')}

            If a key from the keys argument is missing from the dictionary,
            then that row was not found in the table.

        Raises:
            IOError: An error occurred accessing the bigtable.Table object.
        """

        expected = """        Fetches rows from a Bigtable.
        This is a continuation of the opening paragraph.

        Retrieves rows pertaining to the given keys from the Table instance
        represented by big_table.  Silly things may happen if
        other_silly_variable is not None.

        :param big_table: An open Bigtable Table instance.

        :param keys: A sequence of strings representing the key of each table row
            to fetch.

        :param other_silly_variable: Another optional variable, that has a much
            longer name than the other args, and which does nothing.

        :type other_silly_variable: str

        :returns: A dict mapping keys to the corresponding table row data
            fetched. Each row is represented as a tuple of strings. For
            example:

              {'Serak': ('Rigel VII', 'Preparer'),
               'Zim': ('Irk', 'Invader'),
               'Lrrr': ('Omicron Persei 8', 'Emperor')}

            If a key from the keys argument is missing from the dictionary,
            then that row was not found in the table.

        :raises:
            IOError - An error occurred accessing the bigtable.Table object.
        """
        source_lines = source.splitlines()
        actual_lines = parse_hieroglyph_text(source_lines)
        expected_lines = expected.splitlines()
        self.assertEqual(len(actual_lines), len(expected_lines))
        for actual_line, result_line in zip(actual_lines, expected_lines):
            if len(actual_line.strip()) == 0:
                self.assertTrue(len(result_line.strip()) == 0)
            else:
                self.assertEqual(actual_line, result_line)

    def test_comment2(self):
        source = """Determine if all elements in the source sequence satisfy a condition.

        All of the source sequence will be consumed.

        Note: This method uses immediate execution.

        Args:
            predicate: An optional single argument function used to test each
                elements. If omitted, the bool() function is used resulting in
                the elements being tested directly.

        Returns:
            True if all elements in the sequence meet the predicate condition,
            otherwise False.

        Raises:
            ValueError: If the Queryable is closed()
            TypeError: If predicate is not callable.
        """

        expected = """Determine if all elements in the source sequence satisfy a condition.

        All of the source sequence will be consumed.

        .. note::

            This method uses immediate execution.

        :param predicate: An optional single argument function used to test each
            elements. If omitted, the bool() function is used resulting in
            the elements being tested directly.

        :returns: True if all elements in the sequence meet the predicate condition,
            otherwise False.

        :raises:
            * ValueError - If the Queryable is closed()

            * TypeError - If predicate is not callable.
        """
        source_lines = source.splitlines()
        actual_lines = parse_hieroglyph_text(source_lines)
        expected_lines = expected.splitlines()
        self.assertEqual(len(actual_lines), len(expected_lines))
        for actual_line, result_line in zip(actual_lines, expected_lines):
            if len(actual_line.strip()) == 0:
                self.assertTrue(len(result_line.strip()) == 0)
            else:
                self.assertEqual(actual_line, result_line)

    def test_comment3(self):
        source = """Determine if all elements in the source sequence satisfy a condition.

        All of the source sequence will be consumed.

        Note: This method uses immediate execution.

        Args:
            predicate: An optional single argument function used to test each
                elements. If omitted, the bool() function is used resulting in
                the elements being tested directly.

        Returns:
            True if all elements in the sequence meet the predicate condition,
            otherwise False.

        Raises:
            ValueError: If the Queryable is closed()
            TypeError: If predicate is not callable.
        """

        expected = """Determine if all elements in the source sequence satisfy a condition.

        All of the source sequence will be consumed.

        .. note::

            This method uses immediate execution.

        :param predicate: An optional single argument function used to test each
            elements. If omitted, the bool() function is used resulting in
            the elements being tested directly.

        :returns: True if all elements in the sequence meet the predicate condition,
            otherwise False.

        :raises:
            * ValueError - If the Queryable is closed()

            * TypeError - If predicate is not callable.
        """
        source_lines = source.splitlines()
        actual_lines = parse_hieroglyph_text(source_lines)
        expected_lines = expected.splitlines()
        self.assertEqual(len(actual_lines), len(expected_lines))
        for actual_line, result_line in zip(actual_lines, expected_lines):
            if len(actual_line.strip()) == 0:
                self.assertTrue(len(result_line.strip()) == 0)
            else:
                self.assertEqual(actual_line, result_line)

    def test_comment4(self):
        source_lines = [u'Determine if all elements in the source sequence satisfy a condition.',
                        u'',
                        u'All of the source sequence will be consumed.',
                        u'',
                        u'Note: This method uses immediate execution.',
                        u'',
                        u'Args:',
                        u'    predicate: An optional single argument function used to test each',
                        u'        elements. If omitted, the bool() function is used resulting in',
                        u'        the elements being tested directly.',
                        u'',
                        u'Returns:',
                        u'    True if all elements in the sequence meet the predicate condition,',
                        u'    otherwise False.',
                        u'',
                        u'Raises:',
                        u'    ValueError: If the Queryable is closed()',
                        u'    TypeError: If predicate is not callable.',
                        u'']

        expected = """Determine if all elements in the source sequence satisfy a condition.

All of the source sequence will be consumed.

.. note::

    This method uses immediate execution.

:param predicate: An optional single argument function used to test each
    elements. If omitted, the bool() function is used resulting in
    the elements being tested directly.

:returns: True if all elements in the sequence meet the predicate condition,
    otherwise False.

:raises:
    * ValueError - If the Queryable is closed()

    * TypeError - If predicate is not callable.

"""
        actual_lines = parse_hieroglyph_text(source_lines)
        expected_lines = expected.splitlines()
        self.assertEqual(len(actual_lines), len(expected_lines))
        for actual_line, result_line in zip(actual_lines, expected_lines):
            if len(actual_line.strip()) == 0:
                self.assertTrue(len(result_line.strip()) == 0)
            else:
                self.assertEqual(actual_line, result_line)

    def test_comment5(self):
        source_lines = [u'An empty Queryable.',
                        u'',
                        u'Note: The same empty instance will be returned each time.',
                        u'',
                        u'Returns: A Queryable over an empty sequence.',
                        u'']

        expected = """An empty Queryable.

.. note::

    The same empty instance will be returned each time.

:returns: A Queryable over an empty sequence.

"""
        actual_lines = parse_hieroglyph_text(source_lines)
        expected_lines = expected.splitlines()
        self.assertEqual(len(actual_lines), len(expected_lines))
        for actual_line, result_line in zip(actual_lines, expected_lines):
            if len(actual_line.strip()) == 0:
                self.assertTrue(len(result_line.strip()) == 0)
            else:
                self.assertEqual(actual_line, result_line)

    def test_comment6(self):
        source_lines = [u'A convenience factory for creating Records.',
                        u'',
                        u'Args:',
                        u'    **kwargs: Each keyword argument will be used to initialise an',
                        u'       attribute with the same name as the argument and the given',
                        u'       value.',
                        u'',
                        u'Returns:',
                        u'    A Record which has a named attribute for each of the keyword arguments.',
                        u'']

        expected = """A convenience factory for creating Records.

:param \*\*kwargs: Each keyword argument will be used to initialise an
   attribute with the same name as the argument and the given
   value.

:returns: A Record which has a named attribute for each of the keyword arguments.

"""
        actual_lines = parse_hieroglyph_text(source_lines)
        expected_lines = expected.splitlines()
        self.assertEqual(len(actual_lines), len(expected_lines))
        for actual_line, result_line in zip(actual_lines, expected_lines):
            if len(actual_line.strip()) == 0:
                self.assertTrue(len(result_line.strip()) == 0)
            else:
                self.assertEqual(actual_line, result_line)

    def test_comment7(self):
        source = """Projects each element of a sequence to an intermediate new sequence,
        flattens the resulting sequences into one sequence and optionally
        transforms the flattened sequence using a selector function.

        Note: This method uses deferred execution.

        Args:
            collection_selector: A unary function mapping each element of the
                source iterable into an intermediate sequence. The single
                argument of the collection_selector is the value of an element
                from the source sequence. The return value should be an
                iterable derived from that element value. The default
                collection_selector, which is the identity function, assumes
                that each element of the source sequence is itself iterable.

            result_selector: An optional unary function mapping the elements in
                the flattened intermediate sequence to corresponding elements
                of the result sequence. The single argument of the
                result_selector is the value of an element from the flattened
                intermediate sequence. The return value should be the
                corresponding value in the result sequence. The default
                result_selector is the identity function.

        Returns:
            A Queryable over a generated sequence whose elements are the result
            of applying the one-to-many collection_selector to each element of
            the source sequence, concatenating the results into an intermediate
            sequence, and then mapping each of those elements through the
            result_selector into the result sequence.

        Raises:
            ValueError: If this Queryable has been closed.
            TypeError: If either collection_selector or result_selector are not
                callable.
        """

        expected = """        Projects each element of a sequence to an intermediate new sequence,
        flattens the resulting sequences into one sequence and optionally
        transforms the flattened sequence using a selector function.

        .. note::

            This method uses deferred execution.

        :param collection_selector: A unary function mapping each element of the
            source iterable into an intermediate sequence. The single
            argument of the collection_selector is the value of an element
            from the source sequence. The return value should be an
            iterable derived from that element value. The default
            collection_selector, which is the identity function, assumes
            that each element of the source sequence is itself iterable.

        :param result_selector: An optional unary function mapping the elements in
            the flattened intermediate sequence to corresponding elements
            of the result sequence. The single argument of the
            result_selector is the value of an element from the flattened
            intermediate sequence. The return value should be the
            corresponding value in the result sequence. The default
            result_selector is the identity function.

        :returns: A Queryable over a generated sequence whose elements are the result
            of applying the one-to-many collection_selector to each element of
            the source sequence, concatenating the results into an intermediate
            sequence, and then mapping each of those elements through the
            result_selector into the result sequence.

        :raises:
            * ValueError - If this Queryable has been closed.

            * TypeError - If either collection_selector or result_selector are not
                callable.
        """
        source_lines = source.splitlines()
        actual_lines = parse_hieroglyph_text(source_lines)
        expected_lines = expected.splitlines()
        self.assertEqual(len(actual_lines), len(expected_lines))
        for actual_line, result_line in zip(actual_lines, expected_lines):
            if len(actual_line.strip()) == 0:
                self.assertTrue(len(result_line.strip()) == 0)
            else:
                self.assertEqual(actual_line, result_line)

    def test_comment8(self):
        source = """A convenience factory for creating Records.

        Args:
            **kwargs: Each keyword argument will be used to initialise an
                attribute with the same name as the argument and the given
                value.

        Returns:
            A Record which has a named attribute for each of the keyword arguments.
        """

        expected = """A convenience factory for creating Records.

        :param \*\*kwargs: Each keyword argument will be used to initialise an
            attribute with the same name as the argument and the given
            value.

        :returns: A Record which has a named attribute for each of the keyword arguments.

"""
        source_lines = source.splitlines()
        actual_lines = parse_hieroglyph_text(source_lines)
        expected_lines = expected.splitlines()
        self.assertEqual(len(actual_lines), len(expected_lines))
        for actual_line, result_line in zip(actual_lines, expected_lines):
            if len(actual_line.strip()) == 0:
                self.assertTrue(len(result_line.strip()) == 0)
            else:
                self.assertEqual(actual_line, result_line)

    def test_comment9(self):
        source_lines = [u'Parse a single line of a tree to determine depth and node.',
                        u'',
                        u'Args:',
                        u'    This line is missing an argument name.',
                        u'    ',
                        u'Returns:',
                        u'    A 2-tuple containing the tree 0 based tree depth as the first',
                        u'    element and the node description as the second element.',
                        u'',
                        u'Raises:',
                        u'    ValueError: If line does not have the expected form.',
                        u'']

        self.assertRaises(HieroglyphError, lambda: parse_hieroglyph_text(source_lines))

    def test_comment10(self):
        source = """
        Execute the command described by concatenating the string function arguments
        with the p4 -s global scripting flag and return the results in a dictionary.

        For example, to run the command::

          p4 -s fstat -T depotFile foo.h

        call::

          p4('fstat', '-T', 'depotFile', 'foo.h')

        Args:
            args: The arguments to the p4 command as a list of objects which will
                be converted to strings.

        Returns:
            A dictionary of lists where each key in the dictionary is the field name
            from the command output, and each value is a list of output lines in
            order.

        Raises:
            PerforceError: If the command could not be run or if the command
                reported an error.
        """

        expected = """
        Execute the command described by concatenating the string function arguments
        with the p4 -s global scripting flag and return the results in a dictionary.

        For example, to run the command::

          p4 -s fstat -T depotFile foo.h

        call::

          p4('fstat', '-T', 'depotFile', 'foo.h')

        :param args: The arguments to the p4 command as a list of objects which will
            be converted to strings.

        :returns: A dictionary of lists where each key in the dictionary is the field name
            from the command output, and each value is a list of output lines in
            order.

        :raises:
            PerforceError - If the command could not be run or if the command
                reported an error.

"""

        source_lines = source.splitlines()
        actual_lines = parse_hieroglyph_text(source_lines)
        expected_lines = expected.splitlines()
        self.assertEqual(len(actual_lines), len(expected_lines))
        for actual_line, result_line in zip(actual_lines, expected_lines):
            if len(actual_line.strip()) == 0:
                self.assertTrue(len(result_line.strip()) == 0)
            else:
                self.assertEqual(actual_line, result_line)

    def test_comment11(self):
        source = """Projects each element of a sequence to an intermediate new sequence,
        flattens the resulting sequences into one sequence and optionally
        transforms the flattened sequence using a selector function.

        Warning: This method may explode at short notice.

        Args:
            collection_selector: A unary function mapping each element of the
                source iterable into an intermediate sequence. The single
                argument of the collection_selector is the value of an element
                from the source sequence. The return value should be an
                iterable derived from that element value. The default
                collection_selector, which is the identity function, assumes
                that each element of the source sequence is itself iterable.

            result_selector: An optional unary function mapping the elements in
                the flattened intermediate sequence to corresponding elements
                of the result sequence. The single argument of the
                result_selector is the value of an element from the flattened
                intermediate sequence. The return value should be the
                corresponding value in the result sequence. The default
                result_selector is the identity function.

        Returns:
            A Queryable over a generated sequence whose elements are the result
            of applying the one-to-many collection_selector to each element of
            the source sequence, concatenating the results into an intermediate
            sequence, and then mapping each of those elements through the
            result_selector into the result sequence.

        Raises:
            ValueError: If this Queryable has been closed.
            TypeError: If either collection_selector or result_selector are not
                callable.
        """

        expected = """        Projects each element of a sequence to an intermediate new sequence,
        flattens the resulting sequences into one sequence and optionally
        transforms the flattened sequence using a selector function.

        .. warning::

            This method may explode at short notice.

        :param collection_selector: A unary function mapping each element of the
            source iterable into an intermediate sequence. The single
            argument of the collection_selector is the value of an element
            from the source sequence. The return value should be an
            iterable derived from that element value. The default
            collection_selector, which is the identity function, assumes
            that each element of the source sequence is itself iterable.

        :param result_selector: An optional unary function mapping the elements in
            the flattened intermediate sequence to corresponding elements
            of the result sequence. The single argument of the
            result_selector is the value of an element from the flattened
            intermediate sequence. The return value should be the
            corresponding value in the result sequence. The default
            result_selector is the identity function.

        :returns: A Queryable over a generated sequence whose elements are the result
            of applying the one-to-many collection_selector to each element of
            the source sequence, concatenating the results into an intermediate
            sequence, and then mapping each of those elements through the
            result_selector into the result sequence.

        :raises:
            * ValueError - If this Queryable has been closed.

            * TypeError - If either collection_selector or result_selector are not
                callable.
        """
        source_lines = source.splitlines()
        actual_lines = parse_hieroglyph_text(source_lines)
        expected_lines = expected.splitlines()
        self.assertEqual(len(actual_lines), len(expected_lines))
        for actual_line, result_line in zip(actual_lines, expected_lines):
            if len(actual_line.strip()) == 0:
                self.assertTrue(len(result_line.strip()) == 0)
            else:
                self.assertEqual(actual_line, result_line)

    def test_comment12(self):
        source = """Determine if all elements in the source sequence satisfy a condition.

        All of the source sequence will be consumed.

        Note: This method uses immediate execution.

        Args:
            predicate: An optional single argument function used to test each
                elements. If omitted, the bool() function is used resulting in
                the elements being tested directly.

        Returns:
            True if all elements in the sequence meet the predicate condition,
            otherwise False.

        Raises:
            This is not a proper exception description
        """

        source_lines = source.splitlines()
        self.assertRaises(HieroglyphError, lambda: parse_hieroglyph_text(source_lines))