1 module plist.tests.array; 2 import plist; 3 import plist.types; 4 5 unittest { 6 /* Can we even parse? */ 7 { 8 string xml = `<?xml version="1.0" encoding="UTF-8"?> 9 <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> 10 <plist version="1.0"> 11 <array> 12 <string>Foobar</string> 13 </array> 14 </plist>`; 15 Plist parse = new Plist(); 16 parse.read(xml); 17 18 assert(parse.length == 1, "Expected length of 1"); 19 20 assert(parse.write() == xml, "Writing doesn't output the same what we put in"); 21 22 assert(parse[0].type() == PlistElementType.PLIST_ELEMENT_ARRAY, "Expected array type"); 23 } 24 /* Empty tags should also work */ 25 { 26 string xml = `<?xml version="1.0" encoding="UTF-8"?> 27 <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> 28 <plist version="1.0"> 29 <array/> 30 </plist>`; 31 Plist parse = new Plist(); 32 parse.read(xml); 33 34 assert(parse.length == 1, "Expected length of 1"); 35 36 assert(parse.write() == xml, "Writing doesn't output the same what we put in"); 37 } 38 /* We internally convert an array with length == 0 to an empty tag */ 39 { 40 string xml = `<?xml version="1.0" encoding="UTF-8"?> 41 <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> 42 <plist version="1.0"> 43 <array> 44 </array> 45 </plist>`; 46 string expected = `<?xml version="1.0" encoding="UTF-8"?> 47 <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> 48 <plist version="1.0"> 49 <array/> 50 </plist>`; 51 52 Plist parse = new Plist(); 53 parse.read(xml); 54 55 assert(parse.length == 1, "Expected length of 1"); 56 57 assert(parse.write() == expected, "Unexpected output"); 58 } 59 /* Standard use case: embedded within a dict */ 60 { 61 string xml = `<?xml version="1.0" encoding="UTF-8"?> 62 <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> 63 <plist version="1.0"> 64 <dict> 65 <key>Foo</key> 66 <array> 67 <string>bar</string> 68 </array> 69 </dict> 70 </plist>`; 71 72 Plist parse = new Plist(); 73 parse.read(xml); 74 75 assert(parse.length == 1, "Expected length of 1"); 76 assert(parse.write() == xml, "Did not get out what we put in"); 77 assert(parse[0].type() == PlistElementType.PLIST_ELEMENT_DICT, "Expected dict at index 0"); 78 PlistElementDict dict = cast(PlistElementDict)(parse[0]); 79 assert(dict["Foo"].type() == PlistElementType.PLIST_ELEMENT_ARRAY, "Expected array with key 'Foo'"); 80 PlistElementArray array = cast(PlistElementArray)(dict["Foo"]); 81 assert(array.length == 1, "Incorrect array length"); 82 assert(array[0].type() == PlistElementType.PLIST_ELEMENT_STRING, "Expected string at index 0"); 83 PlistElementString str = cast(PlistElementString)(array[0]); 84 assert(str.value == "bar", "Wrong value for string"); 85 } 86 /* Test appending objects TO the plist */ 87 { 88 string xml = `<?xml version="1.0" encoding="UTF-8"?> 89 <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> 90 <plist version="1.0"> 91 <dict> 92 <key>Foo</key> 93 <array> 94 <string>bar</string> 95 </array> 96 </dict> 97 </plist>`; 98 string expected = `<?xml version="1.0" encoding="UTF-8"?> 99 <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> 100 <plist version="1.0"> 101 <dict> 102 <key>Foo</key> 103 <array> 104 <string>bar</string> 105 <string>beef</string> 106 </array> 107 </dict> 108 </plist>`; 109 110 111 Plist parse = new Plist(); 112 parse.read(xml); 113 114 assert(parse.length == 1, "Expected length of 1"); 115 assert(parse.write() == xml, "Did not get out what we put in"); 116 assert(parse[0].type() == PlistElementType.PLIST_ELEMENT_DICT, "Expected dict at index 0"); 117 PlistElementDict dict = cast(PlistElementDict)(parse[0]); 118 assert(dict["Foo"].type() == PlistElementType.PLIST_ELEMENT_ARRAY, "Expected array with key 'Foo'"); 119 PlistElementArray array = cast(PlistElementArray)(dict["Foo"]); 120 PlistElementString beef = new PlistElementString(); 121 beef.value = "beef"; 122 array ~= cast(PlistElement)beef; 123 assert(parse.write() == expected, "Did not get expected output"); 124 assert(array[1].type() == PlistElementType.PLIST_ELEMENT_STRING, "Expected string at index 1"); 125 assert(array.length == 2, "Size did not grow"); 126 PlistElement[] arr2 = array.entries(); 127 arr2 ~= beef; 128 assert(array.length == 2, "Size should've NOT grown"); 129 } 130 }