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