DocumentHelperTests.js 5.8 KB

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