|
| 1 | +""" |
| 2 | +# Copyright (c) 2025 PaddlePaddle Authors. All Rights Reserved. |
| 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 | +# 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 | +import unittest |
| 18 | + |
| 19 | +from partial_json_parser.core.options import Allow |
| 20 | + |
| 21 | +from fastdeploy.entrypoints.openai.tool_parsers import utils |
| 22 | + |
| 23 | + |
| 24 | +class TestPartialJsonUtils(unittest.TestCase): |
| 25 | + """Unit test suite for partial JSON utility functions.""" |
| 26 | + |
| 27 | + def test_find_common_prefix(self): |
| 28 | + """Test common prefix detection between two strings.""" |
| 29 | + string1 = '{"fruit": "ap"}' |
| 30 | + string2 = '{"fruit": "apple"}' |
| 31 | + self.assertEqual(utils.find_common_prefix(string1, string2), '{"fruit": "ap') |
| 32 | + |
| 33 | + def test_find_common_suffix(self): |
| 34 | + """Test common suffix detection between two strings.""" |
| 35 | + string1 = '{"fruit": "ap"}' |
| 36 | + string2 = '{"fruit": "apple"}' |
| 37 | + self.assertEqual(utils.find_common_suffix(string1, string2), '"}') |
| 38 | + |
| 39 | + def test_extract_intermediate_diff(self): |
| 40 | + """Test extraction of intermediate difference between current and old strings.""" |
| 41 | + old_string = '{"fruit": "ap"}' |
| 42 | + current_string = '{"fruit": "apple"}' |
| 43 | + self.assertEqual(utils.extract_intermediate_diff(current_string, old_string), "ple") |
| 44 | + |
| 45 | + def test_find_all_indices(self): |
| 46 | + """Test finding all occurrence indices of a substring in a string.""" |
| 47 | + target_string = "banana" |
| 48 | + substring = "an" |
| 49 | + self.assertEqual(utils.find_all_indices(target_string, substring), [1, 3]) |
| 50 | + |
| 51 | + def test_partial_json_loads_complete(self): |
| 52 | + """Test partial_json_loads with a complete JSON string.""" |
| 53 | + input_json = '{"a": 1, "b": 2}' |
| 54 | + parse_flags = Allow.ALL |
| 55 | + parsed_obj, parsed_length = utils.partial_json_loads(input_json, parse_flags) |
| 56 | + self.assertEqual(parsed_obj, {"a": 1, "b": 2}) |
| 57 | + self.assertEqual(parsed_length, len(input_json)) |
| 58 | + |
| 59 | + def test_is_complete_json(self): |
| 60 | + """Test JSON completeness check.""" |
| 61 | + self.assertTrue(utils.is_complete_json('{"a": 1}')) |
| 62 | + self.assertFalse(utils.is_complete_json('{"a": 1')) |
| 63 | + |
| 64 | + def test_consume_space(self): |
| 65 | + """Test whitespace consumption from the start of a string.""" |
| 66 | + input_string = " \t\nabc" |
| 67 | + # 3 spaces + 1 tab + 1 newline = 5 whitespace characters |
| 68 | + first_non_whitespace_idx = utils.consume_space(0, input_string) |
| 69 | + self.assertEqual(first_non_whitespace_idx, 5) |
| 70 | + |
| 71 | + |
| 72 | +if __name__ == "__main__": |
| 73 | + unittest.main() |
0 commit comments