diff --git a/lib/writer.js b/lib/writer.js index 34c779c6..302ae37d 100644 --- a/lib/writer.js +++ b/lib/writer.js @@ -75,4 +75,10 @@ p.clear = function() { this.offset=0; } +p.flush = function() { + var result = this.join(); + this.clear(); + return result; +} + module.exports = Writer; diff --git a/test/unit/writer-tests.js b/test/unit/writer-tests.js index 0ff55dc2..3e0f08f8 100644 --- a/test/unit/writer-tests.js +++ b/test/unit/writer-tests.js @@ -78,7 +78,7 @@ test('cString', function() { var result = subject.addCString("!!!").join(); assert.equalBuffers(result, [33, 33, 33, 0]); }) - + test('writes multiple cstrings', function() { var subject = new Writer(); var result = subject.addCString("!").addCString("!").join(); @@ -115,4 +115,22 @@ test('clearing', function() { subject.addInt32(10401); subject.clear(); assert.equalBuffers(subject.join(), []); + test('can keep writing', function() { + var joinedResult = subject.addCString("!").addInt32(9).addInt16(2).join(); + assert.equalBuffers(joinedResult, [33, 0, 0, 0, 0, 9, 0, 2]); + test('flush', function() { + var flushedResult = subject.flush(); + test('returns result', function() { + assert.equalBuffers(flushedResult, [33, 0, 0, 0, 0, 9, 0, 2]) + }) + test('clears the writer', function() { + assert.equalBuffers(subject.join(), []) + assert.equalBuffers(subject.flush(), []) + }) + }) + }) + }) + + +