Beispiel #1
0
    def test_ensure_str(self):
        values = (1, Number(5), "1", Symbol("hello"))
        for val in values:
            self.assertEqual(str(val), ensure_str(val))

        bad_values = (None, Null(), Glob("*"), Regex(".*"), [], ())
        for val in bad_values:
            self.assertRaises(SifterError, ensure_str, val)
Beispiel #2
0
    def test_ensure_int(self):
        values = (1, Number(5))
        for val in values:
            self.assertTrue(int(val) is ensure_int(val))

        bad_values = (None, Null(), Glob("*"), Regex(".*"), Symbol("100"),
                      "100", [], ())
        for val in bad_values:
            self.assertRaises(SifterError, ensure_int, val)
Beispiel #3
0
    def test_int_or_str_sieve(self):
        good_values = (1, Number(5), "hello", Symbol("hello"))

        vals = ExIntStrSieve(Sifter((), ""), *good_values).tokens
        self.assertEqual(good_values, vals)

        bad_values = (None, Null(), Glob("*"), Regex(".*"), [], ())

        self.assertRaises(SifterError, ExIntStrSieve, None, *bad_values)
Beispiel #4
0
    def test_symbol_sieve(self):
        good_values = (Symbol("Hello"), Symbol("World"))

        vals = ExSymbolSieve(Sifter((), ""), *good_values).tokens
        self.assertEqual(good_values, vals)

        bad_values = (None, Null(), 123, Number(5), "wut", Glob("*"),
                      Regex(".*"), [], ())

        self.assertRaises(SifterError, ExSymbolSieve, None, *bad_values)
Beispiel #5
0
    def test_matcher_sieve(self):
        good_values = ("hello", Null(), Number(5), Symbol("hello"), Glob("*"),
                       Regex(".*"))

        vals = ExMatcherSieve(Sifter((), ""), *good_values).tokens
        self.assertEqual(good_values, vals)

        bad_values = (None, [], (), 123)

        self.assertRaises(SifterError, ExMatcherSieve, None, *bad_values)
Beispiel #6
0
    def test_ensure_matcher(self):
        values = ("hello", Null(), Number(5), Symbol("hello"), Glob("*"),
                  Regex(".*"))
        for val in values:
            self.assertTrue(val is ensure_matcher(val))

        bad_values = (None, [], (), 123)
        for val in bad_values:
            self.assertRaises(SifterError, ensure_matcher, val)

        self.assertEqual(list(values), ensure_all_matcher(values))
        self.assertRaises(SifterError, ensure_all_matcher, bad_values)
Beispiel #7
0
    def test_ensure_symbol(self):
        val = Symbol("Hello")
        self.assertTrue(val is ensure_symbol(val))

        good_values = [val]

        bad_values = (None, Null(), 123, Number(5), "wut", Glob("*"),
                      Regex(".*"), [], ())
        for val in bad_values:
            self.assertRaises(SifterError, ensure_symbol, val)

        self.assertEqual(good_values, ensure_all_symbol(good_values))
        self.assertRaises(SifterError, ensure_all_symbol, bad_values)
Beispiel #8
0
    def test_ensure_int_or_str(self):
        int_values = (1, Number(5))
        for val in int_values:
            res = ensure_int_or_str(val)
            self.assertTrue(type(res), int)
            self.assertTrue(val == res)

        str_values = ("hello", Symbol("hello"))
        for val in str_values:
            res = ensure_int_or_str(val)
            self.assertTrue(type(res), str)
            self.assertTrue(val == res)

        bad_values = (None, Null(), Glob("*"), Regex(".*"), [], ())
        for val in bad_values:
            self.assertRaises(SifterError, ensure_int_or_str, val)

        good_values = []
        good_values.extend(int_values)
        good_values.extend(str_values)

        self.assertEqual(good_values, ensure_all_int_or_str(good_values))
        self.assertRaises(SifterError, ensure_all_int_or_str, bad_values)
Beispiel #9
0
    def test_regex(self):
        self.in_data(Regex("Hello"), 4, "Hello")
        self.in_data(Regex("ll"), 4, "Hello")
        self.in_data(Regex("o$"), 4, "Hello")

        self.in_data(Regex("HELLO", "i"), 4, "Hello")
        self.in_data(Regex("LL", "i"), 4, "Hello")
        self.in_data(Regex("O$", "i"), 4, "Hello")
        self.in_data(Regex("hello", "i"), 4, "Hello")
        self.in_data(Regex("ll", "i"), 4, "Hello")
        self.in_data(Regex("o$", "i"), 4, "Hello")

        self.in_data(Regex("^.o"), 5, "World")
        self.in_data(Regex("d$"), 5, "World")

        self.in_data(Regex(r"\d"), 2, "3")
        self.in_data(Regex(r"\d\d\d"), 13, "987")
        self.in_data(Regex(r"^\d{3}$"), 13, "987")
        self.in_data(Regex(r"\d\d"), 13, "987")
        self.in_data(Regex(r"\d{2}"), 13, "987")
        self.in_data(Regex(r"^\d\d$"), 14, "98")
        self.in_data(Regex(r"^\d{2}$"), 14, "98")

        self.in_data(Regex(""), 2, "3")
        self.in_data(Regex("()"), 2, "3")
        self.in_data(Regex("^$"), 11, "")

        self.in_data(Regex(r"\d"), 2, "3")

        self.not_in_data(Regex("hello"))
        self.not_in_data(Regex(r"\(\)"))
        self.not_in_data(Regex(r"\[\]"))
        self.not_in_data(Regex("None"))

        self.assertRaises(RegexError, Regex, "[")