PageRenderTime 41ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/src/xtemplate/tests/specs/error.js

https://github.com/jiang-nan/kissy
JavaScript | 105 lines | 84 code | 13 blank | 8 comment | 3 complexity | ba1a645d996f45d98c52789ec5a98968 MD5 | raw file
  1. /**
  2. * error test tc
  3. * @author yiminghe@gmail.com
  4. */
  5. /*jshint quotmark:false*/
  6. var XTemplate = require('xtemplate');
  7. var util = require('util');
  8. describe('error detection', function () {
  9. // https://github.com/kissyteam/kissy/issues/516
  10. it('error when string encounter \\', function () {
  11. var ret;
  12. try {
  13. ret = new XTemplate("{{'\\'}}").render();
  14. } catch (e) {
  15. ret = e.message;
  16. }
  17. expect(ret.indexOf('expect shift:LPAREN, shift:MINUS, shift:NOT, shift:STRING, shift:GLOBAL, shift:NUMBER')).not.toBe(-1);
  18. });
  19. it('error when string include \\n', function () {
  20. var ret;
  21. try {
  22. ret = new XTemplate("\n\n\n\n{{ x + '1\n222222' }}",{name:'string'}).render();
  23. } catch (e) {
  24. ret = e.message;
  25. }
  26. expect(ret.indexOf("\n {{ x + '1 222222' }}\n-----------^")).not.toBe(-1);
  27. });
  28. it('detect lexer error', function () {
  29. var ret;
  30. try {
  31. ret = new XTemplate("{{'}}").render();
  32. } catch (e) {
  33. ret = e.message;
  34. }
  35. expect(ret.indexOf('expect shift:LPAREN, shift:MINUS, shift:NOT, shift:STRING, shift:GLOBAL, shift:NUMBER, shift:ID')).not.toBe(-1);
  36. });
  37. it('detect un-closed block tag', function () {
  38. var tpl = '{{#if(title)}}\n' +
  39. 'shoot\n' +
  40. '',
  41. data = {
  42. title: 'o'
  43. }, info;
  44. try {
  45. new XTemplate(tpl).render(data);
  46. } catch (e) {
  47. info = e.message;
  48. }
  49. if (location.search.indexOf('build') === -1) {
  50. expect(util.startsWith(info, 'Syntax error at line 3:\n' +
  51. '{{#if(title)}} shoot\n\n' +
  52. '--------------------^\n' +
  53. 'expect'));
  54. // OPEN_END_BLOCK
  55. }
  56. });
  57. it('detect unmatched', function () {
  58. if (!KISSY.config('debug')) {
  59. return;
  60. }
  61. var tpl = '{{#if(n === n1)}}\n' +
  62. 'n eq n1\n' +
  63. '{{/with}}';
  64. var data = {
  65. n: 1,
  66. n1: 2
  67. };
  68. expect(function () {
  69. try {
  70. new XTemplate(tpl).render(data);
  71. } catch (e) {
  72. //S.log('!'+e.replace(/\n/g,'\\n').replace(/\r/g,'\\r')+'!');
  73. throw e;
  74. }
  75. }).toThrow('Syntax error at line 3:\n' +
  76. 'expect {{/if}} not {{/with}}');
  77. });
  78. it('detect unmatched custom command', function () {
  79. if (!KISSY.config('debug')) {
  80. return;
  81. }
  82. var tpl = '{{#x.y()}}\n{{/x}}';
  83. expect(function () {
  84. try {
  85. new XTemplate(tpl).render();
  86. } catch (e) {
  87. throw e;
  88. }
  89. }).toThrow('Syntax error at line 2:\n' +
  90. 'expect {{/x,y}} not {{/x}}');
  91. });
  92. });