16
16
* See the License for the specific language governing permissions and
17
17
* limitations under the License.
18
18
*/
19
-
20
- var utf8 = require ( '../../lib/v1/internal/utf8' ) . default ;
21
- var buffers = require ( '../../lib/v1/internal/buf' ) ;
22
19
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' , ( ) => {
25
26
// When
26
- var buffer = utf8 . encode ( " hello, world!" ) ;
27
+ const buffer = utf8 . encode ( ' hello, world!' ) ;
27
28
28
29
// Then
29
30
expect ( buffer . position ) . toBe ( 0 ) ;
30
31
} ) ;
31
32
32
- it ( 'should respect position of single buffer' , function ( ) {
33
+ it ( 'should respect position of single buffer' , ( ) => {
33
34
// When
34
- var buffer = utf8 . encode ( " hello, world!" ) ;
35
+ const buffer = utf8 . encode ( ' hello, world!' ) ;
35
36
buffer . readInt8 ( ) ;
36
- var decoded = utf8 . decode ( buffer , buffer . length - 1 ) ;
37
+ const decoded = utf8 . decode ( buffer , buffer . length - 1 ) ;
37
38
// Then
38
39
expect ( decoded ) . toBe ( "ello, world!" ) ;
39
40
expect ( buffer . position ) . toEqual ( 13 )
40
41
} ) ;
41
42
42
43
43
- it ( 'should be able to decode substring' , function ( ) {
44
+ it ( 'should be able to decode substring' , ( ) => {
44
45
// When
45
- var buffer = utf8 . encode ( " hello, world!" ) ;
46
+ const buffer = utf8 . encode ( ' hello, world!' ) ;
46
47
buffer . readInt8 ( ) ;
47
- var decoded = utf8 . decode ( buffer , 3 ) ;
48
+ const decoded = utf8 . decode ( buffer , 3 ) ;
48
49
// Then
49
50
expect ( decoded ) . toBe ( "ell" ) ;
50
51
expect ( buffer . position ) . toEqual ( 4 )
51
52
} ) ;
52
53
53
- it ( 'should read/write utf8' , function ( ) {
54
+ it ( 'should read/write utf8' , ( ) => {
54
55
expect ( packAndUnpack ( "" ) ) . toBe ( "" ) ;
55
56
expect ( packAndUnpack ( "åäö123" ) ) . toBe ( "åäö123" ) ;
56
57
} ) ;
57
58
58
- it ( 'should decode utf8 from a complete combined buffer' , function ( ) {
59
+ it ( 'should decode utf8 from a complete combined buffer' , ( ) => {
59
60
// 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 ] ) ;
66
67
67
68
// When
68
- var decoded = utf8 . decode ( combined , combined . length ) ;
69
+ const decoded = utf8 . decode ( combined , combined . length ) ;
69
70
70
71
// Then
71
72
expect ( decoded ) . toBe ( msg ) ;
72
73
} ) ;
73
74
74
- it ( 'should decode utf8 from part of a combined buffer' , function ( ) {
75
+ it ( 'should decode utf8 from part of a combined buffer' , ( ) => {
75
76
// 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 ] ) ;
83
84
84
85
// When
85
86
// 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 ) ;
87
88
88
89
// Then
89
90
expect ( decoded ) . toBe ( expectMsg ) ;
90
91
} ) ;
91
92
92
- it ( 'should respect the position in the combined buffer' , function ( ) {
93
+ it ( 'should respect the position in the combined buffer' , ( ) => {
93
94
// 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 ] ) ;
99
100
//move position forward
100
101
combined . readInt8 ( ) ;
101
102
combined . readInt8 ( ) ;
102
103
103
104
// When
104
- var decoded = utf8 . decode ( combined , combined . length - 2 ) ;
105
+ const decoded = utf8 . decode ( combined , combined . length - 2 ) ;
105
106
106
107
107
108
// Then
108
109
expect ( decoded ) . toEqual ( "cdefgh" ) ;
109
110
expect ( combined . position ) . toBe ( 8 )
110
111
} ) ;
111
112
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' , ( ) => {
113
114
// 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 ] ) ;
120
121
//move position forward
121
122
combined . readInt8 ( ) ;
122
123
combined . readInt8 ( ) ;
@@ -125,21 +126,21 @@ describe('utf8', function() {
125
126
combined . readInt8 ( ) ;
126
127
127
128
// When
128
- var decoded = utf8 . decode ( combined , 4 ) ;
129
+ const decoded = utf8 . decode ( combined , 4 ) ;
129
130
130
131
// Then
131
132
expect ( decoded ) . toBe ( "fghi" ) ;
132
133
expect ( combined . position ) . toBe ( 9 )
133
134
} ) ;
134
135
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' , ( ) => {
136
137
// 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 ] ) ;
143
144
//move position forward
144
145
combined . readInt8 ( ) ;
145
146
combined . readInt8 ( ) ;
@@ -148,7 +149,7 @@ describe('utf8', function() {
148
149
combined . readInt8 ( ) ;
149
150
150
151
// When
151
- var decoded = utf8 . decode ( combined , 2 ) ;
152
+ const decoded = utf8 . decode ( combined , 2 ) ;
152
153
153
154
// Then
154
155
expect ( decoded ) . toBe ( "fg" ) ;
@@ -157,6 +158,6 @@ describe('utf8', function() {
157
158
} ) ;
158
159
159
160
function packAndUnpack ( str ) {
160
- var buffer = utf8 . encode ( str ) ;
161
+ const buffer = utf8 . encode ( str ) ;
161
162
return utf8 . decode ( buffer , buffer . length ) ;
162
163
}
0 commit comments