Browse Source

Made sorting tests predictable without changing the behavior of sorter plugin

main
Lakshmi Vyasarajan 13 years ago
parent
commit
9a7da0e8fa
3 changed files with 15 additions and 10 deletions
  1. +1
    -2
      hyde/ext/plugins/sorter.py
  2. +13
    -7
      hyde/tests/ext/test_sorter.py
  3. +1
    -1
      hyde/tests/templates/jinja2/index.html

+ 1
- 2
hyde/ext/plugins/sorter.py View File

@@ -44,8 +44,7 @@ def sort_method(node, settings=None):
if settings and hasattr(settings, 'attr') and settings.attr: if settings and hasattr(settings, 'attr') and settings.attr:
attr = settings.attr attr = settings.attr
filter_ = partial(filter_method, settings=settings) filter_ = partial(filter_method, settings=settings)
resources = sorted(ifilter(filter_, node.walk_resources()),
key=lambda f: f.name)
resources = ifilter(filter_, node.walk_resources())
reverse = False reverse = False
if settings and hasattr(settings, 'reverse'): if settings and hasattr(settings, 'reverse'):
reverse = settings.reverse reverse = settings.reverse


+ 13
- 7
hyde/tests/ext/test_sorter.py View File

@@ -31,7 +31,7 @@ class TestSorter(object):
s = Site(TEST_SITE) s = Site(TEST_SITE)
s.load() s.load()
s.config.plugins = ['hyde.ext.sorter.SorterPlugin'] s.config.plugins = ['hyde.ext.sorter.SorterPlugin']
s.config.sorter = Expando(dict(kind=dict(attr='source_file.kind')))
s.config.sorter = Expando(dict(kind=dict(attr=['source_file.kind', 'name'])))


SorterPlugin(s).begin_site() SorterPlugin(s).begin_site()


@@ -49,13 +49,13 @@ class TestSorter(object):
pages = [page.name for page in pages = [page.name for page in
s.content.walk_resources_sorted_by_kind()] s.content.walk_resources_sorted_by_kind()]


assert pages == sorted(sorted(expected), key=lambda f: File(f).kind)
assert pages == sorted(expected, key=lambda f: (File(f).kind, f))


def test_walk_resources_sorted_reverse(self): def test_walk_resources_sorted_reverse(self):
s = Site(TEST_SITE) s = Site(TEST_SITE)
s.load() s.load()
s.config.plugins = ['hyde.ext.sorter.SorterPlugin'] s.config.plugins = ['hyde.ext.sorter.SorterPlugin']
s.config.sorter = Expando(dict(kind=dict(attr='source_file.kind', reverse=True)))
s.config.sorter = Expando(dict(kind=dict(attr=['source_file.kind', 'name'], reverse=True)))


SorterPlugin(s).begin_site() SorterPlugin(s).begin_site()


@@ -74,7 +74,7 @@ class TestSorter(object):
s.content.walk_resources_sorted_by_kind()] s.content.walk_resources_sorted_by_kind()]




assert pages == sorted(sorted(expected), key=lambda f: File(f).kind, reverse=True)
assert pages == sorted(expected, key=lambda f: (File(f).kind, f), reverse=True)


def test_walk_resources_sorted_with_filters(self): def test_walk_resources_sorted_with_filters(self):
s = Site(TEST_SITE) s = Site(TEST_SITE)
@@ -110,6 +110,7 @@ class TestSorter(object):
attr: attr:
- source_file.kind - source_file.kind
- node.name - node.name
- name


""" """
s.config = Config(TEST_SITE, config_dict=yaml.load(cfg)) s.config = Config(TEST_SITE, config_dict=yaml.load(cfg))
@@ -131,10 +132,10 @@ class TestSorter(object):


expected_sorted = [File(page).name expected_sorted = [File(page).name
for page in for page in
sorted(sorted(expected),
sorted(expected,
key=lambda p: tuple( key=lambda p: tuple(
[File(p).kind, [File(p).kind,
File(p).parent.name]))]
File(p).parent.name, p]))]
assert pages == expected_sorted assert pages == expected_sorted


def test_walk_resources_sorted_no_default_is_processable(self): def test_walk_resources_sorted_no_default_is_processable(self):
@@ -146,6 +147,8 @@ class TestSorter(object):
kind2: kind2:
filters: filters:
source_file.kind: html source_file.kind: html
attr:
- name
""" """
s.config = Config(TEST_SITE, config_dict=yaml.load(cfg)) s.config = Config(TEST_SITE, config_dict=yaml.load(cfg))
s.load() s.load()
@@ -169,6 +172,8 @@ class TestSorter(object):
kind2: kind2:
filters: filters:
source_file.kind: html source_file.kind: html
attr:
- name
""" """
s.config = Config(TEST_SITE, config_dict=yaml.load(cfg)) s.config = Config(TEST_SITE, config_dict=yaml.load(cfg))
s.load() s.load()
@@ -201,7 +206,8 @@ class TestSorter(object):
- hyde.ext.sorter.SorterPlugin - hyde.ext.sorter.SorterPlugin
sorter: sorter:
folder_name: folder_name:
attr: node.name
attr:
- node.name
reverse: True reverse: True
filters: filters:
source_file.kind: html source_file.kind: html


+ 1
- 1
hyde/tests/templates/jinja2/index.html View File

@@ -6,7 +6,7 @@
<div class="article"> <div class="article">
<h2><a href="{{ article.href|e }}">{{ article.title|e }}</a></h2> <h2><a href="{{ article.href|e }}">{{ article.title|e }}</a></h2>
<p class="meta">written by <a href="{{ article.user.href|e <p class="meta">written by <a href="{{ article.user.href|e
}}">{{ article.user.username|e }}</a> on {{ article.pub_date.strftime('%Y-%m-%d') }}</p>
}}">{{ article.user.username|e }}</a> on {{ article.pub_date|dateformat }}</p>
<div class="text">{{ article.body }}</div> <div class="text">{{ article.body }}</div>
</div> </div>
{%- endfor %} {%- endfor %}


Loading…
Cancel
Save