Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

547 строки
11KB

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