Exemplo n.º 1
0
    def test_colorify(self):
        """
        Test Prints().colorify(). In other word, it test the coloration
        of the line we have to print depending of the status.
        """

        File(self.file).delete()

        expected = False
        actual = PyFunceble.path.isfile(self.file)

        # Test with a template that is not designed for colorify
        expected = self.to_print["basic_string"]
        actual = Prints(None, "Hehehe", output_file=None, only_on_file=False)._colorify(
            self.to_print["basic_string"]
        )

        self.assertEqual(expected, actual)

        # Test with a template that is designed for colorify + Status is UP
        expected = (
            PyFunceble.Fore.BLACK
            + PyFunceble.Back.GREEN
            + self.to_print["basic_string"]
        )
        actual = Prints(
            ["This is a test", PyFunceble.STATUS["official"]["up"]],
            "Generic",
            output_file=None,
            only_on_file=False,
        )._colorify(self.to_print["basic_string"])

        self.assertEqual(expected, actual)

        # Test with a template that is designed for colorify + Status is DOWN
        expected = (
            PyFunceble.Fore.BLACK + PyFunceble.Back.RED + self.to_print["basic_string"]
        )
        actual = Prints(
            ["This is a test", PyFunceble.STATUS["official"]["down"]],
            "Generic",
            output_file=None,
            only_on_file=False,
        )._colorify(self.to_print["basic_string"])

        self.assertEqual(expected, actual)

        # Test with a template that is designed for colorify + Status is
        # UNKNOWN or INVALID
        expected = (
            PyFunceble.Fore.BLACK + PyFunceble.Back.CYAN + self.to_print["basic_string"]
        )
        actual = Prints(
            ["This is a test", PyFunceble.STATUS["official"]["invalid"]],
            "Generic",
            output_file=None,
            only_on_file=False,
        )._colorify(self.to_print["basic_string"])

        self.assertEqual(expected, actual)
Exemplo n.º 2
0
    def test_header_constructor_without_separator(self):  # pylint: disable=invalid-name
        """
        Test Prints()._header_constructor() for the case that we
        want to print the result of the test.
        """

        File(self.file).delete()

        expected = False
        actual = PyFunceble.path.isfile(self.file)

        expected = ["hello world  here    is       PyFunceble"]
        actual = Prints(
            None, None, output_file=None, only_on_file=False
        )._header_constructor(self.to_print["basic"], None)

        self.assertEqual(expected, actual)

        # Test of the case that we want to print the hosts file format.
        expected = [" ".join(self.to_print["hosts"].keys())]

        actual = Prints(
            None, None, output_file=None, only_on_file=False
        )._header_constructor(self.to_print["hosts"], None)

        self.assertEqual(expected, actual)
Exemplo n.º 3
0
    def test_data_constructor(self):
        """
        Test Prints()._data_constructor().
        """

        File(self.file).delete()

        expected = False
        actual = PyFunceble.path.isfile(self.file)

        expected = PyFunceble.OrderedDict()
        to_print = []

        chars = ["H", "E", "L", "L", "O", "!"]

        for i, size in enumerate(self.to_print["size_constructor"]):
            index = chars[i] * size
            expected[index] = size
            to_print.append(index)

        actual = Prints(
            to_print, None, output_file=None, only_on_file=False
        )._data_constructor(self.to_print["size_constructor"])

        self.assertEqual(expected, actual)

        # Test the case that there is an issue.
        expected = PyFunceble.OrderedDict()
        to_print = []

        chars = ["H", "E", "L", "L", "O", "!"]

        for i, size in enumerate(self.to_print["size_constructor"]):
            index = chars[i] * size
            expected[index] = size
            to_print.append(index)

        del to_print[-1]

        self.assertRaisesRegex(
            Exception,
            "Inputed: %d; Size: %d"
            % (
                len(self.to_print["size_constructor"]) - 1,
                len(self.to_print["size_constructor"]),
            ),
            lambda: Prints(
                to_print, None, output_file=None, only_on_file=False
            )._data_constructor(self.to_print["size_constructor"]),
        )
Exemplo n.º 4
0
    def test_before_header(self, header_constructor_patch):
        """
        Test the functionability of Prints().before_header()
        """

        File(self.file).delete()

        expected = False
        actual = PyFunceble.path.isfile(self.file)

        self.assertEqual(expected, actual)

        expected = """# File generated by %s
# Date of generation: %s

""" % (
            PyFunceble.LINKS["repo"],
            PyFunceble.CURRENT_TIME + " ",
        )

        Prints(None, None, output_file=self.file, only_on_file=False)._before_header()

        self.assertEqual(expected, File(self.file).read())

        # Test of the case that we have a Generic_File template

        File(self.file).delete()

        expected = False
        actual = PyFunceble.path.isfile(self.file)

        self.assertEqual(expected, actual)

        expected = """# File generated by %s
# Date of generation: %s

Hello World!
""" % (
            PyFunceble.LINKS["repo"],
            PyFunceble.CURRENT_TIME + " ",
        )

        header_constructor_patch.return_value = ["Hello World!"]
        Prints(
            None, "Generic_File", output_file=self.file, only_on_file=False
        )._before_header()

        self.assertEqual(expected, File(self.file).read())
Exemplo n.º 5
0
    def test_size_from_header(self):
        """
        Test Prints()._size_from_header() which is used to extract
        the static sizes.
        """

        File(self.file).delete()

        expected = False
        actual = PyFunceble.path.isfile(self.file)

        expected = [element for _, element in self.to_print["basic"].items()]

        actual = Prints(
            None, None, output_file=None, only_on_file=False
        )._size_from_header(self.to_print["basic"])

        self.assertEqual(expected, actual)
Exemplo n.º 6
0
    def test_header_constructor_with_separator(self):  # pylint: disable=invalid-name
        """
        Test Prints()._header_constructor() for the case that we
        want to print the header.
        """

        File(self.file).delete()

        expected = False
        actual = PyFunceble.path.isfile(self.file)

        expected = [
            "hello world  here    is       PyFunceble",
            "----- ------ ------- -------- ----------",
        ]
        actual = Prints(
            None, None, output_file=None, only_on_file=False
        )._header_constructor(self.to_print["basic"])

        self.assertEqual(expected, actual)