Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

572 lignes
12KB

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