2012-01-05 23:13:55 +04:00
|
|
|
Buffer = require 'buffer'
|
2011-12-16 02:13:34 +04:00
|
|
|
Editor = require 'editor'
|
|
|
|
$ = require 'jquery'
|
|
|
|
ck = require 'coffeekup'
|
2011-12-17 02:42:38 +04:00
|
|
|
fs = require 'fs'
|
2011-12-16 02:13:34 +04:00
|
|
|
|
|
|
|
describe "Editor", ->
|
2011-12-17 04:25:33 +04:00
|
|
|
mainDiv = null
|
|
|
|
editor = null
|
|
|
|
filePath = null
|
|
|
|
tempFilePath = null
|
2011-12-16 02:13:34 +04:00
|
|
|
|
|
|
|
beforeEach ->
|
|
|
|
filePath = require.resolve 'fixtures/sample.txt'
|
2011-12-17 02:42:38 +04:00
|
|
|
tempFilePath = '/tmp/temp.txt'
|
2011-12-29 23:12:13 +04:00
|
|
|
editor = Editor.build()
|
2011-12-16 02:13:34 +04:00
|
|
|
|
|
|
|
afterEach ->
|
2011-12-17 02:42:38 +04:00
|
|
|
fs.remove tempFilePath
|
2011-12-16 02:13:34 +04:00
|
|
|
editor.destroy()
|
|
|
|
|
2011-12-29 23:12:13 +04:00
|
|
|
describe "initialize", ->
|
2011-12-30 00:23:04 +04:00
|
|
|
it "has a buffer", ->
|
|
|
|
expect(editor.buffer).toBeDefined()
|
2011-12-16 02:13:34 +04:00
|
|
|
|
2012-01-14 12:36:15 +04:00
|
|
|
describe '.set/getPosition', ->
|
2012-01-14 07:18:04 +04:00
|
|
|
it "gets the cursor location / moves the cursor location", ->
|
2012-01-12 03:00:40 +04:00
|
|
|
editor.buffer.setText("012345")
|
2012-01-14 12:36:15 +04:00
|
|
|
expect(editor.getPosition()).toEqual {column: 6, row: 0}
|
|
|
|
editor.setPosition(column: 2, row: 0)
|
|
|
|
expect(editor.getPosition()).toEqual {column: 2, row: 0}
|
2012-01-12 03:00:40 +04:00
|
|
|
|
2011-12-16 02:13:34 +04:00
|
|
|
describe 'destroy', ->
|
2011-12-29 23:12:13 +04:00
|
|
|
it 'destroys the ace editor', ->
|
2011-12-17 04:25:33 +04:00
|
|
|
spyOn(editor.aceEditor, 'destroy').andCallThrough()
|
2011-12-16 02:13:34 +04:00
|
|
|
editor.destroy()
|
|
|
|
expect(editor.aceEditor.destroy).toHaveBeenCalled()
|
|
|
|
|
2012-01-05 23:13:55 +04:00
|
|
|
describe "setBuffer(buffer)", ->
|
|
|
|
it "sets the document on the aceSession", ->
|
|
|
|
buffer = new Buffer filePath
|
|
|
|
editor.setBuffer buffer
|
|
|
|
|
|
|
|
fileContents = fs.read(filePath)
|
|
|
|
expect(editor.getAceSession().getValue()).toBe fileContents
|
|
|
|
|
2012-01-06 04:33:53 +04:00
|
|
|
it "restores the ace edit session for a previously assigned buffer", ->
|
|
|
|
buffer = new Buffer filePath
|
|
|
|
editor.setBuffer buffer
|
|
|
|
|
|
|
|
aceSession = editor.getAceSession()
|
|
|
|
|
|
|
|
editor.setBuffer new Buffer(tempFilePath)
|
|
|
|
expect(editor.getAceSession()).not.toBe(aceSession)
|
|
|
|
|
|
|
|
editor.setBuffer(buffer)
|
|
|
|
expect(editor.getAceSession()).toBe aceSession
|
|
|
|
|
2012-01-05 23:13:55 +04:00
|
|
|
it "sets the language mode based on the file extension", ->
|
|
|
|
buffer = new Buffer "something.js"
|
|
|
|
editor.setBuffer buffer
|
|
|
|
|
|
|
|
expect(editor.getAceSession().getMode().name).toBe 'javascript'
|
|
|
|
|
2011-12-16 02:13:34 +04:00
|
|
|
describe "when the text is changed via the ace editor", ->
|
|
|
|
it "updates the buffer text", ->
|
2012-01-05 23:26:14 +04:00
|
|
|
buffer = new Buffer(filePath)
|
|
|
|
editor.setBuffer(buffer)
|
|
|
|
expect(buffer.getText()).not.toMatch /^.ooo/
|
2011-12-23 20:48:11 +04:00
|
|
|
editor.getAceSession().insert {row: 0, column: 1}, 'ooo'
|
2012-01-05 23:26:14 +04:00
|
|
|
expect(buffer.getText()).toMatch /^.ooo/
|
2011-12-17 02:42:38 +04:00
|
|
|
|
2012-01-11 05:42:42 +04:00
|
|
|
describe ".save()", ->
|
|
|
|
it "is triggered by the 'save' event", ->
|
|
|
|
spyOn(editor, 'save')
|
|
|
|
editor.trigger('save')
|
|
|
|
expect(editor.save).toHaveBeenCalled()
|
|
|
|
|
2011-12-17 04:25:33 +04:00
|
|
|
describe "when the current buffer has a url", ->
|
|
|
|
beforeEach ->
|
2012-01-05 23:26:14 +04:00
|
|
|
buffer = new Buffer(tempFilePath)
|
|
|
|
editor.setBuffer(buffer)
|
2011-12-17 04:25:33 +04:00
|
|
|
|
|
|
|
it "saves the current buffer to disk", ->
|
|
|
|
editor.buffer.setText 'Edited buffer!'
|
|
|
|
expect(fs.exists(tempFilePath)).toBeFalsy()
|
|
|
|
|
|
|
|
editor.save()
|
2011-12-17 02:42:38 +04:00
|
|
|
|
2011-12-17 04:25:33 +04:00
|
|
|
expect(fs.exists(tempFilePath)).toBeTruthy()
|
|
|
|
expect(fs.read(tempFilePath)).toBe 'Edited buffer!'
|
2011-12-17 02:42:38 +04:00
|
|
|
|
2011-12-17 04:25:33 +04:00
|
|
|
describe "when the current buffer has no url", ->
|
|
|
|
selectedFilePath = null
|
2011-12-17 02:42:38 +04:00
|
|
|
beforeEach ->
|
2011-12-17 04:25:33 +04:00
|
|
|
expect(editor.buffer.url).toBeUndefined()
|
|
|
|
editor.buffer.setText 'Save me to a new url'
|
|
|
|
spyOn(atom.native, 'savePanel').andCallFake -> selectedFilePath
|
2011-12-17 02:42:38 +04:00
|
|
|
|
2011-12-17 04:25:33 +04:00
|
|
|
it "presents a 'save as' dialog", ->
|
|
|
|
editor.save()
|
|
|
|
expect(atom.native.savePanel).toHaveBeenCalled()
|
2011-12-17 02:42:38 +04:00
|
|
|
|
2011-12-17 04:25:33 +04:00
|
|
|
describe "when a url is chosen", ->
|
|
|
|
it "saves the buffer to the chosen url", ->
|
|
|
|
selectedFilePath = '/tmp/temp.txt'
|
2011-12-17 02:42:38 +04:00
|
|
|
|
2011-12-17 04:25:33 +04:00
|
|
|
editor.save()
|
2011-12-17 02:42:38 +04:00
|
|
|
|
2011-12-17 04:25:33 +04:00
|
|
|
expect(fs.exists(selectedFilePath)).toBeTruthy()
|
|
|
|
expect(fs.read(selectedFilePath)).toBe 'Save me to a new url'
|
2011-12-17 02:42:38 +04:00
|
|
|
|
2011-12-17 04:25:33 +04:00
|
|
|
describe "when dialog is cancelled", ->
|
|
|
|
it "does not save the buffer", ->
|
|
|
|
selectedFilePath = null
|
2011-12-17 02:42:38 +04:00
|
|
|
|
2011-12-17 04:25:33 +04:00
|
|
|
editor.save()
|
2011-12-17 02:42:38 +04:00
|
|
|
|
2011-12-17 04:25:33 +04:00
|
|
|
expect(fs.exists(selectedFilePath)).toBeFalsy()
|
2011-12-17 02:42:38 +04:00
|
|
|
|
2012-01-11 07:16:46 +04:00
|
|
|
describe "key event handling", ->
|
2012-01-11 03:32:13 +04:00
|
|
|
handler = null
|
2012-01-11 07:16:46 +04:00
|
|
|
returnValue = null
|
2012-01-11 03:32:13 +04:00
|
|
|
|
|
|
|
beforeEach ->
|
2012-01-11 07:16:46 +04:00
|
|
|
handler =
|
|
|
|
handleKeyEvent: jasmine.createSpy('handleKeyEvent').andCallFake ->
|
|
|
|
returnValue
|
2012-01-11 03:32:13 +04:00
|
|
|
|
2012-01-11 07:16:46 +04:00
|
|
|
describe "when onCommandKey is called on the aceEditor (triggered by a keydown event on the textarea)", ->
|
|
|
|
event = null
|
2012-01-11 03:32:13 +04:00
|
|
|
|
|
|
|
beforeEach ->
|
2012-01-11 07:16:46 +04:00
|
|
|
event = keydownEvent 'x'
|
|
|
|
spyOn(event, 'stopPropagation')
|
2012-01-11 03:32:13 +04:00
|
|
|
|
2012-01-11 07:16:46 +04:00
|
|
|
describe "when no key event handler has been assigned", ->
|
2012-01-11 03:32:13 +04:00
|
|
|
beforeEach ->
|
2012-01-11 07:16:46 +04:00
|
|
|
expect(editor.keyEventHandler).toBeNull()
|
2012-01-11 03:32:13 +04:00
|
|
|
|
2012-01-11 07:16:46 +04:00
|
|
|
it "handles the event without crashing", ->
|
2012-01-11 03:32:13 +04:00
|
|
|
editor.aceEditor.onCommandKey event, 0, event.which
|
|
|
|
|
2012-01-11 07:16:46 +04:00
|
|
|
describe "when a key event handler has been assigned", ->
|
2012-01-11 03:32:13 +04:00
|
|
|
beforeEach ->
|
2012-01-11 07:16:46 +04:00
|
|
|
editor.keyEventHandler = handler
|
2012-01-11 03:32:13 +04:00
|
|
|
|
2012-01-11 07:16:46 +04:00
|
|
|
it "asks the key event handler to handle the event", ->
|
2012-01-11 03:32:13 +04:00
|
|
|
editor.aceEditor.onCommandKey event, 0, event.which
|
2012-01-11 07:16:46 +04:00
|
|
|
expect(handler.handleKeyEvent).toHaveBeenCalled()
|
|
|
|
|
2012-01-12 00:51:42 +04:00
|
|
|
describe "if the atom key event handler returns true, indicating that it did not handle the event", ->
|
2012-01-11 07:16:46 +04:00
|
|
|
beforeEach ->
|
2012-01-12 00:51:42 +04:00
|
|
|
returnValue = true
|
2012-01-11 07:16:46 +04:00
|
|
|
|
|
|
|
it "does not stop the propagation of the event, allowing Ace to handle it as normal", ->
|
|
|
|
editor.aceEditor.onCommandKey event, 0, event.which
|
|
|
|
expect(event.stopPropagation).not.toHaveBeenCalled()
|
|
|
|
|
2012-01-12 00:51:42 +04:00
|
|
|
describe "if the atom key event handler returns false, indicating that it handled the event", ->
|
2012-01-11 07:16:46 +04:00
|
|
|
beforeEach ->
|
2012-01-12 00:51:42 +04:00
|
|
|
returnValue = false
|
2012-01-11 07:16:46 +04:00
|
|
|
|
|
|
|
it "stops propagation of the event, so Ace does not attempt to handle it", ->
|
|
|
|
editor.aceEditor.onCommandKey event, 0, event.which
|
|
|
|
expect(event.stopPropagation).toHaveBeenCalled()
|
|
|
|
|
|
|
|
describe "when onTextInput is called on the aceEditor (triggered by an input event)", ->
|
|
|
|
it "does not call handleKeyEvent on the key event handler, because there is no event", ->
|
|
|
|
editor.keyEventHandler = handler
|
|
|
|
editor.aceEditor.onTextInput("x", false)
|
|
|
|
expect(handler.handleKeyEvent).not.toHaveBeenCalled()
|
|
|
|
|