Beispiel #1
0
def test_compare():
    a = Link('a', 'a', 'a')
    b = Link('a', 'a', 'b')
    assert a != b
    assert a <= b
    b.annotation = 'a'
    assert a == b
Beispiel #2
0
def test_repr():
    l = Link('foo', 'bar')
    assert repr(l) == "Link(%s, %s)" % (repr('foo'), repr('bar'))
    l.annotation = 'x'
    assert repr(l) == "Link(%s, %s, %s)" % (repr('foo'), repr('bar'),
                                            repr('x'))
    l.target = None
    assert repr(l) == "Link(%s, annotation=%s)" % (repr('foo'), repr('x'))
Beispiel #3
0
def test_read():
    s = ' fö\t, "b""r"\n'
    reader = csv.Reader(io.StringIO(s), header=True)
    reader.start()
    assert list(reader.links()) == []

    reader = csv.Reader(io.StringIO(s), header=False)
    reader.start()
    assert list(reader.links()) == [Link('fö', 'b"r')]

    s = 'target, source\nbar, foo\n , \ndoz'
    reader = csv.Reader(io.StringIO(s), header=True)
    reader.start()
    assert list(reader.links()) == [Link('foo', 'bar'), Link(None, 'doz')]
Beispiel #4
0
def test_write():
    out = io.StringIO("")
    writer = csv.Writer(out, header=True)
    writer.start({})
    assert out.getvalue() == "source, target, annotation\n"

    writer.write_link(Link('foo', 'b"r'))
    assert out.getvalue() == 'source, target, annotation\nfoo, "b""r"\n'
Beispiel #5
0
def test_reader():
    tests = {
        '': ({},[]),
        ' |\t|  ': ({},[]),
        'foo|bar': ({},[Link(source='foo',annotation='bar')]),
        ' foo  ||\tbar ': ({},[Link(source='foo',target='bar')]),
        'foo|bar|doz|baz': ({},[Link('foo','doz',annotation='bar')]),
        '#TARGET:  foo\nx': ({'target':'foo$1'},[Link('x')]),
        'x\ny\nz': ({},[Link(s) for s in ['x','y','z']]),
    }

    for string, expect in tests.items():
        reader = beacon.Reader(io.StringIO(string))
        links = reader.start()
        links = [l for l in reader.links() if l]
        assert reader.meta == expect[0]
        assert list(links) == expect[1]
Beispiel #6
0
def test_sets():
    a = Link('foo', 'bar', 'doz')
    b = Link('foo', 'bar', 'doz')
    c = Link('42')

    assert isinstance(a, collections.Hashable)
    assert hash(a) == hash(b)
    assert hash(b) != hash(c)

    s1 = set([a])
    s2 = set([b])
    s3 = set([c])

    assert (s1 - s2) == set()
    assert (s1 | s2) == set([a])
    assert (s1 | s2 | s3) == set([a, b, c])
    assert ((s1 | s2 | s3) - s1) == set([c])
Beispiel #7
0
def test_empty_string():
    l = Link('\t', 'bar')
    assert l.source is None
    l.source = 'foo'
    l.target = ' '
    assert l.source is not None
    assert l.target is None
    assert bool(l) is True
    assert l.tokens() == ('foo', None, None)

    l.source = None
    assert l.source is None
    assert bool(l) is False
    assert l.tokens() == (None, None, None)
Beispiel #8
0
def test_write_no_header():
    out = io.StringIO('')
    writer = beacon.Writer(out, header=False)
    writer.write_link(Link('foo', 'b"r'))
    assert out.getvalue() == 'foo||b"r\n'
Beispiel #9
0
def test_constructor():
    l = Link(' f\r\t\n ö\n', annotation=None)
    assert l.source == 'f ö'
    assert l.target is None
    assert l.annotation is None
Beispiel #10
0
def test_expand():
    l = Link('a', 'b', 'c')
    assert l.expand('source', 'x:$1') == 'x:a'
    assert l.expand('target', 'x:') == 'x:b'
Beispiel #11
0
def test_exception():
    with pytest.raises(ValueError):
        Link('foo', 'bar').target = []
Beispiel #12
0
def test_access():
    l = Link('a', 'b', 'c')
    assert l.source is l['source']