class TestGeneratorAdderOnCode:
    def setUp(self):
        self.generator = UnittestTestGenerator()
        self.project = EmptyProject()
        self.module = self.project.create_module("module.py")
        self.test_module = self.project.create_module("test_module.py")

    def _test_module_from_code(self, code):
        return inspect_code(self.project, "test_module.py", code)

    def _test_class_from_code(self, code, name, method):
        return self.generator._generate_test_class(
            name, [TestMethodDescription(method, self.generator.template)],
            self.module, code)

    def test_appends_new_test_methods_to_test_classes_with_proper_indentation(
            self):
        module = self._test_module_from_code(
            "class NewTestClass(unittest.TestCase):\n"\
            "    def test_some_method(self):\n"\
            "        assert False # c'mon, implement me\n")
        klass = self._test_class_from_code(
            "class NewTestClass(unittest.TestCase):\n"\
            "    def test_new_method(self):\n"\
            "        assert True # ha!\n",
            "NewTestClass", "test_new_method")
        expected_output = "class NewTestClass(unittest.TestCase):\n"\
            "    def test_some_method(self):\n"\
            "        assert False # c'mon, implement me\n\n"\
            "    def test_new_method(self):\n"\
            "        assert True # ha!\n"

        add_test_case_to_project(self.project, klass)

        assert_equal_strings(expected_output, module.get_content())

    def test_keeps_future_imports_first(self):
        module = self._test_module_from_code(
            "from __future__ import division\n")
        self.generator.ensure_import(('nose', 'SkipTest'))
        klass = self._test_class_from_code("", "TestClass", "test_method")

        add_test_case_to_project(self.project, klass)

        assert_matches(
            r"from __future__ import division.*from nose import SkipTest",
            module.get_content())
class TestGeneratorAdderOnCode:
    def setUp(self):
        self.generator = UnittestTestGenerator()
        self.project = EmptyProject()
        self.module = self.project.create_module("module.py")
        self.test_module = self.project.create_module("test_module.py")

    def _test_module_from_code(self, code):
        return inspect_code(self.project, "test_module.py", code)

    def _test_class_from_code(self, code, name, method):
        return self.generator._generate_test_class(name,
            [TestMethodDescription(method, self.generator.template)], self.module, code)

    def test_appends_new_test_methods_to_test_classes_with_proper_indentation(self):
        module = self._test_module_from_code(
            "class NewTestClass(unittest.TestCase):\n"\
            "    def test_some_method(self):\n"\
            "        assert False # c'mon, implement me\n")
        klass = self._test_class_from_code(
            "class NewTestClass(unittest.TestCase):\n"\
            "    def test_new_method(self):\n"\
            "        assert True # ha!\n",
            "NewTestClass", "test_new_method")
        expected_output = "class NewTestClass(unittest.TestCase):\n"\
            "    def test_some_method(self):\n"\
            "        assert False # c'mon, implement me\n\n"\
            "    def test_new_method(self):\n"\
            "        assert True # ha!\n"

        add_test_case_to_project(self.project, klass)

        assert_equal_strings(expected_output, module.get_content())

    def test_keeps_future_imports_first(self):
        module = self._test_module_from_code("from __future__ import division\n")
        self.generator.ensure_import(('nose', 'SkipTest'))
        klass = self._test_class_from_code("", "TestClass", "test_method")

        add_test_case_to_project(self.project, klass)

        assert_matches(r"from __future__ import division.*from nose import SkipTest",
            module.get_content())
 def setUp(self):
     self.generator = UnittestTestGenerator()
     self.project = EmptyProject()
     self.module = self.project.create_module("module.py")
     self.test_module = self.project.create_module("test_module.py")
 def setUp(self):
     self.generator = UnittestTestGenerator()
     self.project = EmptyProject()
     self.module = self.project.create_module("module.py")
     self.test_module = self.project.create_module("test_module.py")