OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2013, 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:codec'; |
| 6 import 'dart:convert'; |
| 7 |
| 8 import 'package:expect/expect.dart'; |
| 9 |
| 10 class MyCodec extends Codec<int, String> { |
| 11 const MyCodec(); |
| 12 |
| 13 Converter<int, String> get encoder => new IntStringConverter(); |
| 14 Converter<String, int> get decoder => new StringIntConverter(); |
| 15 } |
| 16 |
| 17 class IntStringConverter extends Converter<int, String> { |
| 18 String convert(int i) => i.toString(); |
| 19 } |
| 20 |
| 21 class StringIntConverter extends Converter<String, int> { |
| 22 int convert(String str) => int.parse(str); |
| 23 } |
| 24 |
| 25 class MyCodec2 extends Codec<int, String> { |
| 26 const MyCodec2(); |
| 27 |
| 28 Converter<int, String> get encoder => new IntStringConverter2(); |
| 29 Converter<String, int> get decoder => new StringIntConverter2(); |
| 30 } |
| 31 |
| 32 class IntStringConverter2 extends Converter<int, String> { |
| 33 String convert(int i) => (i + 99).toString(); |
| 34 } |
| 35 |
| 36 class StringIntConverter2 extends Converter<String, int> { |
| 37 int convert(String str) => int.parse(str) + 400; |
| 38 } |
| 39 |
| 40 const TEST_CODEC = const MyCodec(); |
| 41 const TEST_CODEC2 = const MyCodec2(); |
| 42 |
| 43 main() { |
| 44 Expect.equals("0", TEST_CODEC.encode(0)); |
| 45 Expect.equals(5, TEST_CODEC.decode("5")); |
| 46 Expect.equals(3, TEST_CODEC.decode(TEST_CODEC.encode(3))); |
| 47 |
| 48 Expect.equals("99", TEST_CODEC2.encode(0)); |
| 49 Expect.equals(405, TEST_CODEC2.decode("5")); |
| 50 Expect.equals(499, TEST_CODEC2.decode(TEST_CODEC2.encode(0))); |
| 51 |
| 52 var inverted, fused; |
| 53 inverted = TEST_CODEC.inverted; |
| 54 fused = TEST_CODEC.fuse(inverted); |
| 55 Expect.equals(499, fused.encode(499)); |
| 56 Expect.equals(499, fused.decode(499)); |
| 57 |
| 58 fused = inverted.fuse(TEST_CODEC); |
| 59 Expect.equals("499", fused.encode("499")); |
| 60 Expect.equals("499", fused.decode("499")); |
| 61 |
| 62 inverted = TEST_CODEC2.inverted; |
| 63 fused = TEST_CODEC2.fuse(inverted); |
| 64 Expect.equals(499, fused.encode(0)); |
| 65 Expect.equals(499, fused.decode(0)); |
| 66 |
| 67 fused = TEST_CODEC.fuse(inverted); |
| 68 Expect.equals(405, fused.encode(5)); |
| 69 Expect.equals(101, fused.decode(2)); |
| 70 } |
OLD | NEW |