Skip to content

Commit

Permalink
Python 2.6 fix.
Browse files Browse the repository at this point in the history
  • Loading branch information
c0fec0de committed Mar 13, 2017
1 parent 60d66bc commit f7d2734
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 12 deletions.
27 changes: 18 additions & 9 deletions tests/test_resolver.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,21 @@
# -*- coding: utf-8 -*-
from nose.tools import assert_raises
from contextlib import contextmanager
from nose.tools import eq_

import anytree as at


# hack own assert_raises, because py26 has a diffrent impelmentation
@contextmanager
def assert_raises(exccls, msg):
try:
yield
assert False, "%r not raised" % exccls
except Exception as exc:
assert isinstance(exc, exccls), "%r is not a %r" % (exc, exccls)
eq_(str(exc), msg)


def test_get():
"""Get."""
top = at.Node("top", parent=None)
Expand All @@ -18,17 +29,15 @@ def test_get():
eq_(r.get(sub1, "../sub0/sub0sub1"), sub0sub1)
eq_(r.get(sub1, "."), sub1)
eq_(r.get(sub1, ""), sub1)
with assert_raises(at.ChildResolverError) as raised:
with assert_raises(at.ChildResolverError,
"Node('/top') has no child sub2. Children are: 'sub0', 'sub1'."):
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:
with assert_raises(at.ResolverError, "root node missing. root is '/top'."):
r.get(sub0sub0, "/")
eq_(str(raised.exception), "root node missing. root is '/top'.")
with assert_raises(at.ResolverError) as raised:
with assert_raises(at.ResolverError, "unknown root node '/bar'. root is '/top'."):
r.get(sub0sub0, "/bar")
eq_(str(raised.exception), "unknown root node '/bar'. root is '/top'.")


def test_glob():
Expand All @@ -48,9 +57,9 @@ def test_glob():
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:
with assert_raises(at.ChildResolverError,
"Node('/top/sub1') has no child sub1. Children are: 'sub0'."):
r.glob(top, "sub1/sub1")
eq_(str(raised.exception), "Node('/top/sub1') has no child sub1. Children are: 'sub0'.")


def test_glob_cache():
Expand Down
17 changes: 14 additions & 3 deletions tests/test_walker.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,23 @@
from nose.tools import assert_raises
from contextlib import contextmanager
from nose.tools import eq_

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


# hack own assert_raises, because py26 has a diffrent impelmentation
@contextmanager
def assert_raises(exccls, msg):
try:
yield
assert False, "%r not raised" % exccls
except Exception as exc:
assert isinstance(exc, exccls), "%r is not a %r" % (exc, exccls)
eq_(str(exc), msg)



def test_walker():
"""walk test."""
f = Node("f")
Expand All @@ -25,6 +37,5 @@ def test_walker():
eq_(w.walk(b, f), ([f], []))
eq_(w.walk(h, e), ([i, g, f], [b, d, e]))

with assert_raises(WalkError) as raised:
with assert_raises(WalkError, "Node('/a') and Node('/b') are not part of the same tree."):
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 f7d2734

Please sign in to comment.