Skip to content

Commit

Permalink
address new comments
Browse files Browse the repository at this point in the history
Signed-off-by: Chloe Yip <[email protected]>
  • Loading branch information
cyip10 committed Jul 12, 2024
1 parent d51db54 commit 93f77e9
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 11 deletions.
4 changes: 2 additions & 2 deletions node/src/BaseClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1058,8 +1058,8 @@ export class BaseClient {
}

/**
* Sets the list element at index to element.
* The index is zero-based, so 0 means the first element,1 the second element and so on.
* Sets the list element at `index` to `element`.
* The index is zero-based, so 0 means the first element, 1 the second element and so on.
* Negative indices can be used to designate elements starting at the tail of
* the list. Here, -1 means the last element, -2 means the penultimate and so forth.
*
Expand Down
4 changes: 2 additions & 2 deletions node/src/Transaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -583,8 +583,8 @@ export class BaseTransaction<T extends BaseTransaction<T>> {
}

/**
* Sets the list element at index to element.
* The index is zero-based, so 0 means the first element,1 the second element and so on.
* Sets the list element at `index` to `element`.
* The index is zero-based, so 0 means the first element, 1 the second element and so on.
* Negative indices can be used to designate elements starting at the tail of
* the list. Here, -1 means the last element, -2 means the penultimate and so forth.
*
Expand Down
28 changes: 21 additions & 7 deletions node/tests/SharedTests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -874,11 +874,11 @@ export function runBaseTests<Context>(config: {
);

it.each([ProtocolVersion.RESP2, ProtocolVersion.RESP3])(
`lset with nonexisting key`,
`lset test_%p`,
async (protocol) => {
await runTest(async (client: BaseClient) => {
const key = "test_key";
const non_existing_key = "nonexisting";
const nonExistingKey = "nonexisting";
const index = 0;
const oobIndex = 10;
const negativeIndex = -1;
Expand All @@ -890,7 +890,7 @@ export function runBaseTests<Context>(config: {
// key does not exist
try {
expect(
await client.lset(non_existing_key, index, element),
await client.lset(nonExistingKey, index, element),
).toThrow();
} catch (e) {
expect((e as Error).message).toMatch(
Expand All @@ -910,18 +910,32 @@ export function runBaseTests<Context>(config: {
}

// assert lset result
expect(await client.lset(key, index, element)).toEqual("OK");
checkSimple(await client.lset(key, index, element)).toEqual(
"OK",
);
checkSimple(await client.lrange(key, 0, negativeIndex)).toEqual(
expectedList,
);

// assert lset with a negative index for the last element in the list
expect(await client.lset(key, negativeIndex, element)).toEqual(
"OK",
);
checkSimple(
await client.lset(key, negativeIndex, element),
).toEqual("OK");
checkSimple(await client.lrange(key, 0, negativeIndex)).toEqual(
expectedList2,
);

// assert lset against a non-list key
const nonListKey = "nonListKey";
expect(await client.sadd(nonListKey, ["a"]),).toEqual(1);

try {
checkSimple(await client.lset(nonListKey, 0, "b"));
} catch (e) {
expect((e as Error).message).toMatch(
"WRONGTYPE: Operation against a key holding the wrong kind of value",
);
}
}, protocol);
},
config.timeout,
Expand Down

0 comments on commit 93f77e9

Please sign in to comment.