blob: 2cf37e2d39eaeceb0ba5a30892c309287ccf7949 [file] [log] [blame]
Christopher Dunnf9864232007-06-14 21:01:26 +00001import sys
2import os
3import os.path
Baptiste Lepilleur64e07e52009-11-18 21:27:06 +00004from glob import glob
Christopher Dunnf9864232007-06-14 21:01:26 +00005
6
7def compareOutputs( expected, actual, message ):
8 expected = expected.strip().replace('\r','').split('\n')
9 actual = actual.strip().replace('\r','').split('\n')
10 diff_line = 0
11 max_line_to_compare = min( len(expected), len(actual) )
12 for index in xrange(0,max_line_to_compare):
13 if expected[index].strip() != actual[index].strip():
14 diff_line = index + 1
15 break
16 if diff_line == 0 and len(expected) != len(actual):
17 diff_line = max_line_to_compare+1
18 if diff_line == 0:
19 return None
20 def safeGetLine( lines, index ):
21 index += -1
22 if index >= len(lines):
23 return ''
24 return lines[index].strip()
25 return """ Difference in %s at line %d:
26 Expected: '%s'
27 Actual: '%s'
28""" % (message, diff_line,
29 safeGetLine(expected,diff_line),
30 safeGetLine(actual,diff_line) )
31
32def safeReadFile( path ):
33 try:
34 return file( path, 'rt' ).read()
35 except IOError, e:
36 return '<File "%s" is missing: %s>' % (path,e)
37
38def runAllTests( jsontest_executable_path, input_dir = None ):
39 if not input_dir:
40 input_dir = os.getcwd()
Baptiste Lepilleur64e07e52009-11-18 21:27:06 +000041 tests = glob( os.path.join( input_dir, '*.json' ) )
42 test_jsonchecker = glob( os.path.join( input_dir, 'jsonchecker', '*.json' ) )
Christopher Dunnf9864232007-06-14 21:01:26 +000043 failed_tests = []
Baptiste Lepilleur64e07e52009-11-18 21:27:06 +000044 for input_path in tests + test_jsonchecker:
45 is_json_checker_test = input_path in test_jsonchecker
Christopher Dunnf9864232007-06-14 21:01:26 +000046 print 'TESTING:', input_path,
Baptiste Lepilleur64e07e52009-11-18 21:27:06 +000047 options = is_json_checker_test and '--json-checker' or ''
48 pipe = os.popen( "%s %s %s" % (jsontest_executable_path, options,
49 input_path) )
Christopher Dunnf9864232007-06-14 21:01:26 +000050 process_output = pipe.read()
51 status = pipe.close()
Baptiste Lepilleur64e07e52009-11-18 21:27:06 +000052 if is_json_checker_test:
53 expect_failure = os.path.basename( input_path ).startswith( 'fail' )
54 if expect_failure:
55 if status is None:
56 print 'FAILED'
57 failed_tests.append( (input_path, 'Parsing should have failed') )
58 else:
59 print 'OK'
Christopher Dunnf9864232007-06-14 21:01:26 +000060 else:
Baptiste Lepilleur64e07e52009-11-18 21:27:06 +000061 if status is not None:
62 print 'FAILED'
63 failed_tests.append( (input_path, 'Parsing failed:\n' + process_output) )
64 else:
65 print 'OK'
66 else:
67 base_path = os.path.splitext(input_path)[0]
68 actual_output = safeReadFile( base_path + '.actual' )
69 actual_rewrite_output = safeReadFile( base_path + '.actual-rewrite' )
70 file(base_path + '.process-output','wt').write( process_output )
71 if status:
72 print 'parsing failed'
73 failed_tests.append( (input_path, 'Parsing failed:\n' + process_output) )
74 else:
75 expected_output_path = os.path.splitext(input_path)[0] + '.expected'
76 expected_output = file( expected_output_path, 'rt' ).read()
77 detail = ( compareOutputs( expected_output, actual_output, 'input' )
78 or compareOutputs( expected_output, actual_rewrite_output, 'rewrite' ) )
79 if detail:
80 print 'FAILED'
81 failed_tests.append( (input_path, detail) )
82 else:
83 print 'OK'
Christopher Dunnf9864232007-06-14 21:01:26 +000084
85 if failed_tests:
86 print
87 print 'Failure details:'
88 for failed_test in failed_tests:
89 print '* Test', failed_test[0]
90 print failed_test[1]
91 print
92 print 'Test results: %d passed, %d failed.' % (len(tests)-len(failed_tests),
93 len(failed_tests) )
94 return 1
95 else:
96 print 'All %d tests passed.' % len(tests)
97 return 0
98
99if __name__ == '__main__':
100 if len(sys.argv) < 1 or len(sys.argv) > 2:
101 print "Usage: %s jsontest-executable-path [input-testcase-directory]" % sys.argv[0]
102 sys.exit( 1 )
103
104 jsontest_executable_path = os.path.normpath( os.path.abspath( sys.argv[1] ) )
105 if len(sys.argv) > 2:
106 input_path = os.path.normpath( os.path.abspath( sys.argv[2] ) )
107 else:
108 input_path = None
109 status = runAllTests( jsontest_executable_path, input_path )
Baptiste Lepilleur4cd8bae2007-03-15 22:11:38 +0000110 sys.exit( status )