25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

633 lines
13KB

  1. // the compiler tests are the exact same as the interpreter tests
  2. // so instead of writing all the tests twice, override the to_html
  3. // method
  4. module('Compiler', {
  5. setup: function() {
  6. this._oldToHtml = Mustache.to_html;
  7. Mustache.to_html = function(template, view, partials) {
  8. var compiler = Mustache.compile(template, partials);
  9. return compiler(view);
  10. }
  11. },
  12. teardown: function() {
  13. Mustache.to_html = this._oldToHtml;
  14. }
  15. });
  16. test("Argument validation", function() {
  17. expect(4);
  18. equals(Mustache.to_html(undefined), '', 'No parameters');
  19. equals(Mustache.to_html('{{hi}}'), '', ' No View or Partials');
  20. equals(Mustache.to_html('{{hi}}', {hi:'Hi.'}), 'Hi.', 'No Partials');
  21. equals(Mustache.to_html('{{>hi}}', undefined, {hi:'{{p}}'}), '', 'Partial but no view');
  22. });
  23. test("Parser", function() {
  24. expect(4);
  25. // matches whitespace_partial.html
  26. equals(
  27. Mustache.to_html(
  28. '<h1>{{ greeting }}</h1>\n{{> partial }}\n<h3>{{ farewell }}</h3>',
  29. {
  30. greeting: function() {
  31. return "Welcome";
  32. },
  33. farewell: function() {
  34. return "Fair enough, right?";
  35. },
  36. partial: {
  37. name: "Chris",
  38. value: 10000,
  39. taxed_value: function() {
  40. return this.value - (this.value * 0.4);
  41. },
  42. in_ca: true
  43. }
  44. },
  45. {partial:'Hello {{ name}}\nYou have just won ${{value }}!\n{{# in_ca }}\nWell, ${{ taxed_value }}, after taxes.\n{{/ in_ca }}\n'}
  46. ),
  47. '<h1>Welcome</h1>\nHello Chris\nYou have just won $10000!\n\nWell, $6000, after taxes.\n\n\n<h3>Fair enough, right?</h3>',
  48. 'Whitespace in Tag names'
  49. );
  50. equals(
  51. Mustache.to_html(
  52. '{{tag1}}\n\n\n{{tag2}}\n',
  53. { tag1: 'Hello', tag2: 'World' },
  54. {}
  55. ),
  56. 'Hello\n\n\nWorld\n',
  57. 'Preservation of white space'
  58. );
  59. try {
  60. Mustache.to_html(
  61. '{{=tag1}}',
  62. { tag1: 'Hello' },
  63. {}
  64. );
  65. ok(false);
  66. } catch (e) {
  67. equals(e.message, 'Unexpected end of document.');
  68. }
  69. var partials = { 'partial' : '{{key}}' };
  70. Mustache.compile('{{>partial}}', partials );
  71. equals(partials['partial'], '{{key}}', 'Partials compiler must be non-destructive');
  72. });
  73. test("Basic Variables", function() {
  74. expect(3);
  75. // matches escaped.html
  76. equals(
  77. Mustache.to_html(
  78. '<h1>{{title}}</h1>\nBut not {{entities}}.\n',
  79. {
  80. title: function() {
  81. return "Bear > Shark";
  82. },
  83. entities: "&quot;"
  84. },
  85. {}
  86. ),
  87. '<h1>Bear &gt; Shark</h1>\nBut not &amp;quot;.\n',
  88. 'HTML Escaping'
  89. );
  90. // matches null_string.html
  91. equals(
  92. Mustache.to_html(
  93. 'Hello {{name}}\nglytch {{glytch}}\nbinary {{binary}}\nvalue {{value}}\nnumeric {{numeric}}',
  94. {
  95. name: "Elise",
  96. glytch: true,
  97. binary: false,
  98. value: null,
  99. numeric: function() {
  100. return NaN;
  101. }
  102. },
  103. {}
  104. ),
  105. 'Hello Elise\nglytch true\nbinary false\nvalue \nnumeric NaN',
  106. 'Different variable types'
  107. );
  108. // matches two_in_a_row.html
  109. equals(
  110. Mustache.to_html(
  111. '{{greeting}}, {{name}}!',
  112. {
  113. name: "Joe",
  114. greeting: "Welcome"
  115. },
  116. {}
  117. ),
  118. 'Welcome, Joe!'
  119. );
  120. });
  121. test("'{' or '&' (Unescaped Variable)", function() {
  122. expect(2);
  123. // matches unescaped.html
  124. equals(
  125. Mustache.to_html(
  126. '<h1>{{{title}}}</h1>',
  127. {
  128. title: function() {
  129. return "Bear > Shark";
  130. }
  131. },
  132. {}
  133. ),
  134. '<h1>Bear > Shark</h1>',
  135. '{ character'
  136. );
  137. equals(
  138. Mustache.to_html(
  139. '<h1>{{&title}}</h1>',
  140. {
  141. title: function() {
  142. return "Bear > Shark";
  143. }
  144. },
  145. {}
  146. ),
  147. '<h1>Bear > Shark</h1>',
  148. '& character'
  149. );
  150. });
  151. test("'#' (Sections)", function() {
  152. expect(7);
  153. // matches array_of_partials_implicit_partial.html
  154. equals(
  155. Mustache.to_html(
  156. 'Here is some stuff!\n{{#numbers}}\n{{>partial}}\n{{/numbers}}',
  157. { numbers: ['1', '2', '3', '4'] },
  158. { partial: '{{.}}' }
  159. ),
  160. 'Here is some stuff!\n\n1\n\n2\n\n3\n\n4\n',
  161. 'Array of Partials (Implicit)'
  162. );
  163. // matches array_of_partials_partial.html
  164. equals(
  165. Mustache.to_html(
  166. 'Here is some stuff!\n{{#numbers}}\n{{>partial}}\n{{/numbers}}',
  167. { numbers: [{i: '1'}, {i: '2'}, {i: '3'}, {i: '4'}] },
  168. { partial: '{{i}}' }
  169. ),
  170. 'Here is some stuff!\n\n1\n\n2\n\n3\n\n4\n',
  171. 'Array of Partials (Explicit)'
  172. );
  173. // matches array_of_strings.html
  174. equals(
  175. Mustache.to_html(
  176. '{{#array_of_strings}}{{.}} {{/array_of_strings}}',
  177. {array_of_strings: ['hello', 'world']},
  178. {}
  179. ),
  180. 'hello world ',
  181. 'Array of Strings'
  182. );
  183. // mathces higher_order_sections.html
  184. equals(
  185. Mustache.to_html(
  186. '{{#bolder}}Hi {{name}}.{{/bolder}}\n',
  187. {
  188. "name": "Tater",
  189. "helper": "To tinker?",
  190. "bolder": function() {
  191. return function(text, render) {
  192. return "<b>" + render(text) + '</b> ' + this.helper;
  193. }
  194. }
  195. },
  196. {}
  197. ),
  198. '<b>Hi Tater.</b> To tinker?\n'
  199. );
  200. // matches recursion_with_same_names.html
  201. equals(
  202. Mustache.to_html(
  203. '{{ name }}\n{{ description }}\n\n{{#terms}}\n {{name}}\n {{index}}\n{{/terms}}\n',
  204. {
  205. name: 'name',
  206. description: 'desc',
  207. terms: [
  208. {name: 't1', index: 0},
  209. {name: 't2', index: 1},
  210. ]
  211. },
  212. {}
  213. ),
  214. 'name\ndesc\n\n\n t1\n 0\n\n t2\n 1\n\n'
  215. );
  216. // matches reuse_of_enumerables.html
  217. equals(
  218. Mustache.to_html(
  219. '{{#terms}}\n {{name}}\n {{index}}\n{{/terms}}\n{{#terms}}\n {{name}}\n {{index}}\n{{/terms}}\n',
  220. {
  221. terms: [
  222. {name: 't1', index: 0},
  223. {name: 't2', index: 1},
  224. ]
  225. },
  226. {}
  227. ),
  228. '\n t1\n 0\n\n t2\n 1\n\n\n t1\n 0\n\n t2\n 1\n\n',
  229. 'Lazy match of Section and Inverted Section'
  230. );
  231. // matches section_as_context.html
  232. equals(
  233. Mustache.to_html(
  234. '{{#a_object}}\n <h1>{{title}}</h1>\n <p>{{description}}</p>\n <ul>\n {{#a_list}}\n <li>{{label}}</li>\n {{/a_list}}\n </ul>\n{{/a_object}}\n',
  235. {
  236. a_object: {
  237. title: 'this is an object',
  238. description: 'one of its attributes is a list',
  239. a_list: [{label: 'listitem1'}, {label: 'listitem2'}]
  240. }
  241. },
  242. {}
  243. ),
  244. '\n <h1>this is an object</h1>\n <p>one of its attributes is a list</p>\n <ul>\n \n <li>listitem1</li>\n \n <li>listitem2</li>\n \n </ul>\n\n',
  245. 'Lazy match of Section and Inverted Section'
  246. );
  247. });
  248. test("'^' (Inverted Section)", function() {
  249. expect(1);
  250. // matches inverted_section.html
  251. equals(
  252. Mustache.to_html(
  253. '{{#repo}}<b>{{name}}</b>{{/repo}}\n{{^repo}}No repos :({{/repo}}\n',
  254. {
  255. "repo": []
  256. },
  257. {}
  258. ),
  259. '\nNo repos :(\n'
  260. );
  261. });
  262. test("'>' (Partials)", function() {
  263. expect(5);
  264. // matches view_partial.html
  265. equals(
  266. Mustache.to_html(
  267. '<h1>{{greeting}}</h1>\n{{>partial}}\n<h3>{{farewell}}</h3>',
  268. {
  269. greeting: function() {
  270. return "Welcome";
  271. },
  272. farewell: function() {
  273. return "Fair enough, right?";
  274. },
  275. partial: {
  276. name: "Chris",
  277. value: 10000,
  278. taxed_value: function() {
  279. return this.value - (this.value * 0.4);
  280. },
  281. in_ca: true
  282. }
  283. },
  284. {partial: 'Hello {{name}}\nYou have just won ${{value}}!\n{{#in_ca}}\nWell, ${{ taxed_value }}, after taxes.\n{{/in_ca}}\n'}
  285. ),
  286. '<h1>Welcome</h1>\nHello Chris\nYou have just won $10000!\n\nWell, $6000, after taxes.\n\n\n<h3>Fair enough, right?</h3>'
  287. );
  288. // matches array_partial.html
  289. equals(
  290. Mustache.to_html(
  291. '{{>partial}}',
  292. {
  293. partial: {
  294. array: ['1', '2', '3', '4']
  295. }
  296. },
  297. { partial: 'Here\'s a non-sense array of values\n{{#array}}\n {{.}}\n{{/array}}' }
  298. ),
  299. 'Here\'s a non-sense array of values\n\n 1\n\n 2\n\n 3\n\n 4\n'
  300. );
  301. // matches template_partial.html
  302. equals(
  303. Mustache.to_html(
  304. '<h1>{{title}}</h1>\n{{>partial}}',
  305. {
  306. title: function() {
  307. return "Welcome";
  308. },
  309. partial: {
  310. again: "Goodbye"
  311. }
  312. },
  313. {partial:'Again, {{again}}!'}
  314. ),
  315. '<h1>Welcome</h1>\nAgain, Goodbye!'
  316. );
  317. // matches partial_recursion.html
  318. equals(
  319. Mustache.to_html(
  320. '{{name}}\n{{#kids}}\n{{>partial}}\n{{/kids}}',
  321. {
  322. name: '1',
  323. kids: [
  324. {
  325. name: '1.1',
  326. children: [
  327. {name: '1.1.1'}
  328. ]
  329. }
  330. ]
  331. },
  332. {partial:'{{name}}\n{{#children}}\n{{>partial}}\n{{/children}}'}
  333. ),
  334. '1\n\n1.1\n\n1.1.1\n\n\n'
  335. );
  336. try {
  337. Mustache.to_html(
  338. '{{>partial}}',
  339. {},
  340. {partal: ''}
  341. );
  342. ok(false);
  343. } catch(e) {
  344. equals(e.message, "Unknown partial 'partial'");
  345. }
  346. });
  347. test("'=' (Set Delimiter)", function() {
  348. expect(1);
  349. // matches delimiter.html
  350. equals(
  351. Mustache.to_html(
  352. '{{=<% %>=}}*\n<% first %>\n* <% second %>\n<%=| |=%>\n* | third |\n|={{ }}=|\n* {{ fourth }}',
  353. {
  354. first: "It worked the first time.",
  355. second: "And it worked the second time.",
  356. third: "Then, surprisingly, it worked the third time.",
  357. fourth: "Fourth time also fine!."
  358. },
  359. {}
  360. ),
  361. '*\nIt worked the first time.\n* And it worked the second time.\n\n* Then, surprisingly, it worked the third time.\n\n* Fourth time also fine!.',
  362. 'Simple Set Delimiter'
  363. );
  364. });
  365. test("'!' (Comments)", function() {
  366. expect(1);
  367. // matches comments.html
  368. equals(
  369. Mustache.to_html(
  370. '<h1>{{title}}{{! just something interesting... or not... }}</h1>\n',
  371. {
  372. title: function() {
  373. return "A Comedy of Errors";
  374. }
  375. },
  376. {}
  377. ),
  378. '<h1>A Comedy of Errors</h1>\n'
  379. );
  380. });
  381. test("'%' (Pragmas)", function() {
  382. expect(3);
  383. // matches array_of_strings_options.html
  384. equals(
  385. Mustache.to_html(
  386. '{{%IMPLICIT-ITERATOR iterator=rob}}\n{{#array_of_strings_options}}{{rob}} {{/array_of_strings_options}}',
  387. {array_of_strings_options: ['hello', 'world']},
  388. {}
  389. ),
  390. '\nhello world ',
  391. 'IMPLICIT-ITERATOR pragma'
  392. );
  393. // matches unknown_pragma.txt
  394. try {
  395. equals(
  396. Mustache.to_html(
  397. '{{%I-HAVE-THE-GREATEST-MUSTACHE}}\n',
  398. {},
  399. {}
  400. ),
  401. 'hello world ',
  402. 'IMPLICIT-ITERATOR pragma'
  403. );
  404. ok(false);
  405. } catch (e) {
  406. equals(e.message, 'This implementation of mustache doesn\'t understand the \'I-HAVE-THE-GREATEST-MUSTACHE\' pragma');
  407. }
  408. equals(
  409. Mustache.to_html(
  410. '{{%IMPLICIT-ITERATOR}}{{#dataSet}}{{.}}:{{/dataSet}}',
  411. { dataSet: [ 'Object 1', 'Object 2', 'Object 3' ] },
  412. {}
  413. ),
  414. "Object 1:Object 2:Object 3:"
  415. );
  416. });
  417. test("Empty", function() {
  418. expect(2);
  419. // matches empty_template.html
  420. equals(
  421. Mustache.to_html(
  422. '<html><head></head><body><h1>Test</h1></body></html>',
  423. {},
  424. {}
  425. ),
  426. '<html><head></head><body><h1>Test</h1></body></html>',
  427. 'Empty Template'
  428. );
  429. // matches empty_partial.html
  430. equals(
  431. Mustache.to_html(
  432. 'hey {{foo}}\n{{>partial}}\n',
  433. {
  434. foo: 1
  435. },
  436. {partial: 'yo'}
  437. ),
  438. 'hey 1\nyo\n',
  439. 'Empty Partial'
  440. );
  441. });
  442. test("Demo", function() {
  443. expect(2);
  444. // matches simple.html
  445. equals(
  446. Mustache.to_html(
  447. 'Hello {{name}}\nYou have just won ${{value}}!\n{{#in_ca}}\nWell, ${{ taxed_value }}, after taxes.\n{{/in_ca}}',
  448. {
  449. name: "Chris",
  450. value: 10000,
  451. taxed_value: function() {
  452. return this.value - (this.value * 0.4);
  453. },
  454. in_ca: true
  455. },
  456. {}
  457. ),
  458. 'Hello Chris\nYou have just won $10000!\n\nWell, $6000, after taxes.\n',
  459. 'A simple template'
  460. );
  461. // matches complex.html
  462. var template = [
  463. '<h1>{{header}}</h1>',
  464. '{{#list}}',
  465. ' <ul>',
  466. ' {{#item}}',
  467. ' {{#current}}',
  468. ' <li><strong>{{name}}</strong></li>',
  469. ' {{/current}}',
  470. ' {{#link}}',
  471. ' <li><a href="{{url}}">{{name}}</a></li>',
  472. ' {{/link}}',
  473. ' {{/item}}',
  474. ' </ul>',
  475. '{{/list}}',
  476. '{{#empty}}',
  477. ' <p>The list is empty.</p>',
  478. '{{/empty}}',
  479. ].join('\n');
  480. var view = {
  481. header: function() {
  482. return "Colors";
  483. },
  484. item: [
  485. {name: "red", current: true, url: "#Red"},
  486. {name: "green", current: false, url: "#Green"},
  487. {name: "blue", current: false, url: "#Blue"}
  488. ],
  489. link: function() {
  490. return this["current"] !== true;
  491. },
  492. list: function() {
  493. return this.item.length !== 0;
  494. },
  495. empty: function() {
  496. return this.item.length === 0;
  497. }
  498. };
  499. var expected_result = '<h1>Colors</h1>\n\n <ul>\n \n \n <li><strong>red</strong></li>\n \n \n <li><a href=\"#Red\">red</a></li>\n \n \n \n \n <li><a href=\"#Green\">green</a></li>\n \n \n \n \n <li><a href=\"#Blue\">blue</a></li>\n \n \n </ul>\n\n';
  500. equals(
  501. Mustache.to_html(
  502. template,
  503. view,
  504. {}
  505. ),
  506. expected_result,
  507. 'A complex template'
  508. );
  509. });
  510. test("Performance", function() {
  511. expect(1);
  512. var start, end;
  513. var view = [];
  514. for (var i=0;i<1000;++i) {
  515. view.push({name:i});
  516. }
  517. var template = '{{#count}}{{name}}\n{{/count}}';
  518. start = Date.now();
  519. for (var j=0;j<1000;++j) {
  520. this._oldToHtml(template, view, {});
  521. }
  522. end = Date.now();
  523. var interpreter_time = end - start;
  524. start = Date.now();
  525. var compiler = Mustache.compile(template, {});
  526. for (var k=0;k<1000;++k) {
  527. compiler(view);
  528. }
  529. end = Date.now();
  530. var compiler_time = end - start;
  531. ok(compiler_time<interpreter_time, 'Compiler is faster.');
  532. });
  533. test("Regression Suite", function() {
  534. expect(3);
  535. // matches bug_11_eating_whitespace.html
  536. equals(
  537. Mustache.to_html(
  538. '{{tag}} foo',
  539. { tag: "yo" },
  540. {}
  541. ),
  542. 'yo foo',
  543. 'Issue 11'
  544. );
  545. // matches delimiters_partial.html
  546. equals(
  547. Mustache.to_html(
  548. '{{#enumerate}}\n{{>partial}}\n{{/enumerate}}',
  549. { enumerate: [ { text: 'A' }, { text: 'B' } ] },
  550. { partial: '{{=[[ ]]=}}\n{{text}}\n[[={{ }}=]]' }
  551. ),
  552. '\n\n{{text}}\n\n\n\n{{text}}\n\n',
  553. 'Issue 44'
  554. );
  555. // matches bug_46_set_delimiter.html
  556. equals(
  557. Mustache.to_html(
  558. '{{=[[ ]]=}}[[#IsMustacheAwesome]]mustache is awesome![[/IsMustacheAwesome]]',
  559. {IsMustacheAwesome: true},
  560. {}
  561. ),
  562. 'mustache is awesome!',
  563. 'Issue 46'
  564. );
  565. });