"""Unit tests for required-base-classes."""

import astroid
from pylint.testutils import CheckerTestCase, Message, set_config

from edx_lint.pylint.required_base_class import RequiredBaseClassChecker


class RequiredBaseClassTestCase(CheckerTestCase):
    """Unittest tests of RequiredBaseClassChecker."""

    CHECKER_CLASS = RequiredBaseClassChecker

    def get_class_node(self, code):
        """Parse `code`, and return the last class node.

        The code should have at least one class definition.

        """
        node = astroid.parse(code)
        class_node = None
        for body_node in node.body:
            if getattr(body_node, 'type', 'none') == "class":
                class_node = body_node
        return class_node

    def test_no_messages_by_default(self):
        node = self.get_class_node('''
            class MyClass(object):
                pass
        ''')
        with self.assertNoMessages():
            self.checker.visit_class(node)

    @set_config(required_base_class=["BaseClass:MyMixin"])
    def test_no_messages_if_class_not_used(self):
        node = self.get_class_node('''
            class MyClass(object):
                pass
        ''')
        with self.assertNoMessages():
            self.checker.visit_class(node)

    @set_config(required_base_class=["unittest.case.TestCase:.MyTestMixin"])
    def test_error_if_class_is_not_used(self):
        node = self.get_class_node('''
            from unittest import TestCase
            class MyClass(TestCase):
                pass
        ''')
        expected_msg = Message(
            'missing-required-base-class',
            node=node,
            args=('MyClass', '.MyTestMixin'),
        )
        with self.assertAddsMessages(expected_msg):
            self.checker.visit_class(node)

    @set_config(required_base_class=["unittest.case.TestCase:.MyTestMixin"])
    def test_no_messages_if_class_is_used(self):
        node = self.get_class_node('''
            from unittest import TestCase
            class MyTestMixin(object):
                pass
            class MyClass(MyTestMixin, TestCase):
                pass
        ''')
        with self.assertNoMessages():
            self.checker.visit_class(node)

    @set_config(required_base_class=["unittest.case.TestCase:.MyTestMixin"])
    def test_old_style_classes(self):
        # We don't support base class checking on old-style classes, but we
        # have to be sure not to fall over at least.
        node = self.get_class_node('''
            class MyClass:
                pass
        ''')
        with self.assertNoMessages():
            self.checker.visit_class(node)

@@ -350,400 +350,8 @@
 er%0A%0A
-    def get_class_node(self, code):%0A        %22%22%22Parse %60code%60, and return the last class node.%0A%0A        The code should have at least one class definition.%0A%0A        %22%22%22%0A        node = astroid.parse(code)%0A        class_node = None%0A        for body_node in node.body:%0A            if getattr(body_node, 'type', 'none') == %22class%22:%0A                class_node = body_node%0A        return class_node%0A%0A
     
@@ -404,34 +404,28 @@
  node = 
-self.get_class_nod
+astroid.pars
 e('''%0A  
@@ -546,35 +546,20 @@
    self.
-checker.visit_class
+walk
 (node)%0A%0A
@@ -682,34 +682,28 @@
  node = 
-self.get_class_nod
+astroid.pars
 e('''%0A  
@@ -824,35 +824,20 @@
    self.
-checker.visit_class
+walk
 (node)%0A%0A
@@ -975,34 +975,28 @@
  node = 
-self.get_class_nod
+astroid.pars
 e('''%0A  
@@ -1202,16 +1202,25 @@
 ode=node
+.body%5B-1%5D
 ,%0A      
@@ -1338,35 +1338,20 @@
    self.
-checker.visit_class
+walk
 (node)%0A%0A
@@ -1491,34 +1491,28 @@
  node = 
-self.get_class_nod
+astroid.pars
 e('''%0A  
@@ -1750,35 +1750,20 @@
    self.
-checker.visit_class
+walk
 (node)%0A%0A
@@ -2025,26 +2025,20 @@
 e = 
-self.get_class_nod
+astroid.pars
 e(''
@@ -2159,27 +2159,12 @@
 elf.
-checker.visit_class
+walk
 (nod

Simplify and robustify the tests