Exemplo n.º 1
0
    def test_iterator(self):
        self.uut = StringConverter("a, test with!!some challenge",
                                   list_delimiters=[",", " ", "!!"])
        self.assertEqual(list(self.uut),
                         ["a", "test", "with", "some", "challenge"])
        self.uut = StringConverter("a\\ \\,\\\\ test with!!some challenge",
                                   list_delimiters=[",", " ", "!!"])
        self.assertEqual(list(self.uut),
                         ["a ,\\", "test", "with", "some", "challenge"])
        self.uut = StringConverter("a, test with!some \\\\\\ challenge\\ ",
                                   list_delimiters=", !",
                                   strip_whitespaces=False)
        self.assertEqual(list(self.uut),
                         ["a", "test", "with", "some", "\\ challenge "])
        self.uut = StringConverter("a, test with!some \\\\\\ challenge\\ ",
                                   list_delimiters=", !",
                                   strip_whitespaces=True)
        self.assertEqual(list(self.uut),
                         ["a", "test", "with", "some", "\\ challenge"])
        self.uut = StringConverter("testval", list_delimiters=[",", "¸"])
        self.uut.value = "a\\n,bug¸g"
        self.assertEqual(list(self.uut), ["an", "bug", "g"])

        self.assertTrue("bug" in self.uut)
        self.assertFalse("but" in self.uut)
Exemplo n.º 2
0
    def test_iterator_escape_whitespaces(self):
        uut = StringConverter("ta, chi, tsu, te, \\ to", list_delimiters=",")
        self.assertEqual(list(uut), ["ta", "chi", "tsu", "te", " to"])

        uut = StringConverter(r"/**, \ *\ , \ */", list_delimiters=",")
        self.assertEqual(list(uut), ["/**", " * ", " */"])

        uut = StringConverter(
            "abc\\\\ , def\\ \\ \\ ,   \\\\ unstrip \\\\\\  ",
            list_delimiters=",")
        self.assertEqual(list(uut), ["abc\\", "def   ", "\\ unstrip \\ "])
Exemplo n.º 3
0
    def test_bool_conversion(self):
        self.assertEqual(bool(self.uut), True)
        self.uut.value = "yeah"
        self.assertEqual(bool(self.uut), True)
        self.uut = StringConverter("y")
        self.assertEqual(bool(self.uut), True)
        self.uut = StringConverter("nope")
        self.assertEqual(bool(self.uut), False)

        self.uut = StringConverter(" i dont know ")
        self.assertRaises(ValueError, bool, self.uut)
Exemplo n.º 4
0
def typed_ordered_dict(key_type, value_type, default):
    """
    Creates a function that converts a setting into an ordered dict with the
    given types.

    :param key_type:   The type conversion function for the keys.
    :param value_type: The type conversion function for the values.
    :param default:    The default value to use if no one is given by the user.
    :return:           A conversion function.
    """
    return lambda setting: OrderedDict(
        (key_type(StringConverter(key)), value_type(StringConverter(value))
         if value != "" else default)
        for key, value in OrderedDict(setting).items())
Exemplo n.º 5
0
    def test_url(self):
        valid_urls = (
            # Scheme tests
            "http://url.com",
            "https://url.com",
            "url.com",
            "ftp://url.com",
            "ftps://url.com",
            # Domain tests
            "http://sub.subsub.url.com",
            "http://sub.url.com",
            "http://url.co.cc",
            "http://localhost",
            "http://sub.url123.com",
            "http://url123.co.cc",
            "http://1.1.1.1",
            "sub.subsub.url.com",
            "sub.url.com",
            "url.co.cc",
            "localhost",
            "1.1.1.1",
            "255.255.255.255",
            "url123.com",
            "url123.co.cc",
            "sub.url123.com",
            # Port number
            "localhost:8888",
            "1.1.1.1:80",
            "url.com:123456",
            # Paths
            "url.com/",
            "url.co.in/",
            "url.com/path/to/something",
            "url.co.in/path/to/something",
            "url.com/path/to/file.php")
        invalid_urls = (
            # Invalid types
            123,
            True,
            None,
            # Invalid links
            "unknown://url.com",
            "123",
            "abcd",
            "url.unknown",
            "user:[email protected]",
            "http://unknownlocalhost",
            "local_host/path")

        for url in valid_urls:
            try:
                StringConverter(url).__url__()
            except ValueError as exception:
                print(exception)
                self.fail("URL {} raised ValueError unexpectedly.".format(url))

        for url in invalid_urls:
            self.assertRaises(ValueError, self.uut.__url__)
