         done()
 
   describe "modified status", ->
-    [filePath] = []
+    [filePath, modifiedHandler, doneFunc] = []
 
     beforeEach (done) ->
       filePath = join(temp.dir, 'atom-tmp-file')


      describe "when the serialized buffer had unsaved changes", ->
        describe "when the disk contents were changed since serialization", ->
          xit "loads the disk contents instead of the previous unsaved state", ->
            buffer.setText("BUFFER CHANGE")
            fs.writeFileSync(filePath, "DISK CHANGE")

            buffer2 = buffer.testSerialization()

            waitsFor ->
              buffer2.cachedDiskContents

            runs ->
              expect(buffer2.getPath()).toBe(buffer.getPath())
              expect(buffer2.getText()).toBe("DISK CHANGE")
              expect(buffer2.isModified()).toBeFalsy()

        describe "when the disk contents are the same since serialization", ->
          [previousText] = []


    afterEach ->
      buffer?.destroy()
      modifiedHandler = null
      doneFunc = null

    describe "after the user changes buffer", ->
      beforeEach (done) ->
        doneFunc = done
        modifiedHandler = jasmine.createSpy("modifiedHandler").and.callFake ->
          doneFunc()
        buffer.onDidChangeModified modifiedHandler

        expect(buffer.isModified()).toBeFalsy()
        buffer.insert([0,0], "hi")
        expect(buffer.isModified()).toBe true

      beforeEach (done) ->
        expect(modifiedHandler).toHaveBeenCalledWith(true)

        modifiedHandler.calls.reset()
        buffer.insert([0,2], "ho")
        setTimeout(->
          expect(modifiedHandler).not.toHaveBeenCalled()
          done()
        , buffer.stoppedChangingDelay * 2)

      it "reports the modified status changing to true or false", (done) ->
        modifiedHandler.calls.reset()
        doneFunc = ->
          expect(modifiedHandler).toHaveBeenCalledWith(false)
          done()
        buffer.undo()
        buffer.undo()

    describe "after a modified buffer is saved", ->
      beforeEach (done) ->
        doneFunc = done
        modifiedHandler = jasmine.createSpy("modifiedHandler").and.callFake ->
          doneFunc()
        buffer.onDidChangeModified modifiedHandler
        buffer.insert([0,0], "hi")

      it "reports the modified status changing to false", (done) ->
        expect(buffer.isModified()).toBe true

        modifiedHandler.calls.reset()
        buffer.save()

        expect(modifiedHandler).toHaveBeenCalledWith(false)
        expect(buffer.isModified()).toBe false
        modifiedHandler.calls.reset()
        doneFunc = ->
          expect(modifiedHandler).toHaveBeenCalledWith(true)
          expect(buffer.isModified()).toBe true
          done()
        buffer.insert([0, 0], 'x')

    describe "after a modified buffer is reloaded", ->
      beforeEach (done) ->
        doneFunc = done
        modifiedHandler = jasmine.createSpy("modifiedHandler").and.callFake ->
          doneFunc()
        buffer.onDidChangeModified modifiedHandler
        buffer.insert([0,0], "hi")

      it "reports the modified status changing to false after a modified buffer is reloaded", ->
        expect(buffer.isModified()).toBe true

        modifiedHandler.calls.reset()
        buffer.reload()

        expect(modifiedHandler).toHaveBeenCalledWith(false)
        expect(buffer.isModified()).toBe false

        modifiedHandler.calls.reset()
        doneFunc = ->
          expect(modifiedHandler).toHaveBeenCalledWith(true)
          expect(buffer.isModified()).toBe true
          done()
        buffer.insert([0, 0], 'x')

    describe "after a buffer to a non-existent file is saved", ->
      beforeEach (done) ->
        buffer.destroy()
        fs.removeSync(filePath)
        expect(fs.existsSync(filePath)).toBeFalsy()

        buffer = new TextBuffer({filePath, load: false})
        buffer.load().then ->
          done()

      beforeEach (done) ->
        doneFunc = done
        modifiedHandler = jasmine.createSpy("modifiedHandler").and.callFake ->
          doneFunc()
        buffer.onDidChangeModified modifiedHandler
        buffer.insert([0,0], "hi")

      it "reports the modified status changing to false", (done) ->
        expect(buffer.isModified()).toBe true

        modifiedHandler.calls.reset()
        buffer.save()

        expect(fs.existsSync(filePath)).toBeTruthy()
        expect(modifiedHandler).toHaveBeenCalledWith(false)
        expect(buffer.isModified()).toBe false

        modifiedHandler.calls.reset()
        doneFunc = ->
          expect(modifiedHandler).toHaveBeenCalledWith(true)
          expect(buffer.isModified()).toBe true
          done()
        buffer.insert([0, 0], 'x')

    it "returns false for an empty buffer with no path", ->
      buffer.destroy()
