Go to the documentation of this file.00001
00002
00003
00004 '''rtsprofile
00005
00006 Copyright (C) 2009-2010
00007 Geoffrey Biggs
00008 RT-Synthesis Research Group
00009 Intelligent Systems Research Institute,
00010 National Institute of Advanced Industrial Science and Technology (AIST),
00011 Japan
00012 All rights reserved.
00013 Licensed under the Eclipse Public License -v 1.0 (EPL)
00014 http://www.opensource.org/licenses/eclipse-1.0.txt
00015
00016 File: test.py
00017
00018 Unit tests.
00019
00020 '''
00021
00022 __version__ = '$Revision: $'
00023
00024
00025
00026 import os.path
00027 from rtsprofile.rts_profile import RtsProfile
00028 import sys
00029 import tempfile
00030 from traceback import print_exc
00031
00032
00033 def main(argv):
00034 print \
00035 '''This test loads a given XML or YAML file (type determined by the extension)
00036 into an RtsProfile object. The object is printed to a string, which is stored.
00037
00038 It then attempts to save the object using the XML output. This output is loaded
00039 back in, printed to a string, and that string compared to the original. They
00040 should be the same.
00041
00042 This save-load-check process is then repeated for the YAML output.
00043 '''
00044
00045
00046 input_name = argv[1]
00047 type = os.path.splitext(input_name)[1][1:]
00048 f = open(input_name)
00049 if type == 'xml':
00050 orig_prof = RtsProfile(xml_spec=f)
00051 elif type == 'yaml':
00052 orig_prof = RtsProfile(yaml_spec=f)
00053 else:
00054 print >>sys.stderr, 'Unknown input type: {0}'.format(type)
00055 return 1
00056 f.close()
00057 orig_prof_str = str(orig_prof)
00058 print 'Loaded original.'
00059 print
00060
00061
00062 failed = False
00063 xml_output = ''
00064 xml_prof_str = ''
00065 try:
00066 xml_output = orig_prof.save_to_xml()
00067 print 'Saved as XML.'
00068 xml_prof = RtsProfile(xml_spec=xml_output)
00069 print 'Loaded XML.'
00070 xml_prof_str = str(xml_prof)
00071 print 'Printed XML.'
00072 print
00073 except:
00074 print_exc()
00075 print
00076 failed = True
00077 if xml_prof_str != orig_prof_str:
00078 print 'XML profile does not equal original profile.'
00079 failed = True
00080 if failed:
00081 print >>sys.stderr, 'XML test failed.'
00082 f = open('original_prof.dump', 'w')
00083 f.write(orig_prof_str)
00084 f.close()
00085 f = open('xml_prof.dump', 'w')
00086 f.write(xml_prof_str)
00087 f.close()
00088 f = open('xml_raw.dump', 'w')
00089 f.write(xml_output)
00090 f.close()
00091 return 1
00092
00093
00094 failed = False
00095 yaml_output = ''
00096 yaml_prof_str = ''
00097 try:
00098 yaml_output = orig_prof.save_to_yaml()
00099 print 'Saved as YAML.'
00100 yaml_prof = RtsProfile(yaml_spec=yaml_output)
00101 print 'Loaded YAML.'
00102 yaml_prof_str = str(yaml_prof)
00103 print 'Printed YAML.'
00104 print
00105 except:
00106 print_exc()
00107 print
00108 failed = True
00109 if yaml_prof_str != orig_prof_str:
00110 print 'YAML profile does not equal original profile.'
00111 failed = True
00112 if failed:
00113 print >>sys.stderr, 'YAML test failed.'
00114 f = open('original_prof.dump', 'w')
00115 f.write(orig_prof_str)
00116 f.close()
00117 f = open('yaml_prof.dump', 'w')
00118 f.write(yaml_prof_str)
00119 f.close()
00120 f = open('yaml_raw.dump', 'w')
00121 f.write(yaml_output)
00122 f.close()
00123 return 1
00124
00125 print >>sys.stderr, 'Tests passed.'
00126 return 0
00127
00128
00129 if __name__ == '__main__':
00130 sys.exit(main(sys.argv))
00131
00132
00133
00134