-
Notifications
You must be signed in to change notification settings - Fork 49
/
Copy pathapi_property_int_test.dart
330 lines (294 loc) · 10.3 KB
/
api_property_int_test.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
// Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
library api_property_int_tests;
import 'dart:mirrors';
import 'package:rpc/rpc.dart';
import 'package:rpc/src/config.dart';
import 'package:rpc/src/parser.dart';
import 'package:rpc/src/discovery/config.dart' as discovery;
import 'package:test/test.dart';
class CorrectInt {
int anInt;
@ApiProperty(name: 'anotherName', description: 'Description of an integer.')
int aNamedInt;
@ApiProperty(defaultValue: 42)
int anIntWithDefault;
@ApiProperty(required: true)
int aRequiredInt;
@ApiProperty(minValue: 0, maxValue: 2, defaultValue: 1)
int aBoundedInt;
@ApiProperty(
format: 'int32',
minValue: -0x80000000, // -2^31
maxValue: 0x7FFFFFFF, // 2^31-1,
defaultValue: -0x80000000)
int aBoundedInt32;
@ApiProperty(
format: 'uint32',
minValue: 0,
maxValue: 0xFFFFFFFF, // 2^32-1,
defaultValue: 0xFFFFFFFF)
int aBoundedUInt32;
@ApiProperty(
format: 'int64',
// ignore: integer_literal_out_of_range
minValue: "-0x8000000000000000", // -2^63
maxValue: "0x7FFFFFFFFFFFFFFF", // 2^63-1,
// ignore: integer_literal_out_of_range
defaultValue: "-0x8000000000000000")
BigInt aBoundedInt64;
@ApiProperty(
format: 'uint64',
minValue: "0",
maxValue: "0xFFFFFFFFFFFFFFFF", // 2^64-1,
defaultValue: "0xFFFFFFFFFFFFFFFF")
BigInt aBoundedUInt64;
@ApiProperty(ignore: true)
int ignored;
}
class WrongInt {
@ApiProperty(values: const {'enumKey': 'enumValue'})
int anIntWithEnumValues;
@ApiProperty(minValue: 0, maxValue: 2, defaultValue: 3)
int aBoundedIntWithTooHighDefault;
@ApiProperty(minValue: 0, maxValue: 2, defaultValue: -1)
int aBoundedIntWithTooLowDefault;
@ApiProperty(minValue: 2, maxValue: 0)
int aBoundedIntWithMaxLessThanMin;
@ApiProperty(
format: 'int32',
minValue: -0x80000001, // -2^31-1
maxValue: 0x7FFFFFFF, // 2^31-1,
defaultValue: 0x7FFFFFFF)
int anInt32TooSmallMin;
@ApiProperty(
format: 'int32',
minValue: -0x80000000, // -2^31
maxValue: 0x80000000, // 2^31,
defaultValue: 0x80000000)
int anInt32TooLargeMax;
@ApiProperty(
format: 'int32',
minValue: -0x80000000, // -2^31
maxValue: 0x7FFFFFFF, // 2^31-1,
defaultValue: 0x80000000)
int anInt32TooLargeDefault;
@ApiProperty(
format: 'int32',
minValue: -0x80000000, // -2^31
maxValue: 0x7FFFFFFF, // 2^31-1,
defaultValue: -0x80000001)
int anInt32TooSmallDefault;
@ApiProperty(
format: 'uint32',
minValue: -1,
maxValue: 0xFFFFFFFF, // 2^32-1,
defaultValue: 0xFFFFFFFF)
int anUInt32TooSmallMin;
@ApiProperty(
format: 'uint32',
minValue: 0,
maxValue: 0x100000000, // 2^32,
defaultValue: 0xFFFFFFFF)
int anUInt32TooLargeMax;
@ApiProperty(
format: 'uint32',
minValue: 0,
maxValue: 0xFFFFFFFF, // 2^32-1,
defaultValue: 0x100000000) // 2^32
int anUInt32TooLargeDefault;
@ApiProperty(
format: 'uint32',
minValue: 0,
maxValue: 0xFFFFFFFF, // 2^32-1,
defaultValue: -1)
int anUInt32TooSmallDefault;
@ApiProperty(
format: 'int64',
minValue: "-0x8000000000000001", // -2^63-1
maxValue: "0x7FFFFFFFFFFFFFFF", // 2^63-1,
defaultValue: "0x7FFFFFFFFFFFFFFF")
BigInt anInt64TooSmallMin;
@ApiProperty(
format: 'int64',
minValue: "-0x8000000000000000", // -2^63
maxValue: "0x8000000000000000", // 2^63,
defaultValue: "0x7FFFFFFFFFFFFFFF")
BigInt anInt64TooLargeMax;
@ApiProperty(
format: 'int64',
minValue: "-0x8000000000000000", // -2^63
maxValue: "0x7FFFFFFFFFFFFFFF", // 2^63-1,
defaultValue: "0x8000000000000000")
BigInt anInt64TooLargeDefault;
@ApiProperty(
format: 'int64',
minValue: "-0x8000000000000000", // -2^63
maxValue: "0x7FFFFFFFFFFFFFFF", // 2^63-1,
defaultValue: "-0x8000000000000001")
BigInt anInt64TooSmallDefault;
@ApiProperty(
format: 'uint64',
minValue: "-1",
maxValue: "0xFFFFFFFFFFFFFFFF", // 2^64-1,
defaultValue: "0xFFFFFFFFFFFFFFFF")
BigInt anUInt64TooSmallMin;
@ApiProperty(
format: 'uint64',
minValue: "0",
maxValue: "0x10000000000000000", // 2^64,
defaultValue: "0xFFFFFFFFFFFFFFFF")
BigInt anUInt64TooLargeMax;
@ApiProperty(
format: 'uint64',
minValue: "0",
maxValue: "0xFFFFFFFFFFFFFFFF", // 2^64-1,
defaultValue: "0x10000000000000000") // 2^64
BigInt anUInt64TooLargeDefault;
@ApiProperty(
format: 'uint64',
minValue: "0",
maxValue: "0xFFFFFFFFFFFFFFFF", // 2^64-1,
defaultValue: "-1")
BigInt anUInt64TooSmallDefault;
}
final ApiConfigSchema jsonSchema =
new ApiParser().parseSchema(reflectClass(discovery.JsonSchema), false);
void main() {
group('api-integer-property-correct', () {
test('simple', () {
var parser = new ApiParser();
ApiConfigSchema apiSchema =
parser.parseSchema(reflectClass(CorrectInt), true);
expect(parser.isValid, isTrue);
expect(parser.apiSchemas.length, 1);
expect(parser.apiSchemas['CorrectInt'], apiSchema);
var json = jsonSchema.toResponse(apiSchema.asDiscovery);
var expectedJson = {
'id': 'CorrectInt',
'type': 'object',
'properties': {
'anInt': {'type': 'integer', 'format': 'int32'},
'anotherName': {
'type': 'integer',
'description': 'Description of an integer.',
'format': 'int32'
},
'anIntWithDefault': {
'type': 'integer',
'default': '42',
'format': 'int32'
},
'aRequiredInt': {
'type': 'integer',
'required': true,
'format': 'int32'
},
'aBoundedInt': {
'type': 'integer',
'default': '1',
'format': 'int32',
'minimum': '0',
'maximum': '2'
},
'aBoundedInt32': {
'type': 'integer',
'default': '-2147483648',
'format': 'int32',
'minimum': '-2147483648',
'maximum': '2147483647'
},
'aBoundedUInt32': {
'type': 'integer',
'default': '4294967295',
'format': 'uint32',
'minimum': '0',
'maximum': '4294967295'
},
'aBoundedInt64': {
'type': 'string',
'default': '-9223372036854775808',
'format': 'int64',
'minimum': '-9223372036854775808',
'maximum': '9223372036854775807'
},
'aBoundedUInt64': {
'type': 'string',
'default': '18446744073709551615',
'format': 'uint64',
'minimum': '0',
'maximum': '18446744073709551615'
}
}
};
expect(json, expectedJson);
});
});
group('api-integer-property-wrong', () {
test('simple', () {
var parser = new ApiParser();
parser.parseSchema(reflectClass(WrongInt), true);
expect(parser.isValid, isFalse);
var expectedErrors = [
new ApiConfigError('WrongInt: anIntWithEnumValues: Invalid property '
'annotation. Property of type integer does not support the '
'ApiProperty field: values'),
new ApiConfigError(
'WrongInt: aBoundedIntWithTooHighDefault: Default value must be '
'<= 2.'),
new ApiConfigError(
'WrongInt: aBoundedIntWithTooLowDefault: Default value must be '
'>= 0.'),
new ApiConfigError(
'WrongInt: aBoundedIntWithMaxLessThanMin: Invalid min/max range: '
'[2, 0]. Min must be less than max.'),
new ApiConfigError(
'WrongInt: anInt32TooSmallMin: Min value: \'-2147483649\' not in '
'the range of an \'int32\''),
new ApiConfigError(
'WrongInt: anInt32TooLargeMax: Max value: \'2147483648\' not in '
'the range of an \'int32\''),
new ApiConfigError(
'WrongInt: anInt32TooLargeDefault: Default value: \'2147483648\' '
'not in the range of an \'int32\''),
new ApiConfigError(
'WrongInt: anInt32TooSmallDefault: Default value: \'-2147483649\' '
'not in the range of an \'int32\''),
new ApiConfigError(
'WrongInt: anUInt32TooSmallMin: Min value: \'-1\' not in the range '
'of an \'uint32\''),
new ApiConfigError(
'WrongInt: anUInt32TooLargeMax: Max value: \'4294967296\' not in '
'the range of an \'uint32\''),
new ApiConfigError(
'WrongInt: anUInt32TooLargeDefault: Default value: \'4294967296\' '
'not in the range of an \'uint32\''),
new ApiConfigError(
'WrongInt: anUInt32TooSmallDefault: Default value: \'-1\' not in '
'the range of an \'uint32\''),
new ApiConfigError(
'WrongInt: anInt64TooSmallMin: Min value: \'-9223372036854775809\' '
'not in the range of an \'int64\''),
new ApiConfigError(
'WrongInt: anInt64TooLargeMax: Max value: \'9223372036854775808\' '
'not in the range of an \'int64\''),
new ApiConfigError('WrongInt: anInt64TooLargeDefault: Default value: '
'\'9223372036854775808\' not in the range of an \'int64\''),
new ApiConfigError('WrongInt: anInt64TooSmallDefault: Default value: '
'\'-9223372036854775809\' not in the range of an \'int64\''),
new ApiConfigError(
'WrongInt: anUInt64TooSmallMin: Min value: \'-1\' not in the range '
'of an \'uint64\''),
new ApiConfigError('WrongInt: anUInt64TooLargeMax: Max value: '
'\'18446744073709551616\' not in the range of an \'uint64\''),
new ApiConfigError('WrongInt: anUInt64TooLargeDefault: Default value: '
'\'18446744073709551616\' not in the range of an \'uint64\''),
new ApiConfigError(
'WrongInt: anUInt64TooSmallDefault: Default value: \'-1\' not in '
'the range of an \'uint64\'')
];
expect(parser.errors.toString(), expectedErrors.toString());
});
});
}