Exemplo n.º 6
0
    def __extract_keys_and_value(self, line):
        key_part, value = self.__seperate_by_first_occurrence(
            line, self.key_value_delimiters, True, True)
        keys = list(
            StringConverter(key_part,
                            list_delimiters=self.key_delimiters).__iter__(
                                remove_backslashes=False))

        return keys, value
Exemplo n.º 7
0
 def test_equality_comparision(self):
     self.assertEqual(StringConverter(" i dont know "),
                      StringConverter("i dont know"))
     self.assertNotEqual(StringConverter(" dont know "),
                         StringConverter("i dont know "))
     self.assertNotEqual(StringConverter(""),
                         StringConverter("i dont know "))
     self.assertNotEqual(5, StringConverter("i dont know "))
Exemplo n.º 8
0
def typed_list(conversion_func):
    """
    Creates a function that converts a setting into a list of elements each
    converted with the given conversion function.

    :param conversion_func: The conversion function that converts a string into
                            your desired list item object.
    :return:                A conversion function.
    """
    return lambda setting: [
        conversion_func(StringConverter(elem)) for elem in setting]
Exemplo n.º 9
0
def get_ignore_scope(line, keyword):
    """
    Retrieves the bears that are to be ignored defined in the given line.
    :param line:    The line containing the ignore declaration.
    :param keyword: The keyword that was found. Everything after the rightmost
                    occurrence of it will be considered for the scope.
    :return:        A list of lower cased bearnames or an empty list (-> "all")
    """
    toignore = line[line.rfind(keyword) + len(keyword):]
    if toignore.startswith("all"):
        return []
    else:
        return list(StringConverter(toignore, list_delimiters=', '))
Exemplo n.º 10
0
    def test_iterator_remove_empty_iter_elements(self):
        uut = StringConverter("a, b, c, , e, , g", list_delimiters=",")
        self.assertEqual(list(uut), ["a", "b", "c", "e", "g"])

        uut = StringConverter("a, , ,, e, , g",
                              list_delimiters=",",
                              remove_empty_iter_elements=True)
        self.assertEqual(list(uut), ["a", "e", "g"])

        uut = StringConverter(",,, ,",
                              list_delimiters=",",
                              remove_empty_iter_elements=True)
        self.assertEqual(list(uut), [])

        uut = StringConverter("a, b, c, , e, , g",
                              list_delimiters=",",
                              remove_empty_iter_elements=False)
        self.assertEqual(list(uut), ["a", "b", "c", "", "e", "", "g"])

        uut = StringConverter(",,, ,",
                              list_delimiters=",",
                              remove_empty_iter_elements=False)
        self.assertEqual(list(uut), ["", "", "", "", ""])
Exemplo n.º 11
0
    def test_dict_escape_whitespaces(self):
        uut = StringConverter(
            "\\  : \\  , hello: \\ world, \\\\ A \\\\ : B\\ ")
        self.assertEqual(dict(uut), {
            " ": " ",
            "hello": " world",
            "\\ A \\": "B "
        })

        uut = StringConverter(r"/**, \ *\ , \ */")
        self.assertEqual(dict(uut), {"/**": "", " * ": "", " */": ""})

        uut = StringConverter("abc\\\\  :    qew, def\\ \\ \\ ,"
                              "   \\\\ unstrip \\\\\\  ")
        self.assertEqual(dict(uut), {
            "abc\\": "qew",
            "def   ": "",
            "\\ unstrip \\ ": ""
        })

        uut = StringConverter("A:B,C  :  D\\x \\a,42:\\n8 ",
                              strip_whitespaces=False)
        self.assertEqual(dict(uut), {"A": "B", "C  ": "  Dx a", "42": "n8 "})
