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.
 
 
 

334 lines
9.3 KiB

  1. # -*- coding: utf-8 -*-
  2. """
  3. Use nose
  4. `$ pip install nose`
  5. `$ nosetests`
  6. """
  7. from hyde.ext.plugins.meta import GrouperPlugin, MetaPlugin, SorterPlugin
  8. from hyde.generator import Generator
  9. from hyde.site import Site
  10. from hyde.model import Config, Expando
  11. from fswrap import File
  12. import yaml
  13. from util import assert_html_equals
  14. TEST_SITE = File(__file__).parent.parent.child_folder('_test')
  15. class TestGrouperSingleLevel(object):
  16. def setUp(self):
  17. TEST_SITE.make()
  18. TEST_SITE.parent.child_folder(
  19. 'sites/test_grouper').copy_contents_to(TEST_SITE)
  20. self.s = Site(TEST_SITE)
  21. cfg = """
  22. nodemeta: meta.yaml
  23. plugins:
  24. - hyde.ext.plugins.meta.MetaPlugin
  25. - hyde.ext.plugins.meta.SorterPlugin
  26. - hyde.ext.plugins.meta.GrouperPlugin
  27. sorter:
  28. kind:
  29. attr:
  30. - source_file.kind
  31. filters:
  32. is_processable: True
  33. grouper:
  34. section:
  35. description: Sections in the site
  36. sorter: kind
  37. groups:
  38. -
  39. name: start
  40. description: Getting Started
  41. -
  42. name: plugins
  43. description: Plugins
  44. """
  45. self.s.config = Config(TEST_SITE, config_dict=yaml.load(cfg))
  46. self.s.load()
  47. MetaPlugin(self.s).begin_site()
  48. SorterPlugin(self.s).begin_site()
  49. GrouperPlugin(self.s).begin_site()
  50. self.all = ['installation.html', 'overview.html',
  51. 'templating.html', 'plugins.html', 'tags.html']
  52. self.start = ['installation.html', 'overview.html', 'templating.html']
  53. self.plugins = ['plugins.html', 'tags.html']
  54. self.section = self.all
  55. def tearDown(self):
  56. TEST_SITE.delete()
  57. def test_site_grouper_groups(self):
  58. groups = dict([(g.name, g) for g in self.s.grouper['section'].groups])
  59. assert len(groups) == 2
  60. assert 'start' in groups
  61. assert 'plugins' in groups
  62. def test_site_grouper_walk_groups(self):
  63. groups = dict([(g.name, g)
  64. 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)
  72. for grouper in self.s.content.walk_section_groups()])
  73. assert len(groups) == 3
  74. assert 'section' in groups
  75. assert 'start' in groups
  76. assert 'plugins' in groups
  77. for name in ['start', 'plugins']:
  78. res = [resource.name for resource in groups[name].resources]
  79. assert res == getattr(self, name)
  80. def test_walk_start_groups(self):
  81. assert hasattr(self.s.content, 'walk_start_groups')
  82. groups = dict([(g.name, g)
  83. for g, resources in self.s.content.walk_start_groups()])
  84. assert len(groups) == 1
  85. assert 'start' in groups
  86. def test_walk_plugins_groups(self):
  87. assert hasattr(self.s.content, 'walk_plugins_groups')
  88. groups = dict([(g.name, g) for g, resources in
  89. self.s.content.walk_plugins_groups()])
  90. assert len(groups) == 1
  91. assert 'plugins' in groups
  92. def test_walk_section_resources(self):
  93. assert hasattr(self.s.content, 'walk_resources_grouped_by_section')
  94. resources = [resource.name for resource in
  95. self.s.content.walk_resources_grouped_by_section()]
  96. assert resources == self.all
  97. def test_walk_start_resources(self):
  98. assert hasattr(self.s.content, 'walk_resources_grouped_by_start')
  99. start_resources = [resource.name for resource in
  100. self.s.content.walk_resources_grouped_by_start()]
  101. assert start_resources == self.start
  102. def test_walk_plugins_resources(self):
  103. assert hasattr(self.s.content, 'walk_resources_grouped_by_plugins')
  104. plugin_resources = [resource.name for resource in
  105. self.s.content.walk_resources_grouped_by_plugins()]
  106. assert plugin_resources == self.plugins
  107. def test_resource_group(self):
  108. groups = dict([(g.name, g) for g in self.s.grouper['section'].groups])
  109. for name, group in groups.items():
  110. pages = getattr(self, name)
  111. for page in pages:
  112. res = self.s.content.resource_from_relative_path(
  113. 'blog/' + page)
  114. assert hasattr(res, 'section_group')
  115. res_group = getattr(res, 'section_group')
  116. assert res_group == group
  117. def test_resource_belongs_to(self):
  118. groups = dict([(g.name, g) for g in self.s.grouper['section'].groups])
  119. for name, group in groups.items():
  120. pages = getattr(self, name)
  121. for page in pages:
  122. res = self.s.content.resource_from_relative_path(
  123. 'blog/' + page)
  124. res_groups = getattr(res, 'walk_%s_groups' % name)()
  125. assert group in res_groups
  126. def test_prev_next(self):
  127. resources = []
  128. for page in self.all:
  129. resources.append(
  130. self.s.content.resource_from_relative_path('blog/' + page))
  131. index = 0
  132. for res in resources:
  133. if index < 4:
  134. assert res.next_in_section.name == self.all[index + 1]
  135. else:
  136. assert not res.next_in_section
  137. index += 1
  138. index = 0
  139. for res in resources:
  140. if index:
  141. assert res.prev_in_section.name == self.all[index - 1]
  142. else:
  143. assert not res.prev_in_section
  144. index += 1
  145. def test_nav_with_grouper(self):
  146. text = """
  147. {% for group, resources in site.content.walk_section_groups() %}
  148. <ul>
  149. <li>
  150. <h2>{{ group.name|title }}</h2>
  151. <h3>{{ group.description }}</h3>
  152. <ul class="links">
  153. {% for resource in resources %}
  154. <li>{{resource.name}}</li>
  155. {% endfor %}
  156. </ul>
  157. </li>
  158. </ul>
  159. {% endfor %}
  160. """
  161. expected = """
  162. <ul>
  163. <li>
  164. <h2>Section</h2>
  165. <h3>Sections in the site</h3>
  166. <ul class="links"></ul>
  167. </li>
  168. </ul>
  169. <ul>
  170. <li>
  171. <h2>Start</h2>
  172. <h3>Getting Started</h3>
  173. <ul class="links">
  174. <li>installation.html</li>
  175. <li>overview.html</li>
  176. <li>templating.html</li>
  177. </ul>
  178. </li>
  179. </ul>
  180. <ul>
  181. <li>
  182. <h2>Plugins</h2>
  183. <h3>Plugins</h3>
  184. <ul class="links">
  185. <li>plugins.html</li>
  186. <li>tags.html</li>
  187. </ul>
  188. </li>
  189. </ul>
  190. """
  191. gen = Generator(self.s)
  192. gen.load_site_if_needed()
  193. gen.load_template_if_needed()
  194. out = gen.template.render(text, {'site': self.s})
  195. assert_html_equals(out, expected)
  196. def test_nav_with_grouper_sorted(self):
  197. cfg = """
  198. nodemeta: meta.yaml
  199. plugins:
  200. - hyde.ext.plugins.meta.MetaPlugin
  201. - hyde.ext.plugins.meta.SorterPlugin
  202. - hyde.ext.plugins.meta.GrouperPlugin
  203. sorter:
  204. kind:
  205. attr:
  206. - source_file.kind
  207. filters:
  208. is_processable: True
  209. grouper:
  210. section:
  211. description: Sections in the site
  212. sorter: kind
  213. groups:
  214. -
  215. name: start
  216. description: Getting Started
  217. -
  218. name: awesome
  219. description: Awesome
  220. -
  221. name: plugins
  222. description: Plugins
  223. """
  224. self.s.config = Config(TEST_SITE, config_dict=yaml.load(cfg))
  225. self.s.load()
  226. MetaPlugin(self.s).begin_site()
  227. SorterPlugin(self.s).begin_site()
  228. GrouperPlugin(self.s).begin_site()
  229. text = """
  230. {% set sorted = site.grouper['section'].groups|sort(attribute='name') %}
  231. {% for group in sorted %}
  232. <ul>
  233. <li>
  234. <h2>{{ group.name|title }}</h2>
  235. <h3>{{ group.description }}</h3>
  236. <ul class="links">
  237. {% for resource in group.walk_resources_in_node(site.content) %}
  238. <li>{{resource.name}}</li>
  239. {% endfor %}
  240. </ul>
  241. </li>
  242. </ul>
  243. {% endfor %}
  244. """
  245. expected = """
  246. <ul>
  247. <li>
  248. <h2>Awesome</h2>
  249. <h3>Awesome</h3>
  250. <ul class="links">
  251. </ul>
  252. </li>
  253. </ul>
  254. <ul>
  255. <li>
  256. <h2>Plugins</h2>
  257. <h3>Plugins</h3>
  258. <ul class="links">
  259. <li>plugins.html</li>
  260. <li>tags.html</li>
  261. </ul>
  262. </li>
  263. </ul>
  264. <ul>
  265. <li>
  266. <h2>Start</h2>
  267. <h3>Getting Started</h3>
  268. <ul class="links">
  269. <li>installation.html</li>
  270. <li>overview.html</li>
  271. <li>templating.html</li>
  272. </ul>
  273. </li>
  274. </ul>
  275. """
  276. self.s.config.grouper.section.groups.append(
  277. Expando({"name": "awesome", "description": "Aweesoome"}))
  278. gen = Generator(self.s)
  279. gen.load_site_if_needed()
  280. gen.load_template_if_needed()
  281. out = gen.template.render(text, {'site': self.s})
  282. assert_html_equals(out, expected)