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.Slice; 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 J 20 { 21 int[] firstSource; 22 int[] firstSlice; 23 24 int[] secondSlice; 25 int[] secondSource; 26 27 int[] firstEmpty; 28 int[] secondEmpty; 29 30 int[][] thirdEmpty; 31 } 32 33 J j; 34 J jDeserialized; 35 import mambo.core.io; 36 unittest 37 { 38 archive = new XmlArchive!(char); 39 serializer = new Serializer(archive); 40 41 j = new J; 42 j.firstSource = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9].dup; 43 j.firstSlice = j.firstSource[3 .. 7]; 44 45 j.secondSource = [10, 11, 12, 13, 14, 15].dup; 46 j.secondSlice = j.secondSource[1 .. 4]; 47 48 describe! "serialize slices" in { 49 serializer.reset(); 50 serializer.serialize(j); 51 52 it! "should return serialized slices" in { 53 54 assert(archive.data().containsDefaultXmlContent()); 55 assert(archive.data().containsXmlTag("object", `runtimeType="spec.serialization.Slice.J" type="spec.serialization.Slice.J" key="0" id="0"`)); 56 assert(archive.data().containsXmlTag("array", `type="int" length="10" key="firstSource" id="1"`)); 57 58 assert(archive.data().containsXmlTag("int", `key="0" id="2"`, "0")); 59 assert(archive.data().containsXmlTag("int", `key="1" id="3"`, "1")); 60 assert(archive.data().containsXmlTag("int", `key="2" id="4"`, "2")); 61 assert(archive.data().containsXmlTag("int", `key="3" id="5"`, "3")); 62 assert(archive.data().containsXmlTag("int", `key="4" id="6"`, "4")); 63 assert(archive.data().containsXmlTag("int", `key="5" id="7"`, "5")); 64 assert(archive.data().containsXmlTag("int", `key="6" id="8"`, "6")); 65 assert(archive.data().containsXmlTag("int", `key="7" id="9"`, "7")); 66 assert(archive.data().containsXmlTag("int", `key="8" id="10"`, "8")); 67 assert(archive.data().containsXmlTag("int", `key="9" id="11"`, "9")); 68 69 assert(archive.data().containsXmlTag("slice", `key="firstSlice" offset="3" length="4"`, "1")); 70 assert(archive.data().containsXmlTag("slice", `key="secondSlice" offset="1" length="3"`, "21")); 71 72 assert(archive.data().containsXmlTag("array", `type="int" length="6" key="secondSource" id="21"`)); 73 74 assert(archive.data().containsXmlTag("int", `key="0" id="22"`, "10")); 75 assert(archive.data().containsXmlTag("int", `key="1" id="23"`, "11")); 76 assert(archive.data().containsXmlTag("int", `key="2" id="24"`, "12")); 77 assert(archive.data().containsXmlTag("int", `key="3" id="25"`, "13")); 78 assert(archive.data().containsXmlTag("int", `key="4" id="26"`, "14")); 79 assert(archive.data().containsXmlTag("int", `key="5" id="27"`, "15")); 80 81 assert(archive.data().containsXmlTag("array", `type="int" length="0" key="firstEmpty" id="28"`, true)); 82 assert(archive.data().containsXmlTag("array", `type="int" length="0" key="secondEmpty" id="29"`, true)); 83 assert(archive.data().containsXmlTag("array", `type="int[]" length="0" key="thirdEmpty" id="30"`, true)); 84 }; 85 86 it! "should not contain slices to empty arrays" in { 87 assert(!archive.data().containsXmlTag("slice", `key="firstEmpty" offset="0" length="0"`, "30")); 88 assert(!archive.data().containsXmlTag("slice", `key="secondEmpty" offset="0" length="0"`, "30")); 89 assert(!archive.data().containsXmlTag("slice", `key="thirdEmpty" offset="0" length="0"`, "28")); 90 }; 91 }; 92 93 describe! "deserialize slices" in { 94 jDeserialized = serializer.deserialize!(J)(archive.untypedData); 95 96 it! "should return deserialized strings equal to the original strings" in { 97 assert(j.firstSource == jDeserialized.firstSource); 98 assert(j.secondSource == jDeserialized.secondSource); 99 }; 100 101 it! "should return deserialized slices equal to the original slices" in { 102 assert(j.firstSlice == jDeserialized.firstSlice); 103 assert(j.secondSlice == jDeserialized.secondSlice); 104 }; 105 106 it! "the slices should be equal to a slice of the original sources" in { 107 assert(jDeserialized.firstSource[3 .. 7] == jDeserialized.firstSlice); 108 assert(jDeserialized.secondSource[1 .. 4] == jDeserialized.secondSlice); 109 110 assert(j.firstSource[3 .. 7] == jDeserialized.firstSlice); 111 assert(j.secondSource[1 .. 4] == jDeserialized.secondSlice); 112 }; 113 114 it! "the slices should be able to modify the sources" in { 115 jDeserialized.firstSlice[0] = 55; 116 jDeserialized.secondSlice[0] = 3; 117 118 assert(jDeserialized.firstSource == [0, 1, 2, 55, 4, 5, 6, 7, 8, 9]); 119 assert(jDeserialized.secondSource == [10, 3, 12, 13, 14, 15]); 120 }; 121 }; 122 }