Exemplo n.º 12
0
    def test_iterator(self):
        self.uut = StringConverter("a, test with!!some challenge",
                                   list_delimiters=[",", " ", "!!"])
        self.assertEqual(list(self.uut),
                         ["a", "test", "with", "some", "challenge"])
        self.uut = StringConverter("a\\ \\,\\\\ test with!!some challenge",
                                   list_delimiters=[",", " ", "!!"])
        self.assertEqual(list(self.uut),
                         ["a ,\\", "test", "with", "some", "challenge"])
        self.uut = StringConverter("a, test with!some \\\\\\ challenge\\ ",
                                   list_delimiters=", !",
                                   strip_whitespaces=False)
        self.assertEqual(list(self.uut),
                         ["a", "test", "with", "some", "\\ challenge "])
        self.uut = StringConverter("a, test with!some \\\\\\ challenge\\ ",
                                   list_delimiters=", !",
                                   strip_whitespaces=True)
        self.assertEqual(list(self.uut),
                         ["a", "test", "with", "some", "\\ challenge "])
        self.uut = StringConverter("testval", list_delimiters=[",", "¸"])
        self.uut.value = "a\\n,bug¸g"
        self.assertEqual(list(self.uut), ["an", "bug", "g"])
        self.assertEqual(list(self.uut.__iter__(False)), ["a\\n", "bug", "g"])

        self.assertTrue("bug" in self.uut)
        self.assertFalse("but" in self.uut)

        self.uut = StringConverter("a, test, \n",
                                   list_delimiters=[","],
                                   strip_whitespaces=True)
        self.assertEqual(list(self.uut), ["a", "test"])
        self.uut = StringConverter("a, test, \n",
                                   list_delimiters=[","],
                                   strip_whitespaces=False)
        self.assertEqual(list(self.uut), ["a", " test", " \n"])

        uut = StringConverter("A,B,C  ,  D\\x \\a,42,\\n8 ",
                              strip_whitespaces=False)
        self.assertEqual(list(uut), ["A", "B", "C  ", "  Dx a", "42", "n8 "])
Exemplo n.º 13
0
    def test_dict_conversion(self):
        self.uut = StringConverter("test")
        self.assertEqual(dict(self.uut), {"test": ""})
        self.uut = StringConverter("test, t")
        self.assertEqual(dict(self.uut), {"test": "", "t": ""})
        self.uut = StringConverter("test, t: v")
        self.assertEqual(dict(self.uut), {"test": "", "t": "v"})

        # Check escaping
        self.uut = StringConverter("test, t\\: v")
        self.assertEqual(dict(self.uut), {"test": "", "t: v": ""})
        self.uut = StringConverter("test, t\\: v: t")
        self.assertEqual(dict(self.uut), {"test": "", "t: v": "t"})
        self.uut = StringConverter("test\\, t\\: v: t")
        self.assertEqual(dict(self.uut), {"test, t: v": "t"})
        self.uut = StringConverter("test\\, t\\: v: t\\,")
        self.assertEqual(dict(self.uut), {"test, t: v": "t,"})

        # Check that lists ignore colons
        self.assertEqual(list(self.uut), ["test, t: v: t,"])
Exemplo n.º 14
0
 def test_int_conversion(self):
     self.assertEqual(int(self.uut), 1)
     self.uut = StringConverter(" not an int ")
     self.assertRaises(ValueError, int, self.uut)
Exemplo n.º 15
0
 def setUp(self):
     self.uut = StringConverter("\n \\1 \n ")
Exemplo n.º 16
0
 def test_float_conversion(self):
     self.assertEqual(float(self.uut), 1)
     self.uut.value = "0.5 "
     self.assertEqual(float(self.uut), 0.5)
     self.uut = StringConverter(" not a float ")
     self.assertRaises(ValueError, float, self.uut)
Exemplo n.º 17
0
    def test_whitespace_stripping(self):
        self.assertEqual(str(self.uut), "1")

        self.uut = StringConverter("\n 1 \n", strip_whitespaces=False)
        self.assertEqual(str(self.uut), "\n 1 \n")
# converts all keys to counting condition function objects (via the
# condition_dict) and all values to floats while unset values default to 1.
counting_condition_dict = typed_ordered_dict(
    lambda setting: condition_dict[str(setting).lower()], float, 1)

default_cc_dict = counting_condition_dict(
    StringConverter("""
used: 0,
returned: 1.4,
is_condition: 0,
in_condition: 1.4,
in_second_level_condition: 1.4,
in_third_level_condition: 1.0,
is_assignee: 0,
is_assigner: 0.6,
loop_content: 0,
second_level_loop_content,
third_level_loop_content,
is_param: 2,
is_called: 1.4,
is_call_param: 0.0,
in_sum: 2.0,
in_product: 0,
in_binary_operation,
member_accessed"""))


def get_difference(function_pair, count_matrices, average_calculation,
                   poly_postprocessing, exp_postprocessing):
    """
    Retrieves the difference between two functions using the munkres algorithm.