$search
00001 #!/usr/bin/env python 00002 # Software License Agreement (BSD License) 00003 # 00004 # Copyright (c) 2008, Willow Garage, Inc. 00005 # All rights reserved. 00006 # 00007 # Redistribution and use in source and binary forms, with or without 00008 # modification, are permitted provided that the following conditions 00009 # are met: 00010 # 00011 # * Redistributions of source code must retain the above copyright 00012 # notice, this list of conditions and the following disclaimer. 00013 # * Redistributions in binary form must reproduce the above 00014 # copyright notice, this list of conditions and the following 00015 # disclaimer in the documentation and/or other materials provided 00016 # with the distribution. 00017 # * Neither the name of Willow Garage, Inc. nor the names of its 00018 # contributors may be used to endorse or promote products derived 00019 # from this software without specific prior written permission. 00020 # 00021 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 00022 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 00023 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 00024 # FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 00025 # COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 00026 # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 00027 # BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 00028 # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 00029 # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 00030 # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 00031 # ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 00032 # POSSIBILITY OF SUCH DAMAGE. 00033 # 00034 # Revision $Id$ 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 # a bit gory, but go ahead and regression test these important messages 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 # text file #1 is the reference 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 # each md5 should be unique 00134 self.assertEquals(len(md5s), len(files)) 00135 00136 if __name__ == '__main__': 00137 rosunit.unitrun(PKG, NAME, TestGentools, sys.argv, coverage_packages=['roslib.gentools'])