17
17
* limitations under the License.
18
18
*/
19
19
20
- var alloc = require ( '../../lib/v1/internal/buf' ) . alloc ;
21
- var CombinedBuffer = require ( '../../lib/v1/internal/buf' ) . CombinedBuffer ;
22
- var utf8 = require ( '../../lib/v1/internal/utf8' ) . default ;
23
- var Unpacker = require ( "../../lib/v1/internal/packstream.js" ) . Unpacker ;
20
+ import { Unpacker } from '../../src/v1/internal/packstream-v1' ;
21
+ import utf8 from '../../src/v1/internal/utf8' ;
22
+ import { alloc , CombinedBuffer } from '../../src/v1/internal/buf' ;
24
23
25
- describe ( 'buffers' , function ( ) {
26
- it ( 'should have helpful toString' , function ( ) {
24
+ describe ( 'buffers' , ( ) => {
25
+
26
+ it ( 'should have helpful toString' , ( ) => {
27
27
// Given
28
- var b = alloc ( 4 ) ;
28
+ const b = alloc ( 4 ) ;
29
29
b . writeInt8 ( 1 ) ;
30
30
b . writeInt8 ( 8 ) ;
31
31
b . writeInt8 ( 15 ) ;
32
32
b . writeInt8 ( 127 ) ;
33
33
34
34
// When
35
- var str = b . toString ( ) ;
36
- var hex = b . toHex ( ) ;
35
+ const str = b . toString ( ) ;
36
+ const hex = b . toHex ( ) ;
37
37
38
38
// Then
39
- expect ( str ) . toContain ( " ( position=4 )\n 01 08 0f 7f" ) ;
40
- expect ( hex ) . toBe ( " 01 08 0f 7f " ) ;
39
+ expect ( str ) . toContain ( ' ( position=4 )\n 01 08 0f 7f' ) ;
40
+ expect ( hex ) . toBe ( ' 01 08 0f 7f ' ) ;
41
41
} ) ;
42
42
43
- it ( 'should read and write 8-bit unsigned integers' , function ( ) {
43
+ it ( 'should read and write 8-bit unsigned integers' , ( ) => {
44
44
// Given
45
- var b = alloc ( 1 ) ;
45
+ const b = alloc ( 1 ) ;
46
46
47
- for ( var i = 0 ; i < 7 ; i ++ ) {
48
- var n = Math . pow ( 2 , i ) ;
47
+ for ( let i = 0 ; i < 7 ; i ++ ) {
48
+ const n = Math . pow ( 2 , i ) ;
49
49
50
50
// When
51
- b . putUInt8 ( 0 , n ) ;
51
+ b . putUInt8 ( 0 , n ) ;
52
52
53
53
// Then
54
- expect ( b . getUInt8 ( 0 ) ) . toBe ( n ) ;
54
+ expect ( b . getUInt8 ( 0 ) ) . toBe ( n ) ;
55
55
}
56
56
} ) ;
57
57
58
- it ( 'should read and write 16-bit unsigned integers' , function ( ) {
58
+ it ( 'should read and write 16-bit unsigned integers' , ( ) => {
59
59
// Given
60
- var b = alloc ( 2 ) ;
60
+ const b = alloc ( 2 ) ;
61
61
62
- for ( var i = 0 ; i < 15 ; i ++ ) {
63
- var n = Math . pow ( 2 , i ) ;
62
+ for ( let i = 0 ; i < 15 ; i ++ ) {
63
+ const n = Math . pow ( 2 , i ) ;
64
64
65
65
// When
66
- b . putUInt16 ( 0 , n ) ;
66
+ b . putUInt16 ( 0 , n ) ;
67
67
68
68
// Then
69
- expect ( b . getUInt16 ( 0 ) ) . toBe ( n ) ;
69
+ expect ( b . getUInt16 ( 0 ) ) . toBe ( n ) ;
70
70
}
71
71
} ) ;
72
72
73
- it ( 'should read and write 32-bit unsigned integers' , function ( ) {
73
+ it ( 'should read and write 32-bit unsigned integers' , ( ) => {
74
74
// Given
75
- var b = alloc ( 4 ) ;
75
+ const b = alloc ( 4 ) ;
76
76
77
- for ( var i = 0 ; i < 30 ; i ++ ) {
78
- var n = Math . pow ( 2 , i ) ;
77
+ for ( let i = 0 ; i < 30 ; i ++ ) {
78
+ const n = Math . pow ( 2 , i ) ;
79
79
80
80
// When
81
- b . putUInt32 ( 0 , n ) ;
81
+ b . putUInt32 ( 0 , n ) ;
82
82
83
83
// Then
84
- expect ( b . getUInt32 ( 0 ) ) . toBe ( n ) ;
84
+ expect ( b . getUInt32 ( 0 ) ) . toBe ( n ) ;
85
85
}
86
86
} ) ;
87
87
88
- it ( 'should read and write 8-bit signed integers' , function ( ) {
88
+ it ( 'should read and write 8-bit signed integers' , ( ) => {
89
89
// Given
90
- var b = alloc ( 1 ) ;
90
+ const b = alloc ( 1 ) ;
91
91
92
- for ( var i = 0 ; i < 6 ; i ++ ) {
93
- var n = Math . pow ( 2 , i ) ;
92
+ for ( let i = 0 ; i < 6 ; i ++ ) {
93
+ const n = Math . pow ( 2 , i ) ;
94
94
95
95
// When
96
- b . putInt8 ( 0 , n ) ;
96
+ b . putInt8 ( 0 , n ) ;
97
97
98
98
// Then
99
- expect ( b . getInt8 ( 0 ) ) . toBe ( n ) ;
99
+ expect ( b . getInt8 ( 0 ) ) . toBe ( n ) ;
100
100
}
101
101
} ) ;
102
102
103
- it ( 'should read and write 16-bit signed integers' , function ( ) {
103
+ it ( 'should read and write 16-bit signed integers' , ( ) => {
104
104
// Given
105
- var b = alloc ( 2 ) ;
105
+ const b = alloc ( 2 ) ;
106
106
107
- for ( var i = 0 ; i < 14 ; i ++ ) {
108
- var n = Math . pow ( 2 , i ) ;
107
+ for ( let i = 0 ; i < 14 ; i ++ ) {
108
+ const n = Math . pow ( 2 , i ) ;
109
109
110
110
// When
111
- b . putInt16 ( 0 , n ) ;
111
+ b . putInt16 ( 0 , n ) ;
112
112
113
113
// Then
114
- expect ( b . getInt16 ( 0 ) ) . toBe ( n ) ;
114
+ expect ( b . getInt16 ( 0 ) ) . toBe ( n ) ;
115
115
}
116
116
} ) ;
117
117
118
- it ( 'should read and write 32-bit signed integers' , function ( ) {
118
+ it ( 'should read and write 32-bit signed integers' , ( ) => {
119
119
// Given
120
- var b = alloc ( 4 ) ;
120
+ const b = alloc ( 4 ) ;
121
121
122
- for ( var i = 0 ; i < 30 ; i ++ ) {
123
- var n = Math . pow ( 2 , i ) ;
122
+ for ( let i = 0 ; i < 30 ; i ++ ) {
123
+ const n = Math . pow ( 2 , i ) ;
124
124
125
125
// When
126
- b . putInt32 ( 0 , n ) ;
126
+ b . putInt32 ( 0 , n ) ;
127
127
128
128
// Then
129
- expect ( b . getInt32 ( 0 ) ) . toBe ( n ) ;
129
+ expect ( b . getInt32 ( 0 ) ) . toBe ( n ) ;
130
130
}
131
131
} ) ;
132
132
133
- it ( 'should encode list correctly' , function ( ) {
133
+ it ( 'should encode list correctly' , ( ) => {
134
134
// Given
135
- var b = alloc ( 5 ) ;
135
+ let b = alloc ( 5 ) ;
136
136
b . writeUInt8 ( 0x90 | 0x2 ) ;
137
137
b = writeString ( b , 'a' ) ;
138
138
b = writeString ( b , 'b' ) ;
139
139
// When
140
- var hex = b . toHex ( ) ;
140
+ const hex = b . toHex ( ) ;
141
141
// Then
142
- expect ( hex ) . toBe ( " 92 81 61 81 62 " ) ;
142
+ expect ( hex ) . toBe ( ' 92 81 61 81 62 ' ) ;
143
143
} ) ;
144
144
145
- it ( 'should decode list correctly' , function ( ) {
145
+ it ( 'should decode list correctly' , ( ) => {
146
146
// Given
147
- var b = alloc ( 5 ) ;
147
+ const b = alloc ( 5 ) ;
148
148
b . writeUInt8 ( 0x92 ) ;
149
149
b . writeUInt8 ( 0x81 ) ;
150
150
b . writeUInt8 ( 0x61 ) ;
@@ -153,51 +153,52 @@ describe('buffers', function() {
153
153
b . reset ( ) ;
154
154
155
155
// When
156
- var data = new Unpacker ( ) . unpack ( b ) ;
156
+ const data = new Unpacker ( ) . unpack ( b ) ;
157
157
158
158
// Then
159
159
expect ( data [ 0 ] ) . toBe ( 'a' ) ;
160
160
expect ( data [ 1 ] ) . toBe ( 'b' ) ;
161
161
} ) ;
162
162
} ) ;
163
163
164
- describe ( 'CombinedBuffer' , function ( ) {
165
- it ( 'should read int8' , function ( ) {
164
+ describe ( 'CombinedBuffer' , ( ) => {
165
+
166
+ it ( 'should read int8' , ( ) => {
166
167
// Given
167
- var b1 = alloc ( 1 ) ;
168
- var b2 = alloc ( 1 ) ;
168
+ const b1 = alloc ( 1 ) ;
169
+ const b2 = alloc ( 1 ) ;
169
170
b1 . putInt8 ( 0 , 1 ) ;
170
171
b2 . putInt8 ( 0 , 2 ) ;
171
172
172
- var b = new CombinedBuffer ( [ b1 , b2 ] ) ;
173
-
173
+ const b = new CombinedBuffer ( [ b1 , b2 ] ) ;
174
+
174
175
// When
175
- var first = b . readInt8 ( ) ;
176
- var second = b . readInt8 ( ) ;
176
+ const first = b . readInt8 ( ) ;
177
+ const second = b . readInt8 ( ) ;
177
178
178
179
// Then
179
180
expect ( first ) . toBe ( 1 ) ;
180
181
expect ( second ) . toBe ( 2 ) ;
181
182
} ) ;
182
183
183
- it ( 'should read divided float64' , function ( ) {
184
+ it ( 'should read divided float64' , ( ) => {
184
185
// Given
185
- var inner = alloc ( 8 ) ;
186
+ const inner = alloc ( 8 ) ;
186
187
inner . putFloat64 ( 0 , 0.1 ) ;
187
188
188
- var b = new CombinedBuffer ( [ inner . readSlice ( 4 ) , inner . readSlice ( 4 ) ] ) ;
189
-
189
+ const b = new CombinedBuffer ( [ inner . readSlice ( 4 ) , inner . readSlice ( 4 ) ] ) ;
190
+
190
191
// When
191
- var read = b . readFloat64 ( ) ;
192
+ const read = b . readFloat64 ( ) ;
192
193
193
194
// Then
194
195
expect ( read ) . toBe ( 0.1 ) ;
195
196
} ) ;
196
197
} ) ;
197
198
198
199
function writeString ( b , str ) {
199
- var bytes = utf8 . encode ( str ) ;
200
- var size = bytes . length ;
200
+ const bytes = utf8 . encode ( str ) ;
201
+ const size = bytes . length ;
201
202
b . writeUInt8 ( 0x80 | size ) ;
202
203
b . writeBytes ( bytes ) ;
203
204
return b ;
0 commit comments