1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176
|
import pytest
from sphinx_codeautolink.parse import Component
from ._util import refs_equal
class TestUnit:
def test_component_from_unrecognised_ast(self):
with pytest.raises(ValueError, match="Invalid AST"):
Component.from_ast("not ast")
class TestSimple:
@refs_equal
def test_empty_source(self):
return "", []
@refs_equal
def test_no_imports(self):
return "1\na = 2\nb()", []
@refs_equal
def test_builtins(self):
s = "print()"
refs = [("print", "print")]
return s, refs
@pytest.mark.xfail(reason="Magics are currently not tracked.")
@refs_equal
def test_magics(self):
s = "__file__"
refs = [("__file__", "__file__")]
return s, refs
@refs_equal
def test_import_from(self):
s = "from lib import a"
refs = [("lib", "lib"), ("lib.a", "a")]
return s, refs
@refs_equal
def test_import_from_as(self):
s = "from lib import a as b"
refs = [("lib", "lib"), ("lib.a", "a")]
return s, refs
@refs_equal
def test_import_from_multiline(self):
s = "from lib import (\n a,\n b,\n)"
refs = [("lib", "lib"), ("lib.a", "a"), ("lib.b", "b")]
return s, refs
@refs_equal
def test_import_from_as_multiline(self):
s = "from lib import (\n a as b,\n c as d,\n)"
refs = [("lib", "lib"), ("lib.a", "a"), ("lib.c", "c")]
return s, refs
@refs_equal
def test_simple_import_then_access(self):
s = "import lib\nlib"
refs = [("lib", "lib"), ("lib", "lib")]
return s, refs
@refs_equal
def test_inside_list_literal(self):
s = "import lib\n[lib]"
refs = [("lib", "lib"), ("lib", "lib")]
return s, refs
@refs_equal
def test_inside_subscript(self):
s = "import lib\n0[lib]"
refs = [("lib", "lib"), ("lib", "lib")]
return s, refs
@refs_equal
def test_outside_subscript(self):
s = "import lib\nlib[0]"
refs = [("lib", "lib"), ("lib", "lib")]
return s, refs
@refs_equal
def test_simple_import_then_attrib(self):
s = "import lib\nlib.attr"
refs = [("lib", "lib"), ("lib.attr", "lib.attr")]
return s, refs
@refs_equal
def test_subscript_then_attrib_not_linked(self):
s = "import a\na[b].c"
refs = [("a", "a"), ("a", "a")]
return s, refs
@refs_equal
def test_subscript_then_attrib_then_call_not_linked(self):
s = "import a\na[b].c()"
refs = [("a", "a"), ("a", "a")]
return s, refs
@refs_equal
def test_import_as_then_attrib(self):
s = "import lib as b\nb.attr"
refs = [("lib", "lib"), ("lib.attr", "b.attr")]
return s, refs
@refs_equal
def test_import_from_then_attrib(self):
s = "from lib import a\na.attr"
refs = [("lib", "lib"), ("lib.a", "a"), ("lib.a.attr", "a.attr")]
return s, refs
@refs_equal
def test_import_from_as_then_attrib(self):
s = "from lib import a as b\nb.attr"
refs = [("lib", "lib"), ("lib.a", "a"), ("lib.a.attr", "b.attr")]
return s, refs
@refs_equal
def test_dotted_import(self):
s = "import a.b\na.b"
refs = [("a.b", "a.b"), ("a.b", "a.b")]
return s, refs
@refs_equal
def test_dotted_import_then_only_part(self):
s = "import a.b\na"
refs = [("a.b", "a.b"), ("a", "a")]
return s, refs
@refs_equal
def test_dotted_import_then_attrib(self):
s = "import a.b\na.b.c"
refs = [("a.b", "a.b"), ("a.b.c", "a.b.c")]
return s, refs
@refs_equal
def test_dotted_import_then_call_attrib(self):
s = "import a.b\na.b().c"
refs = [("a.b", "a.b"), ("a.b", "a.b"), ("a.b.().c", "c")]
return s, refs
@refs_equal
def test_relative_import_is_noop(self):
s = "from .a import b\nb"
refs = []
return s, refs
@refs_equal
def test_del_removes_import(self):
s = "import a\ndel a\na"
refs = [("a", "a"), ("a", "a")]
return s, refs
@refs_equal
def test_del_dotted_removes_only_part(self):
s = "import a.b\ndel a.b\na"
refs = [("a.b", "a.b"), ("a.b", "a.b"), ("a", "a")]
return s, refs
@pytest.mark.xfail(reason="Assignments to imports are not tracked.")
@refs_equal
def test_overwrite_dotted_not_tracked(self):
s = "import a.b\na.b = 1\na.b.c"
refs = [("a.b", "a.b")]
return s, refs
@refs_equal
def test_import_star(self):
s = "from sphinx_codeautolink import *\nsetup"
refs = [
("sphinx_codeautolink", "sphinx_codeautolink"),
("sphinx_codeautolink.setup", "setup"),
]
return s, refs
|