|
| 1 | +""" |
| 2 | +Copyright 2025 Perforce Software, Inc. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | + 10|Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +""" |
| 16 | + |
| 17 | +from tools.utils import normalize_action_args |
| 18 | + |
| 19 | + |
| 20 | +class TestNormalizeActionArgs: |
| 21 | + def test_none_returns_empty_action_and_args(self): |
| 22 | + action, args = normalize_action_args(None) |
| 23 | + assert action == "" |
| 24 | + assert args == {} |
| 25 | + |
| 26 | + def test_empty_dict_returns_empty_action_and_args(self): |
| 27 | + action, args = normalize_action_args({}) |
| 28 | + assert action == "" |
| 29 | + assert args == {} |
| 30 | + |
| 31 | + def test_nested_action_and_args(self): |
| 32 | + action, args = normalize_action_args({ |
| 33 | + "action": "add_command", |
| 34 | + "args": {"test_id": "PRIVATE:Folder/Test.xml", "command_id": "wait"}, |
| 35 | + }) |
| 36 | + assert action == "add_command" |
| 37 | + assert args == {"test_id": "PRIVATE:Folder/Test.xml", "command_id": "wait"} |
| 38 | + |
| 39 | + def test_flattened_top_level_params_merge_into_args(self): |
| 40 | + action, args = normalize_action_args({ |
| 41 | + "action": "add_command", |
| 42 | + "test_id": "PRIVATE:Folder/Test.xml", |
| 43 | + "command_id": "wait", |
| 44 | + "cmd_arguments": {"duration": "1"}, |
| 45 | + }) |
| 46 | + assert action == "add_command" |
| 47 | + assert args == { |
| 48 | + "test_id": "PRIVATE:Folder/Test.xml", |
| 49 | + "command_id": "wait", |
| 50 | + "cmd_arguments": {"duration": "1"}, |
| 51 | + } |
| 52 | + |
| 53 | + def test_unwraps_double_wrapped_arguments(self): |
| 54 | + action, args = normalize_action_args({ |
| 55 | + "arguments": { |
| 56 | + "action": "add_command", |
| 57 | + "args": {"test_id": "PRIVATE:Folder/Test.xml"}, |
| 58 | + } |
| 59 | + }) |
| 60 | + assert action == "add_command" |
| 61 | + assert args == {"test_id": "PRIVATE:Folder/Test.xml"} |
| 62 | + |
| 63 | + def test_does_not_unwrap_arguments_when_other_keys_present(self): |
| 64 | + action, args = normalize_action_args({ |
| 65 | + "action": "add_command", |
| 66 | + "args": {"test_id": "PRIVATE:Folder/Test.xml", "command_id": "wait"}, |
| 67 | + "arguments": {"duration": "3"}, |
| 68 | + }) |
| 69 | + assert action == "add_command" |
| 70 | + assert args["test_id"] == "PRIVATE:Folder/Test.xml" |
| 71 | + assert args["command_id"] == "wait" |
| 72 | + assert args["arguments"] == {"duration": "3"} |
| 73 | + |
| 74 | + def test_strips_action_whitespace(self): |
| 75 | + action, args = normalize_action_args({"action": " list_tests ", "args": {}}) |
| 76 | + assert action == "list_tests" |
| 77 | + assert args == {} |
| 78 | + |
| 79 | + def test_none_args_value_becomes_empty_dict(self): |
| 80 | + action, args = normalize_action_args({"action": "list_tests", "args": None}) |
| 81 | + assert action == "list_tests" |
| 82 | + assert args == {} |
| 83 | + |
| 84 | + def test_args_is_never_none(self): |
| 85 | + _, args = normalize_action_args(None) |
| 86 | + assert args is not None |
| 87 | + _, args = normalize_action_args({"action": "x"}) |
| 88 | + assert args is not None |
| 89 | + |
| 90 | + def test_top_level_keys_overlay_nested_args(self): |
| 91 | + action, args = normalize_action_args({ |
| 92 | + "action": "add_command", |
| 93 | + "args": {"test_id": "nested", "command_id": "wait"}, |
| 94 | + "test_id": "top-level", |
| 95 | + }) |
| 96 | + assert action == "add_command" |
| 97 | + assert args["test_id"] == "top-level" |
| 98 | + assert args["command_id"] == "wait" |
0 commit comments