Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

577 wiersze
11KB

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