ASpellTests.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. /* eslint-disable
  2. handle-callback-err,
  3. no-undef
  4. */
  5. // TODO: This file was created by bulk-decaffeinate.
  6. // Sanity-check the conversion and remove this comment.
  7. /*
  8. * decaffeinate suggestions:
  9. * DS102: Remove unnecessary code created because of implicit returns
  10. * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
  11. */
  12. const chai = require('chai')
  13. const should = chai.should()
  14. const SandboxedModule = require('sandboxed-module')
  15. const { assert } = require('chai')
  16. describe('ASpell', function() {
  17. beforeEach(function() {
  18. return (this.ASpell = SandboxedModule.require('../../../app/js/ASpell', {
  19. requires: {
  20. 'logger-sharelatex': {
  21. log() {},
  22. info() {},
  23. err() {}
  24. },
  25. 'metrics-sharelatex': {
  26. gauge() {},
  27. inc() {}
  28. }
  29. }
  30. }))
  31. })
  32. describe('a correctly spelled word', function() {
  33. beforeEach(function(done) {
  34. return this.ASpell.checkWords('en', ['word'], (error, result) => {
  35. this.result = result
  36. return done()
  37. })
  38. })
  39. return it('should not correct the word', function() {
  40. return this.result.length.should.equal(0)
  41. })
  42. })
  43. describe('a misspelled word', function() {
  44. beforeEach(function(done) {
  45. return this.ASpell.checkWords('en', ['bussines'], (error, result) => {
  46. this.result = result
  47. return done()
  48. })
  49. })
  50. return it('should correct the word', function() {
  51. this.result.length.should.equal(1)
  52. return this.result[0].suggestions.indexOf('business').should.not.equal(-1)
  53. })
  54. })
  55. describe('multiple words', function() {
  56. beforeEach(function(done) {
  57. return this.ASpell.checkWords(
  58. 'en',
  59. ['bussines', 'word', 'neccesary'],
  60. (error, result) => {
  61. this.result = result
  62. return done()
  63. }
  64. )
  65. })
  66. return it('should correct the incorrect words', function() {
  67. this.result[0].index.should.equal(0)
  68. this.result[0].suggestions.indexOf('business').should.not.equal(-1)
  69. this.result[1].index.should.equal(2)
  70. return this.result[1].suggestions
  71. .indexOf('necessary')
  72. .should.not.equal(-1)
  73. })
  74. })
  75. describe('without a valid language', function() {
  76. beforeEach(function(done) {
  77. return this.ASpell.checkWords('notALang', ['banana'], (error, result) => {
  78. this.error = error
  79. this.result = result
  80. return done()
  81. })
  82. })
  83. return it('should return an error', function() {
  84. return should.exist(this.error)
  85. })
  86. })
  87. describe('when there are no suggestions', function() {
  88. beforeEach(function(done) {
  89. return this.ASpell.checkWords(
  90. 'en',
  91. ['asdkfjalkdjfadhfkajsdhfashdfjhadflkjadhflajsd'],
  92. (error, result) => {
  93. this.error = error
  94. this.result = result
  95. return done()
  96. }
  97. )
  98. })
  99. return it('should return a blank array', function() {
  100. this.result.length.should.equal(1)
  101. return assert.deepEqual(this.result[0].suggestions, [])
  102. })
  103. })
  104. return describe('when the request times out', function() {
  105. beforeEach(function(done) {
  106. const words = __range__(0, 1000, true).map(i => 'abcdefg')
  107. this.ASpell.ASPELL_TIMEOUT = 1
  108. this.start = Date.now()
  109. return this.ASpell.checkWords('en', words, (error, result) => {
  110. this.result = result
  111. return done()
  112. })
  113. })
  114. // Note that this test fails on OS X, due to differing pipe behaviour
  115. // on killing the child process. It can be tested successfully on Travis
  116. // or the CI server.
  117. return it('should return in reasonable time', function() {
  118. const delta = Date.now() - this.start
  119. return delta.should.be.below(this.ASpell.ASPELL_TIMEOUT + 1000)
  120. })
  121. })
  122. })
  123. function __range__(left, right, inclusive) {
  124. let range = []
  125. let ascending = left < right
  126. let end = !inclusive ? right : ascending ? right + 1 : right - 1
  127. for (let i = left; ascending ? i < end : i > end; ascending ? i++ : i--) {
  128. range.push(i)
  129. }
  130. return range
  131. }