def test_item():
    e = html.div(range(10))
    assert str(misc.item(e[xsc.Text], 0)) == "0"
    assert str(misc.item(e[xsc.Text], 9)) == "9"
    assert str(misc.item(e[xsc.Text], -1)) == "9"
    assert str(misc.item(e[xsc.Text], -10)) == "0"
    misc.item(e[xsc.Text], 10) is None
    misc.item(e[xsc.Text], -11) is None
    assert str(misc.item(e[xsc.Text], 10, "x")) == "x"
    assert str(misc.item(e[xsc.Text], -11, "x")) == "x"
Ejemplo n.º 2
0
def test_walkgetitem():
    e = html.div(1, html.div(2, html.div(3)))
    isdiv = xfind.selector(html.div)

    # Test ``walknodes``
    assert str(misc.first(e.walknodes(isdiv))) == "123"
    assert str(misc.last(e.walknodes(isdiv))) == "3"
    misc.item(e.walknodes(isdiv), 3) is None
    misc.item(e.walknodes(isdiv), -4) is None

    # Test ``walkpaths``
    assert str(misc.item(e.walkpaths(isdiv), (0, -1))) == "123"
    assert str(misc.item(e.walkpaths(isdiv), (-1, -1))) == "3"
    misc.item(e.walkpaths(isdiv), 3) is None
    misc.item(e.walkpaths(isdiv), -4) is None
Ejemplo n.º 3
0
	def __contains__(self, path):
		if len(path) > 1:
			if self.index in ("even", "odd"):
				for (i, child) in enumerate(self._find(path)):
					if child is path[-1]:
						return (i % 2) == (self.index == "odd")
			else:
				try:
					return misc.item(self._find(path), self.index) is path[-1]
				except IndexError:
					return False
		return False
def test_walkgetitem():
	e = html.div(
		1,
		html.div(
			2,
			html.div(
				3
			)
		)
	)
	isdiv = xfind.selector(html.div)

	# Test ``walknodes``
	assert str(misc.first(e.walknodes(isdiv))) == "123"
	assert str(misc.last(e.walknodes(isdiv))) == "3"
	misc.item(e.walknodes(isdiv), 3) is None
	misc.item(e.walknodes(isdiv), -4) is None

	# Test ``walkpaths``
	assert str(misc.item(e.walkpaths(isdiv), (0, -1))) == "123"
	assert str(misc.item(e.walkpaths(isdiv), (-1, -1))) == "3"
	misc.item(e.walkpaths(isdiv), 3) is None
	misc.item(e.walkpaths(isdiv), -4) is None
Ejemplo n.º 5
0
def test_item():
    def err(n):
        yield from range(n)
        raise SyntaxError

    e = iter(range(10))
    assert misc.item(e, 0) == 0
    assert misc.item(e, 0) == 1
    assert misc.item(e, -1) == 9
    assert misc.item(e, -1) is None
    assert misc.item(e, -1, 42) == 42

    e = iter(range(10))
    assert misc.item(e, 4) == 4

    e = iter(range(10))
    assert misc.item(e, 10) is None

    e = iter(range(10))
    assert misc.item(e, 10, 42) == 42

    e = iter(range(10))
    assert misc.item(e, -1) == 9

    e = iter(range(10))
    assert misc.item(e, -10) == 0

    e = iter(range(10))
    assert misc.item(e, -11) is None

    e = iter(range(10))
    assert misc.item(e, -11, 42) == 42

    iterable = [17, 23, 37]

    # Wrong arguments
    with pytest.raises(TypeError):
        misc.item()
    with pytest.raises(TypeError):
        misc.item([])
    with pytest.raises(TypeError):
        misc.item(42, 42)

    # Non-negative index
    assert misc.item(iterable, 0) == 17
    assert misc.item(iterable, 2) == 37
    assert misc.item(iterable, 3) is None
    assert misc.item(iterable, 3, 42) == 42
    assert misc.item(err(10), 9) == 9
    with pytest.raises(SyntaxError):
        misc.item(err(10), 10)

    # Negative index
    assert misc.item(iterable, -1) == 37
    assert misc.item(iterable, -3) == 17
    assert misc.item(iterable, -4) is None
    assert misc.item(iterable, -4, 42) == 42
    # iterator is always exhausted
    with pytest.raises(SyntaxError):
        misc.item(err(10), -1)

    # Check index lists
    assert misc.item(["foo", "bar"], (1, -1)) == "r"