|
| 1 | +# SPDX-FileCopyrightText: Copyright (c) 2023 NVIDIA CORPORATION & AFFILIATES. All rights reserved. |
| 2 | +# SPDX-License-Identifier: Apache-2.0 |
| 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 | +from nemoguardrails.actions.llm.generation import safe_eval |
| 17 | + |
| 18 | + |
| 19 | +def test_safe_eval_double_quotes_with_single_quote(): |
| 20 | + """Test safe_eval with a string wrapped in double quotes that contains a single quote.""" |
| 21 | + input_value = '"It\'s a sunny day"' |
| 22 | + result = safe_eval(input_value) |
| 23 | + assert result == "It's a sunny day" |
| 24 | + |
| 25 | + |
| 26 | +def test_safe_eval_double_quotes_with_nested_single_quote(): |
| 27 | + """Test safe_eval with a string wrapped in double quotes that contains a nested single quote.""" |
| 28 | + input_value = "\"He said, 'Hello'\"" |
| 29 | + result = safe_eval(input_value) |
| 30 | + assert result == "He said, 'Hello'" |
| 31 | + |
| 32 | + |
| 33 | +def test_safe_eval_unquoted_string_with_single_quote(): |
| 34 | + """Test safe_eval with a string not wrapped in quotes, containing a single quote.""" |
| 35 | + input_value = "It's a sunny day" |
| 36 | + result = safe_eval(input_value) |
| 37 | + assert result == "It's a sunny day" |
| 38 | + |
| 39 | + |
| 40 | +def test_safe_eval_unquoted_plain_string(): |
| 41 | + """Test safe_eval with a plain string not wrapped in quotes.""" |
| 42 | + input_value = "It is a sunny day" |
| 43 | + result = safe_eval(input_value) |
| 44 | + assert result == "It is a sunny day" |
| 45 | + |
| 46 | + |
| 47 | +def test_safe_eval_empty_string(): |
| 48 | + """Test safe_eval with an empty string as input.""" |
| 49 | + input_value = "" |
| 50 | + result = safe_eval(input_value) |
| 51 | + assert result == "" |
0 commit comments