Exemplo n.º 1
0
def test_Tags_parse_python():
    tags = Tags()
    tags.parse(
        BufferStub([
            'def foo():',
            '    pass',
        ],
                   vars={'current_syntax': b'python'}), 42)
    assert tags.changedtick == 42
    assert tags.tags == [Tag('foo', 1, 2)]
Exemplo n.º 2
0
def test_parse_git_diff():
    buffer = prepare('''
         1|diff --git a/foo/bar.py b/foo/bar.py
         2|index 54737257c..78871090f 100644
         3|--- a/foo/bar.py
         4|+++ b/foo/bar.py
         5|@@ -1,10 +1,16 @@
         6| import re
         7|+import logging
         8| import urllib
         9|+import importlib
        10|
        11|diff --git a/new.txt b/new.txt
        12|new file mode 100644
        13|index 000000000..78871090f
        14|--- /dev/null
        15|+++ b/new.txt
        16|@@ -0,0 +1,2 @@
        17|+hello
        18|+world
    ''')
    tags = Tags()
    parse(buffer, tags)
    assert tags.tags == [
        Tag('foo/bar.py', 1, 10),
        Tag('new.txt', 11, 18),
    ]
Exemplo n.º 3
0
def test_parse():
    buffer = prepare('''
         1|int foo()
         2|{
         3|    return 42;
         4|}
         5|
         6|char *hello(void)
         7|{
         8|    return "world";
         9|}
        10|
        11|int
        12|main()
        13|{
        14|    foo();
        15|    return 0;
        16|}
    ''')
    tags = Tags()
    parse(buffer, tags)
    assert tags.tags == [
        Tag('foo', 1, 4),
        Tag('hello', 6, 9),
        Tag('main', 12, 16),
    ]
Exemplo n.º 4
0
def test_Tags_find():
    tags = Tags()
    foo = tags.add('foo', 3, 7)
    bar = tags.add('bar', 10)
    assert tags.find(1) is None
    assert tags.find(2) is None
    assert tags.find(3) is foo
    assert tags.find(4) is foo
    assert tags.find(7) is foo
    assert tags.find(8) is None
    assert tags.find(9) is None
    assert tags.find(10) is bar
    assert tags.find(11) is bar
Exemplo n.º 5
0
def test_Tags_find_overlapping():
    tags = Tags()
    foo = tags.add('Foo', 3, 20)
    foo_bar = tags.add('Foo.bar', 10, 17)
    assert tags.find(3) is foo
    assert tags.find(10) is foo_bar
    assert tags.find(17) is foo_bar
    assert tags.find(18) is foo
Exemplo n.º 6
0
def test_Tags_get_syntax():
    tags = Tags()
    assert tags.get_syntax(BufferStub()) == ''
    assert tags.get_syntax(BufferStub(vars={'current_syntax': b''})) == ''
    assert tags.get_syntax(
        BufferStub(vars={'current_syntax': b'cpp'})) == 'cpp'
    assert tags.get_syntax(BufferStub(vars={'current_syntax': b'c.x'})) == 'c'
def test_parse_functions():
    buffer = prepare('''
        1|#!/usr/bin/python3
        2|import sys
        3|
        4|def foo(x, y):
        5|    z = x + y
        6|    return z
        7|
        8|def bar():
        9|    pass
    ''')
    tags = Tags()
    parse(buffer, tags)
    assert tags.tags == [Tag('foo', 4, 7), Tag('bar', 8, 9)]
Exemplo n.º 8
0
def test_parse_functions_with_long_signatures():
    buffer = prepare('''
         1|#!/usr/bin/python3
         2|import sys
         3|
         4|def foo(
         5|    x: int,
         6|    y: int,
         7|) -> int:
         8|    z = x + y
         9|    return z
    ''')
    tags = Tags()
    parse(buffer, tags)
    assert tags.tags == [Tag('foo', 4, 9)]
Exemplo n.º 9
0
def test_parse_top_level_statements_stop_functions():
    buffer = prepare('''
         1|#!/usr/bin/python3
         2|import sys
         3|
         4|def foo(x, y):
         5|    z = x + y
         6|
         7|    return z
         8|
         9|foo = 42
        10|
        11|
        12|def bar():
        13|    pass
    ''')
    tags = Tags()
    parse(buffer, tags)
    assert tags.tags == [Tag('foo', 4, 8), Tag('bar', 12, 13)]
