@@ -23,6 +23,7 @@ import * as proto3_ts from "../gen/ts/extra/proto3_pb.js";
23
23
import { create , protoInt64 } from "@bufbuild/protobuf" ;
24
24
import { UserDesc } from "../gen/ts/extra/example_pb.js" ;
25
25
import assert from "node:assert" ;
26
+ import { catchFieldError } from "../helpers.js" ;
26
27
27
28
describe ( "reflectList()" , ( ) => {
28
29
test ( "creates ReflectList" , ( ) => {
@@ -95,87 +96,88 @@ describe("ReflectList", () => {
95
96
test ( "adds item" , ( ) => {
96
97
const local : unknown [ ] = [ "a" ] ;
97
98
const list = reflectList ( repeatedStringField , local ) ;
98
- expect ( list . add ( "b" ) ) . toBeUndefined ( ) ;
99
- expect ( list . add ( "c" ) ) . toBeUndefined ( ) ;
99
+ list . add ( "b" ) ;
100
+ list . add ( "c" ) ;
100
101
expect ( local ) . toStrictEqual ( [ "a" , "b" , "c" ] ) ;
101
102
} ) ;
102
103
test ( "adds items" , ( ) => {
103
104
const local : unknown [ ] = [ ] ;
104
105
const list = reflectList ( repeatedStringField , local ) ;
105
- expect ( list . add ( "a" , "b" , "c" ) ) . toBeUndefined ( ) ;
106
+ list . add ( "a" , "b" , "c" ) ;
106
107
expect ( local ) . toStrictEqual ( [ "a" , "b" , "c" ] ) ;
107
108
} ) ;
108
109
test ( "does not add any item if one of several items is invalid" , ( ) => {
109
110
const local : unknown [ ] = [ ] ;
110
111
const list = reflectList ( repeatedStringField , local ) ;
111
- expect ( list . add ( "a" , "b" , true ) ) . toBeDefined ( ) ;
112
+ const err = catchFieldError ( ( ) => list . add ( "a" , "b" , true ) ) ;
113
+ expect ( err ) . toBeDefined ( ) ;
112
114
expect ( local ) . toStrictEqual ( [ ] ) ;
113
115
} ) ;
114
116
test ( "converts number, string, bigint to bigint for 64-bit integer field" , ( ) => {
115
117
const local : unknown [ ] = [ ] ;
116
118
const list = reflectList ( repeatedInt64Field , local ) ;
117
- expect ( list . add ( 1 ) ) . toBeUndefined ( ) ;
118
- expect ( list . add ( "2" ) ) . toBeUndefined ( ) ;
119
- expect ( list . add ( n3 ) ) . toBeUndefined ( ) ;
119
+ list . add ( 1 ) ;
120
+ list . add ( "2" ) ;
121
+ list . add ( n3 ) ;
120
122
expect ( local ) . toStrictEqual ( [ n1 , n2 , n3 ] ) ;
121
123
} ) ;
122
124
test ( "converts number, string, bigint to string for 64-bit integer field with jstype=JS_STRING" , ( ) => {
123
125
const local : unknown [ ] = [ ] ;
124
126
const list = reflectList ( repeatedInt64JsStringField , local ) ;
125
- expect ( list . add ( 1 ) ) . toBeUndefined ( ) ;
126
- expect ( list . add ( "2" ) ) . toBeUndefined ( ) ;
127
- expect ( list . add ( n3 ) ) . toBeUndefined ( ) ;
127
+ list . add ( 1 ) ;
128
+ list . add ( "2" ) ;
129
+ list . add ( n3 ) ;
128
130
expect ( local ) . toStrictEqual ( [ "1" , "2" , "3" ] ) ;
129
131
} ) ;
130
132
test ( "returns error for wrong message type" , ( ) => {
131
133
const list = reflectList ( repeatedMessageField , [ ] ) ;
132
- const err = list . add ( reflect ( UserDesc ) ) ;
134
+ const err = catchFieldError ( ( ) => list . add ( reflect ( UserDesc ) ) ) ;
133
135
expect ( err ?. message ) . toMatch (
134
136
/ ^ l i s t i t e m # 1 : e x p e c t e d R e f l e c t M e s s a g e \( s p e c .P r o t o 3 M e s s a g e \) , g o t R e f l e c t M e s s a g e \( d o c s .U s e r \) $ / ,
135
137
) ;
136
138
} ) ;
137
139
test ( "returns error for invalid scalar" , ( ) => {
138
140
const list = reflectList ( repeatedStringField , [ ] ) ;
139
- const err = list . add ( true ) ;
141
+ const err = catchFieldError ( ( ) => list . add ( true ) ) ;
140
142
expect ( err ?. message ) . toMatch ( / ^ l i s t i t e m # 1 : e x p e c t e d s t r i n g , g o t t r u e $ / ) ;
141
143
} ) ;
142
144
} ) ;
143
145
describe ( "set()" , ( ) => {
144
146
test ( "replaces item at index" , ( ) => {
145
147
const local : unknown [ ] = [ "a" , "b" ] ;
146
148
const list = reflectList ( repeatedStringField , local ) ;
147
- expect ( list . set ( 0 , "c" ) ) . toBeUndefined ( ) ;
149
+ list . set ( 0 , "c" ) ;
148
150
expect ( local ) . toStrictEqual ( [ "c" , "b" ] ) ;
149
151
} ) ;
150
152
test ( "converts number, string, bigint to bigint for 64-bit integer field" , ( ) => {
151
153
const local : unknown [ ] = [ n0 , n0 , n0 ] ;
152
154
const list = reflectList ( repeatedInt64Field , local ) ;
153
- expect ( list . set ( 0 , 1 ) ) . toBeUndefined ( ) ;
154
- expect ( list . set ( 1 , "2" ) ) . toBeUndefined ( ) ;
155
- expect ( list . set ( 2 , n3 ) ) . toBeUndefined ( ) ;
155
+ list . set ( 0 , 1 ) ;
156
+ list . set ( 1 , "2" ) ;
157
+ list . set ( 2 , n3 ) ;
156
158
expect ( local ) . toStrictEqual ( [ n1 , n2 , n3 ] ) ;
157
159
} ) ;
158
160
test ( "converts number, string, bigint to string for 64-bit integer field with jstype=JS_STRING" , ( ) => {
159
161
const local : unknown [ ] = [ "0" , "0" , "0" ] ;
160
162
const list = reflectList ( repeatedInt64JsStringField , local ) ;
161
- expect ( list . set ( 0 , 1 ) ) . toBeUndefined ( ) ;
162
- expect ( list . set ( 1 , "2" ) ) . toBeUndefined ( ) ;
163
- expect ( list . set ( 2 , n3 ) ) . toBeUndefined ( ) ;
163
+ list . set ( 0 , 1 ) ;
164
+ list . set ( 1 , "2" ) ;
165
+ list . set ( 2 , n3 ) ;
164
166
expect ( local ) . toStrictEqual ( [ "1" , "2" , "3" ] ) ;
165
167
} ) ;
166
- test ( "returns error if out of range" , ( ) => {
168
+ test ( "throws error if out of range" , ( ) => {
167
169
const list = reflectList ( repeatedStringField , [ ] ) ;
168
- const err = list . set ( 0 , "abc" ) ;
170
+ const err = catchFieldError ( ( ) => list . set ( 0 , "abc" ) ) ;
169
171
expect ( err ?. message ) . toMatch ( / ^ l i s t i t e m # 1 : o u t o f r a n g e $ / ) ;
170
172
} ) ;
171
- test ( "returns error for invalid scalar" , ( ) => {
173
+ test ( "throws error for invalid scalar" , ( ) => {
172
174
const list = reflectList ( repeatedStringField , [ null ] ) ;
173
- const err = list . set ( 0 , true ) ;
175
+ const err = catchFieldError ( ( ) => list . set ( 0 , true ) ) ;
174
176
expect ( err ?. message ) . toMatch ( / ^ l i s t i t e m # 1 : e x p e c t e d s t r i n g , g o t t r u e $ / ) ;
175
177
} ) ;
176
- test ( "returns error for wrong message type" , ( ) => {
178
+ test ( "throws error for wrong message type" , ( ) => {
177
179
const list = reflectList ( repeatedMessageField , [ null ] ) ;
178
- const err = list . set ( 0 , reflect ( UserDesc ) ) ;
180
+ const err = catchFieldError ( ( ) => list . set ( 0 , reflect ( UserDesc ) ) ) ;
179
181
expect ( err ?. message ) . toMatch (
180
182
/ ^ l i s t i t e m # 1 : e x p e c t e d R e f l e c t M e s s a g e \( s p e c .P r o t o 3 M e s s a g e \) , g o t R e f l e c t M e s s a g e \( d o c s .U s e r \) $ / ,
181
183
) ;
0 commit comments