HighlightedWordManagerTests.coffee 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. define [
  2. "spelling/HighlightedWordManager"
  3. "libs/chai"
  4. ], (HighlightedWordsManager, chai) ->
  5. should = chai.should()
  6. describe "HighlightedWordsManager", ->
  7. beforeEach ->
  8. @currentMarkerId = 0
  9. @ide =
  10. editor:
  11. addMarker: () => ++@currentMarkerId
  12. removeMarker: sinon.stub()
  13. sinon.spy @ide.editor, "addMarker"
  14. @highlightedWordsManager = new HighlightedWordsManager(@ide)
  15. @highlightedWordsManager.doesHighlightExist = (row, column, word) ->
  16. for highlight in @highlights.rows[row]
  17. if highlight.row == row and highlight.column == column and highlight.word == word
  18. return true
  19. return false
  20. describe "addHighlight", ->
  21. beforeEach ->
  22. @highlightedWordsManager.addHighlight
  23. row: 1
  24. column: 5
  25. word: "sharelatex"
  26. it "should add a marker into ace", ->
  27. @ide.editor.addMarker.called.should.equal true
  28. [options, klass] = @ide.editor.addMarker.args[0]
  29. options.should.deep.equal row: 1, column: 5, length: 10
  30. klass.should.equal "sharelatex-spelling-highlight"
  31. it "should record the highlight internally", ->
  32. row = @highlightedWordsManager.highlights.rows[1]
  33. highlight = row[0]
  34. highlight.word.should.equal "sharelatex"
  35. highlight.markerId.should.equal @currentMarkerId
  36. describe "removeHighlight", ->
  37. beforeEach ->
  38. @highlightedWordsManager.addHighlight row: 0, column: 5, word: "sharelatex"
  39. @highlightedWordsManager.addHighlight row: 0, column: 17, word: "sharelatex"
  40. @highlightedWordsManager.addHighlight row: 0, column: 42, word: "sharelatex"
  41. @highlight = @highlightedWordsManager.highlights.rows[0][1]
  42. @highlightedWordsManager.removeHighlight @highlight
  43. it "should remove the marker", ->
  44. @ide.editor.removeMarker.calledWith(@highlight.markerId).should.equal true
  45. it "should remove the highlight internally", ->
  46. @highlightedWordsManager.doesHighlightExist(0, 17, "sharelatex").should.equal false
  47. describe "removeWord", ->
  48. beforeEach ->
  49. @highlightedWordsManager.addHighlight row: 0, column: 5, word: "sharelatex"
  50. @highlightedWordsManager.addHighlight row: 1, column: 5, word: "banana"
  51. @highlightedWordsManager.addHighlight row: 3, column: 5, word: "sharelatex"
  52. @highlightedWordsManager.addHighlight row: 4, column: 5, word: "monkey"
  53. @highlightedWordsManager.removeWord "sharelatex"
  54. it "should remove all instances of the word", ->
  55. @highlightedWordsManager.doesHighlightExist(0, 5, "sharelatex").should.equal false
  56. @highlightedWordsManager.doesHighlightExist(3, 5, "sharelatex").should.equal false
  57. it "should not remove other highlights", ->
  58. @highlightedWordsManager.doesHighlightExist(1, 5, "banana").should.equal true
  59. @highlightedWordsManager.doesHighlightExist(4, 5, "monkey").should.equal true
  60. describe "moveHighlight", ->
  61. beforeEach ->
  62. @highlightedWordsManager.addHighlight row: 0, column: 5, word: "sharelatex"
  63. @highlight = @highlightedWordsManager.highlights.rows[0][0]
  64. @oldMarkerId = @highlight.markerId
  65. @highlightedWordsManager.moveHighlight @highlight, row: 1, column: 17
  66. it "should remove the old marker", ->
  67. @ide.editor.removeMarker.calledWith(@oldMarkerId).should.equal true
  68. it "should insert a new marker", ->
  69. @ide.editor.addMarker.calledWith(
  70. {row: 1, column: 17, length: 10},
  71. "sharelatex-spelling-highlight"
  72. ).should.equal true
  73. it "should move the highlight internally", ->
  74. @highlightedWordsManager.doesHighlightExist(0, 5, "sharelatex").should.equal false
  75. @highlightedWordsManager.doesHighlightExist(1, 17, "sharelatex").should.equal true
  76. describe "clearHighlights", ->
  77. beforeEach ->
  78. @highlightedWordsManager.addHighlight row: 0, column: 5, word: "sharelatex"
  79. @highlightedWordsManager.addHighlight row: 1, column: 5, word: "sharelatex"
  80. @highlightedWordsManager.addHighlight row: 3, column: 5, word: "sharelatex"
  81. @highlightedWordsManager.addHighlight row: 4, column: 5, word: "sharelatex"
  82. describe "with a range", ->
  83. beforeEach ->
  84. @highlightedWordsManager.clearRows 1, 3
  85. it "should clear the given rows", ->
  86. @highlightedWordsManager.doesHighlightExist(1, 5, "sharelatex").should.equal false
  87. @highlightedWordsManager.doesHighlightExist(3, 5, "sharelatex").should.equal false
  88. it "should not clear the other rows", ->
  89. @highlightedWordsManager.doesHighlightExist(0, 5, "sharelatex").should.equal true
  90. @highlightedWordsManager.doesHighlightExist(4, 5, "sharelatex").should.equal true
  91. describe "with no range", ->
  92. beforeEach ->
  93. @highlightedWordsManager.clearRows()
  94. it "should clear all the rows", ->
  95. @highlightedWordsManager.doesHighlightExist(0, 5, "sharelatex").should.equal false
  96. @highlightedWordsManager.doesHighlightExist(1, 5, "sharelatex").should.equal false
  97. @highlightedWordsManager.doesHighlightExist(3, 5, "sharelatex").should.equal false
  98. @highlightedWordsManager.doesHighlightExist(4, 5, "sharelatex").should.equal false
  99. describe "applyChange", ->
  100. beforeEach ->
  101. @highlightedWordsManager.addHighlight row: 0, column: 5, word: "sharelatex"
  102. @highlightedWordsManager.addHighlight row: 0, column: 17, word: "latex"
  103. @highlightedWordsManager.addHighlight row: 0, column: 25, word: "monkey"
  104. @highlightedWordsManager.addHighlight row: 1, column: 5, word: "banana"
  105. describe "inserting text into a single line", ->
  106. beforeEach ->
  107. @highlightedWordsManager.applyChange
  108. action: "insertText"
  109. range:
  110. start:
  111. row: 0
  112. column: 17
  113. end:
  114. row: 0
  115. column: 22
  116. text: "share"
  117. it "should move highlights after the change", ->
  118. @highlightedWordsManager.doesHighlightExist(0, 25, "monkey").should.equal false
  119. @highlightedWordsManager.doesHighlightExist(0, 30, "monkey").should.equal true
  120. it "should remove highlights affected by the change", ->
  121. @highlightedWordsManager.doesHighlightExist(0, 17, "latex").should.equal false
  122. it "should not affect highlights before the change", ->
  123. @highlightedWordsManager.doesHighlightExist(0, 5, "sharelatex").should.equal true
  124. describe "inserting text into multiple lines", ->
  125. beforeEach ->
  126. @highlightedWordsManager.applyChange
  127. action: "insertText"
  128. range:
  129. start:
  130. row: 0
  131. column: 18
  132. end:
  133. row: 1
  134. column: 0
  135. text: "\n"
  136. it "should move highlights after the change onto the new line", ->
  137. @highlightedWordsManager.doesHighlightExist(0, 25, "monkey").should.equal false
  138. @highlightedWordsManager.doesHighlightExist(1, 7, "monkey").should.equal true
  139. it "should remove highlights affected by the change", ->
  140. @highlightedWordsManager.doesHighlightExist(0, 17, "latex").should.equal false
  141. it "should not affect highlights before the change", ->
  142. @highlightedWordsManager.doesHighlightExist(0, 5, "sharelatex").should.equal true
  143. describe "inserting lines", ->
  144. beforeEach ->
  145. @highlightedWordsManager.applyChange
  146. action: "insertLines"
  147. range:
  148. start:
  149. row: 1
  150. column: 0
  151. end:
  152. row: 3
  153. column: 0
  154. lines: ["", ""]
  155. it "should move the highlights after the inserted lines", ->
  156. @highlightedWordsManager.doesHighlightExist(1, 5, "banana").should.equal false
  157. @highlightedWordsManager.doesHighlightExist(3, 5, "banana").should.equal true
  158. it "should not affect highlights before the inserted lines", ->
  159. @highlightedWordsManager.doesHighlightExist(0, 5, "sharelatex").should.equal true
  160. describe "deleting text from a single line", ->
  161. beforeEach ->
  162. @highlightedWordsManager.applyChange
  163. action: "removeText"
  164. range:
  165. start:
  166. row: 0
  167. column: 19
  168. end:
  169. row: 0
  170. column: 20
  171. text: "t"
  172. it "should move highlights after the change", ->
  173. @highlightedWordsManager.doesHighlightExist(0, 25, "monkey").should.equal false
  174. @highlightedWordsManager.doesHighlightExist(0, 24, "monkey").should.equal true
  175. it "should remove highlights affected by the change", ->
  176. @highlightedWordsManager.doesHighlightExist(0, 17, "latex").should.equal false
  177. it "should not affect highlights before the change", ->
  178. @highlightedWordsManager.doesHighlightExist(0, 5, "sharelatex").should.equal true
  179. describe "deleting text from a multiple lines", ->
  180. beforeEach ->
  181. @highlightedWordsManager.applyChange
  182. action: "removeText"
  183. range:
  184. start:
  185. row: 0
  186. column: 27
  187. end:
  188. row: 1
  189. column: 3
  190. it "should move highlights after the change onto the first line", ->
  191. @highlightedWordsManager.doesHighlightExist(1, 5, "banana").should.equal false
  192. @highlightedWordsManager.doesHighlightExist(0, 29, "banana").should.equal true
  193. it "should remove highlights affected by the change", ->
  194. @highlightedWordsManager.doesHighlightExist(0, 25, "monkey").should.equal false
  195. it "should not affect highlights before the change", ->
  196. @highlightedWordsManager.doesHighlightExist(0, 5, "sharelatex").should.equal true
  197. describe "deleting lines", ->
  198. beforeEach ->
  199. @highlightedWordsManager.applyChange
  200. action: "removeLines"
  201. range:
  202. start:
  203. row: 0
  204. column: 0
  205. end:
  206. row: 1
  207. column: 0
  208. lines: [""]
  209. it "should move the highlights after the inserted lines", ->
  210. @highlightedWordsManager.doesHighlightExist(1, 5, "banana").should.equal false
  211. @highlightedWordsManager.doesHighlightExist(0, 5, "banana").should.equal true
  212. it "should remove the highlights in the removed lines", ->
  213. @highlightedWordsManager.doesHighlightExist(0, 5, "sharelatex").should.equal false
  214. describe "findHighlightWithinRange", ->
  215. beforeEach ->
  216. @highlightedWordsManager.addHighlight row: 0, column: 5, word: "sharelatex"
  217. @highlightedWordsManager.addHighlight row: 0, column: 17, word: "latex"
  218. @highlightedWordsManager.addHighlight row: 0, column: 25, word: "monkey"
  219. @highlightedWordsManager.addHighlight row: 1, column: 5, word: "banana"
  220. describe "with range inside word", ->
  221. beforeEach ->
  222. @highlight = @highlightedWordsManager.findHighlightWithinRange
  223. start: row: 0, column: 6
  224. end: row: 0, column: 7
  225. it "should return the highlight", ->
  226. @highlight.row.should.equal 0
  227. @highlight.column.should.equal 5
  228. @highlight.word.should.equal "sharelatex"
  229. describe "with range outside word", ->
  230. beforeEach ->
  231. @highlight = @highlightedWordsManager.findHighlightWithinRange
  232. start: row: 0, column: 3
  233. end: row: 0, column: 4
  234. it "should return null", ->
  235. should.not.exist @highlight
  236. describe "with range equal to word", ->
  237. beforeEach ->
  238. @highlight = @highlightedWordsManager.findHighlightWithinRange
  239. start: row: 0, column: 5
  240. end: row: 0, column: 15
  241. it "should return null", ->
  242. @highlight.row.should.equal 0
  243. @highlight.column.should.equal 5
  244. @highlight.word.should.equal "sharelatex"