|
1 | 1 | import string |
2 | 2 | import random |
| 3 | +import io |
3 | 4 | import pytest |
4 | 5 | import numpy as np |
5 | 6 |
|
@@ -531,28 +532,36 @@ def _import_path(self, klass=None, func=None): |
531 | 532 |
|
532 | 533 | @capture_stderr |
533 | 534 | def test_good_class(self): |
534 | | - assert validate_one(self._import_path( |
535 | | - klass='GoodDocStrings')) == 0 |
| 535 | + errors = validate_one(self._import_path( |
| 536 | + klass='GoodDocStrings'))['errors'] |
| 537 | + assert isinstance(errors, list) |
| 538 | + assert not errors |
536 | 539 |
|
537 | 540 | @capture_stderr |
538 | 541 | @pytest.mark.parametrize("func", [ |
539 | 542 | 'plot', 'sample', 'random_letters', 'sample_values', 'head', 'head1', |
540 | 543 | 'contains', 'mode']) |
541 | 544 | def test_good_functions(self, func): |
542 | | - assert validate_one(self._import_path( |
543 | | - klass='GoodDocStrings', func=func)) == 0 |
| 545 | + errors = validate_one(self._import_path( |
| 546 | + klass='GoodDocStrings', func=func))['errors'] |
| 547 | + assert isinstance(errors, list) |
| 548 | + assert not errors |
544 | 549 |
|
545 | 550 | @capture_stderr |
546 | 551 | def test_bad_class(self): |
547 | | - assert validate_one(self._import_path( |
548 | | - klass='BadGenericDocStrings')) > 0 |
| 552 | + errors = validate_one(self._import_path( |
| 553 | + klass='BadGenericDocStrings'))['errors'] |
| 554 | + assert isinstance(errors, list) |
| 555 | + assert errors |
549 | 556 |
|
550 | 557 | @capture_stderr |
551 | 558 | @pytest.mark.parametrize("func", [ |
552 | 559 | 'func', 'astype', 'astype1', 'astype2', 'astype3', 'plot', 'method']) |
553 | 560 | def test_bad_generic_functions(self, func): |
554 | | - assert validate_one(self._import_path( # noqa:F821 |
555 | | - klass='BadGenericDocStrings', func=func)) > 0 |
| 561 | + errors = validate_one(self._import_path( # noqa:F821 |
| 562 | + klass='BadGenericDocStrings', func=func))['errors'] |
| 563 | + assert isinstance(errors, list) |
| 564 | + assert errors |
556 | 565 |
|
557 | 566 | @pytest.mark.parametrize("klass,func,msgs", [ |
558 | 567 | # Summary tests |
@@ -594,7 +603,82 @@ def test_bad_generic_functions(self, func): |
594 | 603 | marks=pytest.mark.xfail) |
595 | 604 | ]) |
596 | 605 | def test_bad_examples(self, capsys, klass, func, msgs): |
597 | | - validate_one(self._import_path(klass=klass, func=func)) # noqa:F821 |
598 | | - err = capsys.readouterr().err |
| 606 | + result = validate_one(self._import_path(klass=klass, func=func)) # noqa:F821 |
599 | 607 | for msg in msgs: |
600 | | - assert msg in err |
| 608 | + assert msg in ' '.join(result['errors']) |
| 609 | + |
| 610 | + |
| 611 | +class ApiItems(object): |
| 612 | + @property |
| 613 | + def api_doc(self): |
| 614 | + return io.StringIO(''' |
| 615 | +.. currentmodule:: itertools |
| 616 | +
|
| 617 | +Itertools |
| 618 | +--------- |
| 619 | +
|
| 620 | +Infinite |
| 621 | +~~~~~~~~ |
| 622 | +
|
| 623 | +.. autosummary:: |
| 624 | +
|
| 625 | + cycle |
| 626 | + count |
| 627 | +
|
| 628 | +Finite |
| 629 | +~~~~~~ |
| 630 | +
|
| 631 | +.. autosummary:: |
| 632 | +
|
| 633 | + chain |
| 634 | +
|
| 635 | +.. currentmodule:: random |
| 636 | +
|
| 637 | +Random |
| 638 | +------ |
| 639 | +
|
| 640 | +All |
| 641 | +~~~ |
| 642 | +
|
| 643 | +.. autosummary:: |
| 644 | +
|
| 645 | + seed |
| 646 | + randint |
| 647 | +''') |
| 648 | + |
| 649 | + @pytest.mark.parametrize('idx,name', [(0, 'itertools.cycle'), |
| 650 | + (1, 'itertools.count'), |
| 651 | + (2, 'itertools.chain'), |
| 652 | + (3, 'random.seed'), |
| 653 | + (4, 'random.randint')]) |
| 654 | + def test_item_name(self, idx, name): |
| 655 | + result = list(validate_docstrings.get_api_items(self.api_doc)) |
| 656 | + assert result[idx][0] == name |
| 657 | + |
| 658 | + @pytest.mark.parametrize('idx,func', [(0, 'cycle'), |
| 659 | + (1, 'count'), |
| 660 | + (2, 'chain'), |
| 661 | + (3, 'seed'), |
| 662 | + (4, 'randint')]) |
| 663 | + def test_item_function(self, idx, func): |
| 664 | + result = list(validate_docstrings.get_api_items(self.api_doc)) |
| 665 | + assert callable(result[idx][1]) |
| 666 | + assert result[idx][1].__name__ == func |
| 667 | + |
| 668 | + @pytest.mark.parametrize('idx,section', [(0, 'Itertools'), |
| 669 | + (1, 'Itertools'), |
| 670 | + (2, 'Itertools'), |
| 671 | + (3, 'Random'), |
| 672 | + (4, 'Random')]) |
| 673 | + def test_item_section(self, idx, section): |
| 674 | + result = list(validate_docstrings.get_api_items(self.api_doc)) |
| 675 | + assert result[idx][2] == section |
| 676 | + |
| 677 | + @pytest.mark.parametrize('idx,subsection', [(0, 'Infinite'), |
| 678 | + (1, 'Infinite'), |
| 679 | + (2, 'Finite'), |
| 680 | + (3, 'All'), |
| 681 | + (4, 'All')]) |
| 682 | + def test_item_subsection(self, idx, subsection): |
| 683 | + result = list(validate_docstrings.get_api_items(self.api_doc)) |
| 684 | + assert result[idx][3] == subsection |
0 commit comments