Support Python 3.
Chris Pressey
6 years ago
67 | 67 | return 1 |
68 | 68 | |
69 | 69 | if options.dump: |
70 | print "Functionalities:" | |
70 | print("Functionalities:") | |
71 | 71 | for name in functionalities: |
72 | print " " + name | |
72 | print(" " + name) | |
73 | 73 | for implementation in functionalities[name].implementations: |
74 | print " +-" + str(implementation) | |
75 | print "Tests:" | |
74 | print(" +-" + str(implementation)) | |
75 | print("Tests:") | |
76 | 76 | for test in tests: |
77 | print " " + str(test) | |
77 | print(" " + str(test)) | |
78 | 78 | return 0 |
79 | 79 | |
80 | 80 | # run tests |
84 | 84 | try: |
85 | 85 | for test in tests: |
86 | 86 | if options.verbose: |
87 | print str(test) | |
87 | print(str(test)) | |
88 | 88 | these_results = test.run(options=options) |
89 | 89 | if options.verbose: |
90 | 90 | for result in these_results: |
101 | 101 | if options.verbose: |
102 | 102 | for key in dup_check: |
103 | 103 | if len(dup_check[key]) != 1: |
104 | print "WARNING: test/impl combination %s was run %d times %r" % ( | |
104 | print("WARNING: test/impl combination %s was run %d times %r" % ( | |
105 | 105 | key, len(dup_check[key]), dup_check[key] |
106 | ) | |
106 | )) | |
107 | 107 | |
108 | 108 | # report on results |
109 | 109 | for result in results: |
111 | 111 | num_results = len(results) |
112 | 112 | num_failures = len([x for x in results if not x.is_successful()]) |
113 | 113 | if not all_ran: |
114 | print '**************************************************************' | |
115 | print '** TESTING TERMINATED PREMATURELY -- NOT ALL TESTS WERE RUN **' | |
116 | print '**************************************************************' | |
114 | print('**************************************************************') | |
115 | print('** TESTING TERMINATED PREMATURELY -- NOT ALL TESTS WERE RUN **') | |
116 | print('**************************************************************') | |
117 | 117 | |
118 | print '--------------------------------' | |
119 | print 'Total test runs: %d, failures: %d' % (num_results, num_failures) | |
120 | print '--------------------------------' | |
118 | print('--------------------------------') | |
119 | print('Total test runs: %d, failures: %d' % (num_results, num_failures)) | |
120 | print('--------------------------------') | |
121 | 121 | |
122 | 122 | if num_failures == 0: |
123 | 123 | return 0 |
3 | 3 | from subprocess import Popen, PIPE |
4 | 4 | from tempfile import mkstemp |
5 | 5 | |
6 | # Note: the __unicode__ method of all the classes defined herein should | |
6 | # Python 2/3 | |
7 | try: | |
8 | unicode = unicode | |
9 | except NameError: | |
10 | unicode = str | |
11 | ||
12 | # Note: the __str__ method of all the classes defined herein should | |
7 | 13 | # produce a short, human-readable summary of the contents of the object, |
8 | 14 | # suitable for displaying in the test results but not necessarily |
9 | 15 | # complete. __repr__ should produce something complete, when it is |
54 | 60 | |
55 | 61 | |
56 | 62 | class OutputOutcome(Outcome): |
57 | def __unicode__(self): | |
63 | def __str__(self): | |
58 | 64 | return u'output:\n' + self.text |
59 | 65 | |
60 | 66 | |
61 | 67 | class ErrorOutcome(Outcome): |
62 | def __unicode__(self): | |
68 | def __str__(self): | |
63 | 69 | return u'error:\n' + self.text |
64 | 70 | |
65 | 71 | |
121 | 127 | def short_description(self): |
122 | 128 | return 'expected %r, got %r' % (self.test.expectation, self.actual) |
123 | 129 | |
130 | def fmt(self, field, contents): | |
131 | if str == unicode: # Python 3 | |
132 | if isinstance(contents, bytes): | |
133 | contents = contents.decode('utf-8') | |
134 | s = field + contents | |
135 | print(s) | |
136 | else: # Python 2 | |
137 | s = field + contents | |
138 | print(s) | |
139 | ||
124 | 140 | def report(self): |
125 | print "FAILED : " + self.format_text_block(self.test.description) | |
126 | print "Location: " + self.test.body_block.location() | |
127 | print "Function: " + self.format_text_block(self.test.functionality.name) | |
128 | print "Impl : " + self.format_text_block(self.implementation) | |
129 | print "Body : " + self.format_text_block(self.test.body) | |
130 | #if input is not None: | |
131 | #print "Input : " + self.format_text_block(self.test.input) | |
132 | print "Expected: " + self.format_text_block(self.test.expectation) | |
133 | print "Actual : " + self.format_text_block(self.actual) | |
134 | ||
141 | self.fmt("FAILED : ", self.format_text_block(self.test.description)) | |
142 | self.fmt("Location: ", self.test.body_block.location()) | |
143 | self.fmt("Function: ", self.format_text_block(self.test.functionality.name)) | |
144 | self.fmt("Impl : ", self.format_text_block(self.implementation)) | |
145 | self.fmt("Body : ", self.format_text_block(self.test.body)) | |
146 | self.fmt("Expected: ", self.format_text_block(self.test.expectation)) | |
147 | self.fmt("Actual : ", self.format_text_block(self.actual)) | |
148 | print("") | |
135 | 149 | |
136 | 150 | def is_successful(self): |
137 | 151 | return False |
156 | 170 | u'??> ': u'? ', |
157 | 171 | u'???> ': u'? ', |
158 | 172 | } |
159 | FREESTYLE_PREFIXES = FREESTYLE_MAP.keys() | |
173 | FREESTYLE_PREFIXES = list(FREESTYLE_MAP.keys()) | |
160 | 174 | PREFIXES = FREESTYLE_PREFIXES + [ |
161 | 175 | u'| ', |
162 | 176 | u'+ ', |
188 | 202 | self.__class__.__name__, self.line_num, filename_repr |
189 | 203 | ) |
190 | 204 | |
191 | def __unicode__(self): | |
205 | def __str__(self): | |
192 | 206 | return unicode(repr(self)) |
193 | 207 | |
194 | 208 | def location(self): |
487 | 501 | def __repr__(self): |
488 | 502 | return "Functionality(%r)" % self.name |
489 | 503 | |
490 | def __unicode__(self): | |
504 | def __str__(self): | |
491 | 505 | return unicode(repr(self)) |
492 | 506 | |
493 | 507 | def add_implementation(self, implementation): |
522 | 536 | def __repr__(self): |
523 | 537 | return '%s(%r)' % (self.__class__.__name__, self.callable) |
524 | 538 | |
525 | def __unicode__(self): | |
539 | def __str__(self): | |
526 | 540 | return u'callable "%r"' % self.callable |
527 | 541 | |
528 | 542 | def run(self, body=None, input=None): |
540 | 554 | def __repr__(self): |
541 | 555 | return '%s(%r)' % (self.__class__.__name__, self.command) |
542 | 556 | |
543 | def __unicode__(self): | |
557 | def __str__(self): | |
544 | 558 | return u'shell command "%s"' % self.command |
545 | 559 | |
546 | 560 | def __eq__(self, other): |
638 | 652 | return result |
639 | 653 | |
640 | 654 | def normalize_output(self, text): |
641 | text = text.decode('UTF-8', errors='ignore') | |
655 | try: | |
656 | text = text.decode('UTF-8', errors='ignore') | |
657 | except AttributeError: | |
658 | pass | |
642 | 659 | text = re.sub(r'\r\n', '\n', text) |
643 | 660 | return text.strip('\r\n') |
644 | 661 |
1 | 1 | |
2 | 2 | # Really crude test harness for py-falderal itself... |
3 | 3 | |
4 | PYTHONPATH=src python src/falderal/tests.py -v || exit 1 | |
4 | if [ "x$PYTHON" = "x" ]; then | |
5 | PYTHON="python3" | |
6 | fi | |
7 | FALDERAL="$PYTHON ../bin/falderal" | |
8 | ||
9 | PYTHONPATH=src $PYTHON src/falderal/tests.py -v || exit 1 | |
5 | 10 | |
6 | 11 | cd tests |
7 | 12 | |
16 | 21 | " |
17 | 22 | for TEST in ${FIRST_TESTS}; do |
18 | 23 | echo ${TEST}... |
19 | ../bin/falderal --cavalier ${TEST}.markdown > ${TEST}.actual 2>&1 | |
24 | $FALDERAL --cavalier ${TEST}.markdown > ${TEST}.actual 2>&1 | |
20 | 25 | diff -u ${TEST}.expected ${TEST}.actual || exit 1 |
21 | 26 | done |
22 | 27 | |
24 | 29 | LINTING_TESTS="test-no-tests" |
25 | 30 | for TEST in ${LINTING_TESTS}; do |
26 | 31 | echo ${TEST}... |
27 | ../bin/falderal ${TEST}.markdown > ${TEST}.actual 2>&1 | |
32 | $FALDERAL ${TEST}.markdown > ${TEST}.actual 2>&1 | |
28 | 33 | diff -u ${TEST}.expected ${TEST}.actual || exit 1 |
29 | 34 | done |
30 | 35 | |
33 | 38 | " |
34 | 39 | for TEST in ${TWO_PART_TESTS}; do |
35 | 40 | echo ${TEST}... |
36 | ../bin/falderal ${TEST}-a.markdown ${TEST}-b.markdown > ${TEST}.actual 2>&1 | |
41 | $FALDERAL ${TEST}-a.markdown ${TEST}-b.markdown > ${TEST}.actual 2>&1 | |
37 | 42 | diff -u ${TEST}.expected ${TEST}.actual || exit 1 |
38 | 43 | done |
39 | 44 | |
40 | 45 | # special tests: -b |
41 | 46 | TEST=test-substring-error |
42 | 47 | echo ${TEST}... |
43 | ../bin/falderal -b ${TEST}.markdown > ${TEST}.actual 2>&1 | |
48 | $FALDERAL -b ${TEST}.markdown > ${TEST}.actual 2>&1 | |
44 | 49 | diff -u ${TEST}.expected ${TEST}.actual || exit 1 |
45 | 50 | |
46 | 51 | rm -f *.actual |