diff options
Diffstat (limited to 'test/DocTests.py')
-rwxr-xr-x | test/DocTests.py | 74 |
1 files changed, 47 insertions, 27 deletions
diff --git a/test/DocTests.py b/test/DocTests.py index 04241188..f5746ec4 100755 --- a/test/DocTests.py +++ b/test/DocTests.py @@ -19,13 +19,17 @@ class DocTests: self.verbose = args.verbose self.tests = args.examples - self.examples = dict() - self.test_files = list() - self.testin_token = 'command' - self.testout_token = 'output' - self.testdat_token = 'input' + self.examples = dict() + self.test_files = list() + self.testin_token = 'command' + self.testout_token = 'output' + self.testdat_token = 'input' + self.testfile_token = 'file' self.validate_token = 'validate' - self.testwithdat_token = 'with_input' + self.validate_cmd_token = 'validate-command' + self.validate_dat_token = 'validate-data' + self.testwithdat_token = 'with_input' + self.testwithfile_token = 'with_file' def read_example(self): endexample = re.compile(r'^@end\s+smallexample\s*$') @@ -35,15 +39,15 @@ class DocTests: self.current_line += 1 if len(line) <= 0 or endexample.match(line): break # Replace special texinfo character sequences with their ASCII counterpart - example += line.replace("@@","@").replace("@{","{").replace("@}","}") + example += re.sub(r'@([@{}])', r'\1', line) return example def test_id(self, example): return hashlib.sha1(example.rstrip()).hexdigest()[0:7].upper() def find_examples(self): - startexample = re.compile(r'^@smallexample\s+@c\s+(%s|%s|%s)(?::([\dA-Fa-f]+|validate))?(?:,(.*))?' - % (self.testin_token, self.testout_token, self.testdat_token)) + startexample = re.compile(r'^@smallexample\s+@c\s+(%s|%s|%s|%s)(?::([\dA-Fa-f]+|validate))?(?:,(.*))?' + % (self.testin_token, self.testout_token, self.testdat_token, self.testfile_token)) while True: line = self.file.readline() self.current_line += 1 @@ -78,9 +82,9 @@ class DocTests: if test_id == self.validate_token: test_id = "Val-" + str(test_begin_line) if test_kind == self.testin_token: - test_kind = "validate-command" + test_kind = self.validate_cmd_token elif test_kind == self.testdat_token: - test_kind = "validate-data" + test_kind = self.validate_dat_token try: self.examples[test_id] except KeyError: @@ -105,11 +109,11 @@ class DocTests: try: command = example[self.testin_token][self.testin_token] except KeyError: - if 'validate-data' in example: + if self.validate_dat_token in example: command = '$ ledger bal' - elif 'validate-command' in example: + elif self.validate_cmd_token in example: validate_command = True - command = example['validate-command']['validate-command'] + command = example[self.validate_cmd_token][self.validate_cmd_token] else: return None @@ -145,7 +149,7 @@ class DocTests: for test_id in tests: validation = False - if "validate-data" in self.examples[test_id] or "validate-command" in self.examples[test_id]: + if self.validate_dat_token in self.examples[test_id] or self.validate_cmd_token in self.examples[test_id]: validation = True example = self.examples[test_id] try: @@ -167,38 +171,48 @@ class DocTests: input = self.examples[with_input][self.testdat_token][self.testdat_token] except KeyError: try: - input = example['validate-data']['validate-data'] + input = example[self.validate_dat_token][self.validate_dat_token] except KeyError: input = None - if command and (output or validation): + if command and (output != None or validation): test_file_created = False if findex: scriptpath = os.path.dirname(os.path.realpath(__file__)) - test_input_dir = scriptpath + '/../test/input/' + test_input_dir = os.path.join(scriptpath, '..', 'test', 'input') test_file = command[findex] if not os.path.exists(test_file): if input: test_file_created = True with open(test_file, 'w') as f: f.write(input) - elif os.path.exists(test_input_dir + test_file): - command[findex] = test_input_dir + test_file - error = False + elif os.path.exists(os.path.join(test_input_dir, test_file)): + command[findex] = os.path.join(test_input_dir, test_file) + try: + convert_idx = command.index('convert') + convert_file = command[convert_idx+1] + convert_data = example[self.testfile_token][self.testfile_token] + if not os.path.exists(convert_file): + with open(convert_file, 'w') as f: + f.write(convert_data) + except ValueError: + pass + error = None try: verify = subprocess.check_output(command, stderr=subprocess.STDOUT) - except: - verify = str() - error = True - valid = (output == verify) or (not error and validation) + valid = (output == verify) or (not error and validation) + except subprocess.CalledProcessError, e: + error = e.output + valid = False + failed.add(test_id) if valid and test_file_created: os.remove(test_file) if self.verbose > 0: - print test_id, ':', 'Passed' if valid else 'FAILED' + print test_id, ':', 'Passed' if valid else 'FAILED: {}'.format(error) if error else 'FAILED' else: sys.stdout.write('.' if valid else 'E') - if not valid: + if not (valid or error): failed.add(test_id) if self.verbose > 1: print ' '.join(command) @@ -206,6 +220,12 @@ class DocTests: for line in unified_diff(output.split('\n'), verify.split('\n'), fromfile='generated', tofile='expected'): print(line) print + else: + if self.verbose > 0: + print test_id, ':', 'Skipped' + else: + sys.stdout.write('X') + if not self.verbose: print if len(failed) > 0: |