@@ -31,7 +31,7 @@ describe("BinaryWriter", () => {
31
31
expect ( user . firstName ) . toBe ( "Homer" ) ;
32
32
expect ( user . active ) . toBe ( true ) ;
33
33
} ) ;
34
- describe ( "float32" , ( ) => {
34
+ describe ( "float32() " , ( ) => {
35
35
it . each ( [
36
36
1024 ,
37
37
3.142 ,
@@ -41,13 +41,20 @@ describe("BinaryWriter", () => {
41
41
Number . NEGATIVE_INFINITY ,
42
42
Number . NaN ,
43
43
] ) ( "should encode %s" , ( val ) => {
44
- expect ( new BinaryWriter ( ) . float ( val ) . finish ( ) . length ) . toBeGreaterThan ( 0 ) ;
44
+ const bytes = new BinaryWriter ( ) . float ( val ) . finish ( ) ;
45
+ expect ( bytes . length ) . toBeGreaterThan ( 0 ) ;
46
+ // @ts -expect-error test string
47
+ const bytesStr = new BinaryWriter ( ) . float ( val . toString ( ) ) . finish ( ) ;
48
+ expect ( bytesStr . length ) . toBeGreaterThan ( 0 ) ;
49
+ expect ( bytesStr ) . toStrictEqual ( bytes ) ;
45
50
} ) ;
46
51
it . each ( [
47
- { val : "123" , err : / ^ i n v a l i d f l o a t 3 2 : s t r i n g / } ,
48
- { val : true , err : / ^ i n v a l i d f l o a t 3 2 : b o o l / } ,
52
+ { val : null , err : "invalid float32: object" } ,
53
+ { val : new Date ( ) , err : "invalid float32: object" } ,
54
+ { val : undefined , err : "invalid float32: undefined" } ,
55
+ { val : true , err : "invalid float32: bool" } ,
49
56
] ) ( "should error for wrong type $val" , ( { val, err } ) => {
50
- // @ts -expect-error TS2345
57
+ // @ts -expect-error test wrong type
51
58
expect ( ( ) => new BinaryWriter ( ) . float ( val ) ) . toThrow ( err ) ;
52
59
} ) ;
53
60
it . each ( [ Number . MAX_VALUE , - Number . MAX_VALUE ] ) (
@@ -56,40 +63,76 @@ describe("BinaryWriter", () => {
56
63
expect ( ( ) => new BinaryWriter ( ) . float ( val ) ) . toThrow (
57
64
/ ^ i n v a l i d f l o a t 3 2 : .* / ,
58
65
) ;
66
+ // @ts -expect-error test string
67
+ expect ( ( ) => new BinaryWriter ( ) . float ( val . toString ( ) ) ) . toThrow (
68
+ / ^ i n v a l i d f l o a t 3 2 : .* / ,
69
+ ) ;
59
70
} ,
60
71
) ;
61
72
} ) ;
62
- describe ( "int32" , ( ) => {
73
+ // sfixed32, sint32, and int32 are signed 32-bit integers, just with different encoding
74
+ describe . each ( [ "sfixed32" , "sint32" , "int32" ] as const ) ( "%s()" , ( type ) => {
63
75
it . each ( [ - 0x80000000 , 1024 , 0x7fffffff ] ) ( "should encode %s" , ( val ) => {
64
- expect ( new BinaryWriter ( ) . int32 ( val ) . finish ( ) . length ) . toBeGreaterThan ( 0 ) ;
76
+ const bytes = new BinaryWriter ( ) [ type ] ( val ) . finish ( ) ;
77
+ expect ( bytes . length ) . toBeGreaterThan ( 0 ) ;
78
+ // @ts -expect-error test string
79
+ const bytesStr = new BinaryWriter ( ) [ type ] ( val . toString ( ) ) . finish ( ) ;
80
+ expect ( bytesStr . length ) . toBeGreaterThan ( 0 ) ;
81
+ expect ( bytesStr ) . toStrictEqual ( bytes ) ;
65
82
} ) ;
66
83
it . each ( [
67
- { val : "123" , err : / ^ i n v a l i d i n t 3 2 : s t r i n g / } ,
68
- { val : true , err : / ^ i n v a l i d i n t 3 2 : b o o l / } ,
84
+ { val : null , err : "invalid int32: object" } ,
85
+ { val : new Date ( ) , err : "invalid int32: object" } ,
86
+ { val : undefined , err : "invalid int32: undefined" } ,
87
+ { val : true , err : "invalid int32: bool" } ,
69
88
] ) ( "should error for wrong type $val" , ( { val, err } ) => {
70
89
// @ts -expect-error TS2345
71
- expect ( ( ) => new BinaryWriter ( ) . int32 ( val ) ) . toThrow ( err ) ;
90
+ expect ( ( ) => new BinaryWriter ( ) [ type ] ( val ) ) . toThrow ( err ) ;
72
91
} ) ;
73
- it . each ( [ 0x7fffffff + 1 , - 0x80000000 - 1 ] ) (
92
+ it . each ( [ 0x7fffffff + 1 , - 0x80000000 - 1 , 3.142 ] ) (
74
93
"should error for value out of range %s" ,
75
94
( val ) => {
76
- expect ( ( ) => new BinaryWriter ( ) . int32 ( val ) ) . toThrow (
95
+ expect ( ( ) => new BinaryWriter ( ) [ type ] ( val ) ) . toThrow (
96
+ / ^ i n v a l i d i n t 3 2 : .* / ,
97
+ ) ;
98
+ // @ts -expect-error test string
99
+ expect ( ( ) => new BinaryWriter ( ) [ type ] ( val . toString ( ) ) ) . toThrow (
77
100
/ ^ i n v a l i d i n t 3 2 : .* / ,
78
101
) ;
79
102
} ,
80
103
) ;
81
104
} ) ;
82
- describe ( "uint32" , ( ) => {
105
+ // fixed32 and uint32 are unsigned 32-bit integers, just with different encoding
106
+ describe . each ( [ "fixed32" , "uint32" ] as const ) ( "%s()" , ( type ) => {
83
107
it . each ( [ 0 , 1024 , 0xffffffff ] ) ( "should encode %s" , ( val ) => {
84
- expect ( new BinaryWriter ( ) . uint32 ( val ) . finish ( ) . length ) . toBeGreaterThan ( 0 ) ;
108
+ const bytes = new BinaryWriter ( ) [ type ] ( val ) . finish ( ) ;
109
+ expect ( bytes . length ) . toBeGreaterThan ( 0 ) ;
110
+ // @ts -expect-error test string
111
+ const bytesStr = new BinaryWriter ( ) [ type ] ( val . toString ( ) ) . finish ( ) ;
112
+ expect ( bytesStr . length ) . toBeGreaterThan ( 0 ) ;
113
+ expect ( bytesStr ) . toStrictEqual ( bytes ) ;
85
114
} ) ;
86
115
it . each ( [
87
- { val : "123" , err : / ^ i n v a l i d u i n t 3 2 : s t r i n g / } ,
88
- { val : true , err : / ^ i n v a l i d u i n t 3 2 : b o o l / } ,
89
- ] ) ( "should error for wrong type$val" , ( { val, err } ) => {
116
+ { val : null , err : `invalid uint32: object` } ,
117
+ { val : new Date ( ) , err : `invalid uint32: object` } ,
118
+ { val : undefined , err : `invalid uint32: undefined` } ,
119
+ { val : true , err : `invalid uint32: bool` } ,
120
+ ] ) ( "should error for wrong type $val" , ( { val, err } ) => {
90
121
// @ts -expect-error TS2345
91
- expect ( ( ) => new BinaryWriter ( ) . uint32 ( val ) ) . toThrow ( err ) ;
122
+ expect ( ( ) => new BinaryWriter ( ) [ type ] ( val ) ) . toThrow ( err ) ;
92
123
} ) ;
124
+ it . each ( [ 0xffffffff + 1 , - 1 , 3.142 ] ) (
125
+ "should error for value out of range %s" ,
126
+ ( val ) => {
127
+ expect ( ( ) => new BinaryWriter ( ) [ type ] ( val ) ) . toThrow (
128
+ / ^ i n v a l i d u i n t 3 2 : .* / ,
129
+ ) ;
130
+ // @ts -expect-error test string
131
+ expect ( ( ) => new BinaryWriter ( ) [ type ] ( val . toString ( ) ) ) . toThrow (
132
+ / ^ i n v a l i d u i n t 3 2 : .* / ,
133
+ ) ;
134
+ } ,
135
+ ) ;
93
136
} ) ;
94
137
} ) ;
95
138
0 commit comments