| OLD | NEW |
| 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a | 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. | 3 // BSD-style license that can be found in the LICENSE file. |
| 4 | 4 |
| 5 library serialization_test; | 5 library serialization_test; |
| 6 | 6 |
| 7 import 'dart:json' as json; | 7 import 'dart:json' as json; |
| 8 import 'package:unittest/unittest.dart'; | 8 import 'package:unittest/unittest.dart'; |
| 9 import 'package:serialization/serialization.dart'; | 9 import 'package:serialization/serialization.dart'; |
| 10 import 'package:serialization/src/serialization_helpers.dart'; | 10 import 'package:serialization/src/serialization_helpers.dart'; |
| (...skipping 126 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 137 constructorFields: [_collectionSym]); | 137 constructorFields: [_collectionSym]); |
| 138 var state = states(stream, s).first; | 138 var state = states(stream, s).first; |
| 139 // Define names for the variable offsets to make this more readable. | 139 // Define names for the variable offsets to make this more readable. |
| 140 var _collection = 0, position = 1; | 140 var _collection = 0, position = 1; |
| 141 expect(state[_collection],[3,4,5]); | 141 expect(state[_collection],[3,4,5]); |
| 142 expect(state[position], 2); | 142 expect(state[position], 2); |
| 143 }); | 143 }); |
| 144 | 144 |
| 145 test('date', () { | 145 test('date', () { |
| 146 var date = new DateTime.now(); | 146 var date = new DateTime.now(); |
| 147 var s = new Serialization() | 147 var utcDate = new DateTime.utc(date.year, date.month, date.day, |
| 148 ..addRuleFor(date, | 148 date.hour, date.minute, date.second, date.millisecond); |
| 149 constructorFields : ["year", "month", "day", "hour", "minute", | 149 var s = new Serialization(); |
| 150 "second", "millisecond", "isUtc"]) | 150 var out = s.write([date, utcDate]); |
| 151 .configureForMaps(); | 151 expect(s.selfDescribing, isTrue); |
| 152 var state = states(date, s).first; | 152 var input = s.read(out); |
| 153 expect(state["year"],date.year); | 153 expect(input.first, date); |
| 154 expect(state["isUtc"],date.isUtc); | 154 expect(input.last, utcDate); |
| 155 expect(state["millisecond"], date.millisecond); | |
| 156 }); | 155 }); |
| 157 | 156 |
| 158 test('Iteration helpers', () { | 157 test('Iteration helpers', () { |
| 159 var map = {"a" : 1, "b" : 2, "c" : 3}; | 158 var map = {"a" : 1, "b" : 2, "c" : 3}; |
| 160 var list = [1, 2, 3]; | 159 var list = [1, 2, 3]; |
| 161 var set = new Set.from(list); | 160 var set = new Set.from(list); |
| 162 var m = keysAndValues(map); | 161 var m = keysAndValues(map); |
| 163 var l = keysAndValues(list); | 162 var l = keysAndValues(list); |
| 164 var s = keysAndValues(set); | 163 var s = keysAndValues(set); |
| 165 | 164 |
| (...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 213 expect(all.contains(n2), isTrue); | 212 expect(all.contains(n2), isTrue); |
| 214 expect(all.contains(n3), isTrue); | 213 expect(all.contains(n3), isTrue); |
| 215 expect(all.contains(n1.children), isTrue); | 214 expect(all.contains(n1.children), isTrue); |
| 216 }); | 215 }); |
| 217 | 216 |
| 218 test('Flatten references in a cyclical structure', () { | 217 test('Flatten references in a cyclical structure', () { |
| 219 var s = new Serialization(); | 218 var s = new Serialization(); |
| 220 var w = new Writer(s, const InternalMapFormat()); | 219 var w = new Writer(s, const InternalMapFormat()); |
| 221 w.trace = new Trace(w); | 220 w.trace = new Trace(w); |
| 222 w.write(n1); | 221 w.write(n1); |
| 223 expect(w.states.length, 6); // prims, lists, essential lists, basic, symbol | 222 expect(w.states.length, 7); // prims, lists * 2, basic, symbol, date |
| 224 var children = 0, name = 1, parent = 2; | 223 var children = 0, name = 1, parent = 2; |
| 225 var nodeRule = s.rules.firstWhere((x) => x is BasicRule); | 224 var nodeRule = s.rules.firstWhere((x) => x is BasicRule); |
| 226 List rootNode = w.states[nodeRule.number].where( | 225 List rootNode = w.states[nodeRule.number].where( |
| 227 (x) => x[name] == "1").toList(); | 226 (x) => x[name] == "1").toList(); |
| 228 rootNode = rootNode.first; | 227 rootNode = rootNode.first; |
| 229 expect(rootNode[parent], isNull); | 228 expect(rootNode[parent], isNull); |
| 230 var list = w.states[1].first; | 229 var list = w.states[1].first; |
| 231 expect(w.stateForReference(rootNode[children]), list); | 230 expect(w.stateForReference(rootNode[children]), list); |
| 232 var parentNode = w.stateForReference(list[0])[parent]; | 231 var parentNode = w.stateForReference(list[0])[parent]; |
| 233 expect(w.stateForReference(parentNode), rootNode); | 232 expect(w.stateForReference(parentNode), rootNode); |
| (...skipping 581 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 815 | 814 |
| 816 /** | 815 /** |
| 817 * Function used in an isolate to make sure that the output passes through | 816 * Function used in an isolate to make sure that the output passes through |
| 818 * isolate serialization properly. | 817 * isolate serialization properly. |
| 819 */ | 818 */ |
| 820 echo() { | 819 echo() { |
| 821 port.receive((msg, reply) { | 820 port.receive((msg, reply) { |
| 822 reply.send(msg); | 821 reply.send(msg); |
| 823 }); | 822 }); |
| 824 } | 823 } |
| OLD | NEW |