1 module plist.tests._real; 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 <dict> 12 </dict> 13 </plist>`; 14 Plist parse = new Plist(); 15 parse.read(xml); 16 17 assert(parse.length == 1, "Expected length of 1"); 18 19 assert(parse.write() == xml, "Writing doesn't output the same what we put in"); 20 } 21 /* Test whether or not we get a root dictionary */ 22 { 23 string xml = `<?xml version="1.0" encoding="UTF-8"?> 24 <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> 25 <plist version="1.0"> 26 <dict> 27 </dict> 28 </plist>`; 29 Plist parse = new Plist(); 30 parse.read(xml); 31 32 assert(parse.length == 1, "Expected length of 1"); 33 PlistElementDict root = cast(PlistElementDict)parse[0]; 34 } 35 /* Test the value of foobar */ 36 { 37 string xml = `<?xml version="1.0" encoding="UTF-8"?> 38 <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> 39 <plist version="1.0"> 40 <dict> 41 <key>Foobar</key> 42 <real>1.1</real> 43 </dict> 44 </plist>`; 45 Plist parse = new Plist(); 46 parse.read(xml); 47 48 assert(parse.length == 1, "Expected length of 1"); 49 PlistElementDict root = cast(PlistElementDict)parse[0]; 50 assert(root.keys.length == 1, "Expected 1 child"); 51 import std.algorithm.searching; 52 assert(root.keys.canFind("Foobar"), "Expected key to be present in AA keys"); 53 assert(root["Foobar"].type() == PlistElementType.PLIST_ELEMENT_REAL, "Incorrect type found"); 54 PlistElementReal elmt = cast(PlistElementReal)root["Foobar"]; 55 assert(elmt.value == 1.1, "Did not get value properly"); 56 } 57 { 58 string xml = `<?xml version="1.0" encoding="UTF-8"?> 59 <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> 60 <plist version="1.0"> 61 <dict> 62 <key>Foobar</key> 63 <real>1.337</real> 64 </dict> 65 </plist>`; 66 Plist parse = new Plist(); 67 parse.read(xml); 68 69 assert(parse.length == 1, "Expected length of 1"); 70 PlistElementDict root = cast(PlistElementDict)parse[0]; 71 assert(root.keys.length == 1, "Expected 1 child"); 72 import std.algorithm.searching; 73 assert(root.keys.canFind("Foobar"), "Expected key to be present in AA keys"); 74 PlistElementReal elmt = cast(PlistElementReal)root["Foobar"]; 75 assert(elmt.value == 1.337, "Did not get value properly"); 76 } 77 /* Even though this real does not have a decimal, it can still be one */ 78 { 79 string xml = `<?xml version="1.0" encoding="UTF-8"?> 80 <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> 81 <plist version="1.0"> 82 <dict> 83 <key>Foobar</key> 84 <real>41414141414141</real> 85 </dict> 86 </plist>`; 87 Plist parse = new Plist(); 88 parse.read(xml); 89 90 assert(parse.length == 1, "Expected length of 1"); 91 PlistElementDict root = cast(PlistElementDict)parse[0]; 92 assert(root.keys.length == 1, "Expected 1 child"); 93 import std.algorithm.searching; 94 assert(root.keys.canFind("Foobar"), "Expected key to be present in AA keys"); 95 PlistElementReal elmt = cast(PlistElementReal)root["Foobar"]; 96 assert(elmt.value == 41414141414141.0, "Did not get value properly"); 97 } 98 /* This should fail */ 99 { 100 string xml = `<?xml version="1.0" encoding="UTF-8"?> 101 <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> 102 <plist version="1.0"> 103 <dict> 104 <key>Foobar</key> 105 <real>0x1234</real> 106 </dict> 107 </plist>`; 108 Plist parse = new Plist(); 109 bool passed = false; 110 try { 111 parse.read(xml); 112 } catch(Exception e) { 113 passed = true; 114 } 115 116 assert(passed, "This should not work"); 117 } 118 /* Test negative numbers! */ 119 { 120 string xml = `<?xml version="1.0" encoding="UTF-8"?> 121 <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> 122 <plist version="1.0"> 123 <dict> 124 <key>Foobar</key> 125 <real>-41414141414141</real> 126 </dict> 127 </plist>`; 128 Plist parse = new Plist(); 129 parse.read(xml); 130 131 assert(parse.length == 1, "Expected length of 1"); 132 PlistElementDict root = cast(PlistElementDict)parse[0]; 133 assert(root.keys.length == 1, "Expected 1 child"); 134 import std.algorithm.searching; 135 assert(root.keys.canFind("Foobar"), "Expected key to be present in AA keys"); 136 PlistElementReal elmt = cast(PlistElementReal)root["Foobar"]; 137 assert(elmt.value == -41414141414141.0, "Did not get value properly"); 138 } 139 /* Test float precision */ 140 { 141 string xml = `<?xml version="1.0" encoding="UTF-8"?> 142 <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> 143 <plist version="1.0"> 144 <dict> 145 <key>Foobar</key> 146 <real>0.00041414</real> 147 </dict> 148 </plist>`; 149 Plist parse = new Plist(); 150 parse.read(xml); 151 152 assert(parse.length == 1, "Expected length of 1"); 153 PlistElementDict root = cast(PlistElementDict)parse[0]; 154 assert(root.keys.length == 1, "Expected 1 child"); 155 import std.algorithm.searching; 156 assert(root.keys.canFind("Foobar"), "Expected key to be present in AA keys"); 157 PlistElementReal elmt = cast(PlistElementReal)root["Foobar"]; 158 assert(elmt.value == 0.00041414, "Did not get value properly"); 159 } 160 /* Test negative float precision */ 161 { 162 string xml = `<?xml version="1.0" encoding="UTF-8"?> 163 <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> 164 <plist version="1.0"> 165 <dict> 166 <key>Foobar</key> 167 <real>-0.00041414</real> 168 </dict> 169 </plist>`; 170 Plist parse = new Plist(); 171 parse.read(xml); 172 173 assert(parse.length == 1, "Expected length of 1"); 174 PlistElementDict root = cast(PlistElementDict)parse[0]; 175 assert(root.keys.length == 1, "Expected 1 child"); 176 import std.algorithm.searching; 177 assert(root.keys.canFind("Foobar"), "Expected key to be present in AA keys"); 178 PlistElementReal elmt = cast(PlistElementReal)root["Foobar"]; 179 assert(elmt.value == -0.00041414, "Did not get value properly"); 180 } 181 /* Test setting the real */ 182 { 183 string xml = `<?xml version="1.0" encoding="UTF-8"?> 184 <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> 185 <plist version="1.0"> 186 <dict> 187 <key>Foobar</key> 188 <real>-0.00041414</real> 189 </dict> 190 </plist>`; 191 Plist parse = new Plist(); 192 parse.read(xml); 193 194 assert(parse.length == 1, "Expected length of 1"); 195 PlistElementDict root = cast(PlistElementDict)parse[0]; 196 assert(root.keys.length == 1, "Expected 1 child"); 197 import std.algorithm.searching; 198 assert(root.keys.canFind("Foobar"), "Expected key to be present in AA keys"); 199 PlistElementReal elmt = cast(PlistElementReal)root["Foobar"]; 200 assert(elmt.value == -0.00041414, "Did not get value properly"); 201 elmt.value = 4141414; 202 assert(elmt.value == 4141414, "Could not properly set value when in scope"); 203 } 204 /* Test setting the real, then GETTING it via the dict */ 205 { 206 string xml = `<?xml version="1.0" encoding="UTF-8"?> 207 <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> 208 <plist version="1.0"> 209 <dict> 210 <key>Foobar</key> 211 <real>-0.00041414</real> 212 </dict> 213 </plist>`; 214 Plist parse = new Plist(); 215 parse.read(xml); 216 217 assert(parse.length == 1, "Expected length of 1"); 218 PlistElementDict root = cast(PlistElementDict)parse[0]; 219 assert(root.keys.length == 1, "Expected 1 child"); 220 import std.algorithm.searching; 221 assert(root.keys.canFind("Foobar"), "Expected key to be present in AA keys"); 222 PlistElementReal elmt = cast(PlistElementReal)root["Foobar"]; 223 assert(elmt.value == -0.00041414, "Did not get value properly"); 224 elmt.value = 4141414; 225 assert(elmt.value == 4141414, "Could not properly set value when in scope"); 226 227 assert((cast(PlistElementReal)root["Foobar"]).value == 4141414, "Could not get value via dict root"); 228 (cast(PlistElementReal)root["Foobar"]).value = 1234; 229 assert((cast(PlistElementReal)root["Foobar"]).value == 1234, "Could not get value via dict root"); 230 231 elmt.value = -0.00041414; 232 assert(parse.write() == xml, "Generated output is not the same was what was put in"); 233 } 234 }