Skip to content

Commit

Permalink
Implement get_full_qualified_name() to JavascriptDomain (refs: sphinx…
Browse files Browse the repository at this point in the history
  • Loading branch information
tk0miya committed Apr 23, 2017
1 parent db377ce commit 96fa6d2
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 0 deletions.
10 changes: 10 additions & 0 deletions sphinx/domains/javascript.py
Original file line number Diff line number Diff line change
Expand Up @@ -398,6 +398,16 @@ def get_objects(self):
yield refname, refname, type, docname, \
refname.replace('$', '_S_'), 1

def get_full_qualified_name(self, node):
# type: (nodes.Node) -> unicode
modname = node.get('js:module')
prefix = node.get('js:object')
target = node.get('reftarget')
if target is None:
return None
else:
return '.'.join(filter(None, [modname, prefix, target]))


def setup(app):
# type: (Sphinx) -> Dict[unicode, Any]
Expand Down
32 changes: 32 additions & 0 deletions tests/test_domain_js.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,11 @@
"""

import pytest
from mock import Mock
from docutils import nodes

from sphinx import addnodes
from sphinx.domains.javascript import JavaScriptDomain

from util import assert_node

Expand Down Expand Up @@ -133,3 +137,31 @@ def find_obj(mod_name, prefix, obj_name, obj_type, searchmode=0):
( u'module_a.submodule.ModTopLevel.mod_child_2', (u'module', u'method')))
assert (find_obj(u'module_b.submodule', u'ModTopLevel', u'module_a.submodule', u'mod') ==
( u'module_a.submodule', (u'module', u'module')))


def test_get_full_qualified_name():
env = Mock(domaindata={})
domain = JavaScriptDomain(env)

# non-js references
node = nodes.reference()
assert domain.get_full_qualified_name(node) is None

# simple reference
node = nodes.reference(reftarget='func')
assert domain.get_full_qualified_name(node) == 'func'

# with js:module context
kwargs = {'js:module': 'module1'}
node = nodes.reference(reftarget='func', **kwargs)
assert domain.get_full_qualified_name(node) == 'module1.func'

# with js:object context
kwargs = {'js:object': 'Class'}
node = nodes.reference(reftarget='func', **kwargs)
assert domain.get_full_qualified_name(node) == 'Class.func'

# with both js:module and js:object context
kwargs = {'js:module': 'module1', 'js:object': 'Class'}
node = nodes.reference(reftarget='func', **kwargs)
assert domain.get_full_qualified_name(node) == 'module1.Class.func'
24 changes: 24 additions & 0 deletions tests/test_ext_intersphinx.py
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,30 @@ def test_missing_reference_stddomain(tempdir, app, status, warning):
assert rn.astext() == 'ls -l'


def test_missing_reference_jsdomain(tempdir, app, status, warning):
inv_file = tempdir / 'inventory'
inv_file.write_bytes(inventory_v2)
app.config.intersphinx_mapping = {
'https://docs.python.org/': inv_file,
}
app.config.intersphinx_cache_limit = 0

# load the inventory and check if it's done correctly
load_mappings(app)

# no context data
kwargs = {}
node, contnode = fake_node('js', 'meth', 'baz', 'baz()', **kwargs)
rn = missing_reference(app, app.env, node, contnode)
assert rn is None

# js:module and js:object context helps to search objects
kwargs = {'js:module': 'foo', 'js:object': 'bar'}
node, contnode = fake_node('js', 'meth', 'baz', 'baz()', **kwargs)
rn = missing_reference(app, app.env, node, contnode)
assert rn.astext() == 'baz()'


def test_load_mappings_warnings(tempdir, app, status, warning):
"""
load_mappings issues a warning if new-style mapping
Expand Down
4 changes: 4 additions & 0 deletions tests/test_util_inventory.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@
a term std:term -1 glossary.html#term-a-term -
ls.-l std:cmdoption 1 index.html#cmdoption-ls-l -
docname std:doc -1 docname.html -
foo js:module 1 index.html#foo -
foo.bar js:class 1 index.html#foo.bar -
foo.bar.baz js:method 1 index.html#foo.bar.baz -
foo.bar.qux js:data 1 index.html#foo.bar.qux -
a term including:colon std:term -1 glossary.html#term-a-term-including-colon -
'''.encode('utf-8'))

Expand Down

0 comments on commit 96fa6d2

Please sign in to comment.