test.html 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>模板引擎单元测试</title>
  6. <link rel="stylesheet" href="./js/qunit/qunit.css">
  7. <script src="./js/qunit/qunit.js"></script>
  8. <script src="../src/template.js"></script>
  9. <script id="default" type="text/html"><%=value%></script>
  10. <script id="noEscape" type="text/html"><%=#value%></script>
  11. <script id="include" type="text/html"><%include('include-content')%></script>
  12. <script id="include2" type="text/html"><%=include('include-content')%></script>
  13. <script id="include3" type="text/html"><%=#include('include-content')%></script>
  14. <script id="include-content" type="text/html"><%=#value%></script>
  15. <script id="print" type="text/html"><%print(value)%></script>
  16. <script id="print2" type="text/html"><%=print(value)%></script>
  17. <script id="print3" type="text/html"><%=#print(value)%></script>
  18. <script id="sandbox" type="text/html"><%=typeof document%></script>
  19. <script id="sandbox2" type="text/html"><%if (window) window.$sandbox = true;%></script>
  20. <script id="prototype" type="text/html"><%=toString%></script>
  21. <script id="html" type="text/html">""''\\</script>
  22. <script id="debug-render" type="text/html"><%=a.b.c.d.e.f%></script>
  23. <script id="debug-syntax" type="text/html"><%=a b c d e f%></script>
  24. <script id="helper" type="text/html"><%=test (value)%></script>
  25. <script id="helper2" type="text/html"><%=#test (value)%></script>
  26. <script id="helper3" type="text/html"><%=#test (value)%></script>
  27. <script id="parser" type="text/html">
  28. <%if(0===a) {}%><%1===5?2:3%><%%>
  29. </script>
  30. <script>
  31. if (!window.console) {
  32. window.console = {
  33. log: function () {}
  34. }
  35. }
  36. test('基本', function () {
  37. var data = {value: 'hello <em>world</em>'};
  38. equal(typeof template.compile('<%=value%>'), 'function', '编译成函数');
  39. equal(template('default', data), 'hello &#60;em&#62;world&#60;/em&#62;', '编码输出');
  40. equal(template('noEscape', data), 'hello <em>world</em>', '原文输出');
  41. });
  42. test('特殊类型变量输出', function () {
  43. var data = {value: 'hello <em>world</em>'};
  44. equal(template('default', {value:function(){return 'hello world'}}), 'hello world', '函数类型运算后输出');
  45. equal(template('default', {value:0}), '0', 'Number类型输出');
  46. equal(template('default', {value:false}), '', 'Boolean(false)类型输出空值');
  47. equal(template('default', {value:true}), '', 'Boolean(true)类型输出空值');
  48. equal(template('default', {value:{}}), '', 'Object类型输出空值');
  49. });
  50. test('特殊字符输出', function () {
  51. equal(template('html', {}), '""\'\'\\\\', '编码输出js特殊字符');
  52. });
  53. test('特殊字段名输出', function () {
  54. equal(template('prototype', {toString: 'hello world'}), 'hello world', '避免输出出错的值');
  55. });
  56. test('XSS防范测试', function () {
  57. equal(template('default', {value:'<>"\'&'}), '&#60;&#62;&#34;&#39;&#38;', 'HTML字符转义');
  58. });
  59. test('空值处理', function () {
  60. equal(template('default', {value:''}), '', '空字符串输出');
  61. equal(template('default', {}), '', 'undefined转成空值');
  62. equal(template('default', {value:null}), '', 'null转成空值');
  63. });
  64. test('内置方法', function () {
  65. var data = {value: 'hello <em>world</em>'};
  66. equal(template('include', data), 'hello <em>world</em>', 'include');
  67. equal(template('include2', data), 'hello <em>world</em>', '=include');
  68. equal(template('include3', data), 'hello <em>world</em>', '==include');
  69. equal(template('print', data), 'hello <em>world</em>', 'print');
  70. equal(template('print2', data), 'hello <em>world</em>', '=print');
  71. equal(template('print3', data), 'hello <em>world</em>', '==print');
  72. });
  73. test('辅助方法', function () {
  74. var data = {value: 'hello world'};
  75. template.helper('test', function (content) {
  76. return '<em>' + content + '</em>';
  77. });
  78. equal(template('helper', data), '<em>hello world</em>', 'helper');
  79. equal(template('helper2', data), '<em>hello world</em>', '=helper');
  80. equal(template('helper2', data), '<em>hello world</em>', '==helper');
  81. });
  82. test('沙箱', function () {
  83. equal(template('sandbox', {}), 'undefined', '拒绝读取外部对象');
  84. template('sandbox2', {});
  85. equal(window.$sandbox, undefined, '防范污染外部对象');
  86. });
  87. test('调试', function () {
  88. var onerror = template.onerror;
  89. var error = null;
  90. template.onerror = function (e) {
  91. console.log(e)
  92. error = e;
  93. };
  94. template('debug-render-xxxxxxxxx', {});
  95. deepEqual({
  96. name: error.name,
  97. message: error.message
  98. }, {
  99. name: 'Render Error',
  100. message: 'No Template'
  101. }, '没有找到模板');
  102. error = null;
  103. template.onerror = function (e) {
  104. console.log(e)
  105. error = e;
  106. };
  107. template('debug-render', {});
  108. deepEqual({
  109. name: error.name,
  110. line: error.line
  111. }, {
  112. name: 'Render Error',
  113. line: 1
  114. }, '渲染错误调试');
  115. error = null;
  116. template.onerror = function (e) {
  117. console.log(e)
  118. error = e;
  119. };
  120. template('debug-syntax', {});
  121. deepEqual({
  122. name: error.name
  123. }, {
  124. name: 'Syntax Error'
  125. }, '语法错误调试');
  126. template.onerror = onerror;
  127. });
  128. test('配置功能', function () {
  129. template.isEscape = false;
  130. template.openTag = '{{';
  131. template.closeTag = '}}';
  132. var data = {value: 'hello <em>world</em>'};
  133. equal(template.compile('{{=value}}')(data), 'hello <em>world</em>', '自定义界定符');
  134. equal(template.compile('{{=value}}')(data), 'hello <em>world</em>', '关闭默认编码输出');
  135. template.isEscape = true;
  136. template.openTag = '<%';
  137. template.closeTag = '%>';
  138. });
  139. test('语法', function () {
  140. var value = template('parser', {});
  141. equal(value, '');
  142. });
  143. </script>
  144. </head>
  145. <body>
  146. <div id="qunit"></div>
  147. <div id="qunit-fixture">test markup</div>
  148. </body>
  149. </html>