codingutf8 2023 huggingface 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 orglicenseslicense2 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 fake function we will use as tool evaluate returns the value of the last assignment won t work without the tool evaluate returns the value of the last assignment evaluate returns the value of the last assignment evaluate returns the value of the last assignment evaluate returns the value of the last assignment coding utf 8 2023 huggingface 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 fake function we will use as tool evaluate returns the value of the last assignment won t work without the tool evaluate returns the value of the last assignment evaluate returns the value of the last assignment evaluate returns the value of the last assignment evaluate returns the value of the last assignment
import unittest

from transformers.testing_utils import CaptureStdout
from transformers.tools.python_interpreter import evaluate



def add_two(x):
    return x + 2


class PythonInterpreterTester(unittest.TestCase):
    def test_evaluate_assign(self):
        code = "x = 3"
        state = {}
        result = evaluate(code, {}, state=state)
        assert result == 3
        self.assertDictEqual(state, {"x": 3})

        code = "x = y"
        state = {"y": 5}
        result = evaluate(code, {}, state=state)
        
        assert result == 5
        self.assertDictEqual(state, {"x": 5, "y": 5})

    def test_evaluate_call(self):
        code = "y = add_two(x)"
        state = {"x": 3}
        result = evaluate(code, {"add_two": add_two}, state=state)
        assert result == 5
        self.assertDictEqual(state, {"x": 3, "y": 5})

        
        with CaptureStdout() as out:
            result = evaluate(code, {}, state=state)
        assert result is None
        assert "tried to execute add_two" in out.out

    def test_evaluate_constant(self):
        code = "x = 3"
        state = {}
        result = evaluate(code, {}, state=state)
        assert result == 3
        self.assertDictEqual(state, {"x": 3})

    def test_evaluate_dict(self):
        code = "test_dict = {'x': x, 'y': add_two(x)}"
        state = {"x": 3}
        result = evaluate(code, {"add_two": add_two}, state=state)
        self.assertDictEqual(result, {"x": 3, "y": 5})
        self.assertDictEqual(state, {"x": 3, "test_dict": {"x": 3, "y": 5}})

    def test_evaluate_expression(self):
        code = "x = 3\ny = 5"
        state = {}
        result = evaluate(code, {}, state=state)
        
        assert result == 5
        self.assertDictEqual(state, {"x": 3, "y": 5})

    def test_evaluate_f_string(self):
        code = "text = f'This is x: {x}.'"
        state = {"x": 3}
        result = evaluate(code, {}, state=state)
        
        assert result == "This is x: 3."
        self.assertDictEqual(state, {"x": 3, "text": "This is x: 3."})

    def test_evaluate_if(self):
        code = "if x <= 3:\n    y = 2\nelse:\n    y = 5"
        state = {"x": 3}
        result = evaluate(code, {}, state=state)
        
        assert result == 2
        self.assertDictEqual(state, {"x": 3, "y": 2})

        state = {"x": 8}
        result = evaluate(code, {}, state=state)
        
        assert result == 5
        self.assertDictEqual(state, {"x": 8, "y": 5})

    def test_evaluate_list(self):
        code = "test_list = [x, add_two(x)]"
        state = {"x": 3}
        result = evaluate(code, {"add_two": add_two}, state=state)
        self.assertListEqual(result, [3, 5])
        self.assertDictEqual(state, {"x": 3, "test_list": [3, 5]})

    def test_evaluate_name(self):
        code = "y = x"
        state = {"x": 3}
        result = evaluate(code, {}, state=state)
        assert result == 3
        self.assertDictEqual(state, {"x": 3, "y": 3})

    def test_evaluate_subscript(self):
        code = "test_list = [x, add_two(x)]\ntest_list[1]"
        state = {"x": 3}
        result = evaluate(code, {"add_two": add_two}, state=state)
        assert result == 5
        self.assertDictEqual(state, {"x": 3, "test_list": [3, 5]})

        code = "test_dict = {'x': x, 'y': add_two(x)}\ntest_dict['y']"
        state = {"x": 3}
        result = evaluate(code, {"add_two": add_two}, state=state)
        assert result == 5
        self.assertDictEqual(state, {"x": 3, "test_dict": {"x": 3, "y": 5}})

    def test_evaluate_for(self):
        code = "x = 0\nfor i in range(3):\n    x = i"
        state = {}
        result = evaluate(code, {"range": range}, state=state)
        assert result == 2
        self.assertDictEqual(state, {"x": 2, "i": 2})