Skip to content
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

test: add tests for buffer api #438

Merged
merged 1 commit into from May 7, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
30 changes: 24 additions & 6 deletions test/unit/v1.test.js
Expand Up @@ -67,14 +67,16 @@ describe('v1', () => {
);
});

const fullOptions = {
msecs: 1321651533573,
nsecs: 5432,
clockseq: 0x385c,
node: [0x61, 0xcd, 0x3c, 0xbb, 0x32, 0x10],
};

test('explicit options product expected id', () => {
// Verify explicit options produce expected id
const id = v1({
msecs: 1321651533573,
nsecs: 5432,
clockseq: 0x385c,
node: [0x61, 0xcd, 0x3c, 0xbb, 0x32, 0x10],
});
const id = v1(fullOptions);
assert(id === 'd9428888-122b-11e1-b85c-61cd3cbb3210', 'Explicit options produce expected id');
});

Expand All @@ -88,4 +90,20 @@ describe('v1', () => {
const dt = parseInt(after, 16) - parseInt(before, 16);
assert(dt === 1, 'Ids spanning 1ms boundary are 100ns apart');
});

const expectedBytes = [217, 66, 136, 136, 18, 43, 17, 225, 184, 92, 97, 205, 60, 187, 50, 16];

test('fills one UUID into a buffer as expected', () => {
const buffer = [];
const result = v1(fullOptions, buffer);
assert.deepEqual(buffer, expectedBytes);
assert.strictEqual(buffer, result);
});

test('fills two UUIDs into a buffer as expected', () => {
const buffer = [];
v1(fullOptions, buffer, 0);
v1(fullOptions, buffer, 16);
assert.deepEqual(buffer, expectedBytes.concat(expectedBytes));
});
});
35 changes: 8 additions & 27 deletions test/unit/v35.test.js
Expand Up @@ -95,14 +95,10 @@ describe('v5', () => {
0xf6,
];

v3('hello.example.com', v3.DNS, buf);
const result = v3('hello.example.com', v3.DNS, buf);

assert.ok(
buf.length === testBuf.length &&
buf.every(function (elem, idx) {
return elem === testBuf[idx];
}),
);
assert.deepEqual(buf, testBuf);
assert.strictEqual(result, buf);

// test offsets as well
buf = new Array(19);
Expand All @@ -113,13 +109,7 @@ describe('v5', () => {

v3('hello.example.com', v3.DNS, buf, 3);

assert.ok(
buf.length === testBuf.length + 3 &&
buf.every(function (elem, idx) {
return idx >= 3 ? elem === testBuf[idx - 3] : elem === 'landmaster';
}),
'hello',
);
assert.deepEqual(buf, ['landmaster', 'landmaster', 'landmaster'].concat(testBuf));
});

test('v5', () => {
Expand Down Expand Up @@ -153,13 +143,9 @@ describe('v5', () => {
0xec,
];

v5('hello.example.com', v5.DNS, buf);
assert.ok(
buf.length === testBuf.length &&
buf.every(function (elem, idx) {
return elem === testBuf[idx];
}),
);
const result = v5('hello.example.com', v5.DNS, buf);
assert.deepEqual(buf, testBuf);
assert.strictEqual(result, buf);

// test offsets as well
buf = new Array(19);
Expand All @@ -170,12 +156,7 @@ describe('v5', () => {

v5('hello.example.com', v5.DNS, buf, 3);

assert.ok(
buf.length === testBuf.length + 3 &&
buf.every(function (elem, idx) {
return idx >= 3 ? elem === testBuf[idx - 3] : elem === 'landmaster';
}),
);
assert.deepEqual(buf, ['landmaster', 'landmaster', 'landmaster'].concat(testBuf));
});

test('v3/v5 constants', () => {
Expand Down