00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036 PKG = 'test_roslib_comm'
00037 NAME = 'test_gentools'
00038 import roslib; roslib.load_manifest(PKG)
00039
00040 import os
00041 import string
00042 import sys
00043 import unittest
00044 import cStringIO
00045
00046 import roslib.names
00047 import roslib.packages
00048 import rosunit
00049
00050 TEST_CTX = 'rosgraph_msgs'
00051
00052 class TestGentools(unittest.TestCase):
00053
00054 def setUp(self):
00055 pass
00056
00057 def _load_md5_tests(self, dir):
00058 test_dir = os.path.join(roslib.packages.get_pkg_dir(PKG), 'test', 'md5tests', dir)
00059 tests = {}
00060 for f in os.listdir(test_dir):
00061 path = os.path.join(test_dir, f)
00062 if not f.endswith('.txt'):
00063 continue
00064 name = f[:-4]
00065 while name and name[-1].isdigit():
00066 name = name[:-1]
00067 self.assert_(name)
00068 if name in tests:
00069 tests[name].append(path)
00070 else:
00071 tests[name] = [path]
00072 return tests
00073
00074 def _compute_md5(self, f):
00075 from roslib.gentools import compute_md5, get_dependencies
00076 from roslib.msgs import load_from_string
00077
00078 text = open(f, 'r').read()
00079 spec = load_from_string(text, package_context=TEST_CTX)
00080 get_deps_dict = get_dependencies(spec, TEST_CTX, compute_files=False)
00081 return compute_md5(get_deps_dict)
00082
00083 def _compute_md5_text(self, f):
00084 from roslib.gentools import compute_md5_text, get_dependencies
00085 from roslib.msgs import load_from_string
00086
00087 text = open(f, 'r').read()
00088 spec = load_from_string(text, package_context=TEST_CTX)
00089 get_deps_dict = get_dependencies(spec, TEST_CTX, compute_files=False)
00090 return compute_md5_text(get_deps_dict, spec)
00091
00092 def test_compute_md5_text(self):
00093 from std_msgs.msg import Header
00094 Header_md5 = Header._md5sum
00095 rg_msg_dir = os.path.join(roslib.packages.get_pkg_dir(TEST_CTX), 'msg')
00096 clock_msg = os.path.join(rg_msg_dir, 'Clock.msg')
00097
00098 self.assertEquals("time clock", self._compute_md5_text(clock_msg))
00099 log_msg = os.path.join(rg_msg_dir, 'Log.msg')
00100 self.assertEquals("byte DEBUG=1\nbyte INFO=2\nbyte WARN=4\nbyte ERROR=8\nbyte FATAL=16\n%s header\nbyte level\nstring name\nstring msg\nstring file\nstring function\nuint32 line\nstring[] topics"%Header_md5, self._compute_md5_text(log_msg))
00101
00102 tests = self._load_md5_tests('md5text')
00103
00104 for k, files in tests.iteritems():
00105 print "running tests", k
00106 ref_file = [f for f in files if f.endswith('%s1.txt'%k)]
00107 if not ref_file:
00108 self.fail("failed to load %s"%k)
00109 ref_file = ref_file[0]
00110 ref_text = open(ref_file, 'r').read().strip()
00111 print "KEY", k
00112 files = [f for f in files if not f.endswith('%s1.txt'%k)]
00113 for f in files[1:]:
00114 f_text = self._compute_md5_text(f)
00115 self.assertEquals(ref_text, f_text, "failed on %s\n%s\n%s: \n[%s]\nvs.\n[%s]\n"%(k, ref_file, f, ref_text, f_text))
00116
00117 def test_md5_equals(self):
00118 tests = self._load_md5_tests('same')
00119 for k, files in tests.iteritems():
00120 print "running tests", k
00121 md5sum = self._compute_md5(files[0])
00122 for f in files[1:]:
00123 self.assertEquals(md5sum, self._compute_md5(f), "failed on %s: \n[%s]\nvs.\n[%s]\n"%(k, self._compute_md5_text(files[0]), self._compute_md5_text(f)))
00124
00125 def test_md5_not_equals(self):
00126 tests = self._load_md5_tests('different')
00127 for k, files in tests.iteritems():
00128 print "running tests", k
00129 md5s = set()
00130 md6md5sum = self._compute_md5(files[0])
00131 for f in files:
00132 md5s.add(self._compute_md5(f))
00133
00134 self.assertEquals(len(md5s), len(files))
00135
00136 if __name__ == '__main__':
00137 rosunit.unitrun(PKG, NAME, TestGentools, sys.argv, coverage_packages=['roslib.gentools'])