OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2016, 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 import 'dart:async'; |
| 6 import 'dart:convert'; |
| 7 |
| 8 import 'package:stream_channel/stream_channel.dart'; |
| 9 import 'package:test/test.dart'; |
| 10 |
| 11 import 'utils.dart'; |
| 12 |
| 13 void main() { |
| 14 var streamController; |
| 15 var sinkController; |
| 16 var channel; |
| 17 setUp(() { |
| 18 streamController = new StreamController(); |
| 19 sinkController = new StreamController(); |
| 20 channel = new StreamChannel( |
| 21 streamController.stream, sinkController.sink); |
| 22 }); |
| 23 |
| 24 test("decodes JSON emitted by the channel", () { |
| 25 var transformed = channel.transform(jsonDocument); |
| 26 streamController.add('{"foo": "bar"}'); |
| 27 expect(transformed.stream.first, completion(equals({"foo": "bar"}))); |
| 28 }); |
| 29 |
| 30 test("encodes objects added to the channel", () { |
| 31 var transformed = channel.transform(jsonDocument); |
| 32 transformed.sink.add({"foo": "bar"}); |
| 33 expect(sinkController.stream.first, |
| 34 completion(equals(JSON.encode({"foo": "bar"})))); |
| 35 }); |
| 36 |
| 37 test("supports the reviver function", () { |
| 38 var transformed = channel.transform( |
| 39 new JsonDocumentTransformer(reviver: (key, value) => "decoded")); |
| 40 streamController.add('{"foo": "bar"}'); |
| 41 expect(transformed.stream.first, completion(equals("decoded"))); |
| 42 }); |
| 43 |
| 44 test("supports the toEncodable function", () { |
| 45 var transformed = channel.transform( |
| 46 new JsonDocumentTransformer(toEncodable: (object) => "encoded")); |
| 47 transformed.sink.add(new Object()); |
| 48 expect(sinkController.stream.first, completion(equals('"encoded"'))); |
| 49 }); |
| 50 } |
OLD | NEW |