DocumentHelperTests.js 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. /* eslint-disable
  2. max-len,
  3. no-return-assign,
  4. no-unused-vars,
  5. */
  6. // TODO: This file was created by bulk-decaffeinate.
  7. // Fix any style issues and re-enable lint.
  8. /*
  9. * decaffeinate suggestions:
  10. * DS102: Remove unnecessary code created because of implicit returns
  11. * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
  12. */
  13. const sinon = require('sinon')
  14. const { expect } = require('chai')
  15. const modulePath = '../../../../app/src/Features/Documents/DocumentHelper.js'
  16. const SandboxedModule = require('sandboxed-module')
  17. describe('DocumentHelper', function () {
  18. beforeEach(function () {
  19. return (this.DocumentHelper = SandboxedModule.require(modulePath))
  20. })
  21. describe('getTitleFromTexContent', function () {
  22. it('should return the title', function () {
  23. const document = '\\begin{document}\n\\title{foo}\n\\end{document}'
  24. return expect(
  25. this.DocumentHelper.getTitleFromTexContent(document)
  26. ).to.equal('foo')
  27. })
  28. it('should return the title if surrounded by space', function () {
  29. const document = '\\begin{document}\n \\title{foo} \n\\end{document}'
  30. return expect(
  31. this.DocumentHelper.getTitleFromTexContent(document)
  32. ).to.equal('foo')
  33. })
  34. it('should return null if there is no title', function () {
  35. const document = '\\begin{document}\n\\end{document}'
  36. return expect(
  37. this.DocumentHelper.getTitleFromTexContent(document)
  38. ).to.eql(null)
  39. })
  40. it('should accept an array', function () {
  41. const document = ['\\begin{document}', '\\title{foo}', '\\end{document}']
  42. return expect(
  43. this.DocumentHelper.getTitleFromTexContent(document)
  44. ).to.equal('foo')
  45. })
  46. it('should parse out formatting elements from the title', function () {
  47. const document = '\\title{\\textbf{\\large{Second Year LaTeX Exercise}}}'
  48. return expect(
  49. this.DocumentHelper.getTitleFromTexContent(document)
  50. ).to.equal('Second Year LaTeX Exercise')
  51. })
  52. it('should ignore junk after the title', function () {
  53. const document = '\\title{wombat} potato'
  54. return expect(
  55. this.DocumentHelper.getTitleFromTexContent(document)
  56. ).to.equal('wombat')
  57. })
  58. it('should ignore junk before the title', function () {
  59. const document =
  60. '% this is something that v1 relied on, even though it seems odd \\title{wombat}'
  61. return expect(
  62. this.DocumentHelper.getTitleFromTexContent(document)
  63. ).to.equal('wombat')
  64. })
  65. // NICETOHAVE: Current implementation doesn't do this
  66. // it "should keep content that surrounds formatting elements", ->
  67. // document = "\\title{Second Year \\large{LaTeX} Exercise}"
  68. // expect(@DocumentHelper.getTitleFromTexContent(document)).to.equal "Second Year LaTeX Exercise"
  69. it('should collapse whitespace', function () {
  70. const document = '\\title{Second Year LaTeX Exercise}'
  71. return expect(
  72. this.DocumentHelper.getTitleFromTexContent(document)
  73. ).to.equal('Second Year LaTeX Exercise')
  74. })
  75. })
  76. describe('detex', function () {
  77. // note, there are a number of tests for getTitleFromTexContent that also test cases here
  78. it('leaves a non-TeX string unchanged', function () {
  79. expect(this.DocumentHelper.detex('')).to.equal('')
  80. expect(this.DocumentHelper.detex('a')).to.equal('a')
  81. return expect(this.DocumentHelper.detex('a a')).to.equal('a a')
  82. })
  83. it('collapses spaces', function () {
  84. expect(this.DocumentHelper.detex('a a')).to.equal('a a')
  85. return expect(this.DocumentHelper.detex('a \n a')).to.equal('a \n a')
  86. })
  87. it('replaces named commands', function () {
  88. expect(this.DocumentHelper.detex('\\LaTeX')).to.equal('LaTeX')
  89. expect(this.DocumentHelper.detex('\\TikZ')).to.equal('TikZ')
  90. expect(this.DocumentHelper.detex('\\TeX')).to.equal('TeX')
  91. return expect(this.DocumentHelper.detex('\\BibTeX')).to.equal('BibTeX')
  92. })
  93. it('removes general commands', function () {
  94. expect(this.DocumentHelper.detex('\\foo')).to.equal('')
  95. expect(this.DocumentHelper.detex('\\foo{}')).to.equal('')
  96. expect(this.DocumentHelper.detex('\\foo~Test')).to.equal('Test')
  97. expect(this.DocumentHelper.detex('\\"e')).to.equal('e')
  98. return expect(this.DocumentHelper.detex('\\textit{e}')).to.equal('e')
  99. })
  100. it('leaves basic math', function () {
  101. return expect(this.DocumentHelper.detex('$\\cal{O}(n^2)$')).to.equal(
  102. 'O(n^2)'
  103. )
  104. })
  105. it('removes line spacing commands', function () {
  106. return expect(this.DocumentHelper.detex('a \\\\[1.50cm] b')).to.equal(
  107. 'a b'
  108. )
  109. })
  110. })
  111. describe('contentHasDocumentclass', function () {
  112. it('should return true if the content has a documentclass', function () {
  113. const document = ['% line', '% line', '% line', '\\documentclass']
  114. return expect(
  115. this.DocumentHelper.contentHasDocumentclass(document)
  116. ).to.equal(true)
  117. })
  118. it('should allow whitespace before the documentclass', function () {
  119. const document = ['% line', '% line', '% line', ' \\documentclass']
  120. return expect(
  121. this.DocumentHelper.contentHasDocumentclass(document)
  122. ).to.equal(true)
  123. })
  124. it('should not allow non-whitespace before the documentclass', function () {
  125. const document = [
  126. '% line',
  127. '% line',
  128. '% line',
  129. ' asdf \\documentclass',
  130. ]
  131. return expect(
  132. this.DocumentHelper.contentHasDocumentclass(document)
  133. ).to.equal(false)
  134. })
  135. it('should return false when there is no documentclass', function () {
  136. const document = ['% line', '% line', '% line']
  137. return expect(
  138. this.DocumentHelper.contentHasDocumentclass(document)
  139. ).to.equal(false)
  140. })
  141. })
  142. })