filer/tests/spec/fs.read.spec.js

63 lines
1.9 KiB
JavaScript
Raw Normal View History

2014-05-23 20:53:50 +00:00
var Filer = require('../..');
var util = require('../lib/test-utils.js');
var expect = require('chai').expect;
2013-11-27 17:18:09 +00:00
2014-05-23 20:53:50 +00:00
describe('fs.read', function() {
beforeEach(util.setup);
afterEach(util.cleanup);
2013-11-27 17:18:09 +00:00
2014-05-23 20:53:50 +00:00
it('should be a function', function() {
var fs = util.fs();
expect(fs.read).to.be.a('function');
});
2013-11-27 17:18:09 +00:00
2014-05-23 20:53:50 +00:00
it('should read data from a file', function(done) {
var fs = util.fs();
var wbuffer = new Uint8Array([1, 2, 3, 4, 5, 6, 7, 8]);
var rbuffer = new Uint8Array(wbuffer.length);
2013-11-27 17:18:09 +00:00
2014-05-23 20:53:50 +00:00
fs.open('/myfile', 'w+', function(error, fd) {
if(error) throw error;
fs.write(fd, wbuffer, 0, wbuffer.length, 0, function(error, result) {
2013-11-27 17:18:09 +00:00
if(error) throw error;
2014-05-23 20:53:50 +00:00
fs.read(fd, rbuffer, 0, rbuffer.length, 0, function(error, result) {
expect(error).not.to.exist;
expect(result).to.equal(rbuffer.length);
expect(wbuffer).to.deep.equal(rbuffer);
done();
2013-11-27 17:18:09 +00:00
});
});
});
2014-05-23 20:53:50 +00:00
});
it('should update the current file position', function(done) {
var fs = util.fs();
var wbuffer = new Uint8Array([1, 2, 3, 4, 5, 6, 7, 8]);
var rbuffer = new Uint8Array(wbuffer.length);
var _result = 0;
2013-11-27 17:18:09 +00:00
2014-05-23 20:53:50 +00:00
fs.open('/myfile', 'w+', function(error, fd) {
if(error) throw error;
2013-11-27 17:18:09 +00:00
2014-05-23 20:53:50 +00:00
fs.write(fd, wbuffer, 0, wbuffer.length, 0, function(error, result) {
2013-11-27 17:18:09 +00:00
if(error) throw error;
2014-05-23 20:53:50 +00:00
fs.read(fd, rbuffer, 0, rbuffer.length / 2, undefined, function(error, result) {
2013-11-27 17:18:09 +00:00
if(error) throw error;
2014-05-23 20:53:50 +00:00
_result += result;
fs.read(fd, rbuffer, rbuffer.length / 2, rbuffer.length, undefined, function(error, result) {
2013-11-27 17:18:09 +00:00
if(error) throw error;
_result += result;
2014-05-23 20:53:50 +00:00
expect(error).not.to.exist;
expect(_result).to.equal(rbuffer.length);
expect(wbuffer).to.deep.equal(rbuffer);
done();
2013-11-27 17:18:09 +00:00
});
});
});
});
});
2014-05-23 20:53:50 +00:00
});