2007-01-24 22:24:01 +08:00
|
|
|
import py
|
2007-02-07 08:24:21 +08:00
|
|
|
from py.__.apigen.linker import Linker, TempLinker, getrelfspath, relpath
|
2007-01-24 22:24:01 +08:00
|
|
|
|
|
|
|
class TestLinker(object):
|
|
|
|
def test_get_target(self):
|
|
|
|
linker = Linker()
|
|
|
|
lazyhref = linker.get_lazyhref('py.path.local')
|
|
|
|
linker.set_link('py.path.local', 'py/path/local.html')
|
|
|
|
relpath = linker.get_target('py.path.local')
|
|
|
|
assert relpath == 'py/path/local.html'
|
|
|
|
|
|
|
|
def test_target_relative(self):
|
|
|
|
linker = Linker()
|
|
|
|
lazyhref = linker.get_lazyhref('py.path.local')
|
|
|
|
linker.set_link('py.path.local', 'py/path/local.html')
|
|
|
|
relpath = linker.call_withbase('py/index.html',
|
|
|
|
linker.get_target, 'py.path.local')
|
|
|
|
assert relpath == 'path/local.html'
|
|
|
|
|
|
|
|
testspec = [
|
2007-01-25 21:22:04 +08:00
|
|
|
'a a/b a/b /',
|
|
|
|
'/a /a/b a/b /',
|
|
|
|
'a b b /',
|
|
|
|
'/a /b b /',
|
|
|
|
'a/b c/d ../c/d /',
|
|
|
|
'/a/b /c/d ../c/d /',
|
|
|
|
'a/b a ../a /',
|
|
|
|
'/a/b /a ../a /',
|
|
|
|
'c:\\foo\\bar c:\\foo ../foo \\',
|
2007-01-25 20:54:51 +08:00
|
|
|
]
|
2007-01-24 22:24:01 +08:00
|
|
|
|
2007-02-07 08:24:21 +08:00
|
|
|
class TestTempLinker(object):
|
|
|
|
def test_get_target(self):
|
|
|
|
linker = TempLinker()
|
|
|
|
temphref = linker.get_lazyhref('py.path.local')
|
|
|
|
linker.set_link('py.path.local', 'py/path/local.html')
|
|
|
|
relpath = linker.get_target(temphref)
|
|
|
|
assert relpath == 'py/path/local.html'
|
|
|
|
|
|
|
|
def test_functional(self):
|
|
|
|
temp = py.test.ensuretemp('TestTempLinker.test_functional')
|
|
|
|
l = TempLinker()
|
|
|
|
bar = temp.ensure('foo/bar.html', file=True)
|
|
|
|
baz = temp.ensure('foo/baz.html', file=True)
|
|
|
|
l.set_link(baz.strpath, baz.relto(temp))
|
|
|
|
bar.write('<a href="%s">baz</a>' % (l.get_lazyhref(baz.strpath),))
|
|
|
|
l.replace_dirpath(temp)
|
|
|
|
assert bar.read() == '<a href="baz.html">baz</a>'
|
|
|
|
|
2007-01-25 21:22:04 +08:00
|
|
|
def gen_check(frompath, topath, sep, expected):
|
|
|
|
result = relpath(frompath, topath, sep=sep)
|
2007-01-24 22:24:01 +08:00
|
|
|
assert result == expected
|
|
|
|
|
|
|
|
def test_gen_check():
|
|
|
|
for line in testspec:
|
2007-01-25 21:22:04 +08:00
|
|
|
frompath, topath, expected, sep = line.split()
|
|
|
|
yield gen_check, frompath, topath, sep, expected
|
2007-01-24 22:24:01 +08:00
|
|
|
|
|
|
|
def test_check_incompatible():
|
|
|
|
py.test.raises(ValueError, "relpath('/a', 'b')")
|