Skip to content

Commit

Permalink
menu: improvement of default item.active
Browse files Browse the repository at this point in the history
* BETTER Improves how the default active state of items is determined.
  Extends test suite for this property.

Signed-off-by: Marco Neumann <marco@crepererum.net>
Reviewed-by: Jiri Kuncar <jiri.kuncar@cern.ch>
  • Loading branch information
crepererum committed Apr 29, 2015
1 parent 60e3b0f commit 0698254
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 12 deletions.
1 change: 1 addition & 0 deletions AUTHORS
Expand Up @@ -12,3 +12,4 @@ Contact us at `info@invenio-software.org <mailto:info@invenio-software.org>`_
* Matthew Dillon <mrdillon@alaska.edu>
* Eirini Psallida <eirini.psallida@cern.ch>
* Florian Merges <fmerges@fstarter.org>
* Marco Neumann <marco@crepererum.net>
6 changes: 5 additions & 1 deletion flask_menu/__init__.py
Expand Up @@ -82,7 +82,11 @@ def __init__(self, name, parent):

def _active_when(self):
"""Define condition when a menu entry is active."""
return request.endpoint == self._endpoint
matching_endpoint = request.endpoint == self._endpoint
matching_subpath = len(self.url) > 1 \
and request.path.startswith(self.url)
matching_completpath = request.path == self.url
return matching_endpoint or matching_subpath or matching_completpath

def register(self, endpoint, text, order=0,
endpoint_arguments_constructor=None,
Expand Down
81 changes: 70 additions & 11 deletions tests/test_core.py
Expand Up @@ -172,33 +172,92 @@ def normal():
def test_active_when(self):
Menu(self.app)

@self.app.route('/')
@register_menu(self.app, 'root', 'Root')
def root():
return 'root'

@self.app.route('/always')
@register_menu(self.app, 'always', 'Always', active_when=lambda: True)
def always():
return 'never'
return 'always'

@self.app.route('/never')
@register_menu(self.app, 'never', 'Never', active_when=lambda: False)
def never():
return 'never'

@self.app.route('/normal')
@register_menu(self.app, 'normal', 'Normal', active_when=lambda self:
request.endpoint == self._endpoint)
@register_menu(self.app, 'normal', 'Normal')
def normal():
return 'normal'

data = {
'never': {'never': False, 'always': True, 'normal': False},
'always': {'never': False, 'always': True, 'normal': False},
'normal': {'never': False, 'always': True, 'normal': True},
'/never': {
'root': False,
'never': False,
'always': True,
'normal': False
},
'/always': {
'root': False,
'never': False,
'always': True,
'normal': False
},
'/normal': {
'root': False,
'never': False,
'always': True,
'normal': True
},
'/normal/foo': {
'root': False,
'never': False,
'always': True,
'normal': True
},
'/bar/normal': {
'root': False,
'never': False,
'always': True,
'normal': False
},
'/bar/normal/foo': {
'root': False,
'never': False,
'always': True,
'normal': False
},
'/': {
'root': True,
'never': False,
'always': True,
'normal': False
},
'': {
'root': True,
'never': False,
'always': True,
'normal': False
},
}
for (k, v) in data.items():
for (path, testset) in data.items():
with self.app.test_client() as c:
c.get('/' + k)
for (endpoint, active) in v.items():
self.assertEqual(current_menu.submenu(endpoint).active,
active)
c.get(path)
for (endpoint, active_should) in testset.items():
active_is = current_menu.submenu(endpoint).active
self.assertEqual(
active_is,
active_should,
'path="{0}" submenu_by_endpoint="{1}" '
'active_is={2} active_should={3}'.format(
path,
endpoint,
active_is,
active_should
)
)

def test_dynamic_url(self):
Menu(self.app)
Expand Down

0 comments on commit 0698254

Please sign in to comment.