1313// limitations under the License.
1414
1515import { describe , expect , it } from "@jest/globals" ;
16- import { BinaryWriter , WireType } from "@bufbuild/protobuf/wire" ;
17- import { UserDesc } from "../gen/ts/extra/example_pb.js" ;
1816import { fromBinary } from "@bufbuild/protobuf" ;
17+ import { BinaryWriter , BinaryReader , WireType } from "@bufbuild/protobuf/wire" ;
18+ import { UserDesc } from "../gen/ts/extra/example_pb.js" ;
1919
2020describe ( "BinaryWriter example" , ( ) => {
2121 it ( "should work as expected" , ( ) => {
@@ -32,3 +32,78 @@ describe("BinaryWriter example", () => {
3232 expect ( user . active ) . toBe ( true ) ;
3333 } ) ;
3434} ) ;
35+
36+ describe ( "BinaryReader" , ( ) => {
37+ describe ( "skip" , ( ) => {
38+ it ( "should skip group" , ( ) => {
39+ const reader = new BinaryReader (
40+ new BinaryWriter ( )
41+ . tag ( 1 , WireType . StartGroup )
42+ . tag ( 33 , WireType . Varint )
43+ . bool ( true )
44+ . tag ( 1 , WireType . EndGroup )
45+ . finish ( ) ,
46+ ) ;
47+ const [ fieldNo , wireType ] = reader . tag ( ) ;
48+ expect ( fieldNo ) . toBe ( 1 ) ;
49+ expect ( wireType ) . toBe ( WireType . StartGroup ) ;
50+ reader . skip ( WireType . StartGroup , 1 ) ;
51+ expect ( reader . pos ) . toBe ( reader . len ) ;
52+ } ) ;
53+ it ( "should skip nested group" , ( ) => {
54+ const reader = new BinaryReader (
55+ new BinaryWriter ( )
56+ . tag ( 1 , WireType . StartGroup )
57+ . tag ( 1 , WireType . StartGroup )
58+ . tag ( 1 , WireType . EndGroup )
59+ . tag ( 1 , WireType . EndGroup )
60+ . finish ( ) ,
61+ ) ;
62+ const [ fieldNo , wireType ] = reader . tag ( ) ;
63+ expect ( fieldNo ) . toBe ( 1 ) ;
64+ expect ( wireType ) . toBe ( WireType . StartGroup ) ;
65+ reader . skip ( WireType . StartGroup , 1 ) ;
66+ expect ( reader . pos ) . toBe ( reader . len ) ;
67+ } ) ;
68+ it ( "should error on unexpected end group field number" , ( ) => {
69+ const reader = new BinaryReader (
70+ new BinaryWriter ( )
71+ . tag ( 1 , WireType . StartGroup )
72+ . tag ( 2 , WireType . EndGroup )
73+ . finish ( ) ,
74+ ) ;
75+ const [ fieldNo , wireType ] = reader . tag ( ) ;
76+ expect ( fieldNo ) . toBe ( 1 ) ;
77+ expect ( wireType ) . toBe ( WireType . StartGroup ) ;
78+ expect ( ( ) => {
79+ reader . skip ( WireType . StartGroup , 1 ) ;
80+ } ) . toThrow ( / ^ i n v a l i d e n d g r o u p t a g $ / ) ;
81+ } ) ;
82+ it ( "should return skipped group data" , ( ) => {
83+ const reader = new BinaryReader (
84+ new BinaryWriter ( )
85+ . tag ( 1 , WireType . StartGroup )
86+ . tag ( 33 , WireType . Varint )
87+ . bool ( true )
88+ . tag ( 1 , WireType . EndGroup )
89+ . finish ( ) ,
90+ ) ;
91+ reader . tag ( ) ;
92+ const skipped = reader . skip ( WireType . StartGroup , 1 ) ;
93+ const sr = new BinaryReader ( skipped ) ;
94+ {
95+ const [ fieldNo , wireType ] = sr . tag ( ) ;
96+ expect ( fieldNo ) . toBe ( 33 ) ;
97+ expect ( wireType ) . toBe ( WireType . Varint ) ;
98+ const bool = sr . bool ( ) ;
99+ expect ( bool ) . toBe ( true ) ;
100+ }
101+ {
102+ const [ fieldNo , wireType ] = sr . tag ( ) ;
103+ expect ( fieldNo ) . toBe ( 1 ) ;
104+ expect ( wireType ) . toBe ( WireType . EndGroup ) ;
105+ expect ( sr . pos ) . toBe ( sr . len ) ;
106+ }
107+ } ) ;
108+ } ) ;
109+ } ) ;
0 commit comments