     @name = 'SpecError'
 
 getVimState = (args...) ->
-  [file, callback] = []
+  [editor, file, callback] = []
   switch args.length
     when 1 then [callback] = args
     when 2 then [file, callback] = args
-
-  editor = null
 
   waitsForPromise ->
     atom.packages.activatePackage(packageName)

    keydown keys, {element}
  else
    for key in keys.split('')
      if key.match(/[A-Z]/)
        keydown key, {shift: true, element}
      else
        keydown key, {element}

toArray = (obj, cond=null) ->
  if _.isArray(cond ? obj) then obj else [obj]

  constructor: (@vimState) ->
    {@editor, @editorElement} = @vimState

  validOptions = [
    'text', 'cursor', 'cursorBuffer', 'addCursor',
    'register', 'selectedBufferRange', 'spy'
  ]
  validateOptions: (options) ->
    invalidOptions = _.without(_.keys(options), validOptions...)
    if invalidOptions.length
      throw new SpecError("invalidOptions: #{inspect(invalidOptions)}")

  set: (options) =>
    @validateOptions(options)
    optionsOrdered = [
      'text', 'cursor', 'cursorBuffer', 'addCursor', 'register'
      'selectedBufferRange', 'spy'
    ]
    for name in optionsOrdered when options[name]?
      method = _.capitalize(_.camelize(name))
      this["set#{method}"](options[name])

  setText: (text) =>
    @editor.setText(text)

  setCursor: (cursor) =>
    @editor.setCursorScreenPosition(cursor)

  setCursorBuffer: (cursor) =>
    @editor.setCursorBufferPosition(cursor)

  setAddCursor: (cursors) =>
    for point in toArray(cursors, cursors[0])
      @editor.addCursorAtBufferPosition(point)

  setRegister: (register) =>
    if _.isObject(register)
      for name, value of register
        @vimState.register.set(name, value)
    else
      @vimState.register.set '"', text: register

  setSelectedBufferRange: (range) =>
    @editor.setSelectedBufferRange(range)

  setSpy: (spy) ->
    # e.g.
    # spyOn(editor, 'getURI').andReturn('/Users/atom/known_value.txt')
    for s in toArray(spy)
      spyOn(s.obj, s.method).andReturn(s.return)

  ensure: (args...) =>
    [keys, o] = []
