PageRenderTime 49ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 1ms

/test/error.reporting.js

http://github.com/visionmedia/jade
JavaScript | 193 lines | 183 code | 6 blank | 4 comment | 2 complexity | 89324d3e31ee38492191160345ce4a57 MD5 | raw file
Possible License(s): MIT
  1. /**
  2. * Module dependencies.
  3. */
  4. var pug = require('../')
  5. , assert = require('assert')
  6. , fs = require('fs');
  7. // Shortcut
  8. function getError(str, options){
  9. try {
  10. pug.render(str, options);
  11. } catch (ex) {
  12. return ex;
  13. }
  14. throw new Error('Input was supposed to result in an error.');
  15. }
  16. function getFileError(name, options){
  17. try {
  18. pug.renderFile(name, options);
  19. } catch (ex) {
  20. return ex;
  21. }
  22. throw new Error('Input was supposed to result in an error.');
  23. }
  24. describe('error reporting', function () {
  25. describe('compile time errors', function () {
  26. describe('with no filename', function () {
  27. it('includes detail of where the error was thrown', function () {
  28. var err = getError('foo(')
  29. assert(/Pug:1/.test(err.message))
  30. assert(/foo\(/.test(err.message))
  31. });
  32. });
  33. describe('with a filename', function () {
  34. it('includes detail of where the error was thrown including the filename', function () {
  35. var err = getError('foo(', {filename: 'test.pug'})
  36. assert(/test\.pug:1/.test(err.message))
  37. assert(/foo\(/.test(err.message))
  38. });
  39. });
  40. describe('with a layout without block declaration (syntax)', function () {
  41. it('includes detail of where the error was thrown including the filename', function () {
  42. var err = getFileError(__dirname + '/fixtures/compile.with.layout.syntax.error.pug', {})
  43. assert(/[\\\/]layout.syntax.error.pug:2/.test(err.message))
  44. assert(/foo\(/.test(err.message))
  45. });
  46. });
  47. describe('with a layout without block declaration (locals)', function () {
  48. it('includes detail of where the error was thrown including the filename', function () {
  49. var err = getFileError(__dirname + '/fixtures/compile.with.layout.locals.error.pug', {})
  50. assert(/[\\\/]layout.locals.error.pug:2/.test(err.message))
  51. assert(/is not a function/.test(err.message))
  52. });
  53. });
  54. describe('with a include (syntax)', function () {
  55. it('includes detail of where the error was thrown including the filename', function () {
  56. var err = getFileError(__dirname + '/fixtures/compile.with.include.syntax.error.pug', {})
  57. assert(/[\\\/]include.syntax.error.pug:2/.test(err.message))
  58. assert(/foo\(/.test(err.message))
  59. });
  60. });
  61. describe('with a include (locals)', function () {
  62. it('includes detail of where the error was thrown including the filename', function () {
  63. var err = getFileError(__dirname + '/fixtures/compile.with.include.locals.error.pug', {})
  64. assert(/[\\\/]include.locals.error.pug:2/.test(err.message))
  65. assert(/foo\(/.test(err.message))
  66. });
  67. });
  68. describe('with a layout (without block) with an include (syntax)', function () {
  69. it('includes detail of where the error was thrown including the filename', function () {
  70. var err = getFileError(__dirname + '/fixtures/compile.with.layout.with.include.syntax.error.pug', {})
  71. assert(/[\\\/]include.syntax.error.pug:2/.test(err.message))
  72. assert(/foo\(/.test(err.message))
  73. });
  74. });
  75. describe('with a layout (without block) with an include (locals)', function () {
  76. it('includes detail of where the error was thrown including the filename', function () {
  77. var err = getFileError(__dirname + '/fixtures/compile.with.layout.with.include.locals.error.pug', {})
  78. assert(/[\\\/]include.locals.error.pug:2/.test(err.message))
  79. assert(/foo\(/.test(err.message))
  80. });
  81. });
  82. describe('block that is never actually used', function () {
  83. it('includes detail of where the error was thrown including the filename', function () {
  84. var err = getFileError(__dirname + '/fixtures/invalid-block-in-extends.pug', {});
  85. assert(/invalid-block-in-extends.pug:6/.test(err.message));
  86. assert(/content/.test(err.message));
  87. });
  88. });
  89. describe('Unexpected character', function () {
  90. it('includes details of where the error was thrown', function () {
  91. var err = getError('ul?', {});
  92. assert(err.message.indexOf('unexpected text "?"') !== -1);
  93. });
  94. });
  95. describe('Include filtered', function () {
  96. it('includes details of where the error was thrown', function () {
  97. var err = getError('include:verbatim()!', {});
  98. assert(err.message.indexOf('unexpected text "!"') !== -1);
  99. var err = getError('include:verbatim ', {});
  100. assert(err.message.indexOf('missing path for include') !== -1);
  101. });
  102. });
  103. describe('mixin block followed by a lot of blank lines', function () {
  104. it('reports the correct line number', function () {
  105. var err = getError('mixin test\n block\n\ndiv()Test');
  106. var line = /Pug\:(\d+)/.exec(err.message);
  107. assert(line, 'Line number must be included in error message');
  108. assert(line[1] === '4', 'The error should be reported on line 4, not line ' + line[1]);
  109. });
  110. });
  111. });
  112. describe('runtime errors', function () {
  113. describe('with no filename and `compileDebug` left undefined', function () {
  114. it('just reports the line number', function () {
  115. var sentinel = new Error('sentinel');
  116. var err = getError('-foo()', {foo: function () { throw sentinel; }})
  117. assert(/on line 1/.test(err.message))
  118. });
  119. });
  120. describe('with no filename and `compileDebug` set to `true`', function () {
  121. it('includes detail of where the error was thrown', function () {
  122. var sentinel = new Error('sentinel');
  123. var err = getError('-foo()', {foo: function () { throw sentinel; }, compileDebug: true})
  124. assert(/Pug:1/.test(err.message))
  125. assert(/-foo\(\)/.test(err.message))
  126. });
  127. });
  128. describe('with a filename that does not correspond to a real file and `compileDebug` left undefined', function () {
  129. it('just reports the line number', function () {
  130. var sentinel = new Error('sentinel');
  131. var err = getError('-foo()', {foo: function () { throw sentinel; }, filename: 'fake.pug'})
  132. assert(/on line 1/.test(err.message))
  133. });
  134. });
  135. describe('with a filename that corresponds to a real file and `compileDebug` left undefined', function () {
  136. it('includes detail of where the error was thrown including the filename', function () {
  137. var sentinel = new Error('sentinel');
  138. var path = __dirname + '/fixtures/runtime.error.pug'
  139. var err = getError(fs.readFileSync(path, 'utf8'), {foo: function () { throw sentinel; }, filename: path})
  140. assert(/fixtures[\\\/]runtime\.error\.pug:1/.test(err.message))
  141. assert(/-foo\(\)/.test(err.message))
  142. });
  143. });
  144. describe('in a mixin', function () {
  145. it('includes detail of where the error was thrown including the filename', function () {
  146. var err = getFileError(__dirname + '/fixtures/runtime.with.mixin.error.pug', {})
  147. assert(/mixin.error.pug:2/.test(err.message))
  148. assert(/Cannot read property 'length' of null/.test(err.message))
  149. });
  150. });
  151. describe('in a layout', function () {
  152. it('includes detail of where the error was thrown including the filename', function () {
  153. var err = getFileError(__dirname + '/fixtures/runtime.layout.error.pug', {})
  154. assert(/layout.with.runtime.error.pug:3/.test(err.message))
  155. assert(/Cannot read property 'length' of undefined/.test(err.message))
  156. });
  157. });
  158. });
  159. describe('deprecated features', function () {
  160. it('warns about element-with-multiple-attributes', function () {
  161. var consoleWarn = console.warn;
  162. var log = '';
  163. console.warn = function (str) {
  164. log += str;
  165. };
  166. var res = pug.renderFile(__dirname + '/fixtures/element-with-multiple-attributes.pug');
  167. console.warn = consoleWarn;
  168. assert(/element-with-multiple-attributes.pug, line 1:/.test(log));
  169. assert(/You should not have pug tags with multiple attributes/.test(log));
  170. assert(res === '<div attr="val" foo="bar"></div>');
  171. });
  172. });
  173. describe('if you throw something that isn\'t an error', function () {
  174. it('just rethrows without modification', function () {
  175. var err = getError('- throw "foo"');
  176. assert(err === 'foo');
  177. });
  178. });
  179. describe('import without a filename for a basedir', function () {
  180. it('throws an error', function () {
  181. var err = getError('include foo.pug');
  182. assert(/the "filename" option is required to use/.test(err.message));
  183. var err = getError('include /foo.pug');
  184. assert(/the "basedir" option is required to use/.test(err.message));
  185. })
  186. });
  187. });