run-syntax-errors.test.js 1.2 KB

12345678910111213141516171819202122232425262728293031323334
  1. const assert = require('assert');
  2. const fs = require('fs');
  3. const runUtils = require('./run-utils');
  4. const pug = require('../');
  5. const anti = runUtils.findCases(__dirname + '/anti-cases');
  6. describe('certain syntax is not allowed and will throw a compile time error', function () {
  7. anti.forEach(function(test){
  8. var name = test.replace(/[-.]/g, ' ');
  9. it(name, function(){
  10. var path = __dirname.replace(/\\/g, '/') + '/anti-cases/' + test + '.pug';
  11. var str = fs.readFileSync(path, 'utf8');
  12. try {
  13. var fn = pug.compile(str, {
  14. filename: path,
  15. pretty: true,
  16. basedir: __dirname + '/anti-cases',
  17. filters: runUtils.filters
  18. });
  19. } catch (ex) {
  20. if (!ex.code) {
  21. throw ex;
  22. }
  23. assert(ex instanceof Error, 'Should throw a real Error');
  24. assert(ex.code.indexOf('PUG:') === 0, 'It should have a code of "PUG:SOMETHING"');
  25. assert(ex.message.replace(/\\/g, '/').indexOf(path) === 0, 'it should start with the path');
  26. assert(/:\d+$/m.test(ex.message.replace(/\\/g, '/')), 'it should include a line number.');
  27. return;
  28. }
  29. throw new Error(test + ' should have thrown an error');
  30. })
  31. });
  32. });