Reorganize editor auto-indent specs

This commit is contained in:
Max Brunsfeld
2014-11-17 16:53:11 -08:00
parent 5ec38f84c5
commit 24432018ec

View File

@@ -2851,6 +2851,18 @@ describe "TextEditor", ->
expect(editor.getSelectedBufferRange()).toEqual [[0, 1], [3, 0]]
describe ".autoIndentSelectedRows", ->
it "auto-indents the selection", ->
editor.setCursorBufferPosition([2, 0])
editor.insertText("function() {\ninside=true\n}\n i=1\n")
editor.getLastSelection().setBufferRange([[2,0], [6,0]])
editor.autoIndentSelectedRows()
expect(editor.lineTextForBufferRow(2)).toBe " function() {"
expect(editor.lineTextForBufferRow(3)).toBe " inside=true"
expect(editor.lineTextForBufferRow(4)).toBe " }"
expect(editor.lineTextForBufferRow(5)).toBe " i=1"
describe ".toggleLineCommentsInSelection()", ->
it "toggles comments on the selected lines", ->
editor.setSelectedBufferRange([[4, 5], [7, 5]])
@@ -3354,147 +3366,135 @@ describe "TextEditor", ->
expect(editor.getGrammar()).toBe jsGrammar
expect(editor.tokenizedLineForScreenRow(0).tokens.length).toBeGreaterThan 1
describe "auto-indent", ->
describe "editor.autoIndent", ->
describe "when editor.autoIndent is false (default)", ->
describe "when `indent` is triggered", ->
it "does not auto-indent the line", ->
editor.setCursorBufferPosition([1, 30])
editor.insertText("\n ")
expect(editor.lineTextForBufferRow(2)).toBe " "
atom.config.set("editor.autoIndent", false)
editor.indent()
expect(editor.lineTextForBufferRow(2)).toBe " "
describe "when editor.autoIndent is true", ->
beforeEach ->
atom.config.set("editor.autoIndent", true)
describe "when `indent` is triggered", ->
it "auto-indents the line", ->
editor.setCursorBufferPosition([1, 30])
editor.insertText("\n ")
expect(editor.lineTextForBufferRow(2)).toBe " "
atom.config.set("editor.autoIndent", true)
editor.indent()
expect(editor.lineTextForBufferRow(2)).toBe " "
describe "when a newline is added", ->
describe "when the line preceding the newline adds a new level of indentation", ->
it "indents the newline to one additional level of indentation beyond the preceding line", ->
editor.setCursorBufferPosition([1, Infinity])
editor.insertText('\n')
expect(editor.indentationForBufferRow(2)).toBe editor.indentationForBufferRow(1) + 1
describe "when the line preceding the newline does't add a level of indentation", ->
it "indents the new line to the same level a as the preceding line", ->
editor.setCursorBufferPosition([5, 14])
editor.insertText('\n')
expect(editor.indentationForBufferRow(6)).toBe editor.indentationForBufferRow(5)
describe "when the line preceding the newline is a comment", ->
it "maintains the indent of the commented line", ->
editor.setCursorBufferPosition([0, 0])
editor.insertText(' //')
editor.setCursorBufferPosition([0, Infinity])
editor.insertText('\n')
expect(editor.indentationForBufferRow(1)).toBe 2
it "does not indent the line preceding the newline", ->
editor.setCursorBufferPosition([2, 0])
editor.insertText(' var this-line-should-be-indented-more\n')
expect(editor.indentationForBufferRow(1)).toBe 1
atom.config.set("editor.autoIndent", true)
editor.setCursorBufferPosition([2, Infinity])
editor.insertText('\n')
expect(editor.indentationForBufferRow(1)).toBe 1
expect(editor.indentationForBufferRow(2)).toBe 1
describe "when the cursor is before whitespace", ->
it "retains the whitespace following the cursor on the new line", ->
editor.setText(" var sort = function() {}")
editor.setCursorScreenPosition([0, 23])
editor.insertNewline()
expect(buffer.lineForRow(0)).toBe ' var sort = function()'
expect(buffer.lineForRow(1)).toBe ' {}'
expect(editor.getCursorScreenPosition()).toEqual [1, 2]
describe "when inserted text matches a decrease indent pattern", ->
describe "when the preceding line matches an increase indent pattern", ->
it "decreases the indentation to match that of the preceding line", ->
editor.setCursorBufferPosition([1, Infinity])
editor.insertText('\n')
expect(editor.indentationForBufferRow(2)).toBe editor.indentationForBufferRow(1) + 1
editor.insertText('}')
expect(editor.indentationForBufferRow(2)).toBe editor.indentationForBufferRow(1)
describe "when the preceding line doesn't match an increase indent pattern", ->
it "decreases the indentation to be one level below that of the preceding line", ->
editor.setCursorBufferPosition([3, Infinity])
editor.insertText('\n ')
expect(editor.indentationForBufferRow(4)).toBe editor.indentationForBufferRow(3)
editor.insertText('}')
expect(editor.indentationForBufferRow(4)).toBe editor.indentationForBufferRow(3) - 1
it "doesn't break when decreasing the indentation on a row that has no indentation", ->
editor.setCursorBufferPosition([12, Infinity])
editor.insertText("\n}; # too many closing brackets!")
expect(editor.lineTextForBufferRow(13)).toBe "}; # too many closing brackets!"
describe "when inserted text does not match a decrease indent pattern", ->
it "does not decrease the indentation", ->
editor.setCursorBufferPosition([12, 0])
editor.insertText(' ')
expect(editor.lineTextForBufferRow(12)).toBe ' };'
editor.insertText('\t\t')
expect(editor.lineTextForBufferRow(12)).toBe ' \t\t};'
describe "when the current line does not match a decrease indent pattern", ->
it "leaves the line unchanged", ->
editor.setCursorBufferPosition([2, 4])
expect(editor.indentationForBufferRow(2)).toBe editor.indentationForBufferRow(1) + 1
editor.insertText('foo')
expect(editor.indentationForBufferRow(2)).toBe editor.indentationForBufferRow(1) + 1
describe 'when scoped settings are used', ->
coffeeEditor = null
beforeEach ->
waitsForPromise ->
atom.packages.activatePackage('language-coffee-script')
waitsForPromise ->
atom.project.open('coffee.coffee', autoIndent: false).then (o) -> coffeeEditor = o
runs ->
atom.config.set('.source.js', 'editor.autoIndent', true)
atom.config.set('.source.coffee', 'editor.autoIndent', false)
afterEach: ->
atom.packages.deactivatePackages()
atom.packages.unloadPackages()
it "does not auto-indent the line for javascript files", ->
describe "editor.autoIndent", ->
describe "when editor.autoIndent is false (default)", ->
describe "when `indent` is triggered", ->
it "does not auto-indent the line", ->
editor.setCursorBufferPosition([1, 30])
editor.insertText("\n")
editor.insertText("\n ")
expect(editor.lineTextForBufferRow(2)).toBe " "
atom.config.set("editor.autoIndent", false)
editor.indent()
expect(editor.lineTextForBufferRow(2)).toBe " "
describe "when editor.autoIndent is true", ->
beforeEach ->
atom.config.set("editor.autoIndent", true)
describe "when `indent` is triggered", ->
it "auto-indents the line", ->
editor.setCursorBufferPosition([1, 30])
editor.insertText("\n ")
expect(editor.lineTextForBufferRow(2)).toBe " "
atom.config.set("editor.autoIndent", true)
editor.indent()
expect(editor.lineTextForBufferRow(2)).toBe " "
coffeeEditor.setCursorBufferPosition([1, 18])
coffeeEditor.insertText("\n")
expect(coffeeEditor.lineTextForBufferRow(2)).toBe ""
describe "when a newline is added", ->
describe "when the line preceding the newline adds a new level of indentation", ->
it "indents the newline to one additional level of indentation beyond the preceding line", ->
editor.setCursorBufferPosition([1, Infinity])
editor.insertText('\n')
expect(editor.indentationForBufferRow(2)).toBe editor.indentationForBufferRow(1) + 1
it "autoIndentSelectedRows auto-indents the selection", ->
editor.setCursorBufferPosition([2, 0])
editor.insertText("function() {\ninside=true\n}\n i=1\n")
editor.getLastSelection().setBufferRange([[2,0], [6,0]])
editor.autoIndentSelectedRows()
describe "when the line preceding the newline does't add a level of indentation", ->
it "indents the new line to the same level a as the preceding line", ->
editor.setCursorBufferPosition([5, 14])
editor.insertText('\n')
expect(editor.indentationForBufferRow(6)).toBe editor.indentationForBufferRow(5)
expect(editor.lineTextForBufferRow(2)).toBe " function() {"
expect(editor.lineTextForBufferRow(3)).toBe " inside=true"
expect(editor.lineTextForBufferRow(4)).toBe " }"
expect(editor.lineTextForBufferRow(5)).toBe " i=1"
describe "when the line preceding the newline is a comment", ->
it "maintains the indent of the commented line", ->
editor.setCursorBufferPosition([0, 0])
editor.insertText(' //')
editor.setCursorBufferPosition([0, Infinity])
editor.insertText('\n')
expect(editor.indentationForBufferRow(1)).toBe 2
it "does not indent the line preceding the newline", ->
editor.setCursorBufferPosition([2, 0])
editor.insertText(' var this-line-should-be-indented-more\n')
expect(editor.indentationForBufferRow(1)).toBe 1
atom.config.set("editor.autoIndent", true)
editor.setCursorBufferPosition([2, Infinity])
editor.insertText('\n')
expect(editor.indentationForBufferRow(1)).toBe 1
expect(editor.indentationForBufferRow(2)).toBe 1
describe "when the cursor is before whitespace", ->
it "retains the whitespace following the cursor on the new line", ->
editor.setText(" var sort = function() {}")
editor.setCursorScreenPosition([0, 23])
editor.insertNewline()
expect(buffer.lineForRow(0)).toBe ' var sort = function()'
expect(buffer.lineForRow(1)).toBe ' {}'
expect(editor.getCursorScreenPosition()).toEqual [1, 2]
describe "when inserted text matches a decrease indent pattern", ->
describe "when the preceding line matches an increase indent pattern", ->
it "decreases the indentation to match that of the preceding line", ->
editor.setCursorBufferPosition([1, Infinity])
editor.insertText('\n')
expect(editor.indentationForBufferRow(2)).toBe editor.indentationForBufferRow(1) + 1
editor.insertText('}')
expect(editor.indentationForBufferRow(2)).toBe editor.indentationForBufferRow(1)
describe "when the preceding line doesn't match an increase indent pattern", ->
it "decreases the indentation to be one level below that of the preceding line", ->
editor.setCursorBufferPosition([3, Infinity])
editor.insertText('\n ')
expect(editor.indentationForBufferRow(4)).toBe editor.indentationForBufferRow(3)
editor.insertText('}')
expect(editor.indentationForBufferRow(4)).toBe editor.indentationForBufferRow(3) - 1
it "doesn't break when decreasing the indentation on a row that has no indentation", ->
editor.setCursorBufferPosition([12, Infinity])
editor.insertText("\n}; # too many closing brackets!")
expect(editor.lineTextForBufferRow(13)).toBe "}; # too many closing brackets!"
describe "when inserted text does not match a decrease indent pattern", ->
it "does not decrease the indentation", ->
editor.setCursorBufferPosition([12, 0])
editor.insertText(' ')
expect(editor.lineTextForBufferRow(12)).toBe ' };'
editor.insertText('\t\t')
expect(editor.lineTextForBufferRow(12)).toBe ' \t\t};'
describe "when the current line does not match a decrease indent pattern", ->
it "leaves the line unchanged", ->
editor.setCursorBufferPosition([2, 4])
expect(editor.indentationForBufferRow(2)).toBe editor.indentationForBufferRow(1) + 1
editor.insertText('foo')
expect(editor.indentationForBufferRow(2)).toBe editor.indentationForBufferRow(1) + 1
describe 'when scoped settings are used', ->
coffeeEditor = null
beforeEach ->
waitsForPromise ->
atom.packages.activatePackage('language-coffee-script')
waitsForPromise ->
atom.project.open('coffee.coffee', autoIndent: false).then (o) -> coffeeEditor = o
runs ->
atom.config.set('.source.js', 'editor.autoIndent', true)
atom.config.set('.source.coffee', 'editor.autoIndent', false)
afterEach: ->
atom.packages.deactivatePackages()
atom.packages.unloadPackages()
it "does not auto-indent the line for javascript files", ->
editor.setCursorBufferPosition([1, 30])
editor.insertText("\n")
expect(editor.lineTextForBufferRow(2)).toBe " "
coffeeEditor.setCursorBufferPosition([1, 18])
coffeeEditor.insertText("\n")
expect(coffeeEditor.lineTextForBufferRow(2)).toBe ""
describe "soft and hard tabs", ->
afterEach ->