DocumentHelper.test.mjs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. import { expect } from 'vitest'
  2. const modulePath = '../../../../app/src/Features/Documents/DocumentHelper.mjs'
  3. describe('DocumentHelper', function () {
  4. beforeEach(async function (ctx) {
  5. ctx.DocumentHelper = (await import(modulePath)).default
  6. })
  7. describe('getTitleFromTexContent', function () {
  8. it('should return the title', function (ctx) {
  9. const document = '\\begin{document}\n\\title{foo}\n\\end{document}'
  10. expect(ctx.DocumentHelper.getTitleFromTexContent(document)).to.equal(
  11. 'foo'
  12. )
  13. })
  14. it('should return the title if surrounded by space', function (ctx) {
  15. const document = '\\begin{document}\n \\title{foo} \n\\end{document}'
  16. expect(ctx.DocumentHelper.getTitleFromTexContent(document)).to.equal(
  17. 'foo'
  18. )
  19. })
  20. it('should return null if there is no title', function (ctx) {
  21. const document = '\\begin{document}\n\\end{document}'
  22. expect(ctx.DocumentHelper.getTitleFromTexContent(document)).to.eql(null)
  23. })
  24. it('should accept an array', function (ctx) {
  25. const document = ['\\begin{document}', '\\title{foo}', '\\end{document}']
  26. expect(ctx.DocumentHelper.getTitleFromTexContent(document)).to.equal(
  27. 'foo'
  28. )
  29. })
  30. it('should parse out formatting elements from the title', function (ctx) {
  31. const document = '\\title{\\textbf{\\large{Second Year LaTeX Exercise}}}'
  32. expect(ctx.DocumentHelper.getTitleFromTexContent(document)).to.equal(
  33. 'Second Year LaTeX Exercise'
  34. )
  35. })
  36. it('should ignore junk after the title', function (ctx) {
  37. const document = '\\title{wombat} potato'
  38. expect(ctx.DocumentHelper.getTitleFromTexContent(document)).to.equal(
  39. 'wombat'
  40. )
  41. })
  42. it('should ignore junk before the title', function (ctx) {
  43. const document =
  44. '% this is something that v1 relied on, even though it seems odd \\title{wombat}'
  45. expect(ctx.DocumentHelper.getTitleFromTexContent(document)).to.equal(
  46. 'wombat'
  47. )
  48. })
  49. // NICETOHAVE: Current implementation doesn't do this
  50. // it "should keep content that surrounds formatting elements", ->
  51. // document = "\\title{Second Year \\large{LaTeX} Exercise}"
  52. // expect(@DocumentHelper.getTitleFromTexContent(document)).to.equal "Second Year LaTeX Exercise"
  53. it('should collapse whitespace', function (ctx) {
  54. const document = '\\title{Second Year LaTeX Exercise}'
  55. expect(ctx.DocumentHelper.getTitleFromTexContent(document)).to.equal(
  56. 'Second Year LaTeX Exercise'
  57. )
  58. })
  59. })
  60. describe('detex', function () {
  61. // note, there are a number of tests for getTitleFromTexContent that also test cases here
  62. it('leaves a non-TeX string unchanged', function (ctx) {
  63. expect(ctx.DocumentHelper.detex('')).to.equal('')
  64. expect(ctx.DocumentHelper.detex('a')).to.equal('a')
  65. expect(ctx.DocumentHelper.detex('a a')).to.equal('a a')
  66. })
  67. it('collapses spaces', function (ctx) {
  68. expect(ctx.DocumentHelper.detex('a a')).to.equal('a a')
  69. expect(ctx.DocumentHelper.detex('a \n a')).to.equal('a \n a')
  70. })
  71. it('replaces named commands', function (ctx) {
  72. expect(ctx.DocumentHelper.detex('\\LaTeX')).to.equal('LaTeX')
  73. expect(ctx.DocumentHelper.detex('\\TikZ')).to.equal('TikZ')
  74. expect(ctx.DocumentHelper.detex('\\TeX')).to.equal('TeX')
  75. expect(ctx.DocumentHelper.detex('\\BibTeX')).to.equal('BibTeX')
  76. })
  77. it('removes general commands', function (ctx) {
  78. expect(ctx.DocumentHelper.detex('\\foo')).to.equal('')
  79. expect(ctx.DocumentHelper.detex('\\foo{}')).to.equal('')
  80. expect(ctx.DocumentHelper.detex('\\foo~Test')).to.equal('Test')
  81. expect(ctx.DocumentHelper.detex('\\"e')).to.equal('e')
  82. expect(ctx.DocumentHelper.detex('\\textit{e}')).to.equal('e')
  83. })
  84. it('leaves basic math', function (ctx) {
  85. expect(ctx.DocumentHelper.detex('$\\cal{O}(n^2)$')).to.equal('O(n^2)')
  86. })
  87. it('removes line spacing commands', function (ctx) {
  88. expect(ctx.DocumentHelper.detex('a \\\\[1.50cm] b')).to.equal('a b')
  89. })
  90. })
  91. describe('contentHasDocumentclass', function () {
  92. it('should return true if the content has a documentclass', function (ctx) {
  93. const document = ['% line', '% line', '% line', '\\documentclass']
  94. expect(ctx.DocumentHelper.contentHasDocumentclass(document)).to.equal(
  95. true
  96. )
  97. })
  98. it('should allow whitespace before the documentclass', function (ctx) {
  99. const document = ['% line', '% line', '% line', ' \\documentclass']
  100. expect(ctx.DocumentHelper.contentHasDocumentclass(document)).to.equal(
  101. true
  102. )
  103. })
  104. it('should not allow non-whitespace before the documentclass', function (ctx) {
  105. const document = [
  106. '% line',
  107. '% line',
  108. '% line',
  109. ' asdf \\documentclass',
  110. ]
  111. expect(ctx.DocumentHelper.contentHasDocumentclass(document)).to.equal(
  112. false
  113. )
  114. })
  115. it('should return false when there is no documentclass', function (ctx) {
  116. const document = ['% line', '% line', '% line']
  117. expect(ctx.DocumentHelper.contentHasDocumentclass(document)).to.equal(
  118. false
  119. )
  120. })
  121. })
  122. })