# python3
# Copyright 2017 The TensorFlow Authors. All Rights Reserved.
#
# 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.
# ==============================================================================
"""Tests for call_trees module."""

from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

import imp

from tensorflow.python.autograph.converters import call_trees
from tensorflow.python.autograph.converters import functions
from tensorflow.python.autograph.core import converter_testing
from tensorflow.python.platform import test


class MockConvertedCall(object):

  def __init__(self):
    self.calls = []

  def __call__(self, f, args, kwargs, caller_fn_scope=None, options=None):
    del caller_fn_scope, options
    self.calls.append((args, kwargs))
    kwargs = kwargs or {}
    return f(*args, **kwargs)


class CallTreesTest(converter_testing.TestCase):

  def _transform_with_mock(self, f):
    mock = MockConvertedCall()
    tr = self.transform(
        f, (functions, call_trees),
        ag_overrides={'converted_call': mock})
    return tr, mock

  def test_function_no_args(self):

    def f(f):
      return f() + 20

    tr, mock = self._transform_with_mock(f)

    self.assertEqual(tr(lambda: 1), 21)
    self.assertListEqual(mock.calls, [((), None)])

  def test_function_with_expression_in_argument(self):