| 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'; |
| 11 import 'package:serialization/src/mirrors_helpers.dart'; | 11 import 'package:serialization/src/mirrors_helpers.dart'; |
| 12 | 12 |
| 13 part 'test_models.dart'; | 13 part 'test_models.dart'; |
| 14 | 14 |
| 15 main() { | 15 main() { |
| 16 var p1 = new Person(); | 16 var p1 = new Person(); |
| 17 var a1 = new Address(); | 17 var a1 = new Address(); |
| 18 a1.street = 'N 34th'; | 18 a1.street = 'N 34th'; |
| 19 a1.city = 'Seattle'; | 19 a1.city = 'Seattle'; |
| 20 | 20 |
| 21 var formats = [new SimpleFlatFormat(), new SimpleMapFormat(), | 21 var formats = [const SimpleFlatFormat(), const SimpleMapFormat(), |
| 22 new SimpleJsonFormat(storeRoundTripInfo: true)]; | 22 const SimpleJsonFormat(storeRoundTripInfo: true)]; |
| 23 | 23 |
| 24 test('Basic extraction of a simple object', () { | 24 test('Basic extraction of a simple object', () { |
| 25 // TODO(alanknight): Switch these to use literal types. Issue | 25 // TODO(alanknight): Switch these to use literal types. Issue |
| 26 var s = new Serialization() | 26 var s = new Serialization() |
| 27 ..addRuleFor(a1).configureForMaps(); | 27 ..addRuleFor(a1).configureForMaps(); |
| 28 Map extracted = states(a1, s).first; | 28 Map extracted = states(a1, s).first; |
| 29 expect(extracted.length, 4); | 29 expect(extracted.length, 4); |
| 30 expect(extracted['street'], 'N 34th'); | 30 expect(extracted['street'], 'N 34th'); |
| 31 expect(extracted['city'], 'Seattle'); | 31 expect(extracted['city'], 'Seattle'); |
| 32 expect(extracted['state'], null); | 32 expect(extracted['state'], null); |
| (...skipping 307 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 340 var out = s.write(a1); | 340 var out = s.write(a1); |
| 341 var newAddress = s.read(out); | 341 var newAddress = s.read(out); |
| 342 expect(newAddress.street, a1.street); | 342 expect(newAddress.street, a1.street); |
| 343 expect(newAddress.city, "Kirkland"); | 343 expect(newAddress.city, "Kirkland"); |
| 344 expect(newAddress.state, "WA"); | 344 expect(newAddress.state, "WA"); |
| 345 expect(newAddress.zip, "98103"); | 345 expect(newAddress.zip, "98103"); |
| 346 }); | 346 }); |
| 347 | 347 |
| 348 test("Straight JSON format", () { | 348 test("Straight JSON format", () { |
| 349 var s = new Serialization(); | 349 var s = new Serialization(); |
| 350 var writer = s.newWriter(new SimpleJsonFormat()); | 350 var writer = s.newWriter(const SimpleJsonFormat()); |
| 351 var out = json.stringify(writer.write(a1)); | 351 var out = json.stringify(writer.write(a1)); |
| 352 var reconstituted = json.parse(out); | 352 var reconstituted = json.parse(out); |
| 353 expect(reconstituted.length, 4); | 353 expect(reconstituted.length, 4); |
| 354 expect(reconstituted[0], "Seattle"); | 354 expect(reconstituted[0], "Seattle"); |
| 355 }); | 355 }); |
| 356 | 356 |
| 357 test("Straight JSON format, nested objects", () { | 357 test("Straight JSON format, nested objects", () { |
| 358 var p1 = new Person()..name = 'Alice'..address = a1; | 358 var p1 = new Person()..name = 'Alice'..address = a1; |
| 359 var s = new Serialization()..selfDescribing = false; | 359 var s = new Serialization()..selfDescribing = false; |
| 360 var addressRule = s.addRuleFor(a1)..configureForMaps(); | 360 var addressRule = s.addRuleFor(a1)..configureForMaps(); |
| 361 var personRule = s.addRuleFor(p1)..configureForMaps(); | 361 var personRule = s.addRuleFor(p1)..configureForMaps(); |
| 362 var writer = s.newWriter(new SimpleJsonFormat(storeRoundTripInfo: true)); | 362 var writer = s.newWriter(const SimpleJsonFormat(storeRoundTripInfo: true)); |
| 363 var out = json.stringify(writer.write(p1)); | 363 var out = json.stringify(writer.write(p1)); |
| 364 var reconstituted = json.parse(out); | 364 var reconstituted = json.parse(out); |
| 365 var expected = { | 365 var expected = { |
| 366 "name" : "Alice", | 366 "name" : "Alice", |
| 367 "rank" : null, | 367 "rank" : null, |
| 368 "serialNumber" : null, | 368 "serialNumber" : null, |
| 369 "_rule" : personRule.number, | 369 "_rule" : personRule.number, |
| 370 "address" : { | 370 "address" : { |
| 371 "street" : "N 34th", | 371 "street" : "N 34th", |
| 372 "city" : "Seattle", | 372 "city" : "Seattle", |
| 373 "state" : null, | 373 "state" : null, |
| 374 "zip" : null, | 374 "zip" : null, |
| 375 "_rule" : addressRule.number | 375 "_rule" : addressRule.number |
| 376 } | 376 } |
| 377 }; | 377 }; |
| 378 expect(expected, reconstituted); | 378 expect(expected, reconstituted); |
| 379 }); | 379 }); |
| 380 | 380 |
| 381 test("Straight JSON format, round-trip", () { | 381 test("Straight JSON format, round-trip", () { |
| 382 // Note that we can't use the usual round-trip test because it has cycles. | 382 // Note that we can't use the usual round-trip test because it has cycles. |
| 383 var p1 = new Person()..name = 'Alice'..address = a1; | 383 var p1 = new Person()..name = 'Alice'..address = a1; |
| 384 // Use maps for one rule, lists for the other. | 384 // Use maps for one rule, lists for the other. |
| 385 var s = new Serialization() | 385 var s = new Serialization() |
| 386 ..addRuleFor(a1) | 386 ..addRuleFor(a1) |
| 387 ..addRuleFor(p1).configureForMaps(); | 387 ..addRuleFor(p1).configureForMaps(); |
| 388 var p2 = writeAndReadBack(s, | 388 var p2 = writeAndReadBack(s, |
| 389 new SimpleJsonFormat(storeRoundTripInfo: true), p1); | 389 const SimpleJsonFormat(storeRoundTripInfo: true), p1); |
| 390 expect(p2.name, "Alice"); | 390 expect(p2.name, "Alice"); |
| 391 var a2 = p2.address; | 391 var a2 = p2.address; |
| 392 expect(a2.street, "N 34th"); | 392 expect(a2.street, "N 34th"); |
| 393 expect(a2.city, "Seattle"); | 393 expect(a2.city, "Seattle"); |
| 394 }); | 394 }); |
| 395 | 395 |
| 396 test("Straight JSON format, non-string key", () { | 396 test("Straight JSON format, non-string key", () { |
| 397 // This tests what happens if we have a key that's not a string. That's | 397 // This tests what happens if we have a key that's not a string. That's |
| 398 // not allowed by json, so we don't actually turn it into a json string, | 398 // not allowed by json, so we don't actually turn it into a json string, |
| 399 // but someone might reasonably convert to a json-able structure without | 399 // but someone might reasonably convert to a json-able structure without |
| 400 // going through the string representation. | 400 // going through the string representation. |
| 401 var p1 = new Person()..name = 'Alice'..address = a1; | 401 var p1 = new Person()..name = 'Alice'..address = a1; |
| 402 var s = new Serialization() | 402 var s = new Serialization() |
| 403 ..addRule(new PersonRuleReturningMapWithNonStringKey()); | 403 ..addRule(new PersonRuleReturningMapWithNonStringKey()); |
| 404 var p2 = writeAndReadBack(s, | 404 var p2 = writeAndReadBack(s, |
| 405 new SimpleJsonFormat(storeRoundTripInfo: true), p1); | 405 const SimpleJsonFormat(storeRoundTripInfo: true), p1); |
| 406 expect(p2.name, "Alice"); | 406 expect(p2.name, "Alice"); |
| 407 expect(p2.address.street, "N 34th"); | 407 expect(p2.address.street, "N 34th"); |
| 408 }); | 408 }); |
| 409 | 409 |
| 410 test("Root is a Map", () { | 410 test("Root is a Map", () { |
| 411 // Note that we can't use the usual round-trip test because it has cycles. | 411 // Note that we can't use the usual round-trip test because it has cycles. |
| 412 var p1 = new Person()..name = 'Alice'..address = a1; | 412 var p1 = new Person()..name = 'Alice'..address = a1; |
| 413 // Use maps for one rule, lists for the other. | 413 // Use maps for one rule, lists for the other. |
| 414 var s = new Serialization() | 414 var s = new Serialization() |
| 415 ..addRuleFor(a1) | 415 ..addRuleFor(a1) |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 455 test("Simple JSON format, round-trip with named objects", () { | 455 test("Simple JSON format, round-trip with named objects", () { |
| 456 // Note that we can't use the usual round-trip test because it has cycles. | 456 // Note that we can't use the usual round-trip test because it has cycles. |
| 457 var p1 = new Person()..name = 'Alice'..address = a1; | 457 var p1 = new Person()..name = 'Alice'..address = a1; |
| 458 // Use maps for one rule, lists for the other. | 458 // Use maps for one rule, lists for the other. |
| 459 var s = new Serialization() | 459 var s = new Serialization() |
| 460 ..selfDescribing = false | 460 ..selfDescribing = false |
| 461 ..addRule(new NamedObjectRule()) | 461 ..addRule(new NamedObjectRule()) |
| 462 ..addRuleFor(a1) | 462 ..addRuleFor(a1) |
| 463 ..addRuleFor(p1).configureForMaps() | 463 ..addRuleFor(p1).configureForMaps() |
| 464 ..namedObjects["foo"] = a1; | 464 ..namedObjects["foo"] = a1; |
| 465 var writer = s.newWriter(new SimpleJsonFormat(storeRoundTripInfo: true)); | 465 var writer = s.newWriter(const SimpleJsonFormat(storeRoundTripInfo: true)); |
| 466 var out = writer.write(p1); | 466 var out = writer.write(p1); |
| 467 var reader = s.newReader(new SimpleJsonFormat(storeRoundTripInfo: true)); | 467 var reader = s.newReader(const SimpleJsonFormat(storeRoundTripInfo: true)); |
| 468 var p2 = reader.read(out, {"foo" : 12}); | 468 var p2 = reader.read(out, {"foo" : 12}); |
| 469 expect(p2.name, "Alice"); | 469 expect(p2.name, "Alice"); |
| 470 var a2 = p2.address; | 470 var a2 = p2.address; |
| 471 expect(a2, 12); | 471 expect(a2, 12); |
| 472 }); | 472 }); |
| 473 | 473 |
| 474 test("More complicated Maps", () { | 474 test("More complicated Maps", () { |
| 475 var s = new Serialization()..selfDescribing = false; | 475 var s = new Serialization()..selfDescribing = false; |
| 476 var p1 = new Person()..name = 'Alice'..address = a1; | 476 var p1 = new Person()..name = 'Alice'..address = a1; |
| 477 var data = new Map(); | 477 var data = new Map(); |
| (...skipping 23 matching lines...) Expand all Loading... |
| 501 expect(input[p2], same(a2)); | 501 expect(input[p2], same(a2)); |
| 502 expect(input[a2], same(p2)); | 502 expect(input[a2], same(p2)); |
| 503 } | 503 } |
| 504 } | 504 } |
| 505 }); | 505 }); |
| 506 | 506 |
| 507 test("Map with string keys stays that way", () { | 507 test("Map with string keys stays that way", () { |
| 508 var s = new Serialization()..addRuleFor(new Person()); | 508 var s = new Serialization()..addRuleFor(new Person()); |
| 509 var data = {"abc" : 1, "def" : "ghi"}; | 509 var data = {"abc" : 1, "def" : "ghi"}; |
| 510 data["person"] = new Person()..name = "Foo"; | 510 data["person"] = new Person()..name = "Foo"; |
| 511 var output = s.write(data, new SimpleMapFormat()); | 511 var output = s.write(data, const SimpleMapFormat()); |
| 512 var mapRule = s.rules.firstWhere((x) => x is MapRule); | 512 var mapRule = s.rules.firstWhere((x) => x is MapRule); |
| 513 var map = output["data"][mapRule.number][0]; | 513 var map = output["data"][mapRule.number][0]; |
| 514 expect(map is Map, isTrue); | 514 expect(map is Map, isTrue); |
| 515 expect(map["abc"], 1); | 515 expect(map["abc"], 1); |
| 516 expect(map["def"], "ghi"); | 516 expect(map["def"], "ghi"); |
| 517 expect(new Reader(s).asReference(map["person"]) is Reference, isTrue); | 517 expect(new Reader(s).asReference(map["person"]) is Reference, isTrue); |
| 518 }); | 518 }); |
| 519 | 519 |
| 520 test("MirrorRule with lookup by qualified name rather than named object", () { | 520 test("MirrorRule with lookup by qualified name rather than named object", () { |
| 521 var s = new Serialization()..addRule(new MirrorRule()); | 521 var s = new Serialization()..addRule(new MirrorRule()); |
| (...skipping 172 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 694 /** | 694 /** |
| 695 * Run a round-trip test on a simple of nodes, but using the flat format | 695 * Run a round-trip test on a simple of nodes, but using the flat format |
| 696 * rather than the maps. | 696 * rather than the maps. |
| 697 */ | 697 */ |
| 698 runRoundTripTestFlat(serializerSetUp) { | 698 runRoundTripTestFlat(serializerSetUp) { |
| 699 Node n1 = new Node("1"), n2 = new Node("2"), n3 = new Node("3"); | 699 Node n1 = new Node("1"), n2 = new Node("2"), n3 = new Node("3"); |
| 700 n1.children = [n2, n3]; | 700 n1.children = [n2, n3]; |
| 701 n2.parent = n1; | 701 n2.parent = n1; |
| 702 n3.parent = n1; | 702 n3.parent = n1; |
| 703 var s = serializerSetUp(n1); | 703 var s = serializerSetUp(n1); |
| 704 var output = s.write(n2, new SimpleFlatFormat()); | 704 var output = s.write(n2, const SimpleFlatFormat()); |
| 705 expect(output is List, isTrue); | 705 expect(output is List, isTrue); |
| 706 var s2 = serializerSetUp(n1); | 706 var s2 = serializerSetUp(n1); |
| 707 var reader = new Reader(s2, new SimpleFlatFormat()); | 707 var reader = new Reader(s2, const SimpleFlatFormat()); |
| 708 var m2 = reader.read(output); | 708 var m2 = reader.read(output); |
| 709 var m1 = m2.parent; | 709 var m1 = m2.parent; |
| 710 expect(m1 is Node, isTrue); | 710 expect(m1 is Node, isTrue); |
| 711 var children = m1.children; | 711 var children = m1.children; |
| 712 expect(m1.name,"1"); | 712 expect(m1.name,"1"); |
| 713 var m3 = m1.children.last; | 713 var m3 = m1.children.last; |
| 714 expect(m2.name, "2"); | 714 expect(m2.name, "2"); |
| 715 expect(m3.name, "3"); | 715 expect(m3.name, "3"); |
| 716 expect(m2.parent, m1); | 716 expect(m2.parent, m1); |
| 717 expect(m3.parent, m1); | 717 expect(m3.parent, m1); |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 755 } | 755 } |
| 756 findValue(String key, Map state) { | 756 findValue(String key, Map state) { |
| 757 var answer; | 757 var answer; |
| 758 for (var each in state.keys) { | 758 for (var each in state.keys) { |
| 759 var value = state[each]; | 759 var value = state[each]; |
| 760 if (value == key) return each; | 760 if (value == key) return each; |
| 761 } | 761 } |
| 762 return null; | 762 return null; |
| 763 } | 763 } |
| 764 } | 764 } |
| OLD | NEW |