Add Buffer.save

This commit is contained in:
Corey Johnson & Nathan Sobo
2011-12-15 15:59:32 -08:00
parent 5fbb320957
commit a07daf26ca
4 changed files with 59 additions and 9 deletions

View File

@@ -3,11 +3,47 @@ fs = require 'fs'
describe 'Buffer', ->
describe 'constructor', ->
it "loads the contents of the given url", ->
filePath = require.resolve 'fixtures/sample.txt'
buffer = new Buffer filePath
expect(buffer.getText()).toBe fs.read(filePath)
describe "when given a url", ->
describe "when a file exists for the url", ->
it "loads the contents of that file", ->
filePath = require.resolve 'fixtures/sample.txt'
buffer = new Buffer filePath
expect(buffer.getText()).toBe fs.read(filePath)
describe "when no file exists for the url", ->
it "creates an empty buffer", ->
filePath = "does-not-exist.txt"
expect(fs.exists(filePath)).toBeFalsy()
buffer = new Buffer filePath
expect(buffer.getText()).toBe ""
describe "when no url is given", ->
it "creates an empty buffer", ->
buffer = new Buffer null
expect(buffer.getText()).toBe ""
describe "save", ->
describe "when the buffer has a url", ->
filePath = null
beforeEach ->
filePath = require.resolve('fixtures') + '/temp.txt'
expect(fs.exists(filePath)).toBeFalsy()
afterEach ->
fs.remove filePath
it "saves the contents of the buffer to the url", ->
buffer = new Buffer filePath
buffer.setText 'Buffer contents!'
buffer.save()
expect(fs.read(filePath)).toEqual 'Buffer contents!'
describe "when the buffer no url", ->
it "throw an exception", ->
buffer = new Buffer
expect(-> buffer.save()).toThrow()
it "loads an empty buffer if no url is given", ->
buffer = new Buffer null
expect(buffer.getText()).toBe ""

View File

@@ -7,9 +7,18 @@ class Buffer
url: null
constructor: (@url) ->
text = if @url then fs.read(@url) else ""
text = if @url and fs.exists(@url)
fs.read(@url)
else
""
@aceDocument = new Document text
getText: ->
@aceDocument.getValue()
setText: (text) ->
@aceDocument.setValue text
save: ->
if not @url then throw new Error("Tried to save buffer with no url")
fs.write @url, @getText()

View File

@@ -25,4 +25,3 @@ class Editor
getAceSession: ->
@aceEditor.getSession()

View File

@@ -67,6 +67,12 @@ module.exports =
listDirectoryTree: (path) ->
@list path, true
# Remove a file at the given path. Throws an error if path is not a
# file or a symbolic link to a file.
remove: (path) ->
fm = OSX.NSFileManager.defaultManager
paths = fm.removeItemAtPath_error path, null
# Open, read, and close a file, returning the file's contents.
read: (path) ->
path = @absolute path