Baptiste Lepilleur | 4cd8bae | 2007-03-15 22:11:38 +0000 | [diff] [blame] | 1 | import sys
|
| 2 | import os
|
| 3 | import os.path
|
| 4 | import glob
|
| 5 |
|
| 6 |
|
| 7 | def 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 |
|
| 32 | def safeReadFile( path ):
|
| 33 | try:
|
| 34 | return file( path, 'rt' ).read()
|
| 35 | except IOError, e:
|
| 36 | return '<File "%s" is missing: %s>' % (path,e)
|
| 37 |
|
| 38 | def runAllTests( jsontest_executable_path, input_dir = None ):
|
| 39 | if not input_dir:
|
| 40 | input_dir = os.getcwd()
|
| 41 | tests = glob.glob( os.path.join( input_dir, '*.json' ) )
|
| 42 | failed_tests = []
|
| 43 | for input_path in tests:
|
| 44 | print 'TESTING:', input_path,
|
| 45 | pipe = os.popen( "%s %s" % (jsontest_executable_path, input_path) )
|
| 46 | process_output = pipe.read()
|
| 47 | status = pipe.close()
|
| 48 | base_path = os.path.splitext(input_path)[0]
|
| 49 | actual_output = safeReadFile( base_path + '.actual' )
|
| 50 | actual_rewrite_output = safeReadFile( base_path + '.actual-rewrite' )
|
| 51 | file(base_path + '.process-output','wt').write( process_output )
|
| 52 | if status:
|
| 53 | print 'parsing failed'
|
| 54 | failed_tests.append( (input_path, 'Parsing failed:\n' + process_output) )
|
| 55 | else:
|
| 56 | expected_output_path = os.path.splitext(input_path)[0] + '.expected'
|
| 57 | expected_output = file( expected_output_path, 'rt' ).read()
|
| 58 | detail = ( compareOutputs( expected_output, actual_output, 'input' )
|
| 59 | or compareOutputs( expected_output, actual_rewrite_output, 'rewrite' ) )
|
| 60 | if detail:
|
| 61 | print 'FAILED'
|
| 62 | failed_tests.append( (input_path, detail) )
|
| 63 | else:
|
| 64 | print 'OK'
|
| 65 |
|
| 66 | if failed_tests:
|
| 67 | print
|
| 68 | print 'Failure details:'
|
| 69 | for failed_test in failed_tests:
|
| 70 | print '* Test', failed_test[0]
|
| 71 | print failed_test[1]
|
| 72 | print
|
| 73 | print 'Test results: %d passed, %d failed.' % (len(tests)-len(failed_tests),
|
| 74 | len(failed_tests) )
|
| 75 | return 1
|
| 76 | else:
|
| 77 | print 'All %d tests passed.' % len(tests)
|
| 78 | return 0
|
| 79 |
|
| 80 | if __name__ == '__main__':
|
| 81 | if len(sys.argv) < 1 or len(sys.argv) > 2:
|
| 82 | print "Usage: %s jsontest-executable-path [input-testcase-directory]" % sys.argv[0]
|
| 83 | sys.exit( 1 )
|
| 84 |
|
| 85 | jsontest_executable_path = os.path.normpath( os.path.abspath( sys.argv[1] ) )
|
Baptiste Lepilleur | 2d4dd28 | 2007-03-17 22:14:59 +0000 | [diff] [blame] | 86 | if len(sys.argv) > 2:
|
Baptiste Lepilleur | 4cd8bae | 2007-03-15 22:11:38 +0000 | [diff] [blame] | 87 | input_path = os.path.normpath( os.path.abspath( sys.argv[2] ) )
|
| 88 | else:
|
| 89 | input_path = None
|
| 90 | status = runAllTests( jsontest_executable_path, input_path )
|
| 91 | sys.exit( status ) |