PHP and Javascript implementations of a simple markdown parser
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.

testjs.html 61KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8">
  5. <title>Markdown Unit Tests</title>
  6. <link rel="icon" href="data:;base64,iVBORw0KGgo=">
  7. <style type="text/css">
  8. :root {
  9. font-family: sans-serif;
  10. --color-passed: #090;
  11. --color-failed: #a00;
  12. --color-errored: #a80;
  13. --color-untested: #888;
  14. --color-background: #fff;
  15. --color-text: #000;
  16. --color-secondary: #888;
  17. background-color: var(--color-background);
  18. color: var(--color-text);
  19. }
  20. .testclass {
  21. border: 1px solid var(--color-secondary);
  22. padding: 0.5em 1em;
  23. margin-bottom: 1em;
  24. max-width: 50em;
  25. }
  26. .testclassname {
  27. font-weight: bold;
  28. font-size: 1.25rem;
  29. padding-bottom: 0.25em;
  30. }
  31. .testclassstatus.passed { color: var(--color-passed); }
  32. .testclassstatus.failed { color: var(--color-failed); }
  33. .testclassstatus.errored { color: var(--color-errored); }
  34. .testclassstatus.untested { color: var(--color-untested); }
  35. .testcase {
  36. clear: both;
  37. padding: 0.2em 0;
  38. margin-left: 2em;
  39. }
  40. .testcase {
  41. border-top: 1px solid var(--color-secondary);
  42. }
  43. .testcasename {
  44. font-family: monospace;
  45. }
  46. .testcasestatus {
  47. font-weight: bold;
  48. font-size: 80%;
  49. float: left;
  50. }
  51. .testcasetiming {
  52. float: right;
  53. color: var(--color-secondary);
  54. font-size: 80%;
  55. }
  56. .testcaseresult {
  57. height: 1em;
  58. }
  59. .testcasemessage {
  60. clear: both;
  61. }
  62. .result-untested {
  63. color: var(--color-untested);
  64. }
  65. .result-testing {
  66. color: var(--color-text);
  67. }
  68. .result-passed {
  69. color: var(--color-passed);
  70. }
  71. .result-failed {
  72. color: var(--color-failed);
  73. }
  74. .result-errored {
  75. color: var(--color-errored);
  76. }
  77. @media (prefers-color-scheme: dark) {
  78. :root {
  79. --color-passed: #090;
  80. --color-failed: #a00;
  81. --color-errored: #a80;
  82. --color-untested: #888;
  83. --color-background: #000;
  84. --color-text: #fff;
  85. --color-secondary: #888;
  86. }
  87. }
  88. </style>
  89. <script src="js/markdown.js"></script>
  90. <script src="js/spreadsheet.js"></script>
  91. <!-- Testing infrastructure -->
  92. <script>
  93. /**
  94. * @param {String} text
  95. * @returns {String}
  96. */
  97. function escapeHTML(text) {
  98. return text.replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;').replace(/\n/g, '<br/>\n');
  99. }
  100. class ResultType {
  101. static untested = new ResultType('untested');
  102. static testing = new ResultType('testing');
  103. static passed = new ResultType('passed');
  104. static failed = new ResultType('failed');
  105. static errored = new ResultType('errored');
  106. #name;
  107. constructor(name) {
  108. this.#name = name;
  109. }
  110. toString() {
  111. return `${this.constructor.name}.${this.#name}`;
  112. }
  113. }
  114. class FailureError extends Error {
  115. constructor(message=null) {
  116. super(message);
  117. }
  118. }
  119. class BaseTest {
  120. setUp() {}
  121. tearDown() {}
  122. /** @var {TestCaseRunner|null} */
  123. currentRunner = null;
  124. fail(failMessage=null) {
  125. throw new FailureError(failMessage || 'failed');
  126. }
  127. assertTrue(test, failMessage=null) {
  128. if (!test) this.fail(failMessage || `expected true, got ${test}`);
  129. }
  130. assertFalse(test, failMessage=null) {
  131. if (test) this.fail(failMessage || `expected false, got ${test}`);
  132. }
  133. assertEqual(a, b, floatDifferenceRatio=0.000001, failMessage=null) {
  134. if (MDUtils.equal(a, b, floatDifferenceRatio)) return;
  135. const aVal = (typeof a == 'string') ? `"${a}"` : `${a}`;
  136. const bVal = (typeof b == 'string') ? `"${b}"` : `${b}`;
  137. if (aVal.length > 20 || bVal.length > 20) {
  138. this.fail(failMessage || `equality failed:\n${aVal}\n!=\n${bVal}`);
  139. } else {
  140. this.fail(failMessage || `equality failed: ${aVal} != ${bVal}`);
  141. }
  142. }
  143. expectError(e=true) {
  144. if (this.currentRunner) this.currentRunner.expectedError = e;
  145. }
  146. /**
  147. * @param {number} maxTime maximum time in seconds
  148. * @param {function} timedCode code to run and time
  149. */
  150. profile(maxTime, timedCode) {
  151. const startTime = performance.now();
  152. const result = timedCode();
  153. const endTime = performance.now();
  154. const seconds = (endTime - startTime) / 1000.0;
  155. if (seconds > maxTime) {
  156. this.fail(`Expected <= ${maxTime}s execution time, actual ${seconds}s.`);
  157. }
  158. return result;
  159. }
  160. }
  161. /**
  162. * Manages the running and results of a single test method on a test
  163. * class.
  164. */
  165. class TestCaseRunner {
  166. /** @var {number} */
  167. static #nextUniqueId = 1;
  168. /** @var {number} */
  169. uniqueId = 0;
  170. /** @var {BaseTest} */
  171. #objectUnderTest;
  172. /** @var {method} */
  173. #method;
  174. /** @var {ResultType} */
  175. result = ResultType.untested;
  176. /** @var {String|null} */
  177. message = null;
  178. expectedError = null;
  179. duration = null;
  180. /** @var {String} */
  181. get className() { return this.#objectUnderTest.constructor.name; }
  182. /** @var {String} */
  183. get methodName() { return this.#method.name; }
  184. /**
  185. * @param {BaseTest} objectUnderTest
  186. * @param {method} method
  187. */
  188. constructor(objectUnderTest, method) {
  189. this.#objectUnderTest = objectUnderTest;
  190. this.#method = method;
  191. this.uniqueId = TestCaseRunner.#nextUniqueId++;
  192. }
  193. run() {
  194. var start;
  195. this.expectedError = null;
  196. this.#objectUnderTest.currentRunner = this;
  197. try {
  198. this.#objectUnderTest.setUp();
  199. } catch (e) {
  200. console.error(`Failed to run ${this.className}.setUp() - ${e.message}`);
  201. this.result = ResultType.errored;
  202. this.message = e.message;
  203. return;
  204. }
  205. try {
  206. start = performance.now();
  207. this.#method.bind(this.#objectUnderTest)();
  208. this.duration = performance.now() - start;
  209. this.result = ResultType.passed;
  210. this.message = null;
  211. } catch (e) {
  212. this.duration = performance.now() - start;
  213. if (e instanceof FailureError) {
  214. this.result = ResultType.failed;
  215. this.message = e.message;
  216. } else if (this.#isExpectedError(e)) {
  217. this.result = ResultType.passed;
  218. this.message = null;
  219. } else {
  220. this.result = ResultType.errored;
  221. this.message = e.message;
  222. if (e.stack !== undefined) {
  223. this.message += "\n" + e.stack;
  224. }
  225. }
  226. } finally {
  227. this.expectedError = null;
  228. try {
  229. this.#objectUnderTest.tearDown();
  230. this.#objectUnderTest.currentRunner = null;
  231. } catch (e0) {
  232. console.error(`Failed to run ${this.className}.tearDown() - ${e0.message}`);
  233. this.result = ResultType.errored;
  234. this.message = e0.message;
  235. }
  236. }
  237. }
  238. get #cssId() { return `testcase${this.uniqueId}`; }
  239. #isExpectedError(e) {
  240. if (this.expectedError === null) return false;
  241. if (this.expectedError === true) return true;
  242. // TODO: Have a way to specify details about what kind of error is expected. Maybe a prototype instance and/or a testing lambda.
  243. return false;
  244. }
  245. toHTML() {
  246. var html = `<div class="testcase" id="${this.#cssId}">`;
  247. html += `<div class="testcasename"><span class="testcasemethod">${this.methodName}</span></div>`;
  248. html += '<div class="testcaseresult">';
  249. switch (this.result) {
  250. case ResultType.untested:
  251. html += '<div class="testcasestatus result-untested">Waiting to test</div>';
  252. break;
  253. case ResultType.testing:
  254. html += '<div class="testcasestatus result-testing">Testing...</div>';
  255. break;
  256. case ResultType.passed:
  257. html += '<div class="testcasestatus result-passed">Passed</div>';
  258. break;
  259. case ResultType.failed:
  260. html += '<div class="testcasestatus result-failed">Failed</div>';
  261. break;
  262. case ResultType.errored:
  263. html += '<div class="testcasestatus result-errored">Errored</div>';
  264. break;
  265. }
  266. if (this.duration !== null) {
  267. html += `<div class="testcasetiming">${Number(this.duration / 1000.0).toFixed(3)}s</div>`;
  268. }
  269. html += '</div>';
  270. if (this.message !== null) {
  271. html += `<div class="testcasemessage">${escapeHTML(this.message)}</div>`;
  272. }
  273. html += `</div>`;
  274. return html;
  275. }
  276. /**
  277. * Updates the HTML node in-place with the current status, or
  278. * adds it if it does not exist yet.
  279. */
  280. updateHTML() {
  281. let existing = document.getElementById(this.#cssId);
  282. if (existing) {
  283. existing.outerHTML = this.toHTML();
  284. } else {
  285. document.getElementById('results').innerHTML += this.toHTML();
  286. }
  287. }
  288. /**
  289. * @param {object} objectUnderTest
  290. * @returns {TestCaseRunner[]}
  291. */
  292. static findTestCases(objectUnderTest) {
  293. if (!(objectUnderTest instanceof BaseTest)) return [];
  294. var members = [];
  295. var obj = objectUnderTest;
  296. do {
  297. members.push(...Object.getOwnPropertyNames(obj));
  298. } while (obj = Object.getPrototypeOf(obj));
  299. return members.sort().filter((e, i, arr) => {
  300. if (e != arr[i + 1] && typeof objectUnderTest[e] == 'function' && e.startsWith('test')) return true;
  301. }).map((name) => {
  302. return new TestCaseRunner(objectUnderTest, objectUnderTest[name]);
  303. });
  304. }
  305. }
  306. class TestClassRunner {
  307. static #nextUniqueId = 1;
  308. #uniqueId = 0;
  309. #theClass;
  310. #instance;
  311. #testCases;
  312. get testCases() { return this.#testCases; }
  313. constructor(theClass) {
  314. this.#theClass = theClass;
  315. this.#instance = new theClass();
  316. this.#testCases = TestCaseRunner.findTestCases(this.#instance);
  317. this.#uniqueId = TestClassRunner.#nextUniqueId++;
  318. }
  319. get #cssId() { return `testclass${this.#uniqueId}`; }
  320. #summaryHTML() {
  321. var anyTesting = false;
  322. var anyFailed = false;
  323. var anyErrored = false;
  324. var anyUntested = false;
  325. var anyPassed = false;
  326. var allPassed = true;
  327. for (const test of this.testCases) {
  328. switch (test.result) {
  329. case ResultType.untested:
  330. anyUntested = true;
  331. allPassed = false;
  332. break;
  333. case ResultType.testing:
  334. anyTesting = true;
  335. allPassed = false;
  336. break;
  337. case ResultType.passed:
  338. anyPassed = true;
  339. break;
  340. case ResultType.failed:
  341. anyFailed = true;
  342. allPassed = false;
  343. break;
  344. case ResultType.errored:
  345. anyErrored = true;
  346. allPassed = false;
  347. break;
  348. }
  349. }
  350. var html = '';
  351. html += `<summary class="testclasssummary" id="${this.#cssId}summary">`;
  352. html += `<span class="testclassname">${this.#theClass.name}</span> `;
  353. if (anyTesting || (anyUntested && (anyPassed || anyFailed || anyErrored))) {
  354. html += '<span class="testclassstatus testing">Testing...</span>';
  355. } else if (anyErrored) {
  356. html += '<span class="testclassstatus errored">Errored</span>';
  357. } else if (anyFailed) {
  358. html += '<span class="testclassstatus failed">Failed</span>';
  359. } else if (allPassed) {
  360. html += '<span class="testclassstatus passed">Passed</span>';
  361. }
  362. html += '</summary>';
  363. return html;
  364. }
  365. toHTML() {
  366. var html = '';
  367. html += `<div class="testclass" id="${this.#cssId}">`;
  368. html += `<details id="${this.#cssId}details">`;
  369. html += this.#summaryHTML();
  370. for (const testCase of this.#testCases) {
  371. html += testCase.toHTML();
  372. }
  373. html += '</details>';
  374. html += '</div>';
  375. return html;
  376. }
  377. updateHTML() {
  378. var existing = document.getElementById(`${this.#cssId}summary`);
  379. if (!existing) {
  380. document.getElementById('results').innerHTML += this.toHTML();
  381. } else {
  382. existing.outerHTML = this.#summaryHTML();
  383. var allPassed = true;
  384. for (const test of this.testCases) {
  385. if (test.result != ResultType.passed) {
  386. allPassed = false;
  387. break;
  388. }
  389. }
  390. document.getElementById(`${this.#cssId}details`).open = !allPassed;
  391. }
  392. }
  393. static runAll(testClasses) {
  394. var tests = []; // tuples of TestClassRunner and TestCaseRunner
  395. for (const testClass of testClasses) {
  396. const classRunner = new TestClassRunner(testClass);
  397. classRunner.updateHTML();
  398. tests.push(...classRunner.testCases.map(function(test) { return [ classRunner, test ] }));
  399. }
  400. var testInterval = setInterval(function() {
  401. if (tests.length == 0) {
  402. clearInterval(testInterval);
  403. testInterval = null;
  404. return;
  405. }
  406. var classRunner;
  407. var testCase;
  408. [ classRunner, testCase ] = tests[0];
  409. if (testCase.result == ResultType.untested) {
  410. testCase.result = ResultType.testing;
  411. testCase.updateHTML();
  412. classRunner.updateHTML();
  413. } else if (testCase.result == ResultType.testing) {
  414. tests.splice(0, 1);
  415. testCase.run();
  416. testCase.updateHTML();
  417. classRunner.updateHTML();
  418. }
  419. }, 1);
  420. }
  421. }
  422. </script>
  423. <!-- Tests -->
  424. <script>
  425. function onLoad() {
  426. let testClasses = [
  427. TokenTests,
  428. UtilsTests,
  429. InlineTests,
  430. BlockTests,
  431. CellValueTests,
  432. CellAddressRangeTests,
  433. ExpressionSetTests,
  434. SpreadsheetMarkdownIntegrationTests,
  435. ];
  436. TestClassRunner.runAll(testClasses);
  437. }
  438. document.addEventListener('DOMContentLoaded', onLoad);
  439. function normalizeWhitespace(str) {
  440. return str.replace(/\s+/g, ' ').replace(/(?:^\s+|\s+$)/g, '');
  441. }
  442. class TokenTests extends BaseTest {
  443. test_findFirstTokens() {
  444. const tokens = [
  445. new MDToken('Lorem', MDTokenType.Text),
  446. new MDToken(' ', MDTokenType.Whitespace),
  447. new MDToken('_', MDTokenType.Underscore),
  448. new MDToken('ipsum', MDTokenType.Text),
  449. new MDToken('_', MDTokenType.Underscore),
  450. new MDToken(' ', MDTokenType.Whitespace),
  451. new MDToken('dolor', MDTokenType.Text),
  452. new MDToken('_', MDTokenType.Underscore),
  453. new MDToken('sit', MDTokenType.Text),
  454. new MDToken('_', MDTokenType.Underscore),
  455. ];
  456. const pattern = [
  457. MDTokenType.Underscore,
  458. ];
  459. const result = MDToken.findFirstTokens(tokens, pattern);
  460. const expected = {
  461. tokens: [ tokens[2] ],
  462. index: 2,
  463. };
  464. this.assertEqual(result, expected);
  465. }
  466. test_findFirstTokens_optionalWhitespace1() {
  467. const tokens = [
  468. new MDToken('Lorem', MDTokenType.Text),
  469. new MDToken(' ', MDTokenType.Whitespace),
  470. new MDToken('[ipsum]', MDTokenType.Label, 'ipsum'),
  471. new MDToken('(link.html)', MDTokenType.URL, 'link.html'),
  472. new MDToken(' ', MDTokenType.Whitespace),
  473. new MDToken('dolor', MDTokenType.Text),
  474. ];
  475. const pattern = [
  476. MDTokenType.Label,
  477. MDTokenType.META_OptionalWhitespace,
  478. MDTokenType.URL,
  479. ];
  480. const result = MDToken.findFirstTokens(tokens, pattern);
  481. const expected = {
  482. tokens: [ tokens[2], tokens[3] ],
  483. index: 2,
  484. };
  485. this.assertEqual(result, expected);
  486. }
  487. test_findFirstTokens_optionalWhitespace2() {
  488. const tokens = [
  489. new MDToken('Lorem', MDTokenType.Text),
  490. new MDToken(' ', MDTokenType.Whitespace),
  491. new MDToken('[ipsum]', MDTokenType.Label, 'ipsum'),
  492. new MDToken(' ', MDTokenType.Whitespace),
  493. new MDToken('(link.html)', MDTokenType.URL, 'link.html'),
  494. new MDToken(' ', MDTokenType.Whitespace),
  495. new MDToken('dolor', MDTokenType.Text),
  496. ];
  497. const pattern = [
  498. MDTokenType.Label,
  499. MDTokenType.META_OptionalWhitespace,
  500. MDTokenType.URL,
  501. ];
  502. const result = MDToken.findFirstTokens(tokens, pattern);
  503. const expected = {
  504. tokens: [ tokens[2], tokens[3], tokens[4] ],
  505. index: 2,
  506. };
  507. this.assertEqual(result, expected);
  508. }
  509. test_findPairedTokens() {
  510. const tokens = [
  511. new MDToken('Lorem', MDTokenType.Text),
  512. new MDToken(' ', MDTokenType.Whitespace),
  513. new MDToken('_', MDTokenType.Underscore),
  514. new MDToken('ipsum', MDTokenType.Text),
  515. new MDToken('_', MDTokenType.Underscore),
  516. new MDToken(' ', MDTokenType.Whitespace),
  517. new MDToken('dolor', MDTokenType.Text),
  518. new MDToken('_', MDTokenType.Underscore),
  519. new MDToken('sit', MDTokenType.Text),
  520. new MDToken('_', MDTokenType.Underscore),
  521. ];
  522. const pattern = [
  523. MDTokenType.Underscore,
  524. ];
  525. const result = MDToken.findPairedTokens(tokens, pattern, pattern);
  526. const expected = {
  527. startTokens: [ tokens[2] ],
  528. contentTokens: [ tokens[3] ],
  529. endTokens: [ tokens[4] ],
  530. startIndex: 2,
  531. contentIndex: 3,
  532. endIndex: 4,
  533. totalLength: 3,
  534. }
  535. this.assertEqual(result, expected);
  536. }
  537. }
  538. class UtilsTests extends BaseTest {
  539. test_stripIndent() {
  540. this.assertEqual(MDUtils.stripIndent(''), '');
  541. this.assertEqual(MDUtils.stripIndent(' '), '');
  542. this.assertEqual(MDUtils.stripIndent('foo'), 'foo');
  543. this.assertEqual(MDUtils.stripIndent(' foo'), 'foo');
  544. this.assertEqual(MDUtils.stripIndent(' foo'), 'foo');
  545. this.assertEqual(MDUtils.stripIndent(' foo'), 'foo');
  546. this.assertEqual(MDUtils.stripIndent(' foo'), 'foo');
  547. this.assertEqual(MDUtils.stripIndent(' foo'), ' foo');
  548. this.assertEqual(MDUtils.stripIndent('\tfoo'), 'foo');
  549. this.assertEqual(MDUtils.stripIndent('\t\tfoo'), '\tfoo');
  550. this.assertEqual(MDUtils.stripIndent('\t\tfoo', 2), 'foo');
  551. this.assertEqual(MDUtils.stripIndent(' foo', 2), 'foo');
  552. }
  553. test_countIndents() {
  554. this.assertEqual(MDUtils.countIndents(''), 0);
  555. this.assertEqual(MDUtils.countIndents(' '), 1);
  556. this.assertEqual(MDUtils.countIndents(' '), 1);
  557. this.assertEqual(MDUtils.countIndents('foo'), 0);
  558. this.assertEqual(MDUtils.countIndents('foo'), 0);
  559. this.assertEqual(MDUtils.countIndents(' foo'), 1);
  560. this.assertEqual(MDUtils.countIndents(' foo'), 1);
  561. this.assertEqual(MDUtils.countIndents(' foo'), 1);
  562. this.assertEqual(MDUtils.countIndents(' foo'), 1);
  563. this.assertEqual(MDUtils.countIndents(' foo'), 2);
  564. this.assertEqual(MDUtils.countIndents('\tfoo'), 1);
  565. this.assertEqual(MDUtils.countIndents('\t\tfoo'), 2);
  566. this.assertEqual(MDUtils.countIndents('', true), 0);
  567. this.assertEqual(MDUtils.countIndents(' ', true), 0);
  568. this.assertEqual(MDUtils.countIndents(' ', true), 1);
  569. this.assertEqual(MDUtils.countIndents('foo', true), 0);
  570. this.assertEqual(MDUtils.countIndents(' foo', true), 0);
  571. this.assertEqual(MDUtils.countIndents(' foo', true), 0);
  572. this.assertEqual(MDUtils.countIndents(' foo', true), 0);
  573. this.assertEqual(MDUtils.countIndents(' foo', true), 1);
  574. this.assertEqual(MDUtils.countIndents(' foo', true), 1);
  575. this.assertEqual(MDUtils.countIndents('\tfoo', true), 1);
  576. this.assertEqual(MDUtils.countIndents('\t\tfoo', true), 2);
  577. }
  578. test_tokenizeLabel() {
  579. // Escapes are preserved
  580. this.assertEqual(MDUtils.tokenizeLabel('[foo] bar'), [ '[foo]', 'foo' ]);
  581. this.assertEqual(MDUtils.tokenizeLabel('[foo\\[] bar'), [ '[foo\\[]', 'foo\\[' ]);
  582. this.assertEqual(MDUtils.tokenizeLabel('[foo\\]] bar'), [ '[foo\\]]', 'foo\\]' ]);
  583. this.assertEqual(MDUtils.tokenizeLabel('[foo[]] bar'), [ '[foo[]]', 'foo[]' ]);
  584. this.assertEqual(MDUtils.tokenizeLabel('[foo\\(] bar'), [ '[foo\\(]', 'foo\\(' ]);
  585. this.assertEqual(MDUtils.tokenizeLabel('[foo\\)] bar'), [ '[foo\\)]', 'foo\\)' ]);
  586. this.assertEqual(MDUtils.tokenizeLabel('[foo()] bar'), [ '[foo()]', 'foo()' ]);
  587. this.assertEqual(MDUtils.tokenizeLabel('foo bar'), null);
  588. this.assertEqual(MDUtils.tokenizeLabel('[foo\\] bar'), null);
  589. this.assertEqual(MDUtils.tokenizeLabel('[foo bar'), null);
  590. this.assertEqual(MDUtils.tokenizeLabel('[foo[] bar'), null);
  591. }
  592. test_tokenizeURL() {
  593. this.assertEqual(MDUtils.tokenizeURL('(page.html) foo'), [ '(page.html)', 'page.html', null ]);
  594. this.assertEqual(MDUtils.tokenizeURL('(page.html "link title") foo'), [ '(page.html "link title")', 'page.html', 'link title' ]);
  595. this.assertEqual(MDUtils.tokenizeURL('(https://example.com/path/page.html?query=foo&bar=baz#fragment) foo'), [ '(https://example.com/path/page.html?query=foo&bar=baz#fragment)', 'https://example.com/path/page.html?query=foo&bar=baz#fragment', null ]);
  596. this.assertEqual(MDUtils.tokenizeURL('page.html foo'), null);
  597. this.assertEqual(MDUtils.tokenizeURL('(page.html foo'), null);
  598. this.assertEqual(MDUtils.tokenizeURL('page.html) foo'), null);
  599. this.assertEqual(MDUtils.tokenizeURL('(page.html "title) foo'), null);
  600. this.assertEqual(MDUtils.tokenizeURL('(page .html) foo'), null);
  601. this.assertEqual(MDUtils.tokenizeURL('(user@example.com) foo'), null);
  602. this.assertEqual(MDUtils.tokenizeURL('(user@example.com "title") foo'), null);
  603. }
  604. test_tokenizeEmail() {
  605. this.assertEqual(MDUtils.tokenizeEmail('(user@example.com)'), [ '(user@example.com)', 'user@example.com', null ]);
  606. this.assertEqual(MDUtils.tokenizeEmail('(user@example.com "link title")'), [ '(user@example.com "link title")', 'user@example.com', 'link title' ]);
  607. this.assertEqual(MDUtils.tokenizeEmail('(https://example.com) foo'), null);
  608. this.assertEqual(MDUtils.tokenizeEmail('(https://example.com "link title") foo'), null);
  609. this.assertEqual(MDUtils.tokenizeEmail('(user@example.com "link title) foo'), null);
  610. this.assertEqual(MDUtils.tokenizeEmail('(user@example.com foo'), null);
  611. this.assertEqual(MDUtils.tokenizeEmail('user@example.com) foo'), null);
  612. }
  613. }
  614. class InlineTests extends BaseTest {
  615. /** @type {Markdown} */
  616. parser;
  617. md(markdown) {
  618. return normalizeWhitespace(this.parser.toHTML(markdown));
  619. }
  620. setUp() {
  621. this.parser = Markdown.completeParser;
  622. }
  623. test_simpleText() {
  624. let markdown = 'Lorem ipsum';
  625. let expected = 'Lorem ipsum';
  626. let actual = this.md(markdown);
  627. this.assertEqual(actual, expected);
  628. }
  629. test_strong() {
  630. let markdown = 'Lorem **ipsum** dolor **sit**';
  631. let expected = 'Lorem <strong>ipsum</strong> dolor <strong>sit</strong>';
  632. let actual = this.md(markdown);
  633. this.assertEqual(actual, expected);
  634. }
  635. test_emphasis() {
  636. let markdown = 'Lorem _ipsum_ dolor _sit_';
  637. let expected = 'Lorem <em>ipsum</em> dolor <em>sit</em>';
  638. let actual = this.md(markdown);
  639. this.assertEqual(actual, expected);
  640. }
  641. test_strongEmphasis_cleanNesting1() {
  642. let markdown = 'Lorem **ipsum *dolor* sit** amet';
  643. let expected = 'Lorem <strong>ipsum <em>dolor</em> sit</strong> amet';
  644. let actual = this.md(markdown);
  645. this.assertEqual(actual, expected);
  646. }
  647. test_strongEmphasis_cleanNesting2() {
  648. let markdown = 'Lorem *ipsum **dolor** sit* amet';
  649. let expected = 'Lorem <em>ipsum <strong>dolor</strong> sit</em> amet';
  650. let actual = this.md(markdown);
  651. this.assertEqual(actual, expected);
  652. }
  653. test_strongEmphasis_tightNesting() {
  654. let markdown = 'Lorem ***ipsum*** dolor';
  655. let expected1 = 'Lorem <strong><em>ipsum</em></strong> dolor';
  656. let expected2 = 'Lorem <em><strong>ipsum</strong></em> dolor';
  657. let actual = this.md(markdown);
  658. this.assertTrue(actual == expected1 || actual == expected2);
  659. }
  660. test_strongEmphasis_lopsidedNesting1() {
  661. let markdown = 'Lorem ***ipsum* dolor** sit';
  662. let expected = 'Lorem <strong><em>ipsum</em> dolor</strong> sit';
  663. let actual = this.md(markdown);
  664. this.assertEqual(actual, expected);
  665. }
  666. test_strongEmphasis_lopsidedNesting2() {
  667. let markdown = 'Lorem ***ipsum** dolor* sit';
  668. let expected = 'Lorem <em><strong>ipsum</strong> dolor</em> sit';
  669. let actual = this.md(markdown);
  670. this.assertEqual(actual, expected);
  671. }
  672. test_strongEmphasis_lopsidedNesting3() {
  673. let markdown = 'Lorem **ipsum *dolor*** sit';
  674. let expected = 'Lorem <strong>ipsum <em>dolor</em></strong> sit';
  675. let actual = this.md(markdown);
  676. this.assertEqual(actual, expected);
  677. }
  678. test_strongEmphasis_lopsidedNesting4() {
  679. let markdown = 'Lorem *ipsum **dolor*** sit';
  680. let expected = 'Lorem <em>ipsum <strong>dolor</strong></em> sit';
  681. let actual = this.md(markdown);
  682. this.assertEqual(actual, expected);
  683. }
  684. test_inlineCode() {
  685. let markdown = 'Lorem `ipsum` dolor';
  686. let expected = 'Lorem <code>ipsum</code> dolor';
  687. let actual = this.md(markdown);
  688. this.assertEqual(actual, expected);
  689. }
  690. test_inlineCode_withInnerBacktick() {
  691. let markdown = 'Lorem ``ip`su`m`` dolor';
  692. let expected = 'Lorem <code>ip`su`m</code> dolor';
  693. let actual = this.md(markdown);
  694. this.assertEqual(actual, expected);
  695. }
  696. test_strikethrough_double() {
  697. let markdown = 'Lorem ~~ipsum~~ dolor';
  698. let expected = 'Lorem <s>ipsum</s> dolor';
  699. let actual = this.md(markdown);
  700. this.assertEqual(actual, expected);
  701. }
  702. test_subscript() {
  703. let markdown = 'H~2~O';
  704. let expected = 'H<sub>2</sub>O';
  705. let actual = this.md(markdown);
  706. this.assertEqual(actual, expected);
  707. }
  708. test_superscript() {
  709. let markdown = 'E=mc^2^';
  710. let expected = 'E=mc<sup>2</sup>';
  711. let actual = this.md(markdown);
  712. this.assertEqual(actual, expected);
  713. }
  714. test_highlight() {
  715. let markdown = 'Lorem ==ipsum== dolor';
  716. let expected = 'Lorem <mark>ipsum</mark> dolor';
  717. let actual = this.md(markdown);
  718. this.assertEqual(actual, expected);
  719. }
  720. test_underline() {
  721. let markdown = 'Lorem __ipsum__ dolor';
  722. let expected = 'Lorem <u>ipsum</u> dolor';
  723. let actual = this.md(markdown);
  724. this.assertEqual(actual, expected);
  725. }
  726. test_link_fullyQualified() {
  727. let markdown = 'Lorem [ipsum](https://example.com/path/page.html) dolor';
  728. let expected = 'Lorem <a href="https://example.com/path/page.html">ipsum</a> dolor';
  729. let actual = this.md(markdown);
  730. this.assertEqual(actual, expected);
  731. }
  732. test_link_relative() {
  733. let markdown = 'Lorem [ipsum](page.html) dolor';
  734. let expected = 'Lorem <a href="page.html">ipsum</a> dolor';
  735. let actual = this.md(markdown);
  736. this.assertEqual(actual, expected);
  737. }
  738. test_link_title() {
  739. let markdown = 'Lorem [ipsum](page.html "link title") dolor';
  740. let expected = 'Lorem <a href="page.html" title="link title">ipsum</a> dolor';
  741. let actual = this.md(markdown);
  742. this.assertEqual(actual, expected);
  743. }
  744. test_link_literal() {
  745. let markdown = 'Lorem <https://example.com> dolor';
  746. let expected = 'Lorem <a href="https://example.com">https://example.com</a> dolor';
  747. let actual = this.md(markdown);
  748. this.assertEqual(actual, expected);
  749. }
  750. test_link_ref() {
  751. let markdown = "Lorem [ipsum][ref] dolor\n\n[ref]: https://example.com";
  752. let expected = '<p>Lorem <a href="https://example.com">ipsum</a> dolor</p>';
  753. let actual = this.md(markdown);
  754. this.assertEqual(actual, expected);
  755. }
  756. test_link_email() {
  757. let markdown = 'Lorem [ipsum](user@example.com) dolor';
  758. let expected = 'Lorem <a href="mailto:&#117;&#115;&#101;&#114;&#64;&#101;&#120;&#97;&#109;&#112;&#108;&#101;&#46;&#99;&#111;&#109;">ipsum</a> dolor';
  759. let actual = this.md(markdown);
  760. this.assertEqual(actual, expected);
  761. }
  762. test_link_email_withTitle() {
  763. let markdown = 'Lorem [ipsum](user@example.com "title") dolor';
  764. let expected = 'Lorem <a href="mailto:&#117;&#115;&#101;&#114;&#64;&#101;&#120;&#97;&#109;&#112;&#108;&#101;&#46;&#99;&#111;&#109;" title="title">ipsum</a> dolor';
  765. let actual = this.md(markdown);
  766. this.assertEqual(actual, expected);
  767. }
  768. test_link_literalEmail() {
  769. let markdown = 'Lorem <user@example.com> dolor';
  770. let expected = 'Lorem <a href="mailto:&#117;&#115;&#101;&#114;&#64;&#101;&#120;&#97;&#109;&#112;&#108;&#101;&#46;&#99;&#111;&#109;">&#117;&#115;&#101;&#114;&#64;&#101;&#120;&#97;&#109;&#112;&#108;&#101;&#46;&#99;&#111;&#109;</a> dolor';
  771. let actual = this.md(markdown);
  772. this.assertEqual(actual, expected);
  773. }
  774. test_link_image() {
  775. let markdown = 'Lorem [![alt](image.jpg)](page.html) ipsum';
  776. let expected = 'Lorem <a href="page.html"><img src="image.jpg" alt="alt"></a> ipsum';
  777. let actual = this.md(markdown);
  778. this.assertEqual(actual, expected);
  779. }
  780. test_link_image_complex() {
  781. let markdown = 'Lorem [![alt] (image.jpg "image title")] (page.html "link title") ipsum';
  782. let expected = 'Lorem <a href="page.html" title="link title"><img src="image.jpg" alt="alt" title="image title"></a> ipsum';
  783. let actual = this.md(markdown);
  784. this.assertEqual(actual, expected);
  785. }
  786. test_image() {
  787. let markdown = 'Lorem ![alt text](image.jpg) dolor';
  788. let expected = 'Lorem <img src="image.jpg" alt="alt text"> dolor';
  789. let actual = this.md(markdown);
  790. this.assertEqual(actual, expected);
  791. }
  792. test_image_noAlt() {
  793. let markdown = 'Lorem ![](image.jpg) dolor';
  794. let expected = 'Lorem <img src="image.jpg"> dolor';
  795. let actual = this.md(markdown);
  796. this.assertEqual(actual, expected);
  797. }
  798. test_image_withTitle() {
  799. let markdown = 'Lorem ![alt text](image.jpg "image title") dolor';
  800. let expected = 'Lorem <img src="image.jpg" alt="alt text" title="image title"> dolor';
  801. let actual = this.md(markdown);
  802. this.assertEqual(actual, expected);
  803. }
  804. test_image_ref() {
  805. let markdown = 'Lorem ![alt text][ref] dolor\n\n' +
  806. '[ref]: image.jpg "image title"';
  807. let expected = '<p>Lorem <img src="image.jpg" alt="alt text" title="image title"> dolor</p>';
  808. let actual = this.md(markdown);
  809. this.assertEqual(actual, expected);
  810. }
  811. test_htmlTags() {
  812. let markdown = 'Lorem <strong title="value" foo=\'with " quote\' bar="with \' apostrophe" attr=unquoted checked>ipsum</strong> dolor';
  813. let expected = 'Lorem <strong title="value" foo="with &quot; quote" bar="with \' apostrophe" attr="unquoted" checked>ipsum</strong> dolor';
  814. let actual = this.md(markdown);
  815. this.assertEqual(actual, expected);
  816. }
  817. }
  818. class BlockTests extends BaseTest {
  819. /** @type {Markdown} */
  820. parser;
  821. md(markdown) {
  822. return normalizeWhitespace(this.parser.toHTML(markdown));
  823. }
  824. setUp() {
  825. this.parser = Markdown.completeParser;
  826. }
  827. test_paragraphs() {
  828. let markdown = "Lorem ipsum\n\nDolor sit amet";
  829. let expected = "<p>Lorem ipsum</p> <p>Dolor sit amet</p>";
  830. let actual = this.md(markdown);
  831. this.assertEqual(actual, expected);
  832. }
  833. test_paragraph_lineGrouping() {
  834. let markdown = "Lorem ipsum\ndolor sit amet";
  835. let expected = "Lorem ipsum dolor sit amet";
  836. let actual = this.md(markdown);
  837. this.assertEqual(actual, expected);
  838. }
  839. test_header_underlineH1() {
  840. let markdown = "Header 1\n===\n\nLorem ipsum";
  841. let expected = "<h1>Header 1</h1> <p>Lorem ipsum</p>";
  842. let actual = this.md(markdown);
  843. this.assertEqual(actual, expected);
  844. }
  845. test_header_underlineH2() {
  846. let markdown = "Header 2\n---\n\nLorem ipsum";
  847. let expected = "<h2>Header 2</h2> <p>Lorem ipsum</p>";
  848. let actual = this.md(markdown);
  849. this.assertEqual(actual, expected);
  850. }
  851. test_header_hash() {
  852. let markdown = "# Header 1\n## Header 2\n### Header 3\n#### Header 4\n##### Header 5\n###### Header 6\n";
  853. let expected = '<h1>Header 1</h1> <h2>Header 2</h2> <h3>Header 3</h3> <h4>Header 4</h4> <h5>Header 5</h5> <h6>Header 6</h6>';
  854. let actual = this.md(markdown);
  855. this.assertEqual(actual, expected);
  856. }
  857. test_header_hash_trailing() {
  858. let markdown = "# Header 1 #\n## Header 2 ##\n### Header 3 ######";
  859. let expected = '<h1>Header 1</h1> <h2>Header 2</h2> <h3>Header 3</h3>';
  860. let actual = this.md(markdown);
  861. this.assertEqual(actual, expected);
  862. }
  863. test_unorderedList() {
  864. let markdown = "* Lorem\n* Ipsum\n* Dolor";
  865. let expected = '<ul> <li>Lorem</li> <li>Ipsum</li> <li>Dolor</li> </ul>';
  866. let actual = this.md(markdown);
  867. this.assertEqual(actual, expected);
  868. }
  869. test_unorderedList_nested() {
  870. let markdown = "* Lorem\n + Ipsum\n* Dolor";
  871. let expected = '<ul> <li>Lorem<ul> <li>Ipsum</li> </ul> </li> <li>Dolor</li> </ul>';
  872. let actual = this.md(markdown);
  873. this.assertEqual(actual, expected);
  874. }
  875. test_unorderedList_hitch() {
  876. // This incomplete bulleted list locked up the browser at one
  877. // point, not forever but a REALLY long time
  878. this.profile(1.0, () => {
  879. let markdown = "Testing\n\n* ";
  880. let expected = '<p>Testing</p> <ul> <li></li> </ul>';
  881. let actual = this.md(markdown);
  882. this.assertEqual(actual, expected);
  883. });
  884. }
  885. test_orderedList() {
  886. let markdown = "1. Lorem\n1. Ipsum\n5. Dolor";
  887. let expected = '<ol> <li>Lorem</li> <li>Ipsum</li> <li>Dolor</li> </ol>';
  888. let actual = this.md(markdown);
  889. this.assertEqual(actual, expected);
  890. }
  891. test_orderedList_numbering() {
  892. let markdown = "4. Lorem\n1. Ipsum\n9. Dolor";
  893. let expected = '<ol start="4"> <li>Lorem</li> <li>Ipsum</li> <li>Dolor</li> </ol>';
  894. let actual = this.md(markdown);
  895. this.assertEqual(actual, expected);
  896. }
  897. test_orderedList_nested1() {
  898. let markdown = "1. Lorem\n 1. Ipsum\n1. Dolor";
  899. let expected = '<ol> <li>Lorem<ol> <li>Ipsum</li> </ol> </li> <li>Dolor</li> </ol>';
  900. let actual = this.md(markdown);
  901. this.assertEqual(actual, expected);
  902. }
  903. test_orderedList_nested2() {
  904. let markdown = "1. Lorem\n 1. Ipsum\n 1. Dolor\n 1. Sit\n1. Amet";
  905. let expected = '<ol> <li>Lorem<ol> <li>Ipsum<ol> <li>Dolor</li> </ol> </li> <li>Sit</li> </ol> </li> <li>Amet</li> </ol>';
  906. let actual = this.md(markdown);
  907. this.assertEqual(actual, expected);
  908. }
  909. test_blockquote() {
  910. let markdown = '> Lorem ipsum dolor';
  911. let expected = '<blockquote>Lorem ipsum dolor</blockquote>';
  912. let actual = this.md(markdown);
  913. this.assertEqual(actual, expected);
  914. }
  915. test_blockquote_paragraphs() {
  916. let markdown = '> Lorem ipsum dolor\n>\n>Sit amet';
  917. let expected = '<blockquote> <p>Lorem ipsum dolor</p> <p>Sit amet</p> </blockquote>';
  918. let actual = this.md(markdown);
  919. this.assertEqual(actual, expected);
  920. }
  921. test_blockquote_list() {
  922. let markdown = '> 1. Lorem\n> 2. Ipsum';
  923. let expected = '<blockquote> <ol> <li>Lorem</li> <li>Ipsum</li> </ol> </blockquote>';
  924. let actual = this.md(markdown);
  925. this.assertEqual(actual, expected);
  926. }
  927. test_codeBlock_indented() {
  928. let markdown = "Code\n\n function foo() {\n return 'bar';\n }\n\nend";
  929. let expected = "<p>Code</p>\n\n<pre><code>function foo() {\n return 'bar';\n}</code></pre>\n\n<p>end</p>";
  930. let actual = this.parser.toHTML(markdown).trim(); // don't normalize whitespace
  931. this.assertEqual(actual.replace(/ /g, '⎵'), expected.replace(/ /g, '⎵'));
  932. }
  933. test_codeBlock_fenced() {
  934. let markdown = "Code\n\n```\nfunction foo() {\n return 'bar';\n}\n```\n\nend";
  935. let expected = "<p>Code</p>\n\n<pre><code>function foo() {\n return 'bar';\n}</code></pre>\n\n<p>end</p>";
  936. let actual = this.parser.toHTML(markdown).trim(); // don't normalize whitespace
  937. this.assertEqual(actual.replace(/ /g, '⎵'), expected.replace(/ /g, '⎵'));
  938. }
  939. test_horizontalRule() {
  940. let markdown = "Before\n\n---\n\n- - -\n\n***\n\n* * * * * * *\n\nafter";
  941. let expected = "<p>Before</p> <hr> <hr> <hr> <hr> <p>after</p>";
  942. let actual = this.md(markdown);
  943. this.assertEqual(actual, expected);
  944. }
  945. test_table_unfenced() {
  946. let markdown = "Column A | Column B | Column C\n--- | --- | ---\n1 | 2 | 3\n4 | 5 | 6";
  947. let expected = "<table> <thead> <tr> <th>Column A</th> <th>Column B</th> <th>Column C</th> </tr> </thead> <tbody> <tr> <td>1</td> <td>2</td> <td>3</td> </tr> <tr> <td>4</td> <td>5</td> <td>6</td> </tr> </tbody> </table>";
  948. let actual = this.md(markdown);
  949. this.assertEqual(actual, expected);
  950. }
  951. test_table_fenced() {
  952. let markdown = "| Column A | Column B | Column C |\n| --- | --- | --- |\n| 1 | 2 | 3\n4 | 5 | 6 |";
  953. let expected = "<table> <thead> <tr> <th>Column A</th> <th>Column B</th> <th>Column C</th> </tr> </thead> <tbody> <tr> <td>1</td> <td>2</td> <td>3</td> </tr> <tr> <td>4</td> <td>5</td> <td>6</td> </tr> </tbody> </table>";
  954. let actual = this.md(markdown);
  955. this.assertEqual(actual, expected);
  956. }
  957. test_table_alignment() {
  958. let markdown = 'Column A | Column B | Column C\n' +
  959. ':--- | :---: | ---:\n' +
  960. '1 | 2 | 3\n' +
  961. '4 | 5 | 6';
  962. let expected = '<table> ' +
  963. '<thead> ' +
  964. '<tr> ' +
  965. '<th style="text-align: left;">Column A</th> ' +
  966. '<th style="text-align: center;">Column B</th> ' +
  967. '<th style="text-align: right;">Column C</th> ' +
  968. '</tr> ' +
  969. '</thead> ' +
  970. '<tbody> ' +
  971. '<tr> ' +
  972. '<td style="text-align: left;">1</td> ' +
  973. '<td style="text-align: center;">2</td> ' +
  974. '<td style="text-align: right;">3</td> ' +
  975. '</tr> ' +
  976. '<tr> ' +
  977. '<td style="text-align: left;">4</td> ' +
  978. '<td style="text-align: center;">5</td> ' +
  979. '<td style="text-align: right;">6</td> ' +
  980. '</tr> ' +
  981. '</tbody> ' +
  982. '</table>';
  983. let actual = this.md(markdown);
  984. this.assertEqual(actual, expected);
  985. }
  986. test_table_holes() {
  987. let markdown = 'Column A||Column C\n' +
  988. '---|---|---\n' +
  989. '|1|2||\n' +
  990. '|4||6|\n' +
  991. '||8|9|';
  992. let expected = '<table> ' +
  993. '<thead> ' +
  994. '<tr> ' +
  995. '<th>Column A</th> ' +
  996. '<th></th> ' +
  997. '<th>Column C</th> ' +
  998. '</tr> ' +
  999. '</thead> ' +
  1000. '<tbody> ' +
  1001. '<tr> ' +
  1002. '<td>1</td> ' +
  1003. '<td>2</td> ' +
  1004. '<td></td> ' +
  1005. '</tr> ' +
  1006. '<tr> ' +
  1007. '<td>4</td> ' +
  1008. '<td></td> ' +
  1009. '<td>6</td> ' +
  1010. '</tr> ' +
  1011. '<tr> ' +
  1012. '<td></td> ' +
  1013. '<td>8</td> ' +
  1014. '<td>9</td> ' +
  1015. '</tr> ' +
  1016. '</tbody> ' +
  1017. '</table>';
  1018. let actual = this.md(markdown);
  1019. this.assertEqual(actual, expected);
  1020. }
  1021. test_definitionList() {
  1022. let markdown = 'term\n' +
  1023. ': definition\n' +
  1024. 'another' +
  1025. ' term\n' +
  1026. ': def 1\n' +
  1027. ' broken on next line\n' +
  1028. ': def 2';
  1029. let expected = '<dl> ' +
  1030. '<dt>term</dt> ' +
  1031. '<dd>definition</dd> ' +
  1032. '<dt>another term</dt> ' +
  1033. '<dd>def 1 broken on next line</dd> ' +
  1034. '<dd>def 2</dd> ' +
  1035. '</dl>';
  1036. let actual = this.md(markdown);
  1037. this.assertEqual(actual, expected);
  1038. }
  1039. test_footnotes() {
  1040. let markdown = 'Lorem ipsum[^1] dolor[^2] sit[^1] amet\n\n[^1]: A footnote\n[^2]: Another footnote';
  1041. let expected = '<p>Lorem ipsum<sup id="footnoteref_1"><a href="#footnote_1">1</a></sup> ' +
  1042. 'dolor<sup id="footnoteref_2"><a href="#footnote_2">2</a></sup> ' +
  1043. 'sit<sup id="footnoteref_3"><a href="#footnote_1">1</a></sup> amet</p> ' +
  1044. '<div class="footnotes">' +
  1045. '<hr/>' +
  1046. '<ol>' +
  1047. '<li value="1" id="footnote_1">A footnote <a href="#footnoteref_1" class="footnote-backref">↩︎</a> <a href="#footnoteref_3" class="footnote-backref">↩︎</a></li> ' +
  1048. '<li value="2" id="footnote_2">Another footnote <a href="#footnoteref_2" class="footnote-backref">↩︎</a></li> ' +
  1049. '</ol>' +
  1050. '</div>';
  1051. let actual = this.md(markdown);
  1052. this.assertEqual(actual, expected);
  1053. }
  1054. test_abbreviations() {
  1055. let markdown = 'Lorem ipsum HTML dolor HTML sit\n' +
  1056. '\n' +
  1057. '*[HTML]: Hypertext Markup Language';
  1058. let expected = '<p>Lorem ipsum <abbr title="Hypertext Markup Language">HTML</abbr> dolor <abbr title="Hypertext Markup Language">HTML</abbr> sit</p>';
  1059. let actual = this.md(markdown);
  1060. this.assertEqual(actual, expected);
  1061. }
  1062. }
  1063. class CellValueTests extends BaseTest {
  1064. test_fromCellString_blank() {
  1065. var value;
  1066. value = CellValue.fromCellString('');
  1067. this.assertEqual(value.type, CellValue.TYPE_BLANK);
  1068. this.assertEqual(value.formattedValue, '');
  1069. this.assertEqual(value.value, null);
  1070. value = CellValue.fromCellString(' ');
  1071. this.assertEqual(value.type, CellValue.TYPE_BLANK);
  1072. this.assertEqual(value.formattedValue, '');
  1073. this.assertEqual(value.value, null);
  1074. }
  1075. test_fromCellString_number() {
  1076. var value;
  1077. value = CellValue.fromCellString('123');
  1078. this.assertEqual(value.type, CellValue.TYPE_NUMBER);
  1079. this.assertEqual(value.formattedValue, '123');
  1080. this.assertEqual(value.value, 123);
  1081. this.assertEqual(value.decimals, 0);
  1082. value = CellValue.fromCellString('-0');
  1083. this.assertEqual(value.type, CellValue.TYPE_NUMBER);
  1084. this.assertEqual(value.formattedValue, '-0');
  1085. this.assertEqual(value.value, 0);
  1086. this.assertEqual(value.decimals, 0);
  1087. value = CellValue.fromCellString('1,234');
  1088. this.assertEqual(value.type, CellValue.TYPE_NUMBER);
  1089. this.assertEqual(value.formattedValue, '1,234');
  1090. this.assertEqual(value.value, 1234);
  1091. this.assertEqual(value.decimals, 0);
  1092. value = CellValue.fromCellString('-1,234,567.89');
  1093. this.assertEqual(value.type, CellValue.TYPE_NUMBER);
  1094. this.assertEqual(value.formattedValue, '-1,234,567.89');
  1095. this.assertEqual(value.value, -1234567.89);
  1096. this.assertEqual(value.decimals, 2);
  1097. }
  1098. test_fromCellString_percent() {
  1099. var value;
  1100. value = CellValue.fromCellString('123%');
  1101. this.assertEqual(value.type, CellValue.TYPE_PERCENT);
  1102. this.assertEqual(value.formattedValue, '123%');
  1103. this.assertEqual(value.value, 1.23, 0.0001);
  1104. this.assertEqual(value.decimals, 0);
  1105. value = CellValue.fromCellString('-12.3%');
  1106. this.assertEqual(value.type, CellValue.TYPE_PERCENT);
  1107. this.assertEqual(value.formattedValue, '-12.3%');
  1108. this.assertEqual(value.value, -0.123, 0.0001);
  1109. this.assertEqual(value.decimals, 1);
  1110. }
  1111. test_fromCellString_currency() {
  1112. var value;
  1113. value = CellValue.fromCellString('$123');
  1114. this.assertEqual(value.type, CellValue.TYPE_CURRENCY);
  1115. this.assertEqual(value.formattedValue, '$123');
  1116. this.assertEqual(value.value, 123);
  1117. this.assertEqual(value.decimals, 0);
  1118. value = CellValue.fromCellString('-$12.34');
  1119. this.assertEqual(value.type, CellValue.TYPE_CURRENCY);
  1120. this.assertEqual(value.formattedValue, '-$12.34');
  1121. this.assertEqual(value.value, -12.34, 0.0001);
  1122. this.assertEqual(value.decimals, 2);
  1123. }
  1124. test_fromCellString_boolean() {
  1125. var value;
  1126. value = CellValue.fromCellString('true');
  1127. this.assertEqual(value.type, CellValue.TYPE_BOOLEAN);
  1128. this.assertEqual(value.formattedValue, 'TRUE');
  1129. this.assertEqual(value.value, true);
  1130. value = CellValue.fromCellString('false');
  1131. this.assertEqual(value.type, CellValue.TYPE_BOOLEAN);
  1132. this.assertEqual(value.formattedValue, 'FALSE');
  1133. this.assertEqual(value.value, false);
  1134. }
  1135. test_fromCellString_string() {
  1136. var value;
  1137. value = CellValue.fromCellString('some text');
  1138. this.assertEqual(value.type, CellValue.TYPE_STRING);
  1139. this.assertEqual(value.formattedValue, 'some text');
  1140. this.assertEqual(value.value, 'some text');
  1141. value = CellValue.fromCellString("'0123");
  1142. this.assertEqual(value.type, CellValue.TYPE_STRING);
  1143. this.assertEqual(value.formattedValue, '0123');
  1144. this.assertEqual(value.value, '0123');
  1145. value = CellValue.fromCellString("'=123");
  1146. this.assertEqual(value.type, CellValue.TYPE_STRING);
  1147. this.assertEqual(value.formattedValue, '=123');
  1148. this.assertEqual(value.value, '=123');
  1149. }
  1150. test_fromCellString_formula() {
  1151. var value;
  1152. value = CellValue.fromCellString('=A*B');
  1153. this.assertEqual(value.type, CellValue.TYPE_FORMULA);
  1154. this.assertEqual(value.formattedValue, '=A*B');
  1155. this.assertEqual(value.value, '=A*B');
  1156. value = CellValue.fromCellString('=MAX(A, 3)');
  1157. this.assertEqual(value.type, CellValue.TYPE_FORMULA);
  1158. this.assertEqual(value.formattedValue, '=MAX(A, 3)');
  1159. this.assertEqual(value.value, '=MAX(A, 3)');
  1160. }
  1161. test_fromValue_null() {
  1162. var value;
  1163. value = CellValue.fromValue(null);
  1164. this.assertEqual(value.type, CellValue.TYPE_BLANK);
  1165. }
  1166. test_fromValue_number() {
  1167. var value;
  1168. value = CellValue.fromValue(123);
  1169. this.assertEqual(value.type, CellValue.TYPE_NUMBER);
  1170. this.assertEqual(value.formattedValue, '123');
  1171. value = CellValue.fromValue(3.141592);
  1172. this.assertEqual(value.type, CellValue.TYPE_NUMBER);
  1173. this.assertEqual(value.formattedValue, '3.141592');
  1174. value = CellValue.fromValue(123456789);
  1175. this.assertEqual(value.type, CellValue.TYPE_NUMBER);
  1176. this.assertEqual(value.formattedValue, '123,456,789');
  1177. }
  1178. test_fromValue_boolean() {
  1179. var value;
  1180. value = CellValue.fromValue(true);
  1181. this.assertEqual(value.type, CellValue.TYPE_BOOLEAN);
  1182. this.assertEqual(value.formattedValue, 'TRUE');
  1183. value = CellValue.fromValue(false);
  1184. this.assertEqual(value.type, CellValue.TYPE_BOOLEAN);
  1185. this.assertEqual(value.formattedValue, 'FALSE');
  1186. }
  1187. test_fromValue_string() {
  1188. var value;
  1189. value = CellValue.fromValue('foo');
  1190. this.assertEqual(value.type, CellValue.TYPE_STRING);
  1191. this.assertEqual(value.formattedValue, 'foo');
  1192. value = CellValue.fromValue('123');
  1193. this.assertEqual(value.type, CellValue.TYPE_STRING);
  1194. this.assertEqual(value.formattedValue, '123');
  1195. }
  1196. test_fromValue_formula() {
  1197. var value;
  1198. value = CellValue.fromValue('=A*B');
  1199. this.assertEqual(value.type, CellValue.TYPE_FORMULA);
  1200. this.assertEqual(value.formattedValue, '=A*B');
  1201. }
  1202. test_operation_add() {
  1203. var a, b, result, expected;
  1204. a = CellValue.fromValue(3);
  1205. b = CellValue.fromValue(4);
  1206. result = a.add(b)
  1207. expected = new CellValue('7', 7, CellValue.TYPE_NUMBER, 0);
  1208. this.assertEqual(result, expected);
  1209. a = CellValue.fromCellString('100%');
  1210. b = CellValue.fromCellString('50%');
  1211. result = a.add(b);
  1212. expected = new CellValue('150%', 1.5, CellValue.TYPE_PERCENT, 0);
  1213. this.assertEqual(result, expected);
  1214. a = CellValue.fromCellString('$123');
  1215. b = CellValue.fromCellString('$321');
  1216. result = a.add(b);
  1217. expected = new CellValue('$444.00', 444, CellValue.TYPE_CURRENCY, 2);
  1218. this.assertEqual(result, expected);
  1219. }
  1220. test_operation_subtract() {
  1221. var a, b, result, expected;
  1222. a = CellValue.fromValue(9);
  1223. b = CellValue.fromValue(4);
  1224. result = a.subtract(b)
  1225. expected = new CellValue('5', 5, CellValue.TYPE_NUMBER, 0);
  1226. this.assertEqual(result, expected);
  1227. a = CellValue.fromCellString('100%');
  1228. b = CellValue.fromCellString('50%');
  1229. result = a.subtract(b);
  1230. expected = new CellValue('50%', 0.5, CellValue.TYPE_PERCENT, 0);
  1231. this.assertEqual(result, expected);
  1232. a = CellValue.fromCellString('$321');
  1233. b = CellValue.fromCellString('$123');
  1234. result = a.subtract(b);
  1235. expected = new CellValue('$198.00', 198, CellValue.TYPE_CURRENCY, 2);
  1236. this.assertEqual(result, expected);
  1237. }
  1238. test_operation_multiply() {
  1239. var a, b, result, expected;
  1240. a = CellValue.fromValue(3);
  1241. b = CellValue.fromValue(4);
  1242. result = a.multiply(b)
  1243. expected = new CellValue('12', 12, CellValue.TYPE_NUMBER, 0);
  1244. this.assertEqual(result, expected);
  1245. a = CellValue.fromCellString('150%');
  1246. b = CellValue.fromCellString('50%');
  1247. result = a.multiply(b);
  1248. expected = new CellValue('75%', 0.75, CellValue.TYPE_PERCENT, 0);
  1249. this.assertEqual(result, expected);
  1250. a = CellValue.fromCellString('$321');
  1251. b = CellValue.fromCellString('50%');
  1252. result = a.multiply(b);
  1253. expected = new CellValue('$160.50', 160.50, CellValue.TYPE_CURRENCY, 2);
  1254. this.assertEqual(result, expected);
  1255. }
  1256. test_operation_divide() {
  1257. var a, b, result, expected;
  1258. a = CellValue.fromValue(12);
  1259. b = CellValue.fromValue(4);
  1260. result = a.divide(b)
  1261. expected = new CellValue('3', 3, CellValue.TYPE_NUMBER, 0);
  1262. this.assertEqual(result, expected);
  1263. a = CellValue.fromCellString('150%');
  1264. b = CellValue.fromCellString('50%');
  1265. result = a.divide(b);
  1266. expected = new CellValue('300%', 3.0, CellValue.TYPE_PERCENT, 0);
  1267. this.assertEqual(result, expected);
  1268. a = CellValue.fromCellString('$321');
  1269. b = CellValue.fromCellString('200%');
  1270. result = a.divide(b);
  1271. expected = new CellValue('$160.50', 160.50, CellValue.TYPE_CURRENCY, 2);
  1272. this.assertEqual(result, expected);
  1273. }
  1274. test_operation_modulo() {
  1275. var a, b, result, expected;
  1276. a = CellValue.fromValue(7);
  1277. b = CellValue.fromValue(4);
  1278. result = a.modulo(b)
  1279. expected = new CellValue('3', 3, CellValue.TYPE_NUMBER, 0);
  1280. this.assertEqual(result, expected);
  1281. a = CellValue.fromCellString('175%');
  1282. b = CellValue.fromCellString('50%');
  1283. result = a.modulo(b);
  1284. expected = new CellValue('25%', 0.25, CellValue.TYPE_PERCENT, 0);
  1285. this.assertEqual(result, expected);
  1286. a = CellValue.fromCellString('$327');
  1287. b = CellValue.fromCellString('$20');
  1288. result = a.modulo(b);
  1289. expected = new CellValue('$7.00', 7.00, CellValue.TYPE_CURRENCY, 2);
  1290. this.assertEqual(result, expected);
  1291. }
  1292. test_operation_unaryNot() {
  1293. this.assertEqual(CellValue.fromValue(true).not(), CellValue.fromValue(false));
  1294. this.assertEqual(CellValue.fromValue(false).not(), CellValue.fromValue(true));
  1295. }
  1296. test_operation_comparators() {
  1297. const a = CellValue.fromValue(3);
  1298. const b = CellValue.fromValue(4);
  1299. const t = CellValue.fromValue(true);
  1300. const f = CellValue.fromValue(false);
  1301. this.assertEqual(a.lt(b), t);
  1302. this.assertEqual(a.lte(b), t);
  1303. this.assertEqual(a.gt(b), f);
  1304. this.assertEqual(a.gte(b), f);
  1305. this.assertEqual(a.eq(b), f);
  1306. this.assertEqual(a.neq(b), t);
  1307. this.assertEqual(b.lt(a), f);
  1308. this.assertEqual(b.lte(a), f);
  1309. this.assertEqual(b.gt(a), t);
  1310. this.assertEqual(b.gte(a), t);
  1311. this.assertEqual(b.eq(a), f);
  1312. this.assertEqual(b.neq(a), t);
  1313. this.assertEqual(a.lt(a), f);
  1314. this.assertEqual(a.lte(a), t);
  1315. this.assertEqual(a.gt(a), f);
  1316. this.assertEqual(a.gte(a), t);
  1317. this.assertEqual(a.eq(a), t);
  1318. this.assertEqual(a.neq(a), f);
  1319. }
  1320. test_operation_concatenate() {
  1321. const a = CellValue.fromValue('abc');
  1322. const b = CellValue.fromValue('xyz');
  1323. const result = a.concatenate(b);
  1324. const expected = CellValue.fromValue('abcxyz');
  1325. this.assertEqual(result, expected);
  1326. }
  1327. }
  1328. class CellAddressRangeTests extends BaseTest {
  1329. test_iterator() {
  1330. const grid = new SpreadsheetGrid(3, 4);
  1331. let range = new CellAddressRange(new CellAddress(0, 0), new CellAddress(2, 3));
  1332. var visited = [];
  1333. var sanity = 100;
  1334. for (const address of range.cellsIn(grid)) {
  1335. visited.push(address.name);
  1336. if (sanity-- < 0) break;
  1337. }
  1338. const result = visited.join(',');
  1339. const expected = 'A1,A2,A3,A4,B1,B2,B3,B4,C1,C2,C3,C4';
  1340. this.assertEqual(result, expected);
  1341. }
  1342. test_iterator_column() {
  1343. const grid = new SpreadsheetGrid(3, 4);
  1344. let range = new CellAddressRange(new CellAddress(1, -1), new CellAddress(2, -1));
  1345. var visited = [];
  1346. var sanity = 100;
  1347. for (const address of range.cellsIn(grid)) {
  1348. visited.push(address.name);
  1349. if (sanity-- < 0) break;
  1350. }
  1351. const result = visited.join(',');
  1352. const expected = 'B1,B2,B3,B4,C1,C2,C3,C4';
  1353. this.assertEqual(result, expected);
  1354. }
  1355. test_iterator_beyondBounds() {
  1356. const grid = new SpreadsheetGrid(3, 4);
  1357. let range = new CellAddressRange(new CellAddress(0, 1), new CellAddress(9, 9));
  1358. var visited = [];
  1359. var sanity = 100;
  1360. for (const address of range.cellsIn(grid)) {
  1361. visited.push(address.name);
  1362. if (sanity-- < 0) break;
  1363. }
  1364. const result = visited.join(',');
  1365. const expected = 'A2,A3,A4,B2,B3,B4,C2,C3,C4';
  1366. this.assertEqual(result, expected);
  1367. }
  1368. test_iterator_outOfBounds() {
  1369. const grid = new SpreadsheetGrid(3, 4);
  1370. let range = new CellAddressRange(new CellAddress(5, 0), new CellAddress(5, 9));
  1371. var visited = [];
  1372. var sanity = 100;
  1373. for (const address of range.cellsIn(grid)) {
  1374. visited.push(address.name);
  1375. if (sanity-- < 0) break;
  1376. }
  1377. const result = visited.join(',');
  1378. const expected = '';
  1379. this.assertEqual(result, expected);
  1380. }
  1381. }
  1382. class ExpressionSetTests extends BaseTest {
  1383. test_simpleMath() {
  1384. const grid = new SpreadsheetGrid(1, 1);
  1385. grid.cells[0][0].originalValue = CellValue.fromCellString('=7*3');
  1386. const expressionSet = new CellExpressionSet(grid);
  1387. expressionSet.calculateCells();
  1388. const expected = CellValue.fromValue(21);
  1389. this.assertEqual(grid.cells[0][0].outputValue, expected);
  1390. }
  1391. test_reference() {
  1392. const grid = new SpreadsheetGrid(3, 1);
  1393. grid.cells[0][0].originalValue = CellValue.fromValue(123);
  1394. grid.cells[1][0].originalValue = CellValue.fromValue(3);
  1395. grid.cells[2][0].originalValue = CellValue.fromCellString('=A1*B1');
  1396. const expressionSet = new CellExpressionSet(grid);
  1397. expressionSet.calculateCells();
  1398. const expected = CellValue.fromValue(369);
  1399. this.assertEqual(grid.cells[2][0].outputValue, expected);
  1400. }
  1401. test_infixPriority() {
  1402. const grid = new SpreadsheetGrid(1, 1);
  1403. grid.cells[0][0].originalValue = CellValue.fromCellString('=4*9/3+7-4');
  1404. const expressionSet = new CellExpressionSet(grid);
  1405. expressionSet.calculateCells();
  1406. const expected = CellValue.fromValue(15);
  1407. this.assertEqual(grid.cells[0][0].outputValue, expected);
  1408. }
  1409. test_filledFormula() {
  1410. const grid = new SpreadsheetGrid(3, 3);
  1411. grid.cells[0][0].originalValue = CellValue.fromValue(4);
  1412. grid.cells[1][0].originalValue = CellValue.fromValue(5);
  1413. grid.cells[2][0].originalValue = CellValue.fromCellString('=A1*B1 FILL');
  1414. grid.cells[0][1].originalValue = CellValue.fromValue(6);
  1415. grid.cells[1][1].originalValue = CellValue.fromValue(7);
  1416. grid.cells[0][2].originalValue = CellValue.fromValue(8);
  1417. grid.cells[1][2].originalValue = CellValue.fromValue(9);
  1418. const expressionSet = new CellExpressionSet(grid);
  1419. expressionSet.calculateCells();
  1420. this.assertEqual(grid.cells[2][0].outputValue, CellValue.fromValue(20));
  1421. this.assertEqual(grid.cells[2][1].outputValue, CellValue.fromValue(42));
  1422. this.assertEqual(grid.cells[2][2].outputValue, CellValue.fromValue(72));
  1423. }
  1424. test_dependencies() {
  1425. const grid = new SpreadsheetGrid(2, 4);
  1426. grid.cells[0][0].originalValue = CellValue.fromValue(1);
  1427. grid.cells[1][0].originalValue = CellValue.fromCellString('=A1+B2');
  1428. grid.cells[0][1].originalValue = CellValue.fromValue(2);
  1429. grid.cells[1][1].originalValue = CellValue.fromCellString('=A2+B3');
  1430. grid.cells[0][2].originalValue = CellValue.fromValue(3);
  1431. grid.cells[1][2].originalValue = CellValue.fromCellString('=A3+B4');
  1432. grid.cells[0][3].originalValue = CellValue.fromValue(4);
  1433. grid.cells[1][3].originalValue = CellValue.fromCellString('=A4');
  1434. const expressionSet = new CellExpressionSet(grid);
  1435. expressionSet.calculateCells();
  1436. this.assertEqual(grid.cells[1][0].outputValue, CellValue.fromValue(10));
  1437. this.assertEqual(grid.cells[1][1].outputValue, CellValue.fromValue(9));
  1438. this.assertEqual(grid.cells[1][2].outputValue, CellValue.fromValue(7));
  1439. this.assertEqual(grid.cells[1][3].outputValue, CellValue.fromValue(4));
  1440. }
  1441. _test_simple_formula(formula, expected) {
  1442. const grid = new SpreadsheetGrid(1, 1);
  1443. grid.cells[0][0].originalValue = CellValue.fromCellString(formula);
  1444. const expressionSet = new CellExpressionSet(grid);
  1445. expressionSet.calculateCells();
  1446. const result = grid.cells[0][0].outputValue;
  1447. const exp = (expected instanceof CellValue) ? expected : CellValue.fromValue(expected);
  1448. this.assertEqual(result.type, exp.type);
  1449. this.assertEqual(result.decimals, exp.decimals);
  1450. this.assertEqual(result.formattedValue, exp.formattedValue);
  1451. this.assertEqual(result.value, exp.value);
  1452. }
  1453. test_func_abs() {
  1454. this._test_simple_formula('=ABS(-3)', 3);
  1455. this._test_simple_formula('=ABS(4)', 4);
  1456. }
  1457. test_func_and() {
  1458. this._test_simple_formula('=AND(FALSE, FALSE)', false);
  1459. this._test_simple_formula('=AND(FALSE, TRUE)', false);
  1460. this._test_simple_formula('=AND(TRUE, FALSE)', false);
  1461. this._test_simple_formula('=AND(TRUE, TRUE)', true);
  1462. this._test_simple_formula('=AND(TRUE, TRUE, TRUE, TRUE)', true);
  1463. this._test_simple_formula('=AND(TRUE, TRUE, TRUE, FALSE)', false);
  1464. }
  1465. test_func_average() {
  1466. this._test_simple_formula('=AVERAGE(4, 6, 2, 4)', 4);
  1467. this._test_simple_formula('=AVERAGE(4, 6, 2, "foo", 4)', 4);
  1468. }
  1469. test_func_ceiling() {
  1470. this._test_simple_formula('=CEILING(3.1)', 4);
  1471. this._test_simple_formula('=CEILING(3)', 3);
  1472. this._test_simple_formula('=CEILING(-3.1)', -3);
  1473. }
  1474. test_func_exp() {
  1475. this._test_simple_formula('=EXP(1)', 2.718281828459045);
  1476. }
  1477. test_func_floor() {
  1478. this._test_simple_formula('=FLOOR(3.1)', 3);
  1479. this._test_simple_formula('=FLOOR(3)', 3);
  1480. this._test_simple_formula('=FLOOR(-3.1)', -4);
  1481. }
  1482. test_func_if() {
  1483. this._test_simple_formula('=IF(FALSE, 4, 6)', 6);
  1484. this._test_simple_formula('=IF(TRUE, 4, 6)', 4);
  1485. }
  1486. test_func_ifs() {
  1487. this._test_simple_formula('=IFS(TRUE, 1, FALSE, 2, FALSE, 3, FALSE, 4, 5)', 1);
  1488. this._test_simple_formula('=IFS(FALSE, 1, TRUE, 2, FALSE, 3, FALSE, 4, 5)', 2);
  1489. this._test_simple_formula('=IFS(FALSE, 1, FALSE, 2, TRUE, 3, FALSE, 4, 5)', 3);
  1490. this._test_simple_formula('=IFS(FALSE, 1, FALSE, 2, FALSE, 3, TRUE, 4, 5)', 4);
  1491. this._test_simple_formula('=IFS(FALSE, 1, FALSE, 2, FALSE, 3, FALSE, 4, 5)', 5);
  1492. }
  1493. test_func_ln() {
  1494. this._test_simple_formula('=LN(2.718281828459045)', 1);
  1495. }
  1496. test_func_log() {
  1497. this._test_simple_formula('=LOG(1000, 10)', 3);
  1498. this._test_simple_formula('=LOG(64, 2)', 6);
  1499. }
  1500. test_func_lower() {
  1501. this._test_simple_formula('=LOWER("MiXeD")', 'mixed');
  1502. }
  1503. test_func_max() {
  1504. this._test_simple_formula('=MAX(4, 8, 5, 2)', 8);
  1505. }
  1506. test_func_min() {
  1507. this._test_simple_formula('=MIN(4, 8, 5, 2)', 2);
  1508. }
  1509. test_func_mod() {
  1510. this._test_simple_formula('=MOD(37, 4)', 1);
  1511. }
  1512. test_func_not() {
  1513. this._test_simple_formula('=NOT(TRUE)', false);
  1514. this._test_simple_formula('=NOT(FALSE)', true);
  1515. }
  1516. test_func_or() {
  1517. this._test_simple_formula('=OR(FALSE, FALSE)', false);
  1518. this._test_simple_formula('=OR(FALSE, TRUE)', true);
  1519. this._test_simple_formula('=OR(TRUE, FALSE)', true);
  1520. this._test_simple_formula('=OR(TRUE, TRUE)', true);
  1521. this._test_simple_formula('=OR(FALSE, FALSE, FALSE, TRUE)', true);
  1522. }
  1523. test_func_power() {
  1524. this._test_simple_formula('=POWER(2, 3)', 8);
  1525. }
  1526. test_func_round() {
  1527. this._test_simple_formula('=ROUND(3.1)', 3);
  1528. this._test_simple_formula('=ROUND(3.5)', 4);
  1529. this._test_simple_formula('=ROUND(4)', 4);
  1530. this._test_simple_formula('=ROUND(-3.1)', -3);
  1531. this._test_simple_formula('=ROUND(-3.5)', -3);
  1532. this._test_simple_formula('=ROUND(-3.9)', -4);
  1533. this._test_simple_formula('=ROUND(3.1415926535, 1)', 3.1);
  1534. this._test_simple_formula('=ROUND(3.1415926535, 2)', 3.14);
  1535. this._test_simple_formula('=ROUND(31.415926535, -1)', 30);
  1536. }
  1537. test_func_sqrt() {
  1538. this._test_simple_formula('=SQRT(16)', 4);
  1539. }
  1540. test_func_substitute() {
  1541. this._test_simple_formula('=SUBSTITUTE("cat sat on the mat", "at", "ot")', 'cot sot on the mot');
  1542. this._test_simple_formula('=SUBSTITUTE("cAt saT on the mat", "at", "ot")', 'cot sot on the mot');
  1543. this._test_simple_formula('=SUBSTITUTE("c.*t s.*t on the m.*t", ".*t", "ot")', 'cot sot on the mot');
  1544. }
  1545. test_func_sum() {
  1546. this._test_simple_formula('=SUM(1, 2, 3, 4, 5)', 15);
  1547. }
  1548. test_func_upper() {
  1549. this._test_simple_formula('=UPPER("mIxEd")', 'MIXED');
  1550. }
  1551. test_func_xor() {
  1552. this._test_simple_formula('=XOR(FALSE, FALSE)', false);
  1553. this._test_simple_formula('=XOR(FALSE, TRUE)', true);
  1554. this._test_simple_formula('=XOR(TRUE, FALSE)', true);
  1555. this._test_simple_formula('=XOR(TRUE, TRUE)', false);
  1556. this._test_simple_formula('=XOR(FALSE, FALSE, TRUE)', true);
  1557. this._test_simple_formula('=XOR(TRUE, FALSE, TRUE)', false);
  1558. }
  1559. test_format() {
  1560. this._test_simple_formula('=2.718281828459045 ; number 3', new CellValue('2.718', 2.718281828459045, 'number', 3));
  1561. this._test_simple_formula('=2.718281828459045 ; percent 2', new CellValue('271.83%', 2.718281828459045, 'percent', 2));
  1562. this._test_simple_formula('=2.718281828459045 ; currency 2', new CellValue('$2.72', 2.718281828459045, 'currency', 2));
  1563. }
  1564. }
  1565. class SpreadsheetMarkdownIntegrationTests extends BaseTest {
  1566. parser;
  1567. setUp() {
  1568. this.parser = new Markdown([ ...Markdown.allReaders, new MDSpreadsheetReader() ]);
  1569. }
  1570. md(markdown) {
  1571. return normalizeWhitespace(this.parser.toHTML(markdown));
  1572. }
  1573. test_integration() {
  1574. const markdown = '| A | B | C |\n| --- | --- | --- |\n| 3 | 4 | =A*B |';
  1575. const expected = '<table> <thead> ' +
  1576. '<tr> <th>A</th> <th>B</th> <th>C</th> </tr> ' +
  1577. '</thead> <tbody> <tr> ' +
  1578. '<td class="spreadsheet-type-number" data-numeric-value="3" data-string-value="3">3</td> ' +
  1579. '<td class="spreadsheet-type-number" data-numeric-value="4" data-string-value="4">4</td> ' +
  1580. '<td class="calculated spreadsheet-type-number" data-numeric-value="12" data-string-value="12">12</td> ' +
  1581. '</tr> </tbody> </table>';
  1582. const result = this.md(markdown);
  1583. this.assertEqual(result, expected);
  1584. }
  1585. test_bailOut() {
  1586. // If no formulas found table isn't modified
  1587. const markdown = '| A | B | C |\n| --- | --- | --- |\n| 3 | 4 | A*B |';
  1588. const expected = '<table> <thead> ' +
  1589. '<tr> <th>A</th> <th>B</th> <th>C</th> </tr> ' +
  1590. '</thead> <tbody> <tr> ' +
  1591. '<td>3</td> ' +
  1592. '<td>4</td> ' +
  1593. '<td>A*B</td> ' +
  1594. '</tr> </tbody> </table>';
  1595. const result = this.md(markdown);
  1596. this.assertEqual(result, expected);
  1597. }
  1598. }
  1599. </script>
  1600. </head>
  1601. <body>
  1602. <div id="results"></div>
  1603. </body>
  1604. </html>