summaryrefslogtreecommitdiff
path: root/cli/tests/unit/buffer_test.ts
diff options
context:
space:
mode:
authorMarcos Casagrande <marcoscvp90@gmail.com>2020-07-13 06:58:59 +0200
committerGitHub <noreply@github.com>2020-07-13 00:58:59 -0400
commit1a96a96e101140a2c39114710f93ab21c98fca01 (patch)
treefe2166564dfad2c34dbbaec2cd72b51446b1d26f /cli/tests/unit/buffer_test.ts
parent63edeb1c36bfcea278c248e8be92f7dbb75f7671 (diff)
feat(cli): add copy argument to Buffer.bytes (#6697)
Diffstat (limited to 'cli/tests/unit/buffer_test.ts')
-rw-r--r--cli/tests/unit/buffer_test.ts44
1 files changed, 40 insertions, 4 deletions
diff --git a/cli/tests/unit/buffer_test.ts b/cli/tests/unit/buffer_test.ts
index 440dd5495..320dddcc1 100644
--- a/cli/tests/unit/buffer_test.ts
+++ b/cli/tests/unit/buffer_test.ts
@@ -402,14 +402,50 @@ unitTest(function testWriteAllSync(): void {
});
unitTest(function testBufferBytesArrayBufferLength(): void {
- const bytes = new TextEncoder().encode("a");
+ // defaults to copy
+ const args = [{}, { copy: undefined }, undefined, { copy: true }];
+ for (const arg of args) {
+ const bufSize = 64 * 1024;
+ const bytes = new TextEncoder().encode("a".repeat(bufSize));
+ const reader = new Deno.Buffer();
+ Deno.writeAllSync(reader, bytes);
+
+ const writer = new Deno.Buffer();
+ writer.readFromSync(reader);
+ const actualBytes = writer.bytes(arg);
+
+ assertEquals(actualBytes.byteLength, bufSize);
+ assert(actualBytes.buffer !== writer.bytes(arg).buffer);
+ assertEquals(actualBytes.byteLength, actualBytes.buffer.byteLength);
+ }
+});
+
+unitTest(function testBufferBytesCopyFalse(): void {
+ const bufSize = 64 * 1024;
+ const bytes = new TextEncoder().encode("a".repeat(bufSize));
const reader = new Deno.Buffer();
Deno.writeAllSync(reader, bytes);
const writer = new Deno.Buffer();
writer.readFromSync(reader);
- const actualBytes = writer.bytes();
+ const actualBytes = writer.bytes({ copy: false });
+
+ assertEquals(actualBytes.byteLength, bufSize);
+ assertEquals(actualBytes.buffer, writer.bytes({ copy: false }).buffer);
+ assert(actualBytes.buffer.byteLength > actualBytes.byteLength);
+});
+
+unitTest(function testBufferBytesCopyFalseGrowExactBytes(): void {
+ const bufSize = 64 * 1024;
+ const bytes = new TextEncoder().encode("a".repeat(bufSize));
+ const reader = new Deno.Buffer();
+ Deno.writeAllSync(reader, bytes);
+
+ const writer = new Deno.Buffer();
+ writer.grow(bufSize);
+ writer.readFromSync(reader);
+ const actualBytes = writer.bytes({ copy: false });
- assertEquals(bytes.byteLength, 1);
- assertEquals(bytes.byteLength, actualBytes.buffer.byteLength);
+ assertEquals(actualBytes.byteLength, bufSize);
+ assertEquals(actualBytes.buffer.byteLength, actualBytes.byteLength);
});