2019-05-29 05:21:06 -04:00
|
|
|
/* eslint-disable
|
|
|
|
max-len,
|
|
|
|
no-return-assign,
|
|
|
|
no-unused-vars,
|
|
|
|
*/
|
|
|
|
// TODO: This file was created by bulk-decaffeinate.
|
|
|
|
// Fix any style issues and re-enable lint.
|
|
|
|
/*
|
|
|
|
* decaffeinate suggestions:
|
|
|
|
* DS102: Remove unnecessary code created because of implicit returns
|
|
|
|
* Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
|
|
|
|
*/
|
|
|
|
const sinon = require('sinon')
|
2021-03-31 08:20:55 -04:00
|
|
|
const { expect } = require('chai')
|
2019-05-29 05:21:06 -04:00
|
|
|
const modulePath = '../../../../app/src/Features/Documents/DocumentHelper.js'
|
|
|
|
const SandboxedModule = require('sandboxed-module')
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
describe('DocumentHelper', function () {
|
|
|
|
beforeEach(function () {
|
2019-05-29 05:21:06 -04:00
|
|
|
return (this.DocumentHelper = SandboxedModule.require(modulePath))
|
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
describe('getTitleFromTexContent', function () {
|
|
|
|
it('should return the title', function () {
|
2019-05-29 05:21:06 -04:00
|
|
|
const document = '\\begin{document}\n\\title{foo}\n\\end{document}'
|
|
|
|
return expect(
|
|
|
|
this.DocumentHelper.getTitleFromTexContent(document)
|
|
|
|
).to.equal('foo')
|
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
it('should return the title if surrounded by space', function () {
|
2019-05-29 05:21:06 -04:00
|
|
|
const document = '\\begin{document}\n \\title{foo} \n\\end{document}'
|
|
|
|
return expect(
|
|
|
|
this.DocumentHelper.getTitleFromTexContent(document)
|
|
|
|
).to.equal('foo')
|
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
it('should return null if there is no title', function () {
|
2019-05-29 05:21:06 -04:00
|
|
|
const document = '\\begin{document}\n\\end{document}'
|
|
|
|
return expect(
|
|
|
|
this.DocumentHelper.getTitleFromTexContent(document)
|
|
|
|
).to.eql(null)
|
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
it('should accept an array', function () {
|
2019-05-29 05:21:06 -04:00
|
|
|
const document = ['\\begin{document}', '\\title{foo}', '\\end{document}']
|
|
|
|
return expect(
|
|
|
|
this.DocumentHelper.getTitleFromTexContent(document)
|
|
|
|
).to.equal('foo')
|
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
it('should parse out formatting elements from the title', function () {
|
2019-05-29 05:21:06 -04:00
|
|
|
const document = '\\title{\\textbf{\\large{Second Year LaTeX Exercise}}}'
|
|
|
|
return expect(
|
|
|
|
this.DocumentHelper.getTitleFromTexContent(document)
|
|
|
|
).to.equal('Second Year LaTeX Exercise')
|
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
it('should ignore junk after the title', function () {
|
2019-05-29 05:21:06 -04:00
|
|
|
const document = '\\title{wombat} potato'
|
|
|
|
return expect(
|
|
|
|
this.DocumentHelper.getTitleFromTexContent(document)
|
|
|
|
).to.equal('wombat')
|
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
it('should ignore junk before the title', function () {
|
2019-05-29 05:21:06 -04:00
|
|
|
const document =
|
|
|
|
'% this is something that v1 relied on, even though it seems odd \\title{wombat}'
|
|
|
|
return expect(
|
|
|
|
this.DocumentHelper.getTitleFromTexContent(document)
|
|
|
|
).to.equal('wombat')
|
|
|
|
})
|
|
|
|
|
|
|
|
// NICETOHAVE: Current implementation doesn't do this
|
|
|
|
// it "should keep content that surrounds formatting elements", ->
|
|
|
|
// document = "\\title{Second Year \\large{LaTeX} Exercise}"
|
|
|
|
// expect(@DocumentHelper.getTitleFromTexContent(document)).to.equal "Second Year LaTeX Exercise"
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
it('should collapse whitespace', function () {
|
2019-05-29 05:21:06 -04:00
|
|
|
const document = '\\title{Second Year LaTeX Exercise}'
|
|
|
|
return expect(
|
|
|
|
this.DocumentHelper.getTitleFromTexContent(document)
|
|
|
|
).to.equal('Second Year LaTeX Exercise')
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
describe('detex', function () {
|
2019-05-29 05:21:06 -04:00
|
|
|
// note, there are a number of tests for getTitleFromTexContent that also test cases here
|
2021-04-14 09:17:21 -04:00
|
|
|
it('leaves a non-TeX string unchanged', function () {
|
2019-05-29 05:21:06 -04:00
|
|
|
expect(this.DocumentHelper.detex('')).to.equal('')
|
|
|
|
expect(this.DocumentHelper.detex('a')).to.equal('a')
|
|
|
|
return expect(this.DocumentHelper.detex('a a')).to.equal('a a')
|
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
it('collapses spaces', function () {
|
2019-05-29 05:21:06 -04:00
|
|
|
expect(this.DocumentHelper.detex('a a')).to.equal('a a')
|
|
|
|
return expect(this.DocumentHelper.detex('a \n a')).to.equal('a \n a')
|
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
it('replaces named commands', function () {
|
2019-05-29 05:21:06 -04:00
|
|
|
expect(this.DocumentHelper.detex('\\LaTeX')).to.equal('LaTeX')
|
|
|
|
expect(this.DocumentHelper.detex('\\TikZ')).to.equal('TikZ')
|
|
|
|
expect(this.DocumentHelper.detex('\\TeX')).to.equal('TeX')
|
|
|
|
return expect(this.DocumentHelper.detex('\\BibTeX')).to.equal('BibTeX')
|
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
it('removes general commands', function () {
|
2019-05-29 05:21:06 -04:00
|
|
|
expect(this.DocumentHelper.detex('\\foo')).to.equal('')
|
|
|
|
expect(this.DocumentHelper.detex('\\foo{}')).to.equal('')
|
|
|
|
expect(this.DocumentHelper.detex('\\foo~Test')).to.equal('Test')
|
|
|
|
expect(this.DocumentHelper.detex('\\"e')).to.equal('e')
|
|
|
|
return expect(this.DocumentHelper.detex('\\textit{e}')).to.equal('e')
|
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
it('leaves basic math', function () {
|
2019-05-29 05:21:06 -04:00
|
|
|
return expect(this.DocumentHelper.detex('$\\cal{O}(n^2)$')).to.equal(
|
|
|
|
'O(n^2)'
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
it('removes line spacing commands', function () {
|
2019-05-29 05:21:06 -04:00
|
|
|
return expect(this.DocumentHelper.detex('a \\\\[1.50cm] b')).to.equal(
|
|
|
|
'a b'
|
|
|
|
)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
describe('contentHasDocumentclass', function () {
|
|
|
|
it('should return true if the content has a documentclass', function () {
|
2019-05-29 05:21:06 -04:00
|
|
|
const document = ['% line', '% line', '% line', '\\documentclass']
|
|
|
|
return expect(
|
|
|
|
this.DocumentHelper.contentHasDocumentclass(document)
|
|
|
|
).to.equal(true)
|
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
it('should allow whitespace before the documentclass', function () {
|
2019-05-29 05:21:06 -04:00
|
|
|
const document = ['% line', '% line', '% line', ' \\documentclass']
|
|
|
|
return expect(
|
|
|
|
this.DocumentHelper.contentHasDocumentclass(document)
|
|
|
|
).to.equal(true)
|
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
it('should not allow non-whitespace before the documentclass', function () {
|
2019-05-29 05:21:06 -04:00
|
|
|
const document = [
|
|
|
|
'% line',
|
|
|
|
'% line',
|
|
|
|
'% line',
|
|
|
|
' asdf \\documentclass'
|
|
|
|
]
|
|
|
|
return expect(
|
|
|
|
this.DocumentHelper.contentHasDocumentclass(document)
|
|
|
|
).to.equal(false)
|
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
it('should return false when there is no documentclass', function () {
|
2019-05-29 05:21:06 -04:00
|
|
|
const document = ['% line', '% line', '% line']
|
|
|
|
return expect(
|
|
|
|
this.DocumentHelper.contentHasDocumentclass(document)
|
|
|
|
).to.equal(false)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|