You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

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