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)
def test_ensure_symbol_expand(self): values = [Symbol("Hi"), convert_token("{Hello,Goodbye}World")] expect = [Symbol("Hi"), Symbol("HelloWorld"), Symbol("GoodbyeWorld")] res = ensure_all_symbol(values) self.assertEqual(len(res), 3) self.assertEqual(res, expect) self.assertRaises(SifterError, ensure_all_symbol, values, expand=False)
def test_symbol(self): self.in_path([Symbol("foo")], [[1, 9]]) self.in_path([Symbol("bar")], [[2, 3, 4, 5, 6]]) self.in_path([Symbol("baz"), Symbol("food")], [True]) self.in_path([Symbol("qux"), Symbol("food")], [False]) self.in_path([Symbol("quxx"), Symbol("food")], []) ip = ItemPath(Symbol("quxx"), Symbol("food")) self.assertEqual(repr(ip), "ItemPath(Item('quxx'), Item('food'))")
def test_bad_kwds(self): vals = [ Symbol("msg:"), ] self.assertRaises(SifterError, gather_args, vals) vals = [ Symbol("foo"), Number(1), Symbol("msg:"), ] self.assertRaises(SifterError, gather_args, vals)
def test_symbol(self): self.in_data(Symbol("Hello"), 4, "Hello") self.in_data(Symbol("3"), 2, "3") self.in_data(Symbol(""), 11, "") self.not_in_data(Symbol("hello")) self.not_in_data(Symbol("ell")) self.not_in_data(Symbol("1")) self.not_in_data(Symbol("()")) self.not_in_data(Symbol("[]")) self.not_in_data(Symbol("None"))
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)
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)
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)
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)
def test_parse_item(self): ip = self.parse("foo") self.assertTrue(isinstance(ip, ItemPath)) self.assertEqual(len(ip.paths), 1) self.assertEqual(type(ip.paths[0]), Item) self.assertEqual(ip.paths[0].key, Symbol("foo")) ip = self.parse(".foo") self.assertTrue(isinstance(ip, ItemPath)) self.assertEqual(len(ip.paths), 1) self.assertEqual(type(ip.paths[0]), Item) self.assertEqual(ip.paths[0].key, Symbol("foo")) ip = self.parse("[foo]") self.assertTrue(isinstance(ip, ItemPath)) self.assertEqual(len(ip.paths), 1) self.assertEqual(type(ip.paths[0]), Item) self.assertEqual(ip.paths[0].key, Symbol("foo")) ip = self.parse("[1:]") self.assertTrue(isinstance(ip, ItemPath)) self.assertEqual(len(ip.paths), 1) self.assertEqual(type(ip.paths[0]), Item) self.assertEqual(ip.paths[0].key, slice(1, None, None)) ip = self.parse("[:-1]") self.assertTrue(isinstance(ip, ItemPath)) self.assertEqual(len(ip.paths), 1) self.assertEqual(type(ip.paths[0]), Item) self.assertEqual(ip.paths[0].key, slice(None, -1, None)) ip = self.parse("[1:3]") self.assertTrue(isinstance(ip, ItemPath)) self.assertEqual(len(ip.paths), 1) self.assertEqual(type(ip.paths[0]), Item) self.assertEqual(ip.paths[0].key, slice(1, 3, None)) ip = self.parse("[1:3:2]") self.assertTrue(isinstance(ip, ItemPath)) self.assertEqual(len(ip.paths), 1) self.assertEqual(type(ip.paths[0]), Item) self.assertEqual(ip.paths[0].key, slice(1, 3, 2))
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)
def test_unterminated_list(self): src = """ ( """ self.assertRaises(ParserError, self.parse, src) src = """ (hello """ self.assertRaises(ParserError, self.parse, src) src = "(hello" self.assertRaises(ParserError, self.parse, src) src = "hello)" self.assertRaises(ParserError, self.parse, src) src = "foo bar" res = self.parse(src) self.assertEqual(res, [Symbol("foo"), Symbol("bar")])
def test_args(self): vals = [] args, kwds = gather_args(vals) self.assertEqual(args, vals) self.assertEqual(kwds, {}) vals = [Symbol("Hello"), Number(5), None] args, kwds = gather_args(vals) self.assertEqual(args, vals) self.assertEqual(kwds, {})
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)
def test_kwds(self): vals = [Symbol("msg:"), Symbol("Hello"), Number(5), None] args, kwds = gather_args(vals) self.assertEqual(args, [Number(5), None]) self.assertEqual(kwds, {"msg": Symbol("Hello")}) vals = [Symbol("msg:"), Symbol("Hello"), Symbol("val:"), Number(5)] args, kwds = gather_args(vals) self.assertEqual(args, []) self.assertEqual(kwds, {"msg": Symbol("Hello"), "val": Number(5)})
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)
def test_symbol_sieve_expand(self): good_values = (convert_token("{Hello,Goodbye}"), Symbol("World")) expect = (Symbol("Hello"), Symbol("Goodbye"), Symbol("World")) vals = ExSymbolSieve(Sifter((), ""), *good_values).tokens self.assertEqual(expect, vals)