def test_empty(self): """Loop over nothing.""" items = [] generator = (item for item in []) self.assertEqual([], list(iterbot.iter_is_last(items))) self.assertEqual([], list(iterbot.iter_is_last(generator)))
def test_multiple(self): """Process big lists.""" items = range(100) generator = (item for item in range(100)) expected = [(False, index) if index < 99 else (True, index) for index in range(100)] self.assertEqual(expected, list(iterbot.iter_is_last(items))) self.assertEqual(expected, list(iterbot.iter_is_last(generator)))
def test_iterate(self): """Loop over some iterable objects.""" items = ["foo", "bar"] generator = (item for item in ["foo", "bar"]) iter_tuple = iter(("foo", "bar")) self.assertEqual([(False, "foo"), (True, "bar")], list(iterbot.iter_is_last(items))) self.assertEqual([(False, "foo"), (True, "bar")], list(iterbot.iter_is_last(generator))) self.assertEqual([(False, "foo"), (True, "bar")], list(iterbot.iter_is_last(iter_tuple)))
def _make_import(namespace_parts, prefix="", alias=None): """Make a new from-import node and insert it into an existing parso graph. Args: namespace_parts (list[str]): The import that will be converted into a parso from-import node. prefix (str, optional): The leading whitespace (indentation) placed before the new import. Returns: :class:`parso.python.tree.ImportFrom`: A newly generated import statement. """ base_names = namespace_parts[:-1] base_count = len(base_names) base_nodes = [] if base_count > 1: for is_last, name in iterbot.iter_is_last(base_names): base_nodes.append(tree.Name(name, (0, 0))) if not is_last: base_nodes.append(tree.Operator(".", (0, 0))) base_nodes[0].prefix = " " base = tree.PythonNode("dotted_name", base_nodes) else: base = tree.Name(base_names[0], (0, 0), prefix=" ") tail = tree.Name(namespace_parts[-1], (0, 0), prefix=" ") if alias: tail = tree.PythonNode( "import_as_name", [tail, tree.Keyword("as", (0, 0), prefix=" "), alias]) return tree.ImportFrom([ tree.Keyword("from", (0, 0), prefix=prefix), base, tree.Keyword("import", (0, 0), prefix=" "), tail, ])
def test_simple_bug(self): """Make sure a simple list works as expected.""" self.assertEqual( [(False, "another"), (False, "jpeg"), (True, "many")], list(iterbot.iter_is_last(["another", "jpeg", "many"])), )
def test_invalid(self): """Make sure bad inputs raise `ValueError`.""" with self.assertRaises(ValueError): list(iterbot.iter_is_last(None))