1 /** 2 * Copyright: Copyright (c) 2011 Jacob Carlborg. All rights reserved. 3 * Authors: Jacob Carlborg 4 * Version: Initial created: Aug 7, 2011 5 * License: $(LINK2 http://www.boost.org/LICENSE_1_0.txt, Boost Software License 1.0) 6 */ 7 module spec.serialization.Events; 8 9 import dspec.Dsl; 10 11 import mambo.core._; 12 import mambo.serialization.Serializer; 13 import mambo.serialization.Events; 14 import mambo.serialization.archives.XmlArchive; 15 import spec.serialization.Util; 16 17 Serializer serializer; 18 XmlArchive!(char) archive; 19 20 int b; 21 int c; 22 23 class Events 24 { 25 int a; 26 int d; 27 28 void serializing () 29 { 30 a = 3; 31 } 32 33 mixin OnSerializing!(serializing); 34 35 void serialized () 36 { 37 b = 4; 38 } 39 40 mixin OnSerialized!(serialized); 41 42 void deserializing () 43 { 44 c = 5; 45 } 46 47 mixin OnDeserializing!(deserializing); 48 49 void deserialized () 50 { 51 d = 6; 52 } 53 54 mixin OnDeserialized!(deserialized); 55 } 56 57 Events events; 58 59 unittest 60 { 61 archive = new XmlArchive!(char); 62 serializer = new Serializer(archive); 63 64 events = new Events; 65 66 describe! "serialize a class with event handlers" in { 67 it! "should return serialized class with the correct values set by the event handlers" in { 68 serializer.reset; 69 serializer.serialize(events); 70 71 assert(archive.data().containsDefaultXmlContent()); 72 assert(archive.data().containsXmlTag("object", `runtimeType="spec.serialization.Events.Events" type="spec.serialization.Events.Events" key="0" id="0"`)); 73 assert(archive.data().containsXmlTag("int", `key="a" id="1"`, "3")); 74 assert(archive.data().containsXmlTag("int", `key="d" id="2"`, "0")); 75 76 assert(b == 4); 77 }; 78 }; 79 80 describe! "deserialize class with a base class" in { 81 it! "should return a deserialized string equal to the original string" in { 82 auto eventsDeserialized = serializer.deserialize!(Events)(archive.untypedData); 83 84 assert(eventsDeserialized.a == 3); 85 assert(eventsDeserialized.d == 6); 86 87 assert(c == 5); 88 }; 89 }; 90 }