-
Notifications
You must be signed in to change notification settings - Fork 19
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add readSkip
and writeSkip
methods
#48
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -614,26 +614,44 @@ describe('Reading/Writing To/From SmartBuffer', () => { | |
}); | ||
|
||
describe('Skipping around data', () => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just to explain the diff noise in this file: There was already this * Actually, in the case of the |
||
let writer = new SmartBuffer(); | ||
writer.writeStringNT('hello'); | ||
writer.writeUInt16LE(6699); | ||
writer.writeStringNT('world!'); | ||
it('Should skip writing for a given length', () => { | ||
const skipLength = 4; | ||
|
||
it('Should equal the UInt16 that was written above', () => { | ||
let reader = SmartBuffer.fromBuffer(writer.toBuffer()); | ||
reader.readOffset += 6; | ||
assert.strictEqual(reader.readUInt16LE(), 6699); | ||
reader.readOffset = 0; | ||
assert.strictEqual(reader.readStringNT(), 'hello'); | ||
reader.readOffset -= 6; | ||
const writer = new SmartBuffer(); | ||
writer.writeStringNT('hello'); | ||
assert.strictEqual(writer.writeOffset, 6); | ||
writer.writeSkip(skipLength); | ||
assert.strictEqual(writer.writeOffset, 6 + skipLength); | ||
|
||
const reader = SmartBuffer.fromBuffer(writer.toBuffer()); | ||
assert.strictEqual(reader.readStringNT(), 'hello'); | ||
assert.deepEqual(reader.readBuffer(skipLength), Buffer.alloc(skipLength)); | ||
|
||
assert.throws(() => { | ||
writer.writeSkip(-1); | ||
}); | ||
}); | ||
|
||
it('Should throw an error when attempting to skip more bytes than actually exist.', () => { | ||
let reader = SmartBuffer.fromBuffer(writer.toBuffer()); | ||
it('Should skip reading for a given length', () => { | ||
const skipLength = 4; | ||
|
||
const writer = new SmartBuffer(); | ||
writer.writeStringNT('hello'); | ||
writer.writeStringNT('world!'); | ||
|
||
const reader = SmartBuffer.fromBuffer(writer.toBuffer()); | ||
assert.strictEqual(reader.readStringNT(), 'hello'); | ||
assert.strictEqual(reader.readOffset, 6); | ||
reader.readSkip(skipLength); | ||
assert.strictEqual(reader.readOffset, 6 + skipLength); | ||
assert.strictEqual(reader.readStringNT(), 'd!'); | ||
|
||
assert.throws(() => { | ||
reader.readOffset = 10000; | ||
reader.readSkip(-1); | ||
}); | ||
|
||
assert.throws(() => { | ||
reader.readSkip(1000); | ||
}); | ||
}); | ||
}); | ||
|
@@ -652,17 +670,35 @@ describe('Setting write and read offsets', () => { | |
assert.strictEqual(writer.readOffset, 10); | ||
}); | ||
|
||
it('should throw an error when given an offset that is out of bounds', () => { | ||
it('should throw an error when given a read offset that is out of bounds', () => { | ||
assert.throws(() => { | ||
writer.readOffset = -1; | ||
}); | ||
|
||
assert.throws(() => { | ||
writer.readOffset = 1000; | ||
}); | ||
}); | ||
|
||
it('should throw an error when given an offset that is out of bounds', () => { | ||
it('should throw an error when given a write offset that is out of bounds', () => { | ||
assert.throws(() => { | ||
writer.writeOffset = -1; | ||
}); | ||
|
||
assert.throws(() => { | ||
writer.writeOffset = 1000; | ||
}); | ||
}); | ||
|
||
it('should skip around data', () => { | ||
const reader = SmartBuffer.fromBuffer(writer.toBuffer()); | ||
reader.readOffset += 10; | ||
assert.strictEqual(reader.readStringNT(), 'mynameisjosh'); | ||
reader.readOffset = 0; | ||
assert.strictEqual(reader.readString(5), 'hello'); | ||
reader.readOffset -= 5; | ||
assert.strictEqual(reader.readStringNT(), 'hellotheremynameisjosh'); | ||
}); | ||
}); | ||
|
||
describe('Setting encoding', () => { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Originally I had this so that it would be more similar to
writeSkip
:But it did not throw an error if
length
went beyond the bounds of the buffer, so I changed it.Let me know if you would prefer it to not throw an error though.