PHP and Javascript implementations of a simple markdown parser
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

testjs.html 63KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796
  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=\'with " quote\' id="with \' apostrophe" lang=unquoted translate forbidden="true">ipsum</strong> dolor';
  813. let expected = 'Lorem <strong title="with &quot; quote" id="with \' apostrophe" lang="unquoted" translate>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_codeBlock_fenced_language() {
  940. let markdown = "Code\n\n```javascript\nfunction foo() {\n return 'bar';\n}\n```\n\nend";
  941. let expected = "<p>Code</p>\n\n<pre><code class=\"language-javascript\">function foo() {\n return 'bar';\n}</code></pre>\n\n<p>end</p>";
  942. let actual = this.parser.toHTML(markdown).trim(); // don't normalize whitespace
  943. this.assertEqual(actual.replace(/ /g, '⎵'), expected.replace(/ /g, '⎵'));
  944. }
  945. test_horizontalRule() {
  946. let markdown = "Before\n\n---\n\n- - -\n\n***\n\n* * * * * * *\n\nafter";
  947. let expected = "<p>Before</p> <hr> <hr> <hr> <hr> <p>after</p>";
  948. let actual = this.md(markdown);
  949. this.assertEqual(actual, expected);
  950. }
  951. test_table_unfenced() {
  952. let markdown = "Column A | Column B | Column C\n--- | --- | ---\n1 | 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_fenced() {
  958. let markdown = "| Column A | Column B | Column C |\n| --- | --- | --- |\n| 1 | 2 | 3\n4 | 5 | 6 |";
  959. 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>";
  960. let actual = this.md(markdown);
  961. this.assertEqual(actual, expected);
  962. }
  963. test_table_alignment() {
  964. let markdown = 'Column A | Column B | Column C\n' +
  965. ':--- | :---: | ---:\n' +
  966. '1 | 2 | 3\n' +
  967. '4 | 5 | 6';
  968. let expected = '<table> ' +
  969. '<thead> ' +
  970. '<tr> ' +
  971. '<th style="text-align: left;">Column A</th> ' +
  972. '<th style="text-align: center;">Column B</th> ' +
  973. '<th style="text-align: right;">Column C</th> ' +
  974. '</tr> ' +
  975. '</thead> ' +
  976. '<tbody> ' +
  977. '<tr> ' +
  978. '<td style="text-align: left;">1</td> ' +
  979. '<td style="text-align: center;">2</td> ' +
  980. '<td style="text-align: right;">3</td> ' +
  981. '</tr> ' +
  982. '<tr> ' +
  983. '<td style="text-align: left;">4</td> ' +
  984. '<td style="text-align: center;">5</td> ' +
  985. '<td style="text-align: right;">6</td> ' +
  986. '</tr> ' +
  987. '</tbody> ' +
  988. '</table>';
  989. let actual = this.md(markdown);
  990. this.assertEqual(actual, expected);
  991. }
  992. test_table_holes() {
  993. let markdown = 'Column A||Column C\n' +
  994. '---|---|---\n' +
  995. '|1|2||\n' +
  996. '|4||6|\n' +
  997. '||8|9|';
  998. let expected = '<table> ' +
  999. '<thead> ' +
  1000. '<tr> ' +
  1001. '<th>Column A</th> ' +
  1002. '<th></th> ' +
  1003. '<th>Column C</th> ' +
  1004. '</tr> ' +
  1005. '</thead> ' +
  1006. '<tbody> ' +
  1007. '<tr> ' +
  1008. '<td>1</td> ' +
  1009. '<td>2</td> ' +
  1010. '<td></td> ' +
  1011. '</tr> ' +
  1012. '<tr> ' +
  1013. '<td>4</td> ' +
  1014. '<td></td> ' +
  1015. '<td>6</td> ' +
  1016. '</tr> ' +
  1017. '<tr> ' +
  1018. '<td></td> ' +
  1019. '<td>8</td> ' +
  1020. '<td>9</td> ' +
  1021. '</tr> ' +
  1022. '</tbody> ' +
  1023. '</table>';
  1024. let actual = this.md(markdown);
  1025. this.assertEqual(actual, expected);
  1026. }
  1027. test_definitionList() {
  1028. let markdown = 'term\n' +
  1029. ': definition\n' +
  1030. 'another' +
  1031. ' term\n' +
  1032. ': def 1\n' +
  1033. ' broken on next line\n' +
  1034. ': def 2';
  1035. let expected = '<dl> ' +
  1036. '<dt>term</dt> ' +
  1037. '<dd>definition</dd> ' +
  1038. '<dt>another term</dt> ' +
  1039. '<dd>def 1 broken on next line</dd> ' +
  1040. '<dd>def 2</dd> ' +
  1041. '</dl>';
  1042. let actual = this.md(markdown);
  1043. this.assertEqual(actual, expected);
  1044. }
  1045. test_footnotes() {
  1046. let markdown = 'Lorem ipsum[^1] dolor[^2] sit[^1] amet\n\n[^1]: A footnote\n[^2]: Another footnote';
  1047. let expected = '<p>Lorem ipsum<sup id="footnoteref_1"><a href="#footnote_1">1</a></sup> ' +
  1048. 'dolor<sup id="footnoteref_2"><a href="#footnote_2">2</a></sup> ' +
  1049. 'sit<sup id="footnoteref_3"><a href="#footnote_1">1</a></sup> amet</p> ' +
  1050. '<div class="footnotes">' +
  1051. '<hr/>' +
  1052. '<ol>' +
  1053. '<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> ' +
  1054. '<li value="2" id="footnote_2">Another footnote <a href="#footnoteref_2" class="footnote-backref">↩︎</a></li> ' +
  1055. '</ol>' +
  1056. '</div>';
  1057. let actual = this.md(markdown);
  1058. this.assertEqual(actual, expected);
  1059. }
  1060. test_abbreviations() {
  1061. let markdown = 'Lorem ipsum HTML dolor HTML sit\n' +
  1062. '\n' +
  1063. '*[HTML]: Hypertext Markup Language';
  1064. let expected = '<p>Lorem ipsum <abbr title="Hypertext Markup Language">HTML</abbr> dolor <abbr title="Hypertext Markup Language">HTML</abbr> sit</p>';
  1065. let actual = this.md(markdown);
  1066. this.assertEqual(actual, expected);
  1067. }
  1068. }
  1069. class CellValueTests extends BaseTest {
  1070. test_fromCellString_blank() {
  1071. var value;
  1072. value = CellValue.fromCellString('');
  1073. this.assertEqual(value.type, CellValue.TYPE_BLANK);
  1074. this.assertEqual(value.formattedValue, '');
  1075. this.assertEqual(value.value, null);
  1076. value = CellValue.fromCellString(' ');
  1077. this.assertEqual(value.type, CellValue.TYPE_BLANK);
  1078. this.assertEqual(value.formattedValue, '');
  1079. this.assertEqual(value.value, null);
  1080. }
  1081. test_fromCellString_number() {
  1082. var value;
  1083. value = CellValue.fromCellString('123');
  1084. this.assertEqual(value.type, CellValue.TYPE_NUMBER);
  1085. this.assertEqual(value.formattedValue, '123');
  1086. this.assertEqual(value.value, 123);
  1087. this.assertEqual(value.decimals, 0);
  1088. value = CellValue.fromCellString('-0');
  1089. this.assertEqual(value.type, CellValue.TYPE_NUMBER);
  1090. this.assertEqual(value.formattedValue, '-0');
  1091. this.assertEqual(value.value, 0);
  1092. this.assertEqual(value.decimals, 0);
  1093. value = CellValue.fromCellString('1,234');
  1094. this.assertEqual(value.type, CellValue.TYPE_NUMBER);
  1095. this.assertEqual(value.formattedValue, '1,234');
  1096. this.assertEqual(value.value, 1234);
  1097. this.assertEqual(value.decimals, 0);
  1098. value = CellValue.fromCellString('-1,234,567.89');
  1099. this.assertEqual(value.type, CellValue.TYPE_NUMBER);
  1100. this.assertEqual(value.formattedValue, '-1,234,567.89');
  1101. this.assertEqual(value.value, -1234567.89);
  1102. this.assertEqual(value.decimals, 2);
  1103. }
  1104. test_fromCellString_percent() {
  1105. var value;
  1106. value = CellValue.fromCellString('123%');
  1107. this.assertEqual(value.type, CellValue.TYPE_PERCENT);
  1108. this.assertEqual(value.formattedValue, '123%');
  1109. this.assertEqual(value.value, 1.23, 0.0001);
  1110. this.assertEqual(value.decimals, 0);
  1111. value = CellValue.fromCellString('-12.3%');
  1112. this.assertEqual(value.type, CellValue.TYPE_PERCENT);
  1113. this.assertEqual(value.formattedValue, '-12.3%');
  1114. this.assertEqual(value.value, -0.123, 0.0001);
  1115. this.assertEqual(value.decimals, 1);
  1116. }
  1117. test_fromCellString_currency() {
  1118. var value;
  1119. value = CellValue.fromCellString('$123');
  1120. this.assertEqual(value.type, CellValue.TYPE_CURRENCY);
  1121. this.assertEqual(value.formattedValue, '$123');
  1122. this.assertEqual(value.value, 123);
  1123. this.assertEqual(value.decimals, 0);
  1124. value = CellValue.fromCellString('-$12.34');
  1125. this.assertEqual(value.type, CellValue.TYPE_CURRENCY);
  1126. this.assertEqual(value.formattedValue, '-$12.34');
  1127. this.assertEqual(value.value, -12.34, 0.0001);
  1128. this.assertEqual(value.decimals, 2);
  1129. }
  1130. test_fromCellString_boolean() {
  1131. var value;
  1132. value = CellValue.fromCellString('true');
  1133. this.assertEqual(value.type, CellValue.TYPE_BOOLEAN);
  1134. this.assertEqual(value.formattedValue, 'TRUE');
  1135. this.assertEqual(value.value, true);
  1136. value = CellValue.fromCellString('false');
  1137. this.assertEqual(value.type, CellValue.TYPE_BOOLEAN);
  1138. this.assertEqual(value.formattedValue, 'FALSE');
  1139. this.assertEqual(value.value, false);
  1140. }
  1141. test_fromCellString_string() {
  1142. var value;
  1143. value = CellValue.fromCellString('some text');
  1144. this.assertEqual(value.type, CellValue.TYPE_STRING);
  1145. this.assertEqual(value.formattedValue, 'some text');
  1146. this.assertEqual(value.value, 'some text');
  1147. value = CellValue.fromCellString("'0123");
  1148. this.assertEqual(value.type, CellValue.TYPE_STRING);
  1149. this.assertEqual(value.formattedValue, '0123');
  1150. this.assertEqual(value.value, '0123');
  1151. value = CellValue.fromCellString("'=123");
  1152. this.assertEqual(value.type, CellValue.TYPE_STRING);
  1153. this.assertEqual(value.formattedValue, '=123');
  1154. this.assertEqual(value.value, '=123');
  1155. }
  1156. test_fromCellString_formula() {
  1157. var value;
  1158. value = CellValue.fromCellString('=A*B');
  1159. this.assertEqual(value.type, CellValue.TYPE_FORMULA);
  1160. this.assertEqual(value.formattedValue, '=A*B');
  1161. this.assertEqual(value.value, '=A*B');
  1162. value = CellValue.fromCellString('=MAX(A, 3)');
  1163. this.assertEqual(value.type, CellValue.TYPE_FORMULA);
  1164. this.assertEqual(value.formattedValue, '=MAX(A, 3)');
  1165. this.assertEqual(value.value, '=MAX(A, 3)');
  1166. }
  1167. test_fromValue_null() {
  1168. var value;
  1169. value = CellValue.fromValue(null);
  1170. this.assertEqual(value.type, CellValue.TYPE_BLANK);
  1171. }
  1172. test_fromValue_number() {
  1173. var value;
  1174. value = CellValue.fromValue(123);
  1175. this.assertEqual(value.type, CellValue.TYPE_NUMBER);
  1176. this.assertEqual(value.formattedValue, '123');
  1177. value = CellValue.fromValue(3.141592);
  1178. this.assertEqual(value.type, CellValue.TYPE_NUMBER);
  1179. this.assertEqual(value.formattedValue, '3.141592');
  1180. value = CellValue.fromValue(123456789);
  1181. this.assertEqual(value.type, CellValue.TYPE_NUMBER);
  1182. this.assertEqual(value.formattedValue, '123,456,789');
  1183. }
  1184. test_fromValue_boolean() {
  1185. var value;
  1186. value = CellValue.fromValue(true);
  1187. this.assertEqual(value.type, CellValue.TYPE_BOOLEAN);
  1188. this.assertEqual(value.formattedValue, 'TRUE');
  1189. value = CellValue.fromValue(false);
  1190. this.assertEqual(value.type, CellValue.TYPE_BOOLEAN);
  1191. this.assertEqual(value.formattedValue, 'FALSE');
  1192. }
  1193. test_fromValue_string() {
  1194. var value;
  1195. value = CellValue.fromValue('foo');
  1196. this.assertEqual(value.type, CellValue.TYPE_STRING);
  1197. this.assertEqual(value.formattedValue, 'foo');
  1198. value = CellValue.fromValue('123');
  1199. this.assertEqual(value.type, CellValue.TYPE_STRING);
  1200. this.assertEqual(value.formattedValue, '123');
  1201. }
  1202. test_fromValue_formula() {
  1203. var value;
  1204. value = CellValue.fromValue('=A*B');
  1205. this.assertEqual(value.type, CellValue.TYPE_FORMULA);
  1206. this.assertEqual(value.formattedValue, '=A*B');
  1207. }
  1208. test_operation_add() {
  1209. var a, b, result, expected;
  1210. a = CellValue.fromValue(3);
  1211. b = CellValue.fromValue(4);
  1212. result = a.add(b)
  1213. expected = new CellValue('7', 7, CellValue.TYPE_NUMBER, 0);
  1214. this.assertEqual(result, expected);
  1215. a = CellValue.fromCellString('100%');
  1216. b = CellValue.fromCellString('50%');
  1217. result = a.add(b);
  1218. expected = new CellValue('150%', 1.5, CellValue.TYPE_PERCENT, 0);
  1219. this.assertEqual(result, expected);
  1220. a = CellValue.fromCellString('$123');
  1221. b = CellValue.fromCellString('$321');
  1222. result = a.add(b);
  1223. expected = new CellValue('$444.00', 444, CellValue.TYPE_CURRENCY, 2);
  1224. this.assertEqual(result, expected);
  1225. }
  1226. test_operation_subtract() {
  1227. var a, b, result, expected;
  1228. a = CellValue.fromValue(9);
  1229. b = CellValue.fromValue(4);
  1230. result = a.subtract(b)
  1231. expected = new CellValue('5', 5, CellValue.TYPE_NUMBER, 0);
  1232. this.assertEqual(result, expected);
  1233. a = CellValue.fromCellString('100%');
  1234. b = CellValue.fromCellString('50%');
  1235. result = a.subtract(b);
  1236. expected = new CellValue('50%', 0.5, CellValue.TYPE_PERCENT, 0);
  1237. this.assertEqual(result, expected);
  1238. a = CellValue.fromCellString('$321');
  1239. b = CellValue.fromCellString('$123');
  1240. result = a.subtract(b);
  1241. expected = new CellValue('$198.00', 198, CellValue.TYPE_CURRENCY, 2);
  1242. this.assertEqual(result, expected);
  1243. }
  1244. test_operation_multiply() {
  1245. var a, b, result, expected;
  1246. a = CellValue.fromValue(3);
  1247. b = CellValue.fromValue(4);
  1248. result = a.multiply(b)
  1249. expected = new CellValue('12', 12, CellValue.TYPE_NUMBER, 0);
  1250. this.assertEqual(result, expected);
  1251. a = CellValue.fromCellString('150%');
  1252. b = CellValue.fromCellString('50%');
  1253. result = a.multiply(b);
  1254. expected = new CellValue('75%', 0.75, CellValue.TYPE_PERCENT, 0);
  1255. this.assertEqual(result, expected);
  1256. a = CellValue.fromCellString('$321');
  1257. b = CellValue.fromCellString('50%');
  1258. result = a.multiply(b);
  1259. expected = new CellValue('$160.50', 160.50, CellValue.TYPE_CURRENCY, 2);
  1260. this.assertEqual(result, expected);
  1261. }
  1262. test_operation_divide() {
  1263. var a, b, result, expected;
  1264. a = CellValue.fromValue(12);
  1265. b = CellValue.fromValue(4);
  1266. result = a.divide(b)
  1267. expected = new CellValue('3', 3, CellValue.TYPE_NUMBER, 0);
  1268. this.assertEqual(result, expected);
  1269. a = CellValue.fromCellString('150%');
  1270. b = CellValue.fromCellString('50%');
  1271. result = a.divide(b);
  1272. expected = new CellValue('300%', 3.0, CellValue.TYPE_PERCENT, 0);
  1273. this.assertEqual(result, expected);
  1274. a = CellValue.fromCellString('$321');
  1275. b = CellValue.fromCellString('200%');
  1276. result = a.divide(b);
  1277. expected = new CellValue('$160.50', 160.50, CellValue.TYPE_CURRENCY, 2);
  1278. this.assertEqual(result, expected);
  1279. }
  1280. test_operation_modulo() {
  1281. var a, b, result, expected;
  1282. a = CellValue.fromValue(7);
  1283. b = CellValue.fromValue(4);
  1284. result = a.modulo(b)
  1285. expected = new CellValue('3', 3, CellValue.TYPE_NUMBER, 0);
  1286. this.assertEqual(result, expected);
  1287. a = CellValue.fromCellString('175%');
  1288. b = CellValue.fromCellString('50%');
  1289. result = a.modulo(b);
  1290. expected = new CellValue('25%', 0.25, CellValue.TYPE_PERCENT, 0);
  1291. this.assertEqual(result, expected);
  1292. a = CellValue.fromCellString('$327');
  1293. b = CellValue.fromCellString('$20');
  1294. result = a.modulo(b);
  1295. expected = new CellValue('$7.00', 7.00, CellValue.TYPE_CURRENCY, 2);
  1296. this.assertEqual(result, expected);
  1297. }
  1298. test_operation_unaryNot() {
  1299. this.assertEqual(CellValue.fromValue(true).not(), CellValue.fromValue(false));
  1300. this.assertEqual(CellValue.fromValue(false).not(), CellValue.fromValue(true));
  1301. }
  1302. test_operation_comparators() {
  1303. const a = CellValue.fromValue(3);
  1304. const b = CellValue.fromValue(4);
  1305. const t = CellValue.fromValue(true);
  1306. const f = CellValue.fromValue(false);
  1307. this.assertEqual(a.lt(b), t);
  1308. this.assertEqual(a.lte(b), t);
  1309. this.assertEqual(a.gt(b), f);
  1310. this.assertEqual(a.gte(b), f);
  1311. this.assertEqual(a.eq(b), f);
  1312. this.assertEqual(a.neq(b), t);
  1313. this.assertEqual(b.lt(a), f);
  1314. this.assertEqual(b.lte(a), f);
  1315. this.assertEqual(b.gt(a), t);
  1316. this.assertEqual(b.gte(a), t);
  1317. this.assertEqual(b.eq(a), f);
  1318. this.assertEqual(b.neq(a), t);
  1319. this.assertEqual(a.lt(a), f);
  1320. this.assertEqual(a.lte(a), t);
  1321. this.assertEqual(a.gt(a), f);
  1322. this.assertEqual(a.gte(a), t);
  1323. this.assertEqual(a.eq(a), t);
  1324. this.assertEqual(a.neq(a), f);
  1325. }
  1326. test_operation_concatenate() {
  1327. const a = CellValue.fromValue('abc');
  1328. const b = CellValue.fromValue('xyz');
  1329. const result = a.concatenate(b);
  1330. const expected = CellValue.fromValue('abcxyz');
  1331. this.assertEqual(result, expected);
  1332. }
  1333. }
  1334. class CellAddressRangeTests extends BaseTest {
  1335. test_iterator() {
  1336. const grid = new SpreadsheetGrid(3, 4);
  1337. let range = new CellAddressRange(new CellAddress(0, 0), new CellAddress(2, 3));
  1338. var visited = [];
  1339. var sanity = 100;
  1340. for (const address of range.cellsIn(grid)) {
  1341. visited.push(address.name);
  1342. if (sanity-- < 0) break;
  1343. }
  1344. const result = visited.join(',');
  1345. const expected = 'A1,A2,A3,A4,B1,B2,B3,B4,C1,C2,C3,C4';
  1346. this.assertEqual(result, expected);
  1347. }
  1348. test_iterator_column() {
  1349. const grid = new SpreadsheetGrid(3, 4);
  1350. let range = new CellAddressRange(new CellAddress(1, -1), new CellAddress(2, -1));
  1351. var visited = [];
  1352. var sanity = 100;
  1353. for (const address of range.cellsIn(grid)) {
  1354. visited.push(address.name);
  1355. if (sanity-- < 0) break;
  1356. }
  1357. const result = visited.join(',');
  1358. const expected = 'B1,B2,B3,B4,C1,C2,C3,C4';
  1359. this.assertEqual(result, expected);
  1360. }
  1361. test_iterator_beyondBounds() {
  1362. const grid = new SpreadsheetGrid(3, 4);
  1363. let range = new CellAddressRange(new CellAddress(0, 1), new CellAddress(9, 9));
  1364. var visited = [];
  1365. var sanity = 100;
  1366. for (const address of range.cellsIn(grid)) {
  1367. visited.push(address.name);
  1368. if (sanity-- < 0) break;
  1369. }
  1370. const result = visited.join(',');
  1371. const expected = 'A2,A3,A4,B2,B3,B4,C2,C3,C4';
  1372. this.assertEqual(result, expected);
  1373. }
  1374. test_iterator_outOfBounds() {
  1375. const grid = new SpreadsheetGrid(3, 4);
  1376. let range = new CellAddressRange(new CellAddress(5, 0), new CellAddress(5, 9));
  1377. var visited = [];
  1378. var sanity = 100;
  1379. for (const address of range.cellsIn(grid)) {
  1380. visited.push(address.name);
  1381. if (sanity-- < 0) break;
  1382. }
  1383. const result = visited.join(',');
  1384. const expected = '';
  1385. this.assertEqual(result, expected);
  1386. }
  1387. }
  1388. class ExpressionSetTests extends BaseTest {
  1389. test_simpleMath() {
  1390. const grid = new SpreadsheetGrid(1, 1);
  1391. grid.cells[0][0].originalValue = CellValue.fromCellString('=7*3');
  1392. const expressionSet = new CellExpressionSet(grid);
  1393. expressionSet.calculateCells();
  1394. const expected = CellValue.fromValue(21);
  1395. this.assertEqual(grid.cells[0][0].outputValue, expected);
  1396. }
  1397. test_reference() {
  1398. const grid = new SpreadsheetGrid(3, 1);
  1399. grid.cells[0][0].originalValue = CellValue.fromValue(123);
  1400. grid.cells[1][0].originalValue = CellValue.fromValue(3);
  1401. grid.cells[2][0].originalValue = CellValue.fromCellString('=A1*B1');
  1402. const expressionSet = new CellExpressionSet(grid);
  1403. expressionSet.calculateCells();
  1404. const expected = CellValue.fromValue(369);
  1405. this.assertEqual(grid.cells[2][0].outputValue, expected);
  1406. }
  1407. test_infixPriority() {
  1408. const grid = new SpreadsheetGrid(1, 1);
  1409. grid.cells[0][0].originalValue = CellValue.fromCellString('=4*9/3+7-4');
  1410. const expressionSet = new CellExpressionSet(grid);
  1411. expressionSet.calculateCells();
  1412. const expected = CellValue.fromValue(15);
  1413. this.assertEqual(grid.cells[0][0].outputValue, expected);
  1414. }
  1415. test_filledFormula() {
  1416. const grid = new SpreadsheetGrid(3, 3);
  1417. grid.cells[0][0].originalValue = CellValue.fromValue(4);
  1418. grid.cells[1][0].originalValue = CellValue.fromValue(5);
  1419. grid.cells[2][0].originalValue = CellValue.fromCellString('=A1*B1 FILL');
  1420. grid.cells[0][1].originalValue = CellValue.fromValue(6);
  1421. grid.cells[1][1].originalValue = CellValue.fromValue(7);
  1422. grid.cells[0][2].originalValue = CellValue.fromValue(8);
  1423. grid.cells[1][2].originalValue = CellValue.fromValue(9);
  1424. const expressionSet = new CellExpressionSet(grid);
  1425. expressionSet.calculateCells();
  1426. this.assertEqual(grid.cells[2][0].outputValue, CellValue.fromValue(20));
  1427. this.assertEqual(grid.cells[2][1].outputValue, CellValue.fromValue(42));
  1428. this.assertEqual(grid.cells[2][2].outputValue, CellValue.fromValue(72));
  1429. }
  1430. test_dependencies() {
  1431. const grid = new SpreadsheetGrid(2, 4);
  1432. grid.cells[0][0].originalValue = CellValue.fromValue(1);
  1433. grid.cells[1][0].originalValue = CellValue.fromCellString('=A1+B2');
  1434. grid.cells[0][1].originalValue = CellValue.fromValue(2);
  1435. grid.cells[1][1].originalValue = CellValue.fromCellString('=A2+B3');
  1436. grid.cells[0][2].originalValue = CellValue.fromValue(3);
  1437. grid.cells[1][2].originalValue = CellValue.fromCellString('=A3+B4');
  1438. grid.cells[0][3].originalValue = CellValue.fromValue(4);
  1439. grid.cells[1][3].originalValue = CellValue.fromCellString('=A4');
  1440. const expressionSet = new CellExpressionSet(grid);
  1441. expressionSet.calculateCells();
  1442. this.assertEqual(grid.cells[1][0].outputValue, CellValue.fromValue(10));
  1443. this.assertEqual(grid.cells[1][1].outputValue, CellValue.fromValue(9));
  1444. this.assertEqual(grid.cells[1][2].outputValue, CellValue.fromValue(7));
  1445. this.assertEqual(grid.cells[1][3].outputValue, CellValue.fromValue(4));
  1446. }
  1447. _test_simple_formula(formula, expected) {
  1448. const grid = new SpreadsheetGrid(1, 1);
  1449. grid.cells[0][0].originalValue = CellValue.fromCellString(formula);
  1450. const expressionSet = new CellExpressionSet(grid);
  1451. expressionSet.calculateCells();
  1452. const result = grid.cells[0][0].outputValue;
  1453. const exp = (expected instanceof CellValue) ? expected : CellValue.fromValue(expected);
  1454. this.assertEqual(result.type, exp.type);
  1455. this.assertEqual(result.decimals, exp.decimals);
  1456. this.assertEqual(result.formattedValue, exp.formattedValue);
  1457. this.assertEqual(result.value, exp.value);
  1458. }
  1459. test_func_abs() {
  1460. this._test_simple_formula('=ABS(-3)', 3);
  1461. this._test_simple_formula('=ABS(4)', 4);
  1462. }
  1463. test_func_and() {
  1464. this._test_simple_formula('=AND(FALSE, FALSE)', false);
  1465. this._test_simple_formula('=AND(FALSE, TRUE)', false);
  1466. this._test_simple_formula('=AND(TRUE, FALSE)', false);
  1467. this._test_simple_formula('=AND(TRUE, TRUE)', true);
  1468. this._test_simple_formula('=AND(TRUE, TRUE, TRUE, TRUE)', true);
  1469. this._test_simple_formula('=AND(TRUE, TRUE, TRUE, FALSE)', false);
  1470. }
  1471. test_func_average() {
  1472. this._test_simple_formula('=AVERAGE(4, 6, 2, 4)', 4);
  1473. this._test_simple_formula('=AVERAGE(4, 6, 2, "foo", 4)', 4);
  1474. }
  1475. test_func_ceiling() {
  1476. this._test_simple_formula('=CEILING(3.1)', 4);
  1477. this._test_simple_formula('=CEILING(3)', 3);
  1478. this._test_simple_formula('=CEILING(-3.1)', -3);
  1479. }
  1480. test_func_exp() {
  1481. this._test_simple_formula('=EXP(1)', 2.718281828459045);
  1482. }
  1483. test_func_floor() {
  1484. this._test_simple_formula('=FLOOR(3.1)', 3);
  1485. this._test_simple_formula('=FLOOR(3)', 3);
  1486. this._test_simple_formula('=FLOOR(-3.1)', -4);
  1487. }
  1488. test_func_if() {
  1489. this._test_simple_formula('=IF(FALSE, 4, 6)', 6);
  1490. this._test_simple_formula('=IF(TRUE, 4, 6)', 4);
  1491. }
  1492. test_func_ifs() {
  1493. this._test_simple_formula('=IFS(TRUE, 1, FALSE, 2, FALSE, 3, FALSE, 4, 5)', 1);
  1494. this._test_simple_formula('=IFS(FALSE, 1, TRUE, 2, FALSE, 3, FALSE, 4, 5)', 2);
  1495. this._test_simple_formula('=IFS(FALSE, 1, FALSE, 2, TRUE, 3, FALSE, 4, 5)', 3);
  1496. this._test_simple_formula('=IFS(FALSE, 1, FALSE, 2, FALSE, 3, TRUE, 4, 5)', 4);
  1497. this._test_simple_formula('=IFS(FALSE, 1, FALSE, 2, FALSE, 3, FALSE, 4, 5)', 5);
  1498. }
  1499. test_func_ln() {
  1500. this._test_simple_formula('=LN(2.718281828459045)', 1);
  1501. }
  1502. test_func_log() {
  1503. this._test_simple_formula('=LOG(1000, 10)', 3);
  1504. this._test_simple_formula('=LOG(64, 2)', 6);
  1505. }
  1506. test_func_lower() {
  1507. this._test_simple_formula('=LOWER("MiXeD")', 'mixed');
  1508. }
  1509. test_func_max() {
  1510. this._test_simple_formula('=MAX(4, 8, 5, 2)', 8);
  1511. }
  1512. test_func_min() {
  1513. this._test_simple_formula('=MIN(4, 8, 5, 2)', 2);
  1514. }
  1515. test_func_mod() {
  1516. this._test_simple_formula('=MOD(37, 4)', 1);
  1517. }
  1518. test_func_not() {
  1519. this._test_simple_formula('=NOT(TRUE)', false);
  1520. this._test_simple_formula('=NOT(FALSE)', true);
  1521. }
  1522. test_func_or() {
  1523. this._test_simple_formula('=OR(FALSE, FALSE)', false);
  1524. this._test_simple_formula('=OR(FALSE, TRUE)', true);
  1525. this._test_simple_formula('=OR(TRUE, FALSE)', true);
  1526. this._test_simple_formula('=OR(TRUE, TRUE)', true);
  1527. this._test_simple_formula('=OR(FALSE, FALSE, FALSE, TRUE)', true);
  1528. }
  1529. test_func_power() {
  1530. this._test_simple_formula('=POWER(2, 3)', 8);
  1531. }
  1532. test_func_round() {
  1533. this._test_simple_formula('=ROUND(3.1)', 3);
  1534. this._test_simple_formula('=ROUND(3.5)', 4);
  1535. this._test_simple_formula('=ROUND(4)', 4);
  1536. this._test_simple_formula('=ROUND(-3.1)', -3);
  1537. this._test_simple_formula('=ROUND(-3.5)', -3);
  1538. this._test_simple_formula('=ROUND(-3.9)', -4);
  1539. this._test_simple_formula('=ROUND(3.1415926535, 1)', 3.1);
  1540. this._test_simple_formula('=ROUND(3.1415926535, 2)', 3.14);
  1541. this._test_simple_formula('=ROUND(31.415926535, -1)', 30);
  1542. }
  1543. test_func_sqrt() {
  1544. this._test_simple_formula('=SQRT(16)', 4);
  1545. }
  1546. test_func_substitute() {
  1547. this._test_simple_formula('=SUBSTITUTE("cat sat on the mat", "at", "ot")', 'cot sot on the mot');
  1548. this._test_simple_formula('=SUBSTITUTE("cAt saT on the mat", "at", "ot")', 'cot sot on the mot');
  1549. this._test_simple_formula('=SUBSTITUTE("c.*t s.*t on the m.*t", ".*t", "ot")', 'cot sot on the mot');
  1550. }
  1551. test_func_sum() {
  1552. this._test_simple_formula('=SUM(1, 2, 3, 4, 5)', 15);
  1553. }
  1554. test_func_upper() {
  1555. this._test_simple_formula('=UPPER("mIxEd")', 'MIXED');
  1556. }
  1557. test_func_xor() {
  1558. this._test_simple_formula('=XOR(FALSE, FALSE)', false);
  1559. this._test_simple_formula('=XOR(FALSE, TRUE)', true);
  1560. this._test_simple_formula('=XOR(TRUE, FALSE)', true);
  1561. this._test_simple_formula('=XOR(TRUE, TRUE)', false);
  1562. this._test_simple_formula('=XOR(FALSE, FALSE, TRUE)', true);
  1563. this._test_simple_formula('=XOR(TRUE, FALSE, TRUE)', false);
  1564. }
  1565. test_format() {
  1566. this._test_simple_formula('=2.718281828459045 ; number 3', new CellValue('2.718', 2.718281828459045, 'number', 3));
  1567. this._test_simple_formula('=2.718281828459045 ; percent 2', new CellValue('271.83%', 2.718281828459045, 'percent', 2));
  1568. this._test_simple_formula('=2.718281828459045 ; currency 2', new CellValue('$2.72', 2.718281828459045, 'currency', 2));
  1569. }
  1570. }
  1571. class SpreadsheetMarkdownIntegrationTests extends BaseTest {
  1572. parser;
  1573. setUp() {
  1574. this.parser = new Markdown([ ...Markdown.allReaders, new MDSpreadsheetReader() ]);
  1575. }
  1576. md(markdown) {
  1577. return normalizeWhitespace(this.parser.toHTML(markdown));
  1578. }
  1579. test_integration() {
  1580. const markdown = '| A | B | C |\n| --- | --- | --- |\n| 3 | 4 | =A*B |';
  1581. const expected = '<table> <thead> ' +
  1582. '<tr> <th>A</th> <th>B</th> <th>C</th> </tr> ' +
  1583. '</thead> <tbody> <tr> ' +
  1584. '<td class="spreadsheet-type-number" data-numeric-value="3" data-string-value="3">3</td> ' +
  1585. '<td class="spreadsheet-type-number" data-numeric-value="4" data-string-value="4">4</td> ' +
  1586. '<td class="calculated spreadsheet-type-number" data-numeric-value="12" data-string-value="12">12</td> ' +
  1587. '</tr> </tbody> </table>';
  1588. const result = this.md(markdown);
  1589. this.assertEqual(result, expected);
  1590. }
  1591. test_bailOut() {
  1592. // If no formulas found table isn't modified
  1593. const markdown = '| A | B | C |\n| --- | --- | --- |\n| 3 | 4 | A*B |';
  1594. const expected = '<table> <thead> ' +
  1595. '<tr> <th>A</th> <th>B</th> <th>C</th> </tr> ' +
  1596. '</thead> <tbody> <tr> ' +
  1597. '<td>3</td> ' +
  1598. '<td>4</td> ' +
  1599. '<td>A*B</td> ' +
  1600. '</tr> </tbody> </table>';
  1601. const result = this.md(markdown);
  1602. this.assertEqual(result, expected);
  1603. }
  1604. test_observedError1() {
  1605. // Saw "Uncaught Error: columnIndex must be number, got string" from this
  1606. const markdown = '| Unit Price | Qty | Subtotal |\n| ---: | ---: | ---: |\n| $1.23 | 2 | =A*B FILL |\n| $4.99 | 6 | |\n| Total | | =SUM(C:C) |';
  1607. const expected = '<table> <thead> <tr> ' +
  1608. '<th style="text-align: right;">Unit Price</th> ' +
  1609. '<th style="text-align: right;">Qty</th> ' +
  1610. '<th>Subtotal</th> ' +
  1611. '</tr> </thead> <tbody> <tr> ' +
  1612. '<td class="spreadsheet-type-currency" style="text-align: right;" data-numeric-value="1.23" data-string-value="1.23">$1.23</td> ' +
  1613. '<td class="spreadsheet-type-number" style="text-align: right;" data-numeric-value="2" data-string-value="2">2</td> ' +
  1614. '<td class="calculated spreadsheet-type-currency" data-numeric-value="2.46" data-string-value="2.46">$2.46</td> ' +
  1615. '</tr> <tr> ' +
  1616. '<td class="spreadsheet-type-currency" style="text-align: right;" data-numeric-value="4.99" data-string-value="4.99">$4.99</td> ' +
  1617. '<td class="spreadsheet-type-number" style="text-align: right;" data-numeric-value="6" data-string-value="6">6</td> ' +
  1618. '<td class="calculated spreadsheet-type-currency" data-numeric-value="29.94" data-string-value="29.94">$29.94</td> ' +
  1619. '</tr> <tr> ' +
  1620. '<td class="spreadsheet-type-string" style="text-align: right;" data-string-value="Total">Total</td> ' +
  1621. '<td class="spreadsheet-type-blank" style="text-align: right;" data-numeric-value="0" data-string-value=""></td> ' +
  1622. '<td class="calculated spreadsheet-type-currency" data-numeric-value="32.4" data-string-value="32.4">$32.40</td> ' +
  1623. '</tr> </tbody> </table>';
  1624. const result = this.md(markdown);
  1625. this.assertEqual(result, expected);
  1626. }
  1627. }
  1628. </script>
  1629. </head>
  1630. <body>
  1631. <div id="results"></div>
  1632. </body>
  1633. </html>