summaryrefslogtreecommitdiff
path: root/cli/js/files_test.ts
diff options
context:
space:
mode:
authorbartOssh <blenart@eagleeyenetworks.com>2020-03-02 17:44:46 +0100
committerGitHub <noreply@github.com>2020-03-02 11:44:46 -0500
commit4a47ffa5c455be213523cb1a7211a0a454b5fcf8 (patch)
tree6359cf99d149461ae1435481cc0c919ee357b36e /cli/js/files_test.ts
parentc14cc84a85224870d2188df1a3741bf02eb8c5da (diff)
seek should return cursor position (#4211)
Diffstat (limited to 'cli/js/files_test.ts')
-rw-r--r--cli/js/files_test.ts47
1 files changed, 40 insertions, 7 deletions
diff --git a/cli/js/files_test.ts b/cli/js/files_test.ts
index 7e3479798..81c379020 100644
--- a/cli/js/files_test.ts
+++ b/cli/js/files_test.ts
@@ -280,6 +280,7 @@ testPerm(
const data = encoder.encode("Hello world!\n");
const file = await Deno.open(filename, "w+");
+ const seekPosition = 0;
// assert file was created
let fileInfo = Deno.statSync(filename);
assert(fileInfo.isFile());
@@ -290,7 +291,12 @@ testPerm(
assertEquals(fileInfo.len, 13);
const buf = new Uint8Array(20);
- await file.seek(0, Deno.SeekMode.SEEK_START);
+ // seeking from beginning of a file
+ const cursorPosition = await file.seek(
+ seekPosition,
+ Deno.SeekMode.SEEK_START
+ );
+ assertEquals(seekPosition, cursorPosition);
const result = await file.read(buf);
assertEquals(result, 13);
file.close();
@@ -302,10 +308,16 @@ testPerm(
testPerm({ read: true }, async function seekStart(): Promise<void> {
const filename = "cli/tests/hello.txt";
const file = await Deno.open(filename);
+ const seekPosition = 6;
// Deliberately move 1 step forward
await file.read(new Uint8Array(1)); // "H"
// Skipping "Hello "
- await file.seek(6, Deno.SeekMode.SEEK_START);
+ // seeking from beginning of a file plus seekPosition
+ const cursorPosition = await file.seek(
+ seekPosition,
+ Deno.SeekMode.SEEK_START
+ );
+ assertEquals(seekPosition, cursorPosition);
const buf = new Uint8Array(6);
await file.read(buf);
const decoded = new TextDecoder().decode(buf);
@@ -316,10 +328,13 @@ testPerm({ read: true }, async function seekStart(): Promise<void> {
testPerm({ read: true }, function seekSyncStart(): void {
const filename = "cli/tests/hello.txt";
const file = Deno.openSync(filename);
+ const seekPosition = 6;
// Deliberately move 1 step forward
file.readSync(new Uint8Array(1)); // "H"
// Skipping "Hello "
- file.seekSync(6, Deno.SeekMode.SEEK_START);
+ // seeking from beginning of a file plus seekPosition
+ const cursorPosition = file.seekSync(seekPosition, Deno.SeekMode.SEEK_START);
+ assertEquals(seekPosition, cursorPosition);
const buf = new Uint8Array(6);
file.readSync(buf);
const decoded = new TextDecoder().decode(buf);
@@ -333,7 +348,13 @@ testPerm({ read: true }, async function seekCurrent(): Promise<void> {
// Deliberately move 1 step forward
await file.read(new Uint8Array(1)); // "H"
// Skipping "ello "
- await file.seek(5, Deno.SeekMode.SEEK_CURRENT);
+ const seekPosition = 5;
+ // seekPosition is relative to current cursor position after read
+ const cursorPosition = await file.seek(
+ seekPosition,
+ Deno.SeekMode.SEEK_CURRENT
+ );
+ assertEquals(seekPosition + 1, cursorPosition);
const buf = new Uint8Array(6);
await file.read(buf);
const decoded = new TextDecoder().decode(buf);
@@ -347,7 +368,13 @@ testPerm({ read: true }, function seekSyncCurrent(): void {
// Deliberately move 1 step forward
file.readSync(new Uint8Array(1)); // "H"
// Skipping "ello "
- file.seekSync(5, Deno.SeekMode.SEEK_CURRENT);
+ const seekPosition = 5;
+ // seekPosition is relative to current cursor position after read
+ const cursorPosition = file.seekSync(
+ seekPosition,
+ Deno.SeekMode.SEEK_CURRENT
+ );
+ assertEquals(seekPosition + 1, cursorPosition);
const buf = new Uint8Array(6);
file.readSync(buf);
const decoded = new TextDecoder().decode(buf);
@@ -358,7 +385,10 @@ testPerm({ read: true }, function seekSyncCurrent(): void {
testPerm({ read: true }, async function seekEnd(): Promise<void> {
const filename = "cli/tests/hello.txt";
const file = await Deno.open(filename);
- await file.seek(-6, Deno.SeekMode.SEEK_END);
+ const seekPosition = -6;
+ // seek from end of file that has 12 chars, 12 - 6 = 6
+ const cursorPosition = await file.seek(seekPosition, Deno.SeekMode.SEEK_END);
+ assertEquals(6, cursorPosition);
const buf = new Uint8Array(6);
await file.read(buf);
const decoded = new TextDecoder().decode(buf);
@@ -369,7 +399,10 @@ testPerm({ read: true }, async function seekEnd(): Promise<void> {
testPerm({ read: true }, function seekSyncEnd(): void {
const filename = "cli/tests/hello.txt";
const file = Deno.openSync(filename);
- file.seekSync(-6, Deno.SeekMode.SEEK_END);
+ const seekPosition = -6;
+ // seek from end of file that has 12 chars, 12 - 6 = 6
+ const cursorPosition = file.seekSync(seekPosition, Deno.SeekMode.SEEK_END);
+ assertEquals(6, cursorPosition);
const buf = new Uint8Array(6);
file.readSync(buf);
const decoded = new TextDecoder().decode(buf);