| 680 | self.ns = ns |
| 681 | |
| 682 | def check_output(self, want, got, optionflags): |
| 683 | # cut it short if they are equal |
| 684 | if want == got: |
| 685 | return True |
| 686 | |
| 687 | # skip stopwords in source |
| 688 | if any(word in self._source for word in self.stopwords): |
| 689 | return True |
| 690 | |
| 691 | # skip random stuff |
| 692 | if any(word in want for word in self.rndm_markers): |
| 693 | return True |
| 694 | |
| 695 | # skip function/object addresses |
| 696 | if self.obj_pattern.search(got): |
| 697 | return True |
| 698 | |
| 699 | # ignore comments (e.g. signal.freqresp) |
| 700 | if want.lstrip().startswith("#"): |
| 701 | return True |
| 702 | |
| 703 | # try the standard doctest |
| 704 | try: |
| 705 | if self.vanilla.check_output(want, got, optionflags): |
| 706 | return True |
| 707 | except Exception: |
| 708 | pass |
| 709 | |
| 710 | # OK then, convert strings to objects |
| 711 | try: |
| 712 | a_want = eval(want, dict(self.ns)) |
| 713 | a_got = eval(got, dict(self.ns)) |
| 714 | except Exception: |
| 715 | # Maybe we're printing a numpy array? This produces invalid python |
| 716 | # code: `print(np.arange(3))` produces "[0 1 2]" w/o commas between |
| 717 | # values. So, reinsert commas and retry. |
| 718 | # TODO: handle (1) abberivation (`print(np.arange(10000))`), and |
| 719 | # (2) n-dim arrays with n > 1 |
| 720 | s_want = want.strip() |
| 721 | s_got = got.strip() |
| 722 | cond = (s_want.startswith("[") and s_want.endswith("]") and |
| 723 | s_got.startswith("[") and s_got.endswith("]")) |
| 724 | if cond: |
| 725 | s_want = ", ".join(s_want[1:-1].split()) |
| 726 | s_got = ", ".join(s_got[1:-1].split()) |
| 727 | return self.check_output(s_want, s_got, optionflags) |
| 728 | |
| 729 | if not self.parse_namedtuples: |
| 730 | return False |
| 731 | # suppose that "want" is a tuple, and "got" is smth like |
| 732 | # MoodResult(statistic=10, pvalue=0.1). |
| 733 | # Then convert the latter to the tuple (10, 0.1), |
| 734 | # and then compare the tuples. |
| 735 | try: |
| 736 | num = len(a_want) |
| 737 | regex = (r'[\w\d_]+\(' + |
| 738 | ', '.join([r'[\w\d_]+=(.+)']*num) + |
| 739 | r'\)') |