1 /** 2 * Copyright: Copyright (c) 2011 Jacob Carlborg. All rights reserved. 3 * Authors: Jacob Carlborg 4 * Version: Initial created: Aug 6, 2011 5 * License: $(LINK2 http://www.boost.org/LICENSE_1_0.txt, Boost Software License 1.0) 6 */ 7 module spec.serialization.AssociativeArray; 8 9 import dspec.Dsl; 10 11 import mambo.core..string; 12 import mambo.serialization.Serializer; 13 import mambo.serialization.archives.XmlArchive; 14 import spec.serialization.Util; 15 16 Serializer serializer; 17 XmlArchive!(char) archive; 18 19 class E 20 { 21 int[int] aa; 22 } 23 24 E e; 25 26 unittest 27 { 28 archive = new XmlArchive!(char); 29 serializer = new Serializer(archive); 30 31 e = new E; 32 e.aa = [3 : 4, 1 : 2, 39 : 472, 6 : 7]; 33 34 describe! "serialize associative array" in { 35 it! "should return a serialized associative array" in { 36 serializer.reset(); 37 serializer.serialize(e); 38 39 assert(archive.data().containsDefaultXmlContent()); 40 assert(archive.data().containsXmlTag("object", `runtimeType="spec.serialization.AssociativeArray.E" type="spec.serialization.AssociativeArray.E" key="0" id="0"`)); 41 assert(archive.data().containsXmlTag("associativeArray", `keyType="int" valueType="int" length="4" key="aa" id="1"`)); 42 43 assert(archive.data().containsXmlTag("key", `key="0"`)); 44 assert(archive.data().containsXmlTag("int", `key="0" id="2"`, "1")); 45 assert(archive.data().containsXmlTag("value", `key="0"`)); 46 assert(archive.data().containsXmlTag("int", `key="0" id="3"`, "2")); 47 48 assert(archive.data().containsXmlTag("key", `key="1"`)); 49 assert(archive.data().containsXmlTag("int", `key="1" id="4"`, "3")); 50 assert(archive.data().containsXmlTag("value", `key="1"`)); 51 assert(archive.data().containsXmlTag("int", `key="1" id="5"`, "4")); 52 53 assert(archive.data().containsXmlTag("key", `key="2"`)); 54 assert(archive.data().containsXmlTag("int", `key="2" id="6"`, "6")); 55 assert(archive.data().containsXmlTag("value", `key="2"`)); 56 assert(archive.data().containsXmlTag("int", `key="2" id="7"`, "7")); 57 58 assert(archive.data().containsXmlTag("key", `key="3"`)); 59 assert(archive.data().containsXmlTag("int", `key="3" id="8"`, "39")); 60 assert(archive.data().containsXmlTag("value", `key="3"`)); 61 assert(archive.data().containsXmlTag("int", `key="3" id="9"`, "472")); 62 }; 63 }; 64 65 describe! "deserialize associative array" in { 66 it! "should return an associative array equal to the original associative array" in { 67 auto eDeserialized = serializer.deserialize!(E)(archive.untypedData); 68 69 foreach (k, v ; eDeserialized.aa) 70 assert(e.aa[k] == v); 71 72 version (D_Version2) 73 assert(e.aa == eDeserialized.aa); 74 }; 75 }; 76 }