Skip to content

Commit 9acaeac

Browse files
committed
Move couple tests to ES6 and dummy channel to test scope
1 parent a4f99e6 commit 9acaeac

File tree

5 files changed

+127
-114
lines changed

5 files changed

+127
-114
lines changed

src/v1/internal/ch-dummy.js renamed to test/internal/ch-dummy.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
* limitations under the License.
1818
*/
1919

20-
import {CombinedBuffer} from './buf';
20+
import {CombinedBuffer} from '../../src/v1/internal/buf';
2121

2222
const observer = {
2323
instance: null,

test/internal/chunking.test.js

Lines changed: 43 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -17,17 +17,18 @@
1717
* limitations under the License.
1818
*/
1919

20-
var Chunker = require('../../lib/v1/internal/chunking').Chunker;
21-
var Dechunker = require('../../lib/v1/internal/chunking').Dechunker;
22-
var alloc = require('../../lib/v1/internal/buf').alloc;
23-
var CombinedBuffer = require('../../lib/v1/internal/buf').CombinedBuffer;
24-
var DummyChannel = require('../../lib/v1/internal/ch-dummy.js').channel;
25-
26-
describe('Chunker', function() {
27-
it('should chunk simple data', function() {
20+
import {Chunker, Dechunker} from '../../src/v1/internal/chunking';
21+
import {alloc} from '../../src/v1/internal/buf';
22+
import {channel} from './ch-dummy';
23+
24+
const DummyChannel = channel;
25+
26+
describe('Chunker', () => {
27+
28+
it('should chunk simple data', () => {
2829
// Given
29-
var ch = new DummyChannel();
30-
var chunker = new Chunker(ch);
30+
const ch = new DummyChannel();
31+
const chunker = new Chunker(ch);
3132

3233
// When
3334
chunker.writeInt32(1);
@@ -37,10 +38,11 @@ describe('Chunker', function() {
3738
// Then
3839
expect(ch.toHex()).toBe('00 08 00 00 00 01 00 00 00 02');
3940
});
40-
it('should chunk blobs larger than the output buffer', function() {
41+
42+
it('should chunk blobs larger than the output buffer', () => {
4143
// Given
42-
var ch = new DummyChannel();
43-
var chunker = new Chunker(ch, 4);
44+
const ch = new DummyChannel();
45+
const chunker = new Chunker(ch, 4);
4446

4547
// When
4648
chunker.writeBytes(bytes( 1,2,3,4,5,6 ));
@@ -49,10 +51,11 @@ describe('Chunker', function() {
4951
// Then
5052
expect(ch.toHex()).toBe('00 02 01 02 00 02 03 04 00 02 05 06');
5153
});
52-
it('should include message boundaries', function() {
54+
55+
it('should include message boundaries', () => {
5356
// Given
54-
var ch = new DummyChannel();
55-
var chunker = new Chunker(ch);
57+
const ch = new DummyChannel();
58+
const chunker = new Chunker(ch);
5659

5760
// When
5861
chunker.writeInt32(1);
@@ -65,13 +68,16 @@ describe('Chunker', function() {
6568
});
6669
});
6770

68-
describe('Dechunker', function() {
69-
it('should unchunk a simple message', function() {
71+
describe('Dechunker', () => {
72+
73+
it('should unchunk a simple message', () => {
7074
// Given
71-
var messages = [];
72-
var dechunker = new Dechunker();
73-
var chunker = new Chunker(dechunker);
74-
dechunker.onmessage = function(buffer) { messages.push(buffer); };
75+
const messages = [];
76+
const dechunker = new Dechunker();
77+
const chunker = new Chunker(dechunker);
78+
dechunker.onmessage = buffer => {
79+
messages.push(buffer);
80+
};
7581

7682
// When
7783
chunker.writeInt16(1);
@@ -86,10 +92,10 @@ describe('Dechunker', function() {
8692
expect(messages[0].toHex()).toBe('00 01 00 02 00 03');
8793
});
8894

89-
it('should handle message split at any point', function() {
95+
it('should handle message split at any point', () => {
9096
// Given
91-
var ch = new DummyChannel();
92-
var chunker = new Chunker(ch);
97+
const ch = new DummyChannel();
98+
const chunker = new Chunker(ch);
9399

94100
// And given the following message
95101
chunker.writeInt8(1);
@@ -100,31 +106,33 @@ describe('Dechunker', function() {
100106
chunker.messageBoundary();
101107
chunker.flush();
102108

103-
var chunked = ch.toBuffer();
109+
const chunked = ch.toBuffer();
104110

105111
// When I try splitting this chunked data at every possible position
106112
// into two separate buffers, and send those to the dechunker
107-
for (var i = 1; i < chunked.length; i++) {
108-
var slice1 = chunked.getSlice( 0, i );
109-
var slice2 = chunked.getSlice( i, chunked.length - i );
113+
for (let i = 1; i < chunked.length; i++) {
114+
const slice1 = chunked.getSlice(0, i);
115+
const slice2 = chunked.getSlice(i, chunked.length - i);
110116

111117
// Dechunk the slices
112-
var messages = [];
113-
var dechunker = new Dechunker();
114-
dechunker.onmessage = function(buffer) { messages.push(buffer); };
118+
const messages = [];
119+
const dechunker = new Dechunker();
120+
dechunker.onmessage = buffer => {
121+
messages.push(buffer);
122+
};
115123
dechunker.write( slice1 );
116124
dechunker.write( slice2 );
117125

118126
// Then, the output should be correct
119127
expect( messages.length ).toBe( 1 );
120128
expect(messages[0].toHex()).toBe('01 00 02 00 00 00 03 04 00 00 00 05');
121-
};
129+
}
122130
});
123131
});
124132

125133
function bytes() {
126-
var b = alloc( arguments.length );
127-
for( var i=0; i<arguments.length; i++ ) {
134+
const b = alloc(arguments.length);
135+
for (let i = 0; i < arguments.length; i++) {
128136
b.writeUInt8( arguments[i] );
129137
}
130138
b.position = 0;

test/internal/connection.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
* limitations under the License.
1818
*/
1919

20-
import * as DummyChannel from '../../src/v1/internal/ch-dummy';
20+
import * as DummyChannel from './ch-dummy';
2121
import Connection from '../../src/v1/internal/connection';
2222
import {Packer} from '../../src/v1/internal/packstream-v1';
2323
import {Chunker} from '../../src/v1/internal/chunking';

test/internal/utf8.test.js

Lines changed: 55 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -16,107 +16,108 @@
1616
* See the License for the specific language governing permissions and
1717
* limitations under the License.
1818
*/
19-
20-
var utf8 = require('../../lib/v1/internal/utf8').default;
21-
var buffers = require('../../lib/v1/internal/buf');
2219

23-
describe('utf8', function() {
24-
it('should have a nice clean buffer position after serializing', function() {
20+
import {alloc, CombinedBuffer} from '../../src/v1/internal/buf';
21+
import utf8 from '../../src/v1/internal/utf8';
22+
23+
describe('utf8', () => {
24+
25+
it('should have a nice clean buffer position after serializing', () => {
2526
// When
26-
var buffer = utf8.encode("hello, world!");
27+
const buffer = utf8.encode('hello, world!');
2728

2829
// Then
2930
expect( buffer.position ).toBe( 0 );
3031
});
3132

32-
it('should respect position of single buffer', function() {
33+
it('should respect position of single buffer', () => {
3334
// When
34-
var buffer = utf8.encode("hello, world!");
35+
const buffer = utf8.encode('hello, world!');
3536
buffer.readInt8();
36-
var decoded = utf8.decode(buffer, buffer.length - 1);
37+
const decoded = utf8.decode(buffer, buffer.length - 1);
3738
// Then
3839
expect( decoded ).toBe( "ello, world!" );
3940
expect(buffer.position).toEqual(13)
4041
});
4142

4243

43-
it('should be able to decode substring', function() {
44+
it('should be able to decode substring', () => {
4445
// When
45-
var buffer = utf8.encode("hello, world!");
46+
const buffer = utf8.encode('hello, world!');
4647
buffer.readInt8();
47-
var decoded = utf8.decode(buffer, 3);
48+
const decoded = utf8.decode(buffer, 3);
4849
// Then
4950
expect( decoded ).toBe( "ell" );
5051
expect(buffer.position).toEqual(4)
5152
});
5253

53-
it('should read/write utf8', function() {
54+
it('should read/write utf8', () => {
5455
expect( packAndUnpack( "" ) ).toBe( "" );
5556
expect( packAndUnpack( "åäö123" ) ).toBe( "åäö123" );
5657
});
5758

58-
it('should decode utf8 from a complete combined buffer', function() {
59+
it('should decode utf8 from a complete combined buffer', () => {
5960
// Given
60-
var msg = "asåfqwer";
61-
var buf = utf8.encode(msg);
62-
var bufa = buf.readSlice(3);
63-
var bufb = buf.readSlice(3);
64-
var bufc = buf.readSlice(3);
65-
var combined = new buffers.CombinedBuffer( [bufa, bufb, bufc] );
61+
const msg = 'asåfqwer';
62+
const buf = utf8.encode(msg);
63+
const bufa = buf.readSlice(3);
64+
const bufb = buf.readSlice(3);
65+
const bufc = buf.readSlice(3);
66+
const combined = new CombinedBuffer([bufa, bufb, bufc]);
6667

6768
// When
68-
var decoded = utf8.decode(combined, combined.length);
69+
const decoded = utf8.decode(combined, combined.length);
6970

7071
// Then
7172
expect(decoded).toBe(msg);
7273
});
7374

74-
it('should decode utf8 from part of a combined buffer', function() {
75+
it('should decode utf8 from part of a combined buffer', () => {
7576
// Given
76-
var msg = "asåfq";
77-
var expectMsg = msg.substring(0, msg.length-1);
78-
var buf = utf8.encode(msg);
79-
var bufa = buf.readSlice(3);
80-
var bufb = buf.readSlice(3);
81-
var unrelatedData = buffers.alloc(3);
82-
var combined = new buffers.CombinedBuffer( [bufa, bufb, unrelatedData] );
77+
const msg = 'asåfq';
78+
const expectMsg = msg.substring(0, msg.length - 1);
79+
const buf = utf8.encode(msg);
80+
const bufa = buf.readSlice(3);
81+
const bufb = buf.readSlice(3);
82+
const unrelatedData = alloc(3);
83+
const combined = new CombinedBuffer([bufa, bufb, unrelatedData]);
8384

8485
// When
8586
// We read all but the unrelatedData and the last character of bufb
86-
var decoded = utf8.decode(combined, combined.length - 1 - unrelatedData.length );
87+
const decoded = utf8.decode(combined, combined.length - 1 - unrelatedData.length);
8788

8889
// Then
8990
expect(decoded).toBe(expectMsg);
9091
});
9192

92-
it('should respect the position in the combined buffer', function() {
93+
it('should respect the position in the combined buffer', () => {
9394
// Given
94-
var msg = "abcdefgh";
95-
var buf = utf8.encode(msg);
96-
var bufa = buf.readSlice(4);
97-
var bufb = buf.readSlice(4);
98-
var combined = new buffers.CombinedBuffer( [bufa, bufb] );
95+
const msg = 'abcdefgh';
96+
const buf = utf8.encode(msg);
97+
const bufa = buf.readSlice(4);
98+
const bufb = buf.readSlice(4);
99+
const combined = new CombinedBuffer([bufa, bufb]);
99100
//move position forward
100101
combined.readInt8();
101102
combined.readInt8();
102103

103104
// When
104-
var decoded = utf8.decode(combined, combined.length - 2);
105+
const decoded = utf8.decode(combined, combined.length - 2);
105106

106107

107108
// Then
108109
expect(decoded).toEqual("cdefgh");
109110
expect(combined.position).toBe(8)
110111
});
111112

112-
it('should be able to decode a substring in a combined buffer across buffers', function() {
113+
it('should be able to decode a substring in a combined buffer across buffers', () => {
113114
// Given
114-
var msg = "abcdefghijkl";
115-
var buf = utf8.encode(msg);
116-
var bufa = buf.readSlice(4);
117-
var bufb = buf.readSlice(4);
118-
var bufc = buf.readSlice(4);
119-
var combined = new buffers.CombinedBuffer( [bufa, bufb, bufc] );
115+
const msg = 'abcdefghijkl';
116+
const buf = utf8.encode(msg);
117+
const bufa = buf.readSlice(4);
118+
const bufb = buf.readSlice(4);
119+
const bufc = buf.readSlice(4);
120+
const combined = new CombinedBuffer([bufa, bufb, bufc]);
120121
//move position forward
121122
combined.readInt8();
122123
combined.readInt8();
@@ -125,21 +126,21 @@ describe('utf8', function() {
125126
combined.readInt8();
126127

127128
// When
128-
var decoded = utf8.decode(combined, 4);
129+
const decoded = utf8.decode(combined, 4);
129130

130131
// Then
131132
expect(decoded).toBe("fghi");
132133
expect(combined.position).toBe(9)
133134
});
134135

135-
it('should be able to decode a substring in a combined within buffer', function() {
136+
it('should be able to decode a substring in a combined within buffer', () => {
136137
// Given
137-
var msg = "abcdefghijkl";
138-
var buf = utf8.encode(msg);
139-
var bufa = buf.readSlice(4);
140-
var bufb = buf.readSlice(4);
141-
var bufc = buf.readSlice(4);
142-
var combined = new buffers.CombinedBuffer( [bufa, bufb, bufc] );
138+
const msg = 'abcdefghijkl';
139+
const buf = utf8.encode(msg);
140+
const bufa = buf.readSlice(4);
141+
const bufb = buf.readSlice(4);
142+
const bufc = buf.readSlice(4);
143+
const combined = new CombinedBuffer([bufa, bufb, bufc]);
143144
//move position forward
144145
combined.readInt8();
145146
combined.readInt8();
@@ -148,7 +149,7 @@ describe('utf8', function() {
148149
combined.readInt8();
149150

150151
// When
151-
var decoded = utf8.decode(combined, 2);
152+
const decoded = utf8.decode(combined, 2);
152153

153154
// Then
154155
expect(decoded).toBe("fg");
@@ -157,6 +158,6 @@ describe('utf8', function() {
157158
});
158159

159160
function packAndUnpack( str ) {
160-
var buffer = utf8.encode( str );
161+
const buffer = utf8.encode(str);
161162
return utf8.decode( buffer, buffer.length );
162163
}

0 commit comments

Comments
 (0)