Go to the documentation of this file.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 import os
00035 import string
00036 import sys
00037 import unittest
00038 import cStringIO
00039
00040 import roslib.names
00041 import roslib.packages
00042
00043 TEST_CTX = 'rosgraph_msgs'
00044
00045 import roslib
00046 PKG='test_roslib_comm'
00047
00048 class TestGentools(unittest.TestCase):
00049
00050 def setUp(self):
00051 pass
00052
00053 def _load_md5_tests(self, dir):
00054 test_dir = os.path.join(roslib.packages.get_pkg_dir(PKG), 'test', 'md5tests', dir)
00055 tests = {}
00056 for f in os.listdir(test_dir):
00057 path = os.path.join(test_dir, f)
00058 if not f.endswith('.txt'):
00059 continue
00060 name = f[:-4]
00061 while name and name[-1].isdigit():
00062 name = name[:-1]
00063 self.assert_(name)
00064 if name in tests:
00065 tests[name].append(path)
00066 else:
00067 tests[name] = [path]
00068 return tests
00069
00070 def _compute_md5(self, f):
00071 from roslib.gentools import compute_md5, get_dependencies
00072 from roslib.msgs import load_from_string
00073
00074 text = open(f, 'r').read()
00075 spec = load_from_string(text, package_context=TEST_CTX)
00076 get_deps_dict = get_dependencies(spec, TEST_CTX, compute_files=False)
00077 return compute_md5(get_deps_dict)
00078
00079 def _compute_md5_text(self, f):
00080 from roslib.gentools import compute_md5_text, get_dependencies
00081 from roslib.msgs import load_from_string
00082
00083 text = open(f, 'r').read()
00084 spec = load_from_string(text, package_context=TEST_CTX)
00085 get_deps_dict = get_dependencies(spec, TEST_CTX, compute_files=False)
00086 return compute_md5_text(get_deps_dict, spec)
00087
00088 def test_compute_md5_text(self):
00089 from std_msgs.msg import Header
00090 Header_md5 = Header._md5sum
00091 rg_msg_dir = os.path.join(roslib.packages.get_pkg_dir(TEST_CTX), 'msg')
00092 clock_msg = os.path.join(rg_msg_dir, 'Clock.msg')
00093
00094 self.assertEquals("time clock", self._compute_md5_text(clock_msg))
00095 log_msg = os.path.join(rg_msg_dir, 'Log.msg')
00096 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))
00097
00098 tests = self._load_md5_tests('md5text')
00099
00100 for k, files in tests.iteritems():
00101 print "running tests", k
00102 ref_file = [f for f in files if f.endswith('%s1.txt'%k)]
00103 if not ref_file:
00104 self.fail("failed to load %s"%k)
00105 ref_file = ref_file[0]
00106 ref_text = open(ref_file, 'r').read().strip()
00107 print "KEY", k
00108 files = [f for f in files if not f.endswith('%s1.txt'%k)]
00109 for f in files[1:]:
00110 f_text = self._compute_md5_text(f)
00111 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))
00112
00113 def test_md5_equals(self):
00114 tests = self._load_md5_tests('same')
00115 for k, files in tests.iteritems():
00116 print "running tests", k
00117 md5sum = self._compute_md5(files[0])
00118 for f in files[1:]:
00119 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)))
00120
00121 def test_md5_not_equals(self):
00122 tests = self._load_md5_tests('different')
00123 for k, files in tests.iteritems():
00124 print "running tests", k
00125 md5s = set()
00126 md6md5sum = self._compute_md5(files[0])
00127 for f in files:
00128 md5s.add(self._compute_md5(f))
00129
00130 self.assertEquals(len(md5s), len(files))