module.exports = function() { var writer = new Writer(); writer.addInt32(10); writer.addInt16(5); writer.addCString('test'); writer.flush('X'); };
test('gets correct byte length', function() { var subject = new Writer(5); assert.equal(subject.getByteLength(), 0) subject.addInt32(0) assert.equal(subject.getByteLength(), 4) subject.addCString("!") assert.equal(subject.getByteLength(), 6) })
test('can write cString too', function() { var result = subject.addCString("!").join(); assert.equalBuffers(result, [33, 33, 0]); test('can resize', function() { var result = subject.addString("!!").join(); assert.equalBuffers(result, [33, 33, 0, 33, 33]); }) })
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(), []) }) }) })
suite('clearing', function() { var subject = new Writer(); subject.addCString("@!!#!#"); subject.addInt32(10401); test('clears', function() { subject.clear(); assert.equalBuffers(subject.join(), []); }); test('writing more', function() { var joinedResult = subject.addCString("!").addInt32(9).addInt16(2).join(); assert.equalBuffers(joinedResult, [33, 0, 0, 0, 0, 9, 0, 2]); }); test('returns result', function() { var flushedResult = subject.flush(); assert.equalBuffers(flushedResult, [33, 0, 0, 0, 0, 9, 0, 2]) }); test('clears the writer', function() { assert.equalBuffers(subject.join(), []) assert.equalBuffers(subject.flush(), []) }); });
test('writes multiple cstrings', function() { var subject = new Writer(); var result = subject.addCString("!").addCString("!").join(); assert.equalBuffers(result, [33, 0, 33, 0]); })
test('resizes if reached end', function() { var subject = new Writer(3); var result = subject.addCString("!!!").join(); assert.equalBuffers(result, [33, 33, 33, 0]); })
test('writes non-empty cstring', function() { var subject = new Writer(); var result = subject.addCString("!!!").join(); assert.equalBuffers(result, [33, 33, 33, 0]); })
test('writes two empty cstrings', function() { var subject = new Writer(); var result = subject.addCString("").addCString("").join(); assert.equalBuffers(result, [0, 0]) })
test('added as a hex code to a full writer', function() { var subject = new Writer(2); var result = subject.addCString("!").flush(0x50) assert.equalBuffers(result, [0x50, 0, 0, 0, 6, 33, 0]); })
test("resizing to much larger", function() { var subject = new Writer(2); var string = "!!!!!!!!"; var result = subject.addCString(string).flush(); assert.equalBuffers(result, [33, 33, 33, 33, 33, 33, 33, 33, 0]) })
test('can add arbitrary buffer to the end', function() { var subject = new Writer(4); subject.addCString("!!!") var result = subject.add(Buffer("@@@")).join(); assert.equalBuffers(result, [33, 33, 33, 0, 0x40, 0x40, 0x40]); })
test('writes empty cstring', function() { var subject = new Writer(); var result = subject.addCString().join(); assert.equalBuffers(result, [0]); });
test('writing more', function() { var joinedResult = subject.addCString("!").addInt32(9).addInt16(2).join(); assert.equalBuffers(joinedResult, [33, 0, 0, 0, 0, 9, 0, 2]); });