   classListContains = (klass) ->
     expect editorElement.classList.contains(klass)
 
-  ensure = (spec={}) ->
-    if spec.text?
-      text().toBe spec.text
-    if spec.cursor?
-      cursor().toEqual spec.cursor
-    if spec.register?
-      register('"').toBe spec.register
-
-    if spec.selectedBufferRange?
-      selectedBufferRange().toEqual spec.selectedBufferRange
-    if spec.selectedBufferRanges?
-      selectedBufferRanges().toEqual spec.selectedBufferRanges
-
-    if spec.selectedScreenRange?
-      selectedScreenRange().toEqual spec.selectedScreenRange
-    if spec.selectedScreenRanges?
-      selectedScreenRanges().toEqual spec.selectedScreenRanges
+  ensure = (_keystroke, options={}) ->
+    keystroke(_keystroke)
+    if options.text?
+      text().toBe options.text
+    if options.cursor?
+      cursor().toEqual options.cursor
+    if options.register?
+      register('"').toBe options.register
+
+    if options.selectedBufferRange?
+      selectedBufferRange().toEqual options.selectedBufferRange
+    if options.selectedBufferRanges?
+      selectedBufferRanges().toEqual options.selectedBufferRanges
+
+    if options.selectedScreenRange?
+      selectedScreenRange().toEqual options.selectedScreenRange
+    if options.selectedScreenRanges?
+      selectedScreenRanges().toEqual options.selectedScreenRanges
 
     notEnsureNormalMode = [
-      spec.selectedBufferRange, spec.selectedBufferRanges
-      spec.selectedScreenRange, spec.selectedScreenRanges
+      options.selectedBufferRange, options.selectedBufferRanges
+      options.selectedScreenRange, options.selectedScreenRanges
     ].some((bool) -> bool)
     unless notEnsureNormalMode
       classListContains('operator-pending-mode').toBe(false)
       classListContains('normal-mode').toBe(true)
 
-  ensureKeystroke = (_keystroke, spec={}) ->
-    keystroke(_keystroke)
-    ensure(spec)
+  set = (options={}) ->
+    text(options.text) if options.text?
+    cursor(options.cursor) if options.cursor?
+    addCursor(options.addCursor) if options.addCursor?
 
   describe "the 'iw' text object", ->
     beforeEach ->
-      text "12345 abcde ABCDE"
-      cursor [0, 9]
-
-    it "applies operators inside the current word in operator-pending mode", ->
-      ensureKeystroke 'diw',
+      set
+        text: "12345 abcde ABCDE"
+        cursor: [0, 9]
+
+    it "applies operators inside the current word in operator-pending mode", ->
+      ensure 'diw',
         text:     "12345  ABCDE"
         cursor:   [0, 6]
         register: 'abcde'
 
     it "selects inside the current word in visual mode", ->
-      ensureKeystroke 'viw',
+      ensure 'viw',
         selectedScreenRange: [[0, 6], [0, 11]]
 
     it "works with multiple cursors", ->
-      addCursor [0, 1]
-      ensureKeystroke 'viw',
+      set
+        addCursor: [0, 1]
+      ensure 'viw',
         selectedBufferRanges: [
           [[0, 6], [0, 11]]
           [[0, 0], [0, 5]]

helpers = require './spec-helper'

describe "TextObjects", ->
  [editor, editorElement, vimState] = []

  beforeEach ->


  describe "the 'iW' text object", ->
    beforeEach ->
      set
        text: "12(45 ab'de ABCDE"
        cursor: [0, 9]

    it "applies operators inside the current whole word in operator-pending mode", ->
      ensure 'diW',
        text:     "12(45  ABCDE"
        cursor:   [0, 6]
        register: "ab'de"

    it "selects inside the current whole word in visual mode", ->
      ensure 'viW',
        selectedScreenRange: [[0, 6], [0, 11]]

  describe "the 'i(' text object", ->
    beforeEach ->
      set
        text: "( something in here and in (here) )"
        cursor: [0, 9]

    it "applies operators inside the current word in operator-pending mode", ->
      ensure 'di(',
        text: "()"
        cursor: [0, 1]

    it "applies operators inside the current word in operator-pending mode (second test)", ->
      set
        cursor: [0, 29]
      ensure 'di(',
        text: "( something in here and in () )"
        cursor: [0, 28]

    it "works with multiple cursors", ->
      set
        text: "( a b ) cde ( f g h ) ijk"
        cursor: [0, 2]
        addCursor: [0, 18]
      ensure 'vi(',
        selectedBufferRanges: [
          [[0, 1],  [0, 6]]
          [[0, 13], [0, 20]]
