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
159160function 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