OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a |
| 3 // BSD-style license that can be found in the LICENSE file. |
| 4 |
| 5 library json_rpc_2.test.server.parameters_test; |
| 6 |
| 7 import 'package:unittest/unittest.dart'; |
| 8 import 'package:json_rpc_2/error_code.dart' as error_code; |
| 9 import 'package:json_rpc_2/json_rpc_2.dart' as json_rpc; |
| 10 |
| 11 import 'utils.dart'; |
| 12 |
| 13 void main() { |
| 14 group("with named parameters", () { |
| 15 var parameters; |
| 16 setUp(() { |
| 17 parameters = new json_rpc.Parameters("foo", { |
| 18 "num": 1.5, |
| 19 "int": 1, |
| 20 "bool": true, |
| 21 "string": "zap", |
| 22 "list": [1, 2, 3], |
| 23 "map": { |
| 24 "num": 4.2, |
| 25 "bool": false |
| 26 } |
| 27 }); |
| 28 }); |
| 29 |
| 30 test("value returns the wrapped value", () { |
| 31 expect(parameters.value, equals({ |
| 32 "num": 1.5, |
| 33 "int": 1, |
| 34 "bool": true, |
| 35 "string": "zap", |
| 36 "list": [1, 2, 3], |
| 37 "map": { |
| 38 "num": 4.2, |
| 39 "bool": false |
| 40 } |
| 41 })); |
| 42 }); |
| 43 |
| 44 test("[int] throws a parameter error", () { |
| 45 expect(() => parameters[0], |
| 46 throwsInvalidParams('Parameters for method "foo" must be passed by ' |
| 47 'position.')); |
| 48 }); |
| 49 |
| 50 test("[].value returns existing parameters", () { |
| 51 expect(parameters['num'].value, equals(1.5)); |
| 52 }); |
| 53 |
| 54 test("[].valueOr returns existing parameters", () { |
| 55 expect(parameters['num'].valueOr(7), equals(1.5)); |
| 56 }); |
| 57 |
| 58 test("[].value fails for absent parameters", () { |
| 59 expect(() => parameters['fblthp'].value, |
| 60 throwsInvalidParams('Request for method "foo" is missing required ' |
| 61 'parameter "fblthp".')); |
| 62 }); |
| 63 |
| 64 test("[].valueOr succeeds for absent parameters", () { |
| 65 expect(parameters['fblthp'].valueOr(7), equals(7)); |
| 66 }); |
| 67 |
| 68 test("[].exists returns true for existing parameters", () { |
| 69 expect(parameters['num'].exists, isTrue); |
| 70 }); |
| 71 |
| 72 test("[].exists returns false for missing parameters", () { |
| 73 expect(parameters['fblthp'].exists, isFalse); |
| 74 }); |
| 75 |
| 76 test("[].asNum returns numeric parameters", () { |
| 77 expect(parameters['num'].asNum, equals(1.5)); |
| 78 expect(parameters['int'].asNum, equals(1)); |
| 79 }); |
| 80 |
| 81 test("[].asNumOr returns numeric parameters", () { |
| 82 expect(parameters['num'].asNumOr(7), equals(1.5)); |
| 83 }); |
| 84 |
| 85 test("[].asNum fails for non-numeric parameters", () { |
| 86 expect(() => parameters['bool'].asNum, |
| 87 throwsInvalidParams('Parameter "bool" for method "foo" must be a ' |
| 88 'number, but was true.')); |
| 89 }); |
| 90 |
| 91 test("[].asNumOr fails for non-numeric parameters", () { |
| 92 expect(() => parameters['bool'].asNumOr(7), |
| 93 throwsInvalidParams('Parameter "bool" for method "foo" must be a ' |
| 94 'number, but was true.')); |
| 95 }); |
| 96 |
| 97 test("[].asNum fails for absent parameters", () { |
| 98 expect(() => parameters['fblthp'].asNum, |
| 99 throwsInvalidParams('Request for method "foo" is missing required ' |
| 100 'parameter "fblthp".')); |
| 101 }); |
| 102 |
| 103 test("[].asNumOr succeeds for absent parameters", () { |
| 104 expect(parameters['fblthp'].asNumOr(7), equals(7)); |
| 105 }); |
| 106 |
| 107 test("[].asInt returns integer parameters", () { |
| 108 expect(parameters['int'].asInt, equals(1)); |
| 109 }); |
| 110 |
| 111 test("[].asIntOr returns integer parameters", () { |
| 112 expect(parameters['int'].asIntOr(7), equals(1)); |
| 113 }); |
| 114 |
| 115 test("[].asInt fails for non-integer parameters", () { |
| 116 expect(() => parameters['bool'].asInt, |
| 117 throwsInvalidParams('Parameter "bool" for method "foo" must be an ' |
| 118 'integer, but was true.')); |
| 119 }); |
| 120 |
| 121 test("[].asIntOr succeeds for absent parameters", () { |
| 122 expect(parameters['fblthp'].asIntOr(7), equals(7)); |
| 123 }); |
| 124 |
| 125 test("[].asBool returns boolean parameters", () { |
| 126 expect(parameters['bool'].asBool, isTrue); |
| 127 }); |
| 128 |
| 129 test("[].asBoolOr returns boolean parameters", () { |
| 130 expect(parameters['bool'].asBoolOr(false), isTrue); |
| 131 }); |
| 132 |
| 133 test("[].asBoolOr fails for non-boolean parameters", () { |
| 134 expect(() => parameters['int'].asBool, |
| 135 throwsInvalidParams('Parameter "int" for method "foo" must be a ' |
| 136 'boolean, but was 1.')); |
| 137 }); |
| 138 |
| 139 test("[].asBoolOr succeeds for absent parameters", () { |
| 140 expect(parameters['fblthp'].asBoolOr(false), isFalse); |
| 141 }); |
| 142 |
| 143 test("[].asString returns string parameters", () { |
| 144 expect(parameters['string'].asString, equals("zap")); |
| 145 }); |
| 146 |
| 147 test("[].asStringOr returns string parameters", () { |
| 148 expect(parameters['string'].asStringOr("bap"), equals("zap")); |
| 149 }); |
| 150 |
| 151 test("[].asString fails for non-string parameters", () { |
| 152 expect(() => parameters['int'].asString, |
| 153 throwsInvalidParams('Parameter "int" for method "foo" must be a ' |
| 154 'string, but was 1.')); |
| 155 }); |
| 156 |
| 157 test("[].asStringOr succeeds for absent parameters", () { |
| 158 expect(parameters['fblthp'].asStringOr("bap"), equals("bap")); |
| 159 }); |
| 160 |
| 161 test("[].asList returns list parameters", () { |
| 162 expect(parameters['list'].asList, equals([1, 2, 3])); |
| 163 }); |
| 164 |
| 165 test("[].asListOr returns list parameters", () { |
| 166 expect(parameters['list'].asListOr([5, 6, 7]), equals([1, 2, 3])); |
| 167 }); |
| 168 |
| 169 test("[].asList fails for non-list parameters", () { |
| 170 expect(() => parameters['int'].asList, |
| 171 throwsInvalidParams('Parameter "int" for method "foo" must be an ' |
| 172 'Array, but was 1.')); |
| 173 }); |
| 174 |
| 175 test("[].asListOr succeeds for absent parameters", () { |
| 176 expect(parameters['fblthp'].asListOr([5, 6, 7]), equals([5, 6, 7])); |
| 177 }); |
| 178 |
| 179 test("[].asMap returns map parameters", () { |
| 180 expect(parameters['map'].asMap, equals({"num": 4.2, "bool": false})); |
| 181 }); |
| 182 |
| 183 test("[].asMapOr returns map parameters", () { |
| 184 expect(parameters['map'].asMapOr({}), |
| 185 equals({"num": 4.2, "bool": false})); |
| 186 }); |
| 187 |
| 188 test("[].asMap fails for non-map parameters", () { |
| 189 expect(() => parameters['int'].asMap, |
| 190 throwsInvalidParams('Parameter "int" for method "foo" must be an ' |
| 191 'Object, but was 1.')); |
| 192 }); |
| 193 |
| 194 test("[].asMapOr succeeds for absent parameters", () { |
| 195 expect(parameters['fblthp'].asMapOr({}), equals({})); |
| 196 }); |
| 197 |
| 198 group("with a nested parameter map", () { |
| 199 var nested; |
| 200 setUp(() => nested = parameters['map']); |
| 201 |
| 202 test("[int] fails with a type error", () { |
| 203 expect(() => nested[0], |
| 204 throwsInvalidParams('Parameter "map" for method "foo" must be an ' |
| 205 'Array, but was {"num":4.2,"bool":false}.')); |
| 206 }); |
| 207 |
| 208 test("[].value returns existing parameters", () { |
| 209 expect(nested['num'].value, equals(4.2)); |
| 210 expect(nested['bool'].value, isFalse); |
| 211 }); |
| 212 |
| 213 test("[].value fails for absent parameters", () { |
| 214 expect(() => nested['fblthp'].value, |
| 215 throwsInvalidParams('Request for method "foo" is missing required ' |
| 216 'parameter map.fblthp.')); |
| 217 }); |
| 218 |
| 219 test("typed getters return correctly-typed parameters", () { |
| 220 expect(nested['num'].asNum, equals(4.2)); |
| 221 }); |
| 222 |
| 223 test("typed getters fail for incorrectly-typed parameters", () { |
| 224 expect(() => nested['bool'].asNum, |
| 225 throwsInvalidParams('Parameter map.bool for method "foo" must be ' |
| 226 'a number, but was false.')); |
| 227 }); |
| 228 }); |
| 229 |
| 230 group("with a nested parameter list", () { |
| 231 var nested; |
| 232 setUp(() => nested = parameters['list']); |
| 233 |
| 234 test("[string] fails with a type error", () { |
| 235 expect(() => nested['foo'], |
| 236 throwsInvalidParams('Parameter "list" for method "foo" must be an ' |
| 237 'Object, but was [1,2,3].')); |
| 238 }); |
| 239 |
| 240 test("[].value returns existing parameters", () { |
| 241 expect(nested[0].value, equals(1)); |
| 242 expect(nested[1].value, equals(2)); |
| 243 }); |
| 244 |
| 245 test("[].value fails for absent parameters", () { |
| 246 expect(() => nested[5].value, |
| 247 throwsInvalidParams('Request for method "foo" is missing required ' |
| 248 'parameter list[5].')); |
| 249 }); |
| 250 |
| 251 test("typed getters return correctly-typed parameters", () { |
| 252 expect(nested[0].asInt, equals(1)); |
| 253 }); |
| 254 |
| 255 test("typed getters fail for incorrectly-typed parameters", () { |
| 256 expect(() => nested[0].asBool, |
| 257 throwsInvalidParams('Parameter list[0] for method "foo" must be ' |
| 258 'a boolean, but was 1.')); |
| 259 }); |
| 260 }); |
| 261 }); |
| 262 |
| 263 group("with positional parameters", () { |
| 264 var parameters; |
| 265 setUp(() => parameters = new json_rpc.Parameters("foo", [1, 2, 3, 4, 5])); |
| 266 |
| 267 test("value returns the wrapped value", () { |
| 268 expect(parameters.value, equals([1, 2, 3, 4, 5])); |
| 269 }); |
| 270 |
| 271 test("[string] throws a parameter error", () { |
| 272 expect(() => parameters['foo'], |
| 273 throwsInvalidParams('Parameters for method "foo" must be passed by ' |
| 274 'name.')); |
| 275 }); |
| 276 |
| 277 test("[].value returns existing parameters", () { |
| 278 expect(parameters[2].value, equals(3)); |
| 279 }); |
| 280 |
| 281 test("[].value fails for out-of-range parameters", () { |
| 282 expect(() => parameters[10].value, |
| 283 throwsInvalidParams('Request for method "foo" is missing required ' |
| 284 'parameter 11.')); |
| 285 }); |
| 286 |
| 287 test("[].exists returns true for existing parameters", () { |
| 288 expect(parameters[0].exists, isTrue); |
| 289 }); |
| 290 |
| 291 test("[].exists returns false for missing parameters", () { |
| 292 expect(parameters[10].exists, isFalse); |
| 293 }); |
| 294 }); |
| 295 |
| 296 test("with a complex parameter path", () { |
| 297 var parameters = new json_rpc.Parameters("foo", { |
| 298 'bar baz': [0, 1, 2, {'bang.zap': {'\n': 'qux'}}] |
| 299 }); |
| 300 |
| 301 expect(() => parameters['bar baz'][3]['bang.zap']['\n']['bip'], |
| 302 throwsInvalidParams('Parameter "bar baz"[3]."bang.zap"."\\n" for ' |
| 303 'method "foo" must be an Object, but was "qux".')); |
| 304 }); |
| 305 } |
OLD | NEW |