2014-01-30 05:37:51 +04:00
|
|
|
{times, random} = require 'underscore-plus'
|
|
|
|
randomWords = require 'random-words'
|
2014-02-19 06:23:14 +04:00
|
|
|
TextBuffer = require 'text-buffer'
|
2014-09-23 01:35:13 +04:00
|
|
|
TextEditor = require '../src/text-editor'
|
2014-01-30 05:37:51 +04:00
|
|
|
|
2014-09-23 01:35:13 +04:00
|
|
|
describe "TextEditor", ->
|
2015-03-20 00:08:36 +03:00
|
|
|
[editor, tokenizedBuffer, buffer, steps] = []
|
2014-01-30 05:37:51 +04:00
|
|
|
|
|
|
|
softWrapColumn = 80
|
|
|
|
|
|
|
|
beforeEach ->
|
|
|
|
atom.config.set('editor.softWrapAtPreferredLineLength', true)
|
|
|
|
atom.config.set('editor.preferredLineLength', softWrapColumn)
|
|
|
|
|
|
|
|
it "properly renders soft-wrapped lines when randomly mutated", ->
|
|
|
|
times 10, (i) ->
|
|
|
|
buffer = new TextBuffer
|
2014-09-23 01:35:13 +04:00
|
|
|
editor = new TextEditor({buffer})
|
2014-01-30 05:37:51 +04:00
|
|
|
editor.setEditorWidthInChars(80)
|
|
|
|
tokenizedBuffer = editor.displayBuffer.tokenizedBuffer
|
|
|
|
steps = []
|
|
|
|
|
|
|
|
times 30, ->
|
|
|
|
randomlyMutateEditor()
|
|
|
|
verifyLines()
|
|
|
|
|
|
|
|
verifyLines = ->
|
|
|
|
{bufferRows, screenLines} = getReferenceScreenLines()
|
|
|
|
for referenceBufferRow, screenRow in bufferRows
|
|
|
|
referenceScreenLine = screenLines[screenRow]
|
|
|
|
actualBufferRow = editor.bufferRowForScreenRow(screenRow)
|
|
|
|
unless actualBufferRow is referenceBufferRow
|
|
|
|
logLines()
|
|
|
|
throw new Error("Invalid buffer row #{actualBufferRow} for screen row #{screenRow}", )
|
|
|
|
|
2014-09-04 00:49:52 +04:00
|
|
|
actualScreenLine = editor.tokenizedLineForScreenRow(screenRow)
|
2014-01-30 05:37:51 +04:00
|
|
|
unless actualScreenLine.text is referenceScreenLine.text
|
|
|
|
logLines()
|
|
|
|
throw new Error("Invalid line text at screen row #{screenRow}")
|
|
|
|
|
|
|
|
logLines = ->
|
|
|
|
console.log "==== screen lines ===="
|
|
|
|
editor.logScreenLines()
|
|
|
|
console.log "==== reference lines ===="
|
|
|
|
{bufferRows, screenLines} = getReferenceScreenLines()
|
|
|
|
for bufferRow, screenRow in bufferRows
|
|
|
|
console.log screenRow, bufferRow, screenLines[screenRow].text
|
2015-03-20 00:08:36 +03:00
|
|
|
console.log "==== steps to reproduce this failure: ==="
|
|
|
|
for step in steps
|
|
|
|
console.log 'editor.' + step[0] + '('+ step[1..].map((a) -> JSON.stringify(a)).join(', ') + ')'
|
2014-01-30 05:37:51 +04:00
|
|
|
|
|
|
|
randomlyMutateEditor = ->
|
|
|
|
if Math.random() < .2
|
2014-09-04 18:42:32 +04:00
|
|
|
softWrapped = not editor.isSoftWrapped()
|
|
|
|
steps.push(['setSoftWrapped', softWrapped])
|
|
|
|
editor.setSoftWrapped(softWrapped)
|
2014-01-30 05:37:51 +04:00
|
|
|
else
|
|
|
|
range = getRandomRange()
|
|
|
|
text = getRandomText()
|
|
|
|
steps.push(['setTextInBufferRange', range, text])
|
|
|
|
editor.setTextInBufferRange(range, text)
|
|
|
|
|
|
|
|
getRandomRange = ->
|
|
|
|
startRow = random(0, buffer.getLastRow())
|
|
|
|
startColumn = random(0, buffer.lineForRow(startRow).length)
|
|
|
|
endRow = random(startRow, buffer.getLastRow())
|
|
|
|
endColumn = random(0, buffer.lineForRow(endRow).length)
|
|
|
|
[[startRow, startColumn], [endRow, endColumn]]
|
|
|
|
|
|
|
|
getRandomText = ->
|
|
|
|
text = []
|
|
|
|
max = buffer.getText().split(/\s/).length * 0.75
|
|
|
|
|
|
|
|
times random(5, max), ->
|
|
|
|
if Math.random() < .1
|
|
|
|
text += '\n'
|
|
|
|
else
|
|
|
|
text += " " if /\w$/.test(text)
|
|
|
|
text += randomWords(exactly: 1)
|
|
|
|
text
|
|
|
|
|
|
|
|
getReferenceScreenLines = ->
|
2015-03-20 00:08:36 +03:00
|
|
|
referenceEditor = new TextEditor({})
|
|
|
|
referenceEditor.setEditorWidthInChars(80)
|
|
|
|
referenceEditor.setText(editor.getText())
|
|
|
|
referenceEditor.setSoftWrapped(editor.isSoftWrapped())
|
|
|
|
screenLines = referenceEditor.tokenizedLinesForScreenRows(0, referenceEditor.getLastScreenRow())
|
|
|
|
bufferRows = referenceEditor.bufferRowsForScreenRows(0, referenceEditor.getLastScreenRow())
|
2014-01-30 05:37:51 +04:00
|
|
|
|
2015-03-20 00:08:36 +03:00
|
|
|
{screenLines, bufferRows}
|