cm_msgs_utils_test.py
Go to the documentation of this file.
00001 #!/usr/bin/env python
00002 
00003 # Copyright (C) 2014, PAL Robotics S.L.
00004 #
00005 # Redistribution and use in source and binary forms, with or without
00006 # modification, are permitted provided that the following conditions are met:
00007 # * Redistributions of source code must retain the above copyright notice,
00008 # this list of conditions and the following disclaimer.
00009 # * Redistributions in binary form must reproduce the above copyright
00010 # notice, this list of conditions and the following disclaimer in the
00011 # documentation and/or other materials provided with the distribution.
00012 # * Neither the name of PAL Robotics S.L. nor the names of its
00013 # contributors may be used to endorse or promote products derived from
00014 # this software without specific prior written permission.
00015 #
00016 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00017 # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00018 # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00019 # ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
00020 # LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
00021 # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
00022 # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
00023 # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
00024 # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
00025 # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00026 # POSSIBILITY OF SUCH DAMAGE.
00027 
00028 from controller_manager_msgs.msg import ControllerState as CtrlState
00029 from controller_manager_msgs.msg import HardwareInterfaceResources
00030 from controller_manager_msgs.utils import *
00031 
00032 ctrl_list = [
00033     CtrlState(name='foo_controller',
00034               state='running',
00035               type='foo_base/foo',
00036               claimed_resources=[
00037                   HardwareInterfaceResources(
00038                       hardware_interface='hardware_interface::FooInterface',
00039                       resources=['one', 'two', 'three'])
00040               ]),
00041     CtrlState(name='bar_controller',
00042               state='running',
00043               type='bar_base/bar',
00044               claimed_resources=[
00045                   HardwareInterfaceResources(
00046                       hardware_interface='hardware_interface::BarInterface',
00047                       resources=['four'])
00048               ]),
00049     CtrlState(name='foobar_controller',
00050               state='stopped',
00051               type='foobar_base/foobar',
00052               claimed_resources=[
00053                   HardwareInterfaceResources(
00054                       hardware_interface='hardware_interface::FooBarInterface',
00055                       resources=['one'])
00056               ]),
00057     CtrlState(name='foobaz_controller',
00058               state='running',
00059               type='foobaz_base/foobaz',
00060               claimed_resources=[
00061                   HardwareInterfaceResources(
00062                       hardware_interface='hardware_interface::FooInterface',
00063                       resources=['one']),
00064                   HardwareInterfaceResources(
00065                       hardware_interface='hardware_interface::BazInterface',
00066                       resources=['one', 'two'])
00067                    ])
00068 ]
00069 
00070 
00071 def filter_by_name_test():
00072     # Non-existing
00073     assert(not filter_by_name(ctrl_list, 'null', match_substring=False))
00074     assert(not filter_by_name(ctrl_list, 'null', match_substring=True))
00075 
00076     # Existing, full match
00077     out = filter_by_name(ctrl_list, 'foo_controller')
00078     assert(1 == len(out))
00079     assert(out[0].name == 'foo_controller')
00080 
00081     # Existing, substring match
00082     out = filter_by_name(ctrl_list, 'foo_controller', match_substring=True)
00083     assert(1 == len(out))
00084     assert(out[0].name == 'foo_controller')
00085 
00086     out = filter_by_name(ctrl_list, 'foo', match_substring=True)
00087     assert(3 == len(out))
00088     assert(out[0].name == 'foo_controller')
00089     assert(out[1].name == 'foobar_controller')
00090     assert(out[2].name == 'foobaz_controller')
00091 
00092 
00093 def filter_by_state_test():
00094     # Non-existing
00095     assert(not filter_by_state(ctrl_list, 'null', match_substring=False))
00096     assert(not filter_by_state(ctrl_list, 'null', match_substring=True))
00097 
00098     # Existing, full match
00099     out = filter_by_state(ctrl_list, 'stopped')
00100     assert(1 == len(out))
00101     assert(out[0].name == 'foobar_controller')
00102 
00103     # Existing, substring match
00104     out = filter_by_state(ctrl_list, 'stopped', match_substring=True)
00105     assert(1 == len(out))
00106     assert(out[0].name == 'foobar_controller')
00107 
00108     out = filter_by_state(ctrl_list, 'run', match_substring=True)
00109     assert(3 == len(out))
00110     assert(out[0].name == 'foo_controller')
00111     assert(out[1].name == 'bar_controller')
00112     assert(out[2].name == 'foobaz_controller')
00113 
00114 
00115 def filter_by_type_test():
00116     # Non-existing
00117     assert(not filter_by_type(ctrl_list, 'null', match_substring=False))
00118     assert(not filter_by_type(ctrl_list, 'null', match_substring=True))
00119 
00120     # Existing, full match
00121     out = filter_by_type(ctrl_list, 'foo_base/foo')
00122     assert(1 == len(out))
00123     assert(out[0].name == 'foo_controller')
00124 
00125     # Existing, substring match
00126     out = filter_by_type(ctrl_list, 'foo_base/foo', match_substring=True)
00127     assert(1 == len(out))
00128     assert(out[0].name == 'foo_controller')
00129 
00130     out = filter_by_type(ctrl_list, 'foo', match_substring=True)
00131     assert(3 == len(out))
00132     assert(out[0].name == 'foo_controller')
00133     assert(out[1].name == 'foobar_controller')
00134     assert(out[2].name == 'foobaz_controller')
00135 
00136 
00137 def filter_by_hardware_interface_test():
00138     # Non-existing
00139     assert(not filter_by_hardware_interface(ctrl_list,
00140                                             'null',
00141                                             match_substring=False))
00142     assert(not filter_by_hardware_interface(ctrl_list,
00143                                             'null',
00144                                             match_substring=True))
00145 
00146     # Existing, full match
00147     out = filter_by_hardware_interface(ctrl_list,
00148                                        'hardware_interface::FooInterface')
00149     assert(2 == len(out))
00150     assert(out[0].name == 'foo_controller')
00151     assert(out[1].name == 'foobaz_controller')
00152 
00153     # Existing, substring match
00154     out = filter_by_hardware_interface(ctrl_list,
00155                                        'hardware_interface::BazInterface',
00156                                        match_substring=True)
00157     assert(1 == len(out))
00158     assert(out[0].name == 'foobaz_controller')
00159 
00160     out = filter_by_hardware_interface(ctrl_list,
00161                                        'BazInterface',
00162                                        match_substring=True)
00163     assert(1 == len(out))
00164     assert(out[0].name == 'foobaz_controller')
00165 
00166 
00167 def filter_by_resources_test():
00168     # Non-existing resource. Consider all hardware interfaces
00169     assert(not filter_by_resources(ctrl_list, ['null'], match_any=False))
00170     assert(not filter_by_resources(ctrl_list, ['null'], match_any=True))
00171 
00172     # Non-existing resource. Consider only specified hardware interfaces
00173     assert(not filter_by_resources(
00174         ctrl_list,
00175         hardware_interface='hardware_interface::FooInterface',
00176         resources=['null'],
00177         match_any=False))
00178 
00179     assert(not filter_by_resources(
00180         ctrl_list,
00181         hardware_interface='hardware_interface::FooInterface',
00182         resources=['null'],
00183         match_any=True))
00184 
00185     # Non-existing resource in specified interface, but existing in other
00186     # interfaces
00187     assert(not filter_by_resources(
00188         ctrl_list,
00189         hardware_interface='hardware_interface::FooInterface',
00190         resources=['four'],
00191         match_any=False))
00192 
00193     assert(not filter_by_resources(
00194         ctrl_list,
00195         hardware_interface='hardware_interface::FooInterface',
00196         resources=['four'],
00197         match_any=True))
00198 
00199     # Existing, full match. Consider all hardware interfaces
00200     res = ctrl_list[0].claimed_resources[0].resources
00201     out = filter_by_resources(ctrl_list, res)
00202     assert(1 == len(out))
00203     assert(out[0].name == 'foo_controller')
00204 
00205     # Existing, full match. Consider only specified hardware interfaces
00206     out = filter_by_resources(
00207         ctrl_list,
00208         hardware_interface='hardware_interface::FooInterface',
00209         resources=res)
00210     assert(1 == len(out))
00211     assert(out[0].name == 'foo_controller')
00212 
00213     out = filter_by_resources(
00214         ctrl_list,
00215         hardware_interface='hardware_interface::FooInterface',
00216         resources=['one'])
00217     assert(2 == len(out))
00218     assert(out[0].name == 'foo_controller')
00219     assert(out[1].name == 'foobaz_controller')
00220 
00221     # Existing, partial match. Consider all hardware interfaces
00222     out = filter_by_resources(ctrl_list, res, match_any=True)
00223     assert(3 == len(out))
00224     assert(out[0].name == 'foo_controller')
00225     assert(out[1].name == 'foobar_controller')
00226     assert(out[2].name == 'foobaz_controller')
00227 
00228     # Existing, partial match. Consider only specified hardware interfaces
00229     out = filter_by_resources(
00230         ctrl_list,
00231         hardware_interface='hardware_interface::BazInterface',
00232         resources=['one'],
00233         match_any=True)
00234     assert(1 == len(out))
00235     assert(out[0].name == 'foobaz_controller')


controller_manager_tests
Author(s): Vijay Pradeep, Adolfo Rodriguez Tsouroukdissian
autogenerated on Thu Dec 1 2016 03:46:03