-
Notifications
You must be signed in to change notification settings - Fork 214
Add unit tests for sub-command tab completers #1227
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 2 commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,65 @@ | ||
| # Copyright 2019 Open Source Robotics Foundation, Inc. | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
|
|
||
|
|
||
| def test_node_name_completer(): | ||
| from unittest.mock import Mock, patch | ||
| from ros2node.api import NodeNameCompleter, NodeName | ||
|
|
||
| parsed_args = Mock() | ||
|
|
||
| # Test 1: no include_hidden_nodes_key, defaults to False | ||
| completer = NodeNameCompleter() | ||
| with patch('ros2node.api.NodeStrategy') as mock_node_strategy: | ||
| mock_node = Mock() | ||
| mock_node_strategy.return_value.__enter__ = Mock(return_value=mock_node) | ||
| mock_node_strategy.return_value.__exit__ = Mock(return_value=False) | ||
| with patch('ros2node.api.get_node_names') as mock_get_names: | ||
| mock_get_names.return_value = [ | ||
| NodeName('node1', '/', '/node1'), | ||
| NodeName('node2', '/ns', '/ns/node2'), | ||
| ] | ||
| result = completer(prefix='', parsed_args=parsed_args) | ||
| assert result == ['/node1', '/ns/node2'] | ||
| mock_get_names.assert_called_once_with( | ||
| node=mock_node, include_hidden_nodes=False) | ||
|
|
||
| # Test 2: include_hidden_nodes_key provided and True | ||
| parsed_args.include_hidden_nodes = True | ||
| completer = NodeNameCompleter(include_hidden_nodes_key='include_hidden_nodes') | ||
| with patch('ros2node.api.NodeStrategy') as mock_node_strategy: | ||
| mock_node = Mock() | ||
| mock_node_strategy.return_value.__enter__ = Mock(return_value=mock_node) | ||
| mock_node_strategy.return_value.__exit__ = Mock(return_value=False) | ||
| with patch('ros2node.api.get_node_names') as mock_get_names: | ||
| mock_get_names.return_value = [ | ||
| NodeName('node1', '/', '/node1'), | ||
| NodeName('_hidden', '/', '/_hidden'), | ||
| ] | ||
| result = completer(prefix='', parsed_args=parsed_args) | ||
| assert result == ['/node1', '/_hidden'] | ||
| mock_get_names.assert_called_once_with( | ||
| node=mock_node, include_hidden_nodes=True) | ||
|
|
||
| # Test 3: no nodes available | ||
| parsed_args.include_hidden_nodes = False | ||
| completer = NodeNameCompleter(include_hidden_nodes_key='include_hidden_nodes') | ||
| with patch('ros2node.api.NodeStrategy') as mock_node_strategy: | ||
| mock_node = Mock() | ||
| mock_node_strategy.return_value.__enter__ = Mock(return_value=mock_node) | ||
| mock_node_strategy.return_value.__exit__ = Mock(return_value=False) | ||
| with patch('ros2node.api.get_node_names') as mock_get_names: | ||
| mock_get_names.return_value = [] | ||
| result = completer(prefix='', parsed_args=parsed_args) | ||
| assert result == [] | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,45 @@ | ||
| # Copyright 2019 Open Source Robotics Foundation, Inc. | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
|
|
||
|
|
||
| def test_executable_name_completer(): | ||
| from unittest.mock import Mock, patch | ||
| from ros2pkg.api import PackageNotFound | ||
| from ros2run.api import ExecutableNameCompleter | ||
|
|
||
| parsed_args = Mock() | ||
| parsed_args.package_name = 'my_package' | ||
| completer = ExecutableNameCompleter(package_name_key='package_name') | ||
|
|
||
| # Test 1: package has executables | ||
| with patch('ros2run.api.get_executable_paths') as mock_get_paths: | ||
| mock_get_paths.return_value = [ | ||
| '/opt/ros/humble/lib/my_package/my_exec', | ||
| '/opt/ros/humble/lib/my_package/other_exec', | ||
| ] | ||
| result = completer(prefix='', parsed_args=parsed_args) | ||
| assert result == ['my_exec', 'other_exec'] | ||
| mock_get_paths.assert_called_once_with(package_name='my_package') | ||
|
|
||
| # Test 2: package not found raises PackageNotFound, returns empty list | ||
| with patch('ros2run.api.get_executable_paths') as mock_get_paths: | ||
| mock_get_paths.side_effect = PackageNotFound('my_package') | ||
| result = completer(prefix='', parsed_args=parsed_args) | ||
| assert result == [] | ||
|
|
||
| # Test 3: package has no executables | ||
| with patch('ros2run.api.get_executable_paths') as mock_get_paths: | ||
| mock_get_paths.return_value = [] | ||
| result = completer(prefix='', parsed_args=parsed_args) | ||
| assert result == [] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,128 @@ | ||
| # Copyright 2019 Open Source Robotics Foundation, Inc. | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
|
|
||
|
|
||
| def test_service_name_completer(): | ||
| from unittest.mock import Mock, patch | ||
| from ros2service.api import ServiceNameCompleter | ||
|
|
||
| parsed_args = Mock() | ||
| parsed_args.include_hidden_services = False | ||
| completer = ServiceNameCompleter( | ||
| include_hidden_services_key='include_hidden_services') | ||
|
|
||
| # Test 1: returns service names | ||
| with patch('ros2service.api.NodeStrategy') as mock_node_strategy: | ||
| mock_node = Mock() | ||
| mock_node_strategy.return_value.__enter__ = Mock(return_value=mock_node) | ||
| mock_node_strategy.return_value.__exit__ = Mock(return_value=False) | ||
| with patch('ros2service.api.get_service_names') as mock_get_names: | ||
| mock_get_names.return_value = ['/service1', '/service2'] | ||
| result = completer(prefix='', parsed_args=parsed_args) | ||
| assert result == ['/service1', '/service2'] | ||
| mock_get_names.assert_called_once_with( | ||
| node=mock_node, include_hidden_services=False) | ||
|
|
||
| # Test 2: no services available | ||
| with patch('ros2service.api.NodeStrategy') as mock_node_strategy: | ||
| mock_node = Mock() | ||
| mock_node_strategy.return_value.__enter__ = Mock(return_value=mock_node) | ||
| mock_node_strategy.return_value.__exit__ = Mock(return_value=False) | ||
| with patch('ros2service.api.get_service_names') as mock_get_names: | ||
| mock_get_names.return_value = [] | ||
| result = completer(prefix='', parsed_args=parsed_args) | ||
| assert result == [] | ||
|
|
||
| # Test 3: include hidden services | ||
| parsed_args.include_hidden_services = True | ||
| with patch('ros2service.api.NodeStrategy') as mock_node_strategy: | ||
| mock_node = Mock() | ||
| mock_node_strategy.return_value.__enter__ = Mock(return_value=mock_node) | ||
| mock_node_strategy.return_value.__exit__ = Mock(return_value=False) | ||
| with patch('ros2service.api.get_service_names') as mock_get_names: | ||
| mock_get_names.return_value = ['/service1', '/_hidden_service'] | ||
| result = completer(prefix='', parsed_args=parsed_args) | ||
| assert result == ['/service1', '/_hidden_service'] | ||
| mock_get_names.assert_called_once_with( | ||
| node=mock_node, include_hidden_services=True) | ||
|
|
||
|
|
||
| def test_service_type_completer(): | ||
| from unittest.mock import Mock, patch | ||
| from ros2service.api import ServiceTypeCompleter | ||
|
|
||
| parsed_args = Mock() | ||
|
|
||
| # Test 1: no service_name_key, returns all service types | ||
| completer = ServiceTypeCompleter() | ||
| with patch('ros2service.api.service_type_completer') as mock_all_types: | ||
| mock_all_types.return_value = ['std_srvs/Empty', 'std_srvs/SetBool'] | ||
| result = completer(prefix='', parsed_args=parsed_args) | ||
| assert result == ['std_srvs/Empty', 'std_srvs/SetBool'] | ||
| mock_all_types.assert_called_once() | ||
|
|
||
| # Test 2: service_name_key provided and service found | ||
| parsed_args.service_name = '/my_service' | ||
| completer = ServiceTypeCompleter(service_name_key='service_name') | ||
| with patch('ros2service.api.NodeStrategy') as mock_node_strategy: | ||
| mock_node = Mock() | ||
| mock_node_strategy.return_value.__enter__ = Mock(return_value=mock_node) | ||
| mock_node_strategy.return_value.__exit__ = Mock(return_value=False) | ||
| with patch('ros2service.api.get_service_names_and_types') as mock_get_names_types: | ||
| mock_get_names_types.return_value = [ | ||
| ('/my_service', ['std_srvs/Empty']), | ||
| ('/other_service', ['std_srvs/SetBool']), | ||
| ] | ||
| result = completer(prefix='', parsed_args=parsed_args) | ||
| assert result == ['std_srvs/Empty'] | ||
|
|
||
| # Test 3: service_name_key provided but service not found, falls back to all types | ||
| parsed_args.service_name = '/nonexistent_service' | ||
| completer = ServiceTypeCompleter(service_name_key='service_name') | ||
| with patch('ros2service.api.NodeStrategy') as mock_node_strategy: | ||
| mock_node = Mock() | ||
| mock_node_strategy.return_value.__enter__ = Mock(return_value=mock_node) | ||
| mock_node_strategy.return_value.__exit__ = Mock(return_value=False) | ||
| with patch('ros2service.api.get_service_names_and_types') as mock_get_names_types: | ||
| mock_get_names_types.return_value = [ | ||
| ('/my_service', ['std_srvs/Empty']), | ||
| ] | ||
| with patch('ros2service.api.service_type_completer') as mock_all_types: | ||
| mock_all_types.return_value = ['std_srvs/Empty', 'std_srvs/SetBool'] | ||
| result = completer(prefix='', parsed_args=parsed_args) | ||
| assert result == ['std_srvs/Empty', 'std_srvs/SetBool'] | ||
|
|
||
|
|
||
| def test_service_prototype_completer(): | ||
| from unittest.mock import Mock, patch | ||
| from ros2service.api import ServicePrototypeCompleter | ||
|
|
||
| parsed_args = Mock() | ||
| parsed_args.service_type = 'std_srvs/SetBool' | ||
| completer = ServicePrototypeCompleter(service_type_key='service_type') | ||
|
|
||
| mock_service_class = Mock() | ||
| mock_request_instance = Mock() | ||
| mock_service_class.Request.return_value = mock_request_instance | ||
|
|
||
| with patch('ros2service.api.get_service') as mock_get_service: | ||
| with patch('ros2service.api.message_to_yaml') as mock_message_to_yaml: | ||
| mock_get_service.return_value = mock_service_class | ||
| mock_message_to_yaml.return_value = 'data: false' | ||
|
|
||
| result = completer(prefix='', parsed_args=parsed_args) | ||
|
|
||
| mock_get_service.assert_called_once_with('std_srvs/SetBool') | ||
| mock_message_to_yaml.assert_called_once_with(mock_request_instance) | ||
| assert result == ['data: false'] |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.