test_roslib_gentools.py
Go to the documentation of this file.
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 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         # a bit gory, but go ahead and regression test these important messages
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         # text file #1 is the reference
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             # each md5 should be unique
00130             self.assertEquals(len(md5s), len(files))


test_roslib_comm
Author(s): Jeremy Leibs/leibs@willowgarage.com, Ken Conley/kwc@willowgarage.com
autogenerated on Sat Dec 28 2013 17:43:03