Chromium Code Reviews| 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("length throws a parameter error", () { | |
| 45 expect(() => parameters.length, | |
| 46 throwsInvalidParams('Parameters for method "foo" must be passed by ' | |
| 47 'position.')); | |
|
Bob Nystrom
2014/03/20 18:25:58
This is a bit confusing. How about: "passed by pos
nweiz
2014/03/20 22:55:41
This is another place I want to stick with the spe
| |
| 48 }); | |
| 49 | |
| 50 test("getPositional throws a parameter error", () { | |
| 51 expect(() => parameters.getPositional(0), | |
| 52 throwsInvalidParams('Parameters for method "foo" must be passed by ' | |
| 53 'position.')); | |
| 54 }); | |
| 55 | |
| 56 test("getNamed returns existing parameters", () { | |
| 57 expect(parameters.getNamed('num'), equals(1.5)); | |
| 58 }); | |
| 59 | |
| 60 test("getNamed fails for absent parameters", () { | |
| 61 expect(() => parameters.getNamed('fblthp'), | |
| 62 throwsInvalidParams('Request for method "foo" is missing required ' | |
| 63 'parameter "fblthp".')); | |
| 64 }); | |
| 65 | |
| 66 test("getNum returns numeric parameters", () { | |
| 67 expect(parameters.getNum('num'), equals(1.5)); | |
| 68 expect(parameters.getNum('int'), equals(1)); | |
| 69 }); | |
| 70 | |
| 71 test("getNum fails for non-numeric parameters", () { | |
| 72 expect(() => parameters.getNum('bool'), | |
| 73 throwsInvalidParams('Parameter "bool" for method "foo" must be a ' | |
| 74 'number, but was "true".')); | |
| 75 }); | |
| 76 | |
| 77 test("getInt returns integer parameters", () { | |
| 78 expect(parameters.getInt('int'), equals(1)); | |
| 79 }); | |
| 80 | |
| 81 test("getInt fails for non-integer parameters", () { | |
| 82 expect(() => parameters.getInt('bool'), | |
| 83 throwsInvalidParams('Parameter "bool" for method "foo" must be an ' | |
| 84 'integer, but was "true".')); | |
| 85 }); | |
| 86 | |
| 87 test("getBool returns boolean parameters", () { | |
| 88 expect(parameters.getBool('bool'), isTrue); | |
| 89 }); | |
| 90 | |
| 91 test("getBool fails for non-boolean parameters", () { | |
| 92 expect(() => parameters.getBool('int'), | |
| 93 throwsInvalidParams('Parameter "int" for method "foo" must be a ' | |
| 94 'boolean, but was "1".')); | |
| 95 }); | |
| 96 | |
| 97 test("getString returns string parameters", () { | |
| 98 expect(parameters.getString('string'), equals("zap")); | |
| 99 }); | |
| 100 | |
| 101 test("getString fails for non-string parameters", () { | |
| 102 expect(() => parameters.getString('int'), | |
| 103 throwsInvalidParams('Parameter "int" for method "foo" must be a ' | |
| 104 'string, but was "1".')); | |
| 105 }); | |
| 106 | |
| 107 test("getList returns list parameters", () { | |
| 108 expect(parameters.getList('list'), equals([1, 2, 3])); | |
| 109 }); | |
| 110 | |
| 111 test("getList fails for non-list parameters", () { | |
| 112 expect(() => parameters.getList('int'), | |
| 113 throwsInvalidParams('Parameter "int" for method "foo" must be an ' | |
| 114 'Array, but was "1".')); | |
| 115 }); | |
| 116 | |
| 117 test("getMap returns map parameters", () { | |
| 118 expect(parameters.getMap('map'), equals({"num": 4.2, "bool": false})); | |
| 119 }); | |
| 120 | |
| 121 test("getMap fails for non-map parameters", () { | |
| 122 expect(() => parameters.getMap('int'), | |
| 123 throwsInvalidParams('Parameter "int" for method "foo" must be an ' | |
| 124 'Object, but was "1".')); | |
| 125 }); | |
| 126 | |
| 127 test("getNested fails for non-map parameters", () { | |
| 128 expect(() => parameters.getNested('int'), | |
| 129 throwsInvalidParams('Parameter "int" for method "foo" must be an ' | |
| 130 'Object, but was "1".')); | |
| 131 }); | |
| 132 | |
| 133 group("with a nested parameter map", () { | |
| 134 var nested; | |
| 135 setUp(() => nested = parameters.getNested('map')); | |
| 136 | |
| 137 test("value returns the nested map", () { | |
| 138 expect(nested.value, equals({"num": 4.2, "bool": false})); | |
| 139 }); | |
| 140 | |
| 141 test("getNamed returns existing parameters", () { | |
| 142 expect(nested.getNamed('num'), equals(4.2)); | |
| 143 }); | |
| 144 | |
| 145 test("getNamed fails for absent parameters", () { | |
| 146 expect(() => nested.getNamed('fblthp'), | |
| 147 throwsInvalidParams('Request for method "foo" is missing required ' | |
| 148 'parameter "map.fblthp".')); | |
| 149 }); | |
| 150 | |
| 151 test("typed getters return correctly-typed parameters", () { | |
| 152 expect(nested.getNum('num'), equals(4.2)); | |
| 153 }); | |
| 154 | |
| 155 test("typed getters fail for incorrectly-typed parameters", () { | |
| 156 expect(() => nested.getNum('bool'), | |
| 157 throwsInvalidParams('Parameter "map.bool" for method "foo" must be ' | |
| 158 'a number, but was "false".')); | |
| 159 }); | |
| 160 }); | |
| 161 }); | |
| 162 | |
| 163 group("with positional parameters", () { | |
| 164 var parameters; | |
| 165 setUp(() => parameters = new json_rpc.Parameters("foo", [1, 2, 3, 4, 5])); | |
| 166 | |
| 167 test("value returns the wrapped value", () { | |
| 168 expect(parameters.value, equals([1, 2, 3, 4, 5])); | |
| 169 }); | |
| 170 | |
| 171 test("length returns the length", () { | |
| 172 expect(parameters.length, equals(5)); | |
| 173 }); | |
| 174 | |
| 175 test("getNamed throws a parameter error", () { | |
| 176 expect(() => parameters.getNamed('foo'), | |
| 177 throwsInvalidParams('Parameters for method "foo" must be passed by ' | |
| 178 'name.')); | |
| 179 }); | |
| 180 | |
| 181 test("getPositional returns existing parameters", () { | |
| 182 expect(parameters.getPositional(2), equals(3)); | |
| 183 }); | |
| 184 | |
| 185 test("getPositional fails for out-of-range parameters", () { | |
| 186 expect(() => parameters.getPositional(10), | |
| 187 throwsInvalidParams('Method "foo" requires at least 11 arguments.')); | |
| 188 }); | |
| 189 }); | |
| 190 } | |
| OLD | NEW |