A fork of hyde, the static site generation. Some patches will be pushed upstream.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

231 lines
6.8 KiB

  1. # -*- coding: utf-8 -*-
  2. """
  3. Use nose
  4. `$ pip install nose`
  5. `$ nosetests`
  6. """
  7. from hyde.ext.plugins.meta import MetaPlugin
  8. from hyde.ext.plugins.sorter import SorterPlugin
  9. from hyde.ext.plugins.grouper import GrouperPlugin
  10. from hyde.fs import File, Folder
  11. from hyde.generator import Generator
  12. from hyde.site import Site
  13. from hyde.model import Config, Expando
  14. from hyde.tests.util import assert_html_equals
  15. import yaml
  16. TEST_SITE = File(__file__).parent.parent.child_folder('_test')
  17. class TestGrouperSingleLevel(object):
  18. def setUp(self):
  19. TEST_SITE.make()
  20. TEST_SITE.parent.child_folder(
  21. 'sites/test_grouper').copy_contents_to(TEST_SITE)
  22. self.s = Site(TEST_SITE)
  23. cfg = """
  24. nodemeta: meta.yaml
  25. plugins:
  26. - hyde.ext.plugins.meta.MetaPlugin
  27. - hyde.ext.plugins.sorter.SorterPlugin
  28. - hyde.ext.plugins.grouper.GrouperPlugin
  29. sorter:
  30. kind:
  31. attr:
  32. - source_file.kind
  33. filters:
  34. is_processable: True
  35. grouper:
  36. section:
  37. description: Sections in the site
  38. sorter: kind
  39. groups:
  40. -
  41. name: start
  42. description: Getting Started
  43. -
  44. name: plugins
  45. description: Plugins
  46. """
  47. self.s.config = Config(TEST_SITE, config_dict=yaml.load(cfg))
  48. self.s.load()
  49. MetaPlugin(self.s).begin_site()
  50. SorterPlugin(self.s).begin_site()
  51. GrouperPlugin(self.s).begin_site()
  52. self.all = ['installation.html', 'overview.html', 'templating.html', 'plugins.html', 'tags.html']
  53. self.start = ['installation.html', 'overview.html', 'templating.html']
  54. self.plugins = ['plugins.html', 'tags.html']
  55. self.section = self.all
  56. def tearDown(self):
  57. TEST_SITE.delete()
  58. def test_site_grouper_groups(self):
  59. groups = dict([(g.name, g) for g in self.s.grouper['section'].groups])
  60. assert len(groups) == 2
  61. assert 'start' in groups
  62. assert 'plugins' in groups
  63. def test_site_grouper_walk_groups(self):
  64. groups = dict([(g.name, g) for g in self.s.grouper['section'].walk_groups()])
  65. assert len(groups) == 3
  66. assert 'section' in groups
  67. assert 'start' in groups
  68. assert 'plugins' in groups
  69. def test_walk_section_groups(self):
  70. assert hasattr(self.s.content, 'walk_section_groups')
  71. groups = dict([(grouper.group.name, grouper) for grouper in self.s.content.walk_section_groups()])
  72. assert len(groups) == 3
  73. assert 'section' in groups
  74. assert 'start' in groups
  75. assert 'plugins' in groups
  76. for name in ['start', 'plugins']:
  77. res = [resource.name for resource in groups[name].resources]
  78. assert res == getattr(self, name)
  79. def test_walk_start_groups(self):
  80. assert hasattr(self.s.content, 'walk_start_groups')
  81. groups = dict([(g.name, g) for g, resources in self.s.content.walk_start_groups()])
  82. assert len(groups) == 1
  83. assert 'start' in groups
  84. def test_walk_plugins_groups(self):
  85. assert hasattr(self.s.content, 'walk_plugins_groups')
  86. groups = dict([(g.name, g) for g, resources in self.s.content.walk_plugins_groups()])
  87. assert len(groups) == 1
  88. assert 'plugins' in groups
  89. def test_walk_section_resources(self):
  90. assert hasattr(self.s.content, 'walk_resources_grouped_by_section')
  91. resources = [resource.name for resource in self.s.content.walk_resources_grouped_by_section()]
  92. assert resources == self.all
  93. def test_walk_start_resources(self):
  94. assert hasattr(self.s.content, 'walk_resources_grouped_by_start')
  95. start_resources = [resource.name for resource in self.s.content.walk_resources_grouped_by_start()]
  96. assert start_resources == self.start
  97. def test_walk_plugins_resources(self):
  98. assert hasattr(self.s.content, 'walk_resources_grouped_by_plugins')
  99. plugin_resources = [resource.name for resource in self.s.content.walk_resources_grouped_by_plugins()]
  100. assert plugin_resources == self.plugins
  101. def test_resource_group(self):
  102. groups = dict([(g.name, g) for g in self.s.grouper['section'].groups])
  103. for name, group in groups.items():
  104. pages = getattr(self, name)
  105. for page in pages:
  106. res = self.s.content.resource_from_relative_path('blog/' + page)
  107. assert hasattr(res, 'section_group')
  108. res_group = getattr(res, 'section_group')
  109. assert res_group == group
  110. def test_resource_belongs_to(self):
  111. groups = dict([(g.name, g) for g in self.s.grouper['section'].groups])
  112. for name, group in groups.items():
  113. pages = getattr(self, name)
  114. for page in pages:
  115. res = self.s.content.resource_from_relative_path('blog/' + page)
  116. res_groups = getattr(res, 'walk_%s_groups' % name)()
  117. assert group in res_groups
  118. def test_prev_next(self):
  119. resources = []
  120. for page in self.all:
  121. resources.append(self.s.content.resource_from_relative_path('blog/' + page))
  122. index = 0
  123. for res in resources:
  124. if index < 4:
  125. assert res.next_in_section.name == self.all[index + 1]
  126. else:
  127. assert not res.next_in_section
  128. index += 1
  129. index = 0
  130. for res in resources:
  131. if index:
  132. assert res.prev_in_section.name == self.all[index - 1]
  133. else:
  134. assert not res.prev_in_section
  135. index += 1
  136. def test_nav_with_grouper(self):
  137. text ="""
  138. {% for group, resources in site.content.walk_section_groups() %}
  139. <ul>
  140. <li>
  141. <h2>{{ group.name|title }}</h2>
  142. <h3>{{ group.description }}</h3>
  143. <ul class="links">
  144. {% for resource in resources %}
  145. <li>{{resource.name}}</li>
  146. {% endfor %}
  147. </ul>
  148. </li>
  149. </ul>
  150. {% endfor %}
  151. """
  152. expected = """
  153. <ul>
  154. <li>
  155. <h2>Section</h2>
  156. <h3>Sections in the site</h3>
  157. <ul class="links"></ul>
  158. </li>
  159. </ul>
  160. <ul>
  161. <li>
  162. <h2>Start</h2>
  163. <h3>Getting Started</h3>
  164. <ul class="links">
  165. <li>installation.html</li>
  166. <li>overview.html</li>
  167. <li>templating.html</li>
  168. </ul>
  169. </li>
  170. </ul>
  171. <ul>
  172. <li>
  173. <h2>Plugins</h2>
  174. <h3>Plugins</h3>
  175. <ul class="links">
  176. <li>plugins.html</li>
  177. <li>tags.html</li>
  178. </ul>
  179. </li>
  180. </ul>
  181. """
  182. gen = Generator(self.s)
  183. gen.load_site_if_needed()
  184. gen.load_template_if_needed()
  185. out = gen.template.render(text, {'site':self.s})
  186. assert_html_equals(out, expected)