Exemplo n.º 10
0
def test_parse_classes():
    buffer = prepare('''
         1|#!/usr/bin/python3
         2|import sys
         3|
         4|class MyClass:
         5|    x = 42
         6|    def __init__(self):
         7|        pass
         8|
         9|    async def bar(self):
        10|        pass
    ''')
    tags = Tags()
    parse(buffer, tags)
    assert tags.tags == [
        Tag('MyClass', 4, 10),
        Tag('MyClass.__init__', 6, 8),
        Tag('MyClass.bar', 9, 10),
    ]
Exemplo n.º 11
0
def test_parse_decorators():
    buffer = prepare('''
         1|#!/usr/bin/python3
         2|import sys
         3|
         4|def foo(x, y):
         5|    z = x + y
         6|    return z
         7|
         8|@pytest.mark.parametrize(['foo', 'bar'], [
         9|    ('a', 'b'),
        10|    ('c', 'd'),
        11|]
        12|@mock.patch('os.system', side_effect=OSError)
        13|def bar():
        14|    pass
    ''')
    tags = Tags()
    parse(buffer, tags)
    assert tags.tags == [Tag('foo', 4, 7), Tag('bar', 8, 14)]
Exemplo n.º 12
0
def test_parse_indented_classes():
    buffer = prepare('''
         1|#!/usr/bin/python3
         2|import sys
         3|
         4|class MyClass:
         5|    x = 42
         6|
         7|try:
         8|    import boo
         9|except ImportError:
        10|    class Bar:
        11|        pass
    ''')
    tags = Tags()
    parse(buffer, tags)
    assert tags.tags == [
        Tag('MyClass', 4, 6),
        Tag('Bar', 10, 11),
    ]
Exemplo n.º 13
0
def test_no_overlaps_if_parser_gets_confused():
    buffer = prepare('''
         1|#if FOO
         2|int foo() {
         3|#else
         4|int foo(int x) {
         5|#endif
         6|    return 42;
         7| }
         8|
         9|int bar(int x)
        10|{
        11|    return 42;
        12| }
    ''')
    tags = Tags()
    parse(buffer, tags)
    assert tags.tags == [
        Tag('foo', 2, 3),
        Tag('foo', 4, 8),  # or should I insist on 4, 7?
        Tag('bar', 9, 12),
    ]
Exemplo n.º 14
0
def test_parse():
    buffer = prepare('''
         1|--- a/foo/bar.py
         2|+++ b/foo/bar.py
         3|@@ -1,10 +1,16 @@
         4| import re
         5|+import logging
         6| import urllib
         7|+import importlib
         8|
         9|--- /dev/null
        10|+++ b/new.txt
        11|@@ -0,0 +1,2 @@
        12|+hello
        13|+world
    ''')
    tags = Tags()
    parse(buffer, tags)
    assert tags.tags == [
        Tag('foo/bar.py', 1, 8),
        Tag('new.txt', 9, 13),
    ]
Exemplo n.º 15
0
def test_parse_cpp():
    buffer = prepare('''
         1|class Foo {
         2|public:
         3|    Foo()
         4|    ~Foo()
         5|private:
         6|    int x;
         7|}
         8|
         9|Foo::Foo() {
        10|    x = 42;
        11|}
        12|
        13|Foo::~Foo() {
        14|}
    ''')
    tags = Tags()
    parse(buffer, tags)
    assert tags.tags == [
        Tag('Foo::Foo', 9, 11),
        Tag('Foo::~Foo', 13, 14),
    ]
Exemplo n.º 16
0
def test_Tags_len():
    tags = Tags()
    assert len(tags) == 0
    tags.add('foo', 1, 2)
    assert len(tags) == 1
Exemplo n.º 17
0
def test_Tags_repr():
    tags = Tags()
    assert repr(tags) == '<Tags: 0 tags>'
    tags.add('foo', 1, 2)
    assert repr(tags) == '<Tags: 1 tags>'
Exemplo n.º 18
0
def test_Tags_iter():
    tags = Tags()
    assert list(tags) == []
    tags.add('foo', 1, 2)
    assert list(tags) == [Tag('foo', 1, 2)]
Exemplo n.º 19
0
def test_Tags_add():
    tags = Tags()
    tag = tags.add('foo', 1, 2)
    assert tag == Tag('foo', 1, 2)
    assert tags.tags == [tag]
Exemplo n.º 20
0
def test_Tags_parse_no_syntax():
    tags = Tags()
    tags.parse(BufferStub(), 42)
    assert tags.changedtick == 42
    assert tags.tags == []