Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(86)

Unified Diff: pkg/json_rpc_2/test/server/parameters_test.dart

Issue 205533005: Create a package that implements a JSON-RPC 2.0 server. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 6 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: pkg/json_rpc_2/test/server/parameters_test.dart
diff --git a/pkg/json_rpc_2/test/server/parameters_test.dart b/pkg/json_rpc_2/test/server/parameters_test.dart
new file mode 100644
index 0000000000000000000000000000000000000000..cda4069148f7be29585a765db476aa6cfc0f054a
--- /dev/null
+++ b/pkg/json_rpc_2/test/server/parameters_test.dart
@@ -0,0 +1,190 @@
+// Copyright (c) 2014, 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 json_rpc_2.test.server.parameters_test;
+
+import 'package:unittest/unittest.dart';
+import 'package:json_rpc_2/error_code.dart' as error_code;
+import 'package:json_rpc_2/json_rpc_2.dart' as json_rpc;
+
+import 'utils.dart';
+
+void main() {
+ group("with named parameters", () {
+ var parameters;
+ setUp(() {
+ parameters = new json_rpc.Parameters("foo", {
+ "num": 1.5,
+ "int": 1,
+ "bool": true,
+ "string": "zap",
+ "list": [1, 2, 3],
+ "map": {
+ "num": 4.2,
+ "bool": false
+ }
+ });
+ });
+
+ test("value returns the wrapped value", () {
+ expect(parameters.value, equals({
+ "num": 1.5,
+ "int": 1,
+ "bool": true,
+ "string": "zap",
+ "list": [1, 2, 3],
+ "map": {
+ "num": 4.2,
+ "bool": false
+ }
+ }));
+ });
+
+ test("length throws a parameter error", () {
+ expect(() => parameters.length,
+ throwsInvalidParams('Parameters for method "foo" must be passed by '
+ '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
+ });
+
+ test("getPositional throws a parameter error", () {
+ expect(() => parameters.getPositional(0),
+ throwsInvalidParams('Parameters for method "foo" must be passed by '
+ 'position.'));
+ });
+
+ test("getNamed returns existing parameters", () {
+ expect(parameters.getNamed('num'), equals(1.5));
+ });
+
+ test("getNamed fails for absent parameters", () {
+ expect(() => parameters.getNamed('fblthp'),
+ throwsInvalidParams('Request for method "foo" is missing required '
+ 'parameter "fblthp".'));
+ });
+
+ test("getNum returns numeric parameters", () {
+ expect(parameters.getNum('num'), equals(1.5));
+ expect(parameters.getNum('int'), equals(1));
+ });
+
+ test("getNum fails for non-numeric parameters", () {
+ expect(() => parameters.getNum('bool'),
+ throwsInvalidParams('Parameter "bool" for method "foo" must be a '
+ 'number, but was "true".'));
+ });
+
+ test("getInt returns integer parameters", () {
+ expect(parameters.getInt('int'), equals(1));
+ });
+
+ test("getInt fails for non-integer parameters", () {
+ expect(() => parameters.getInt('bool'),
+ throwsInvalidParams('Parameter "bool" for method "foo" must be an '
+ 'integer, but was "true".'));
+ });
+
+ test("getBool returns boolean parameters", () {
+ expect(parameters.getBool('bool'), isTrue);
+ });
+
+ test("getBool fails for non-boolean parameters", () {
+ expect(() => parameters.getBool('int'),
+ throwsInvalidParams('Parameter "int" for method "foo" must be a '
+ 'boolean, but was "1".'));
+ });
+
+ test("getString returns string parameters", () {
+ expect(parameters.getString('string'), equals("zap"));
+ });
+
+ test("getString fails for non-string parameters", () {
+ expect(() => parameters.getString('int'),
+ throwsInvalidParams('Parameter "int" for method "foo" must be a '
+ 'string, but was "1".'));
+ });
+
+ test("getList returns list parameters", () {
+ expect(parameters.getList('list'), equals([1, 2, 3]));
+ });
+
+ test("getList fails for non-list parameters", () {
+ expect(() => parameters.getList('int'),
+ throwsInvalidParams('Parameter "int" for method "foo" must be an '
+ 'Array, but was "1".'));
+ });
+
+ test("getMap returns map parameters", () {
+ expect(parameters.getMap('map'), equals({"num": 4.2, "bool": false}));
+ });
+
+ test("getMap fails for non-map parameters", () {
+ expect(() => parameters.getMap('int'),
+ throwsInvalidParams('Parameter "int" for method "foo" must be an '
+ 'Object, but was "1".'));
+ });
+
+ test("getNested fails for non-map parameters", () {
+ expect(() => parameters.getNested('int'),
+ throwsInvalidParams('Parameter "int" for method "foo" must be an '
+ 'Object, but was "1".'));
+ });
+
+ group("with a nested parameter map", () {
+ var nested;
+ setUp(() => nested = parameters.getNested('map'));
+
+ test("value returns the nested map", () {
+ expect(nested.value, equals({"num": 4.2, "bool": false}));
+ });
+
+ test("getNamed returns existing parameters", () {
+ expect(nested.getNamed('num'), equals(4.2));
+ });
+
+ test("getNamed fails for absent parameters", () {
+ expect(() => nested.getNamed('fblthp'),
+ throwsInvalidParams('Request for method "foo" is missing required '
+ 'parameter "map.fblthp".'));
+ });
+
+ test("typed getters return correctly-typed parameters", () {
+ expect(nested.getNum('num'), equals(4.2));
+ });
+
+ test("typed getters fail for incorrectly-typed parameters", () {
+ expect(() => nested.getNum('bool'),
+ throwsInvalidParams('Parameter "map.bool" for method "foo" must be '
+ 'a number, but was "false".'));
+ });
+ });
+ });
+
+ group("with positional parameters", () {
+ var parameters;
+ setUp(() => parameters = new json_rpc.Parameters("foo", [1, 2, 3, 4, 5]));
+
+ test("value returns the wrapped value", () {
+ expect(parameters.value, equals([1, 2, 3, 4, 5]));
+ });
+
+ test("length returns the length", () {
+ expect(parameters.length, equals(5));
+ });
+
+ test("getNamed throws a parameter error", () {
+ expect(() => parameters.getNamed('foo'),
+ throwsInvalidParams('Parameters for method "foo" must be passed by '
+ 'name.'));
+ });
+
+ test("getPositional returns existing parameters", () {
+ expect(parameters.getPositional(2), equals(3));
+ });
+
+ test("getPositional fails for out-of-range parameters", () {
+ expect(() => parameters.getPositional(10),
+ throwsInvalidParams('Method "foo" requires at least 11 arguments.'));
+ });
+ });
+}

Powered by Google App Engine
This is Rietveld 408576698