Skip to content

Commit

Permalink
Improve coverage.
Browse files Browse the repository at this point in the history
  • Loading branch information
c0fec0de committed Mar 13, 2017
1 parent 3a1d6f6 commit 60d66bc
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 3 deletions.
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ universal=1

[nosetests]
with-coverage = True
with-doctest = True
cover-erase = True
cover-html = True
cover-html-dir = coverage-report
cover-inclusive = True
cover-branches = True
# doctest cannot be enabled, due to py2.x issues

[coverage:run]
include = anytree/*
39 changes: 37 additions & 2 deletions tests/test_resolver.py
Original file line number Diff line number Diff line change
@@ -1,22 +1,57 @@
# -*- coding: utf-8 -*-
from nose.tools import assert_raises
from nose.tools import eq_

import anytree as at


def test_get():
"""Get."""
top = at.Node("top", parent=None)
sub0 = at.Node("sub0", parent=top)
sub0sub0 = at.Node("sub0sub0", parent=sub0)
sub0sub1 = at.Node("sub0sub1", parent=sub0)
sub1 = at.Node("sub1", parent=top)
r = at.Resolver('name')
eq_(r.get(top, "sub0/sub0sub0"), sub0sub0)
eq_(r.get(sub1, ".."), top)
eq_(r.get(sub1, "../sub0/sub0sub1"), sub0sub1)
eq_(r.get(sub1, "."), sub1)
eq_(r.get(sub1, ""), sub1)
with assert_raises(at.ChildResolverError) as raised:
r.get(top, "sub2")
eq_(str(raised.exception), "Node('/top') has no child sub2. Children are: 'sub0', 'sub1'.")
eq_(r.get(sub0sub0, "/top"), top)
eq_(r.get(sub0sub0, "/top/sub0"), sub0)
with assert_raises(at.ResolverError) as raised:
r.get(sub0sub0, "/")
eq_(str(raised.exception), "root node missing. root is '/top'.")
with assert_raises(at.ResolverError) as raised:
r.get(sub0sub0, "/bar")
eq_(str(raised.exception), "unknown root node '/bar'. root is '/top'.")


def test_glob():
"""Wildcard."""
top = at.Node("top", parent=None)
sub0 = at.Node("sub0", parent=top)
at.Node("sub0", parent=sub0)
sub0sub0 = at.Node("sub0", parent=sub0)
sub0sub1 = at.Node("sub1", parent=sub0)
sub0sub1sub0 = at.Node("sub0", parent=sub0sub1)
at.Node("sub1", parent=sub0sub1)
sub1 = at.Node("sub1", parent=top)
at.Node("sub0", parent=sub1)
sub1sub0 = at.Node("sub0", parent=sub1)
r = at.Resolver()
eq_(r.glob(top, "*/*/sub0"), [sub0sub1sub0])

eq_(r.glob(top, "sub0/sub?"), [sub0sub0, sub0sub1])
eq_(r.glob(sub1, ".././*"), [sub0, sub1])
eq_(r.glob(top, "*/*"), [sub0sub0, sub0sub1, sub1sub0])
eq_(r.glob(top, "*/sub0"), [sub0sub0, sub1sub0])
with assert_raises(at.ChildResolverError) as raised:
r.glob(top, "sub1/sub1")
eq_(str(raised.exception), "Node('/top/sub1') has no child sub1. Children are: 'sub0'.")


def test_glob_cache():
"""Wildcard Cache."""
Expand Down
30 changes: 30 additions & 0 deletions tests/test_walker.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
from nose.tools import assert_raises
from nose.tools import eq_

from anytree import Node
from anytree import WalkError
from anytree import Walker


def test_walker():
"""walk test."""
f = Node("f")
b = Node("b", parent=f)
a = Node("a", parent=b)
d = Node("d", parent=b)
c = Node("c", parent=d)
e = Node("e", parent=d)
g = Node("g", parent=f)
i = Node("i", parent=g)
h = Node("h", parent=i)
w = Walker()
eq_(w.walk(f, f), ([], []))
eq_(w.walk(f, b), ([], [b]))
eq_(w.walk(b, f), ([f], []))
eq_(w.walk(a, f), ([b, f], []))
eq_(w.walk(b, f), ([f], []))
eq_(w.walk(h, e), ([i, g, f], [b, d, e]))

with assert_raises(WalkError) as raised:
w.walk(Node("a"), Node("b"))
eq_(str(raised.exception), "Node('/a') and Node('/b') are not part of the same tree.")

0 comments on commit 60d66bc

Please sign in to comment.