#    Copyright 2015 EasyStack 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.

import testtools

from sahara.utils.hacking import checks


class HackingTestCase(testtools.TestCase):
    def test_dict_constructor_with_list_copy(self):
        # Following checks for code-lines with pep8 error
        self.assertEqual(1, len(list(checks.dict_constructor_with_list_copy(
            "    dict([(i, connect_info[i])"))))

        self.assertEqual(1, len(list(checks.dict_constructor_with_list_copy(
            "    attrs = dict([(k, _from_json(v))"))))

        self.assertEqual(1, len(list(checks.dict_constructor_with_list_copy(
            "        type_names = dict((value, key) for key, value in"))))

        self.assertEqual(1, len(list(checks.dict_constructor_with_list_copy(
            "   dict((value, key) for key, value in"))))

        self.assertEqual(1, len(list(checks.dict_constructor_with_list_copy(
            "foo(param=dict((k, v) for k, v in bar.items()))"))))

        self.assertEqual(1, len(list(checks.dict_constructor_with_list_copy(
            " dict([[i,i] for i in range(3)])"))))

        self.assertEqual(1, len(list(checks.dict_constructor_with_list_copy(
            "  dd = dict([i,i] for i in range(3))"))))
        # Following checks for ok code-lines
        self.assertEqual(0, len(list(checks.dict_constructor_with_list_copy(
            " dict()"))))

        self.assertEqual(0, len(list(checks.dict_constructor_with_list_copy(
            "        create_kwargs = dict(snapshot=snapshot,"))))

        self.assertEqual(0, len(list(checks.dict_constructor_with_list_copy(
            "      self._render_dict(xml, data_el, data.__dict__)"))))

    def test_use_jsonutils(self):
        self.assertEqual(0, len(list(checks.use_jsonutils(
            "import json    # noqa", "path"))))
        self.assertEqual(0, len(list(checks.use_jsonutils(
            "from oslo_serialization import jsonutils as json", "path"))))
        self.assertEqual(0, len(list(checks.use_jsonutils(
            "import jsonschema", "path"))))
        self.assertEqual(0, len(list(checks.use_jsonutils(
            "import json", "sahara/openstack/common/utils.py"))))

        self.assertEqual(1, len(list(checks.use_jsonutils(
            "import json", "path"))))
        self.assertEqual(1, len(list(checks.use_jsonutils(
            "import json as jsonutils", "path"))))

    def test_no_mutable_default_args(self):
        self.assertEqual(0, len(list(checks.no_mutable_default_args(
            "def foo (bar):"))))
        self.assertEqual(1, len(list(checks.no_mutable_default_args(
            "def foo (bar=[]):"))))
        self.assertEqual(1, len(list(checks.no_mutable_default_args(
            "def foo (bar={}):"))))
