helpers.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  1. var should = require('should')
  2. var polymer = require('../')
  3. var path = require('path')
  4. describe("helpers", function(){
  5. describe('.priorityList(filename)', function(){
  6. it('should return all possible file names for html ordered by priority.', function(done){
  7. var list = polymer.helpers.buildPriorityList('index.html')
  8. list.should.be.an.instanceOf(Array)
  9. list.should.have.lengthOf(8)
  10. var plist = "index.jade, index.ejs, index.md, index.nunjucks, index.html.jade, index.html.ejs, index.html.md, index.html.nunjucks".split(', ')
  11. list.should.eql(plist)
  12. done()
  13. })
  14. it('should build priority list for css file.', function(done){
  15. var list = polymer.helpers.buildPriorityList('main.css')
  16. list.should.be.an.instanceOf(Array)
  17. list.should.have.lengthOf(8)
  18. list.should.eql("main.styl, main.less, main.scss, main.sass, main.css.styl, main.css.less, main.css.scss, main.css.sass".split(', '))
  19. done()
  20. })
  21. it('should return all possible file names for js ordered by priority.', function(done){
  22. var list = polymer.helpers.buildPriorityList('/js/bundle.js')
  23. list.should.be.an.instanceOf(Array)
  24. var plist = "js/bundle.coffee, js/bundle.js.coffee".split(', ')
  25. list.should.eql(plist)
  26. done()
  27. })
  28. it('should build priority list assuming template file when unknown.', function(done){
  29. var list = polymer.helpers.buildPriorityList('feed.xml')
  30. list.should.be.an.instanceOf(Array)
  31. list.should.have.lengthOf(4)
  32. list.should.eql('feed.xml.jade, feed.xml.ejs, feed.xml.md, feed.xml.nunjucks'. split(', '))
  33. done()
  34. })
  35. it('should look for templates on json files.', function(done){
  36. var list = polymer.helpers.buildPriorityList('profile.json')
  37. list.should.be.an.instanceOf(Array)
  38. list.should.have.lengthOf(4)
  39. list.should.include('profile.json.jade')
  40. list.should.include('profile.json.ejs')
  41. list.should.include('profile.json.md')
  42. list.should.include('profile.json.nunjucks')
  43. list.should.eql('profile.json.jade, profile.json.ejs, profile.json.md, profile.json.nunjucks'. split(', '))
  44. done()
  45. })
  46. it('should look for templates when no ext present.', function(done){
  47. var list = polymer.helpers.buildPriorityList('appcache')
  48. list.should.be.an.instanceOf(Array)
  49. list.should.have.lengthOf(4)
  50. list.should.eql('appcache.jade, appcache.ejs, appcache.md, appcache.nunjucks'.split(', '))
  51. done()
  52. })
  53. it('should know when filename is already correct.', function(done){
  54. polymer.helpers.buildPriorityList('index.jade').should.eql(['index.jade'])
  55. // TODO: Implement Me.
  56. //polymer.helpers.buildPriorityList('main.less').should.eql(['main.less'])
  57. done()
  58. })
  59. })
  60. describe('.findNearestLayout(root, filename)', function(){
  61. it('should find closest layout', function(done){
  62. var root = path.join(__dirname, 'fixtures', 'layouts', 'base')
  63. polymer.helpers.findNearestLayout(root, "").should.eql("_layout.jade")
  64. polymer.helpers.findNearestLayout(root, null).should.eql("_layout.jade")
  65. done()
  66. })
  67. it('should find closest layout', function(done){
  68. var root = path.join(__dirname, 'fixtures', 'layouts', 'deep')
  69. polymer.helpers.findNearestLayout(root, "nested").should.eql(['nested', '_layout.jade'].join(path.sep))
  70. polymer.helpers.findNearestLayout(root, path.join("nested", "deeply")).should.eql(['nested', '_layout.jade'].join(path.sep))
  71. done()
  72. })
  73. })
  74. describe('.outputPath(filename)', function(){
  75. it('should convert jade to html.', function(done){
  76. polymer.helpers.outputPath('foobar.html').should.eql('foobar.html')
  77. polymer.helpers.outputPath('foobar.jade').should.eql('foobar.html')
  78. polymer.helpers.outputPath('foobar.html.jade').should.eql('foobar.html')
  79. done()
  80. })
  81. it('should convert less to css.', function(done){
  82. polymer.helpers.outputPath('foobar.css').should.eql('foobar.css')
  83. polymer.helpers.outputPath('foobar.less').should.eql('foobar.css')
  84. polymer.helpers.outputPath('foobar.css.less').should.eql('foobar.css')
  85. done()
  86. })
  87. it('should convert ejs to html.', function(done){
  88. polymer.helpers.outputPath('foobar.html').should.eql('foobar.html')
  89. polymer.helpers.outputPath('foobar.ejs').should.eql('foobar.html')
  90. polymer.helpers.outputPath('foobar.html.ejs').should.eql('foobar.html')
  91. done()
  92. })
  93. it('should allow alternate file extensions.', function(done){
  94. polymer.helpers.outputPath('foobar.foo').should.eql('foobar.foo')
  95. polymer.helpers.outputPath('foobar.bar.jade').should.eql('foobar.bar')
  96. polymer.helpers.outputPath('foobar.bar.ejs').should.eql('foobar.bar')
  97. done()
  98. })
  99. it('should not allow alternate file extensions if disabled.', function(done){
  100. polymer.helpers.outputPath('foobar.foo', false).should.eql('foobar.foo')
  101. polymer.helpers.outputPath('foobar.bar.jade', false).should.eql('foobar.bar.html')
  102. polymer.helpers.outputPath('foobar.bar.ejs', false).should.eql('foobar.bar.html')
  103. done()
  104. })
  105. it('should allow dot character on file name.', function(done){
  106. polymer.helpers.outputPath('foobar-1.0.0.html').should.eql('foobar-1.0.0.html')
  107. polymer.helpers.outputPath('foobar-1.0.0.jade').should.eql('foobar-1.0.0.html')
  108. polymer.helpers.outputPath('foobar-1.0.0.html.jade').should.eql('foobar-1.0.0.html')
  109. polymer.helpers.outputPath('foobar.min.js').should.eql('foobar.min.js')
  110. polymer.helpers.outputPath('foobar.min.js.coffee').should.eql('foobar.min.js')
  111. done()
  112. })
  113. it('should allow dot character on file path.', function(done){
  114. polymer.helpers.outputPath('1.0.0/foobar.html').should.eql('1.0.0/foobar.html')
  115. polymer.helpers.outputPath('1.0.0/foobar.jade').should.eql('1.0.0/foobar.html')
  116. polymer.helpers.outputPath('1.0.0/foobar.html.jade').should.eql('1.0.0/foobar.html')
  117. done()
  118. })
  119. })
  120. describe('.outputType(filename)', function(){
  121. it('should know source type.', function(done){
  122. polymer.helpers.outputType("foo.html").should.eql("html")
  123. polymer.helpers.outputType("foo.jade").should.eql("html")
  124. polymer.helpers.outputType("foo.ejs").should.eql("html")
  125. polymer.helpers.outputType("foo.md").should.eql("html")
  126. polymer.helpers.outputType("foo.css").should.eql("css")
  127. polymer.helpers.outputType("foo.less").should.eql("css")
  128. done()
  129. })
  130. })
  131. describe('.shouldIgnore(filename)', function(){
  132. it('should return true if file begins with underscore.', function(done){
  133. polymer.helpers.shouldIgnore('_foo.html').should.be.true
  134. done()
  135. })
  136. it('should return false file doesnt end with underscore.', function(done){
  137. polymer.helpers.shouldIgnore('foo.html').should.be.false
  138. polymer.helpers.shouldIgnore('foo_.html').should.be.false
  139. polymer.helpers.shouldIgnore('f_oo.html').should.be.false
  140. polymer.helpers.shouldIgnore('f____.html').should.be.false
  141. done()
  142. })
  143. it('should return true if any directory in path starts with underscore.', function(done){
  144. polymer.helpers.shouldIgnore(path.join('foo', '_bar.html')).should.be.true
  145. polymer.helpers.shouldIgnore(path.join('foo', '_bar', 'baz.html')).should.be.true
  146. polymer.helpers.shouldIgnore(path.join('_foo', 'bar', 'baz.html')).should.be.true
  147. polymer.helpers.shouldIgnore(path.sep + path.join('_foo', 'bar', 'baz.html')).should.be.true
  148. done()
  149. })
  150. it('should ignore if starts with underscore.', function(done){
  151. var reply = polymer.helpers.shouldIgnore('_beep.json')
  152. reply.should.be.true
  153. done()
  154. })
  155. it('should not ignore if doesnt start with underscore.', function(done){
  156. var reply = polymer.helpers.shouldIgnore('boop.json')
  157. reply.should.be.false
  158. done()
  159. })
  160. it('should ignore if nested file starts with underscore.', function(done){
  161. var reply = polymer.helpers.shouldIgnore(path.join('beep', '_boop.json'))
  162. reply.should.be.true
  163. done()
  164. })
  165. it('should ignore any part of tree starts with underscore.', function(done){
  166. var reply = polymer.helpers.shouldIgnore(path.join('foo', '_bar', 'baz.json'))
  167. reply.should.be.true
  168. done()
  169. })
  170. it('should ignore .git dirs', function(done){
  171. var reply = polymer.helpers.shouldIgnore(path.join('.git', 'foo.json'))
  172. reply.should.be.true
  173. done()
  174. })
  175. it('should ignore .gitignore files', function(done){
  176. var reply = polymer.helpers.shouldIgnore(path.join('.gitignore'))
  177. reply.should.be.true
  178. done()
  179. })
  180. it('should not ignore if no part of tree starts with underscore.', function(done){
  181. var reply = polymer.helpers.shouldIgnore(path.join('foo', 'bar', 'baz.json'))
  182. reply.should.be.false
  183. done()
  184. })
  185. it('should allow underscore in names.', function(done){
  186. var reply = polymer.helpers.shouldIgnore(path.join('foo_', 'beep.json'))
  187. reply.should.be.false
  188. done()
  189. })
  190. })
  191. describe('.isTemplate(filename)', function(){
  192. it('should return true if jade file.', function(done){
  193. polymer.helpers.isTemplate(path.join('foo.jade')).should.be.true
  194. polymer.helpers.isTemplate(path.join('foo', 'bar', 'baz.jade')).should.be.true
  195. done()
  196. })
  197. it('should return true if markdown file.', function(done){
  198. polymer.helpers.isTemplate(path.join('foo.md')).should.be.true
  199. polymer.helpers.isTemplate(path.join('foo', 'bar', 'baz.md')).should.be.true
  200. done()
  201. })
  202. it('should return false if less file.', function(done){
  203. polymer.helpers.isTemplate(path.join('foo.less')).should.be.false
  204. polymer.helpers.isTemplate(path.join('foo', 'bar', 'baz.less')).should.be.false
  205. done()
  206. })
  207. })
  208. describe('.isStylesheet(filename)', function(){
  209. it('should return true if less file.', function(done){
  210. polymer.helpers.isStylesheet(path.join('foo.less')).should.be.true
  211. polymer.helpers.isStylesheet(path.join('foo', 'bar', 'baz.less')).should.be.true
  212. done()
  213. })
  214. it('should return false if jade file.', function(done){
  215. polymer.helpers.isStylesheet(path.join('foo.less')).should.be.true
  216. polymer.helpers.isStylesheet(path.join('foo', 'bar', 'baz.less')).should.be.true
  217. done()
  218. })
  219. })
  220. describe('.isJavaScript(filename)', function(){
  221. it('should return true if coffescript file.', function(done){
  222. polymer.helpers.isJavaScript(path.join('foo.coffee')).should.be.true
  223. polymer.helpers.isJavaScript(path.join('foo', 'bar', 'baz.coffee')).should.be.true
  224. done()
  225. })
  226. it('should return true if javascript file.', function(done){
  227. polymer.helpers.isJavaScript(path.join('foo.js')).should.be.false
  228. polymer.helpers.isJavaScript(path.join('foo', 'bar', 'baz.js')).should.be.false
  229. done()
  230. })
  231. it('should return true if minified javascript file.', function(done){
  232. polymer.helpers.isJavaScript(path.join('foo.min.js')).should.be.false
  233. polymer.helpers.isJavaScript(path.join('foo', 'bar', 'bas.min.js')).should.be.false
  234. done()
  235. })
  236. it('should return false if less file.', function(done){
  237. polymer.helpers.isStylesheet(path.join('foo.less')).should.be.true
  238. polymer.helpers.isStylesheet(path.join('foo', 'bar', 'baz.less')).should.be.true
  239. done()
  240. })
  241. })
  242. describe('.layoutCascade(filename)', function(){
  243. })
  244. describe('.getCurrent(sourcePath)', function(){
  245. it('should handle folders', function(done){
  246. polymer.helpers.getCurrent('to/some/file.md').should.eql({
  247. 'source': 'file',
  248. 'path': ['to', 'some', 'file']
  249. })
  250. done()
  251. })
  252. it('should handle folders', function(done){
  253. polymer.helpers.getCurrent('to/some/file.json.jade').should.eql({
  254. 'source': 'file.json',
  255. 'path': ['to', 'some', 'file.json']
  256. })
  257. done()
  258. })
  259. it('should handle folders', function(done){
  260. polymer.helpers.getCurrent('to/some/file.html.jade').should.eql({
  261. 'source': 'file.html',
  262. 'path': ['to', 'some', 'file.html']
  263. })
  264. done()
  265. })
  266. it('should handle folders', function(done){
  267. polymer.helpers.getCurrent('a/b/c.md').should.eql({
  268. 'source': 'c',
  269. 'path': ['a', 'b', 'c']
  270. })
  271. done()
  272. })
  273. it('should handle dots in sourcePath.', function(done){
  274. polymer.helpers.getCurrent('v1.3.3.7/1.0/doc.md').should.eql({
  275. 'source': 'doc',
  276. 'path': ['v1.3.3.7', '1.0', 'doc']
  277. })
  278. done()
  279. })
  280. })
  281. })