Skip to content

Commit b222704

Browse files
committed
test: add tests for buffer api
* Simplify the v3/v5 tests for the buffer api * Add test cases to v1/v3/v5 to ensure that if passing a buffer as second parameter that buffer is not only filled but also returned. For v4 this has already been added in #435.
1 parent bf4af0d commit b222704

File tree

2 files changed

+32
-33
lines changed

2 files changed

+32
-33
lines changed

test/unit/v1.test.js

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -67,14 +67,16 @@ describe('v1', () => {
6767
);
6868
});
6969

70+
const fullOptions = {
71+
msecs: 1321651533573,
72+
nsecs: 5432,
73+
clockseq: 0x385c,
74+
node: [0x61, 0xcd, 0x3c, 0xbb, 0x32, 0x10],
75+
};
76+
7077
test('explicit options product expected id', () => {
7178
// Verify explicit options produce expected id
72-
const id = v1({
73-
msecs: 1321651533573,
74-
nsecs: 5432,
75-
clockseq: 0x385c,
76-
node: [0x61, 0xcd, 0x3c, 0xbb, 0x32, 0x10],
77-
});
79+
const id = v1(fullOptions);
7880
assert(id === 'd9428888-122b-11e1-b85c-61cd3cbb3210', 'Explicit options produce expected id');
7981
});
8082

@@ -88,4 +90,20 @@ describe('v1', () => {
8890
const dt = parseInt(after, 16) - parseInt(before, 16);
8991
assert(dt === 1, 'Ids spanning 1ms boundary are 100ns apart');
9092
});
93+
94+
const expectedBytes = [217, 66, 136, 136, 18, 43, 17, 225, 184, 92, 97, 205, 60, 187, 50, 16];
95+
96+
test('fills one UUID into a buffer as expected', () => {
97+
const buffer = [];
98+
const result = v1(fullOptions, buffer);
99+
assert.deepEqual(buffer, expectedBytes);
100+
assert.strictEqual(buffer, result);
101+
});
102+
103+
test('fills two UUIDs into a buffer as expected', () => {
104+
const buffer = [];
105+
v1(fullOptions, buffer, 0);
106+
v1(fullOptions, buffer, 16);
107+
assert.deepEqual(buffer, expectedBytes.concat(expectedBytes));
108+
});
91109
});

test/unit/v35.test.js

Lines changed: 8 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -95,14 +95,10 @@ describe('v5', () => {
9595
0xf6,
9696
];
9797

98-
v3('hello.example.com', v3.DNS, buf);
98+
const result = v3('hello.example.com', v3.DNS, buf);
9999

100-
assert.ok(
101-
buf.length === testBuf.length &&
102-
buf.every(function (elem, idx) {
103-
return elem === testBuf[idx];
104-
}),
105-
);
100+
assert.deepEqual(buf, testBuf);
101+
assert.strictEqual(result, buf);
106102

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

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

116-
assert.ok(
117-
buf.length === testBuf.length + 3 &&
118-
buf.every(function (elem, idx) {
119-
return idx >= 3 ? elem === testBuf[idx - 3] : elem === 'landmaster';
120-
}),
121-
'hello',
122-
);
112+
assert.deepEqual(buf, ['landmaster', 'landmaster', 'landmaster'].concat(testBuf));
123113
});
124114

125115
test('v5', () => {
@@ -153,13 +143,9 @@ describe('v5', () => {
153143
0xec,
154144
];
155145

156-
v5('hello.example.com', v5.DNS, buf);
157-
assert.ok(
158-
buf.length === testBuf.length &&
159-
buf.every(function (elem, idx) {
160-
return elem === testBuf[idx];
161-
}),
162-
);
146+
const result = v5('hello.example.com', v5.DNS, buf);
147+
assert.deepEqual(buf, testBuf);
148+
assert.strictEqual(result, buf);
163149

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

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

173-
assert.ok(
174-
buf.length === testBuf.length + 3 &&
175-
buf.every(function (elem, idx) {
176-
return idx >= 3 ? elem === testBuf[idx - 3] : elem === 'landmaster';
177-
}),
178-
);
159+
assert.deepEqual(buf, ['landmaster', 'landmaster', 'landmaster'].concat(testBuf));
179160
});
180161

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

0 commit comments

Comments
 (0)