2012-01-05 23:13:55 +04:00
|
|
|
Buffer = require 'buffer'
|
2011-12-16 02:13:34 +04:00
|
|
|
Editor = require 'editor'
|
|
|
|
$ = require 'jquery'
|
2011-12-17 02:42:38 +04:00
|
|
|
fs = require 'fs'
|
2011-12-16 02:13:34 +04:00
|
|
|
|
2012-01-18 06:13:50 +04:00
|
|
|
fdescribe "Editor", ->
|
2012-01-17 05:17:36 +04:00
|
|
|
buffer = null
|
2011-12-17 04:25:33 +04:00
|
|
|
editor = null
|
2011-12-16 02:13:34 +04:00
|
|
|
|
|
|
|
beforeEach ->
|
2012-01-17 05:17:36 +04:00
|
|
|
buffer = new Buffer(require.resolve('fixtures/sample.js'))
|
2011-12-29 23:12:13 +04:00
|
|
|
editor = Editor.build()
|
2012-01-18 06:13:50 +04:00
|
|
|
editor.enableKeymap()
|
|
|
|
editor.setBuffer(buffer)
|
2011-12-16 02:13:34 +04:00
|
|
|
|
2012-01-17 05:17:36 +04:00
|
|
|
describe ".setBuffer", ->
|
2012-01-18 06:13:50 +04:00
|
|
|
beforeEach ->
|
|
|
|
|
2012-01-17 05:17:36 +04:00
|
|
|
it "creates a pre element for each line in the buffer", ->
|
|
|
|
expect(editor.lines.find('pre').length).toEqual(buffer.numLines())
|
2012-01-11 07:16:46 +04:00
|
|
|
|
2012-01-17 05:17:36 +04:00
|
|
|
it "sets the cursor to the beginning of the file", ->
|
|
|
|
expect(editor.getPosition()).toEqual(row: 0, col: 0)
|
2012-01-11 07:16:46 +04:00
|
|
|
|
2012-01-18 06:13:50 +04:00
|
|
|
describe "cursor movement", ->
|
|
|
|
it "moves the cursor when arrow keys are pressed", ->
|
|
|
|
editor.trigger keydownEvent('right')
|
|
|
|
expect(editor.getPosition()).toEqual(row: 0, col: 1)
|
|
|
|
|
|
|
|
editor.trigger keydownEvent('down')
|
|
|
|
expect(editor.getPosition()).toEqual(row: 1, col: 1)
|
|
|
|
|
|
|
|
editor.trigger keydownEvent('left')
|
|
|
|
expect(editor.getPosition()).toEqual(row: 1, col: 0)
|
|
|
|
|
|
|
|
editor.trigger keydownEvent('up')
|
|
|
|
expect(editor.getPosition()).toEqual(row: 0, col: 0)
|
|
|
|
|
2012-01-20 02:39:16 +04:00
|
|
|
describe "when up is pressed on the first line", ->
|
|
|
|
it "moves the cursor to the beginning of the line", ->
|
|
|
|
editor.setPosition(row: 0, col: 4)
|
|
|
|
editor.moveUp()
|
|
|
|
expect(editor.getPosition()).toEqual(row: 0, col: 0)
|
|
|
|
|
|
|
|
describe "when down is pressed on the last line", ->
|
|
|
|
it "moves the cursor to the end of line", ->
|
|
|
|
lastLineIndex = buffer.getLines().length - 1
|
|
|
|
lastLine = buffer.getLine(lastLineIndex)
|
|
|
|
expect(lastLine.length).toBeGreaterThan(0)
|
|
|
|
|
|
|
|
editor.setPosition(row: lastLineIndex, col: 0)
|
|
|
|
editor.moveDown()
|
|
|
|
expect(editor.getPosition()).toEqual(row: lastLineIndex, col: lastLine.length)
|
|
|
|
|
|
|
|
describe "when left is pressed on the first column", ->
|
|
|
|
it "wraps to the end of the previous line", ->
|
|
|
|
editor.setPosition(row: 1, col: 0)
|
|
|
|
editor.moveLeft()
|
|
|
|
expect(editor.getPosition()).toEqual(row: 0, col: buffer.getLine(0).length)
|
|
|
|
|
|
|
|
describe "when right is pressed on the last column", ->
|
|
|
|
it "wraps to the beginning of the next line", ->
|
|
|
|
editor.setPosition(row: 0, col: buffer.getLine(0).length)
|
|
|
|
editor.moveRight()
|
|
|
|
expect(editor.getPosition()).toEqual(row: 1, col: 0)
|
2012-01-18 06:13:50 +04:00
|
|
|
|
2012-01-17 10:11:38 +04:00
|
|
|
describe ".setPosition({row, col})", ->
|
2012-01-17 07:23:27 +04:00
|
|
|
it "moves the cursor to cover the character at the given row and column", ->
|
|
|
|
editor.attachToDom()
|
|
|
|
editor.setPosition(row: 2, col: 2)
|
|
|
|
|
|
|
|
expect(editor.cursor.position().top).toBe(2 * editor.lineHeight())
|
|
|
|
expect(editor.cursor.position().left).toBe(2 * editor.charWidth())
|
|
|
|
|
2012-01-18 06:13:50 +04:00
|
|
|
|
|
|
|
describe "when the editor is attached to the dom", ->
|
|
|
|
it "updates the pixel position of the cursor", ->
|
|
|
|
editor.setPosition(row: 2, col: 2)
|
|
|
|
|
|
|
|
editor.attachToDom()
|
|
|
|
|
|
|
|
expect(editor.cursor.position().top).toBe(2 * editor.lineHeight())
|
|
|
|
expect(editor.cursor.position().left).toBe(2 * editor.charWidth())
|
|
|
|
|