1 module plist.tests..string;
2 import plist;
3 import plist.types;
4 
5 unittest {
6     /* Can we even parse the data? */
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     <string>battery correct horse staple</string>
12 </plist>`;
13           Plist parse = new Plist();
14           parse.read(xml);
15           assert(parse.length == 1, "Expected length of 1");
16           assert(parse.write() == xml, "Writing doesn't output the same what we put in");
17           assert(parse[0].type() == PlistElementType.PLIST_ELEMENT_STRING, "Expected string at index 0");
18           PlistElementString str = cast(PlistElementString)parse[0];
19           assert(str.value == "battery correct horse staple", "String value is not the magic number");
20     }
21     /* Blank string */ 
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     <string></string>
27 </plist>`;
28           Plist parse = new Plist();
29           parse.read(xml);
30           assert(parse.length == 1, "Expected length of 1");
31           assert(parse.write() == xml, "Writing doesn't output the same what we put in");
32           assert(parse[0].type() == PlistElementType.PLIST_ELEMENT_STRING, "Expected string at index 0");
33           PlistElementString str = cast(PlistElementString)parse[0];
34           assert(str.value == "", "Expected blank string");
35           assert(str.value.length == 0, "Expected length of 0");
36     }
37     /* Change the data, check */
38     {
39           string xml = `<?xml version="1.0" encoding="UTF-8"?>
40 <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
41 <plist version="1.0">
42     <string>battery correct horse staple</string>
43 </plist>`;
44           Plist parse = new Plist();
45           parse.read(xml);
46           assert(parse.length == 1, "Expected length of 1");
47           assert(parse.write() == xml, "Writing doesn't output the same what we put in");
48           assert(parse[0].type() == PlistElementType.PLIST_ELEMENT_STRING, "Expected string at index 0");
49           PlistElementString str = cast(PlistElementString)parse[0];
50           assert(str.value == "battery correct horse staple", "String value is not the magic number");
51           str.value = "3 is the magic number";
52           assert(str.value == "3 is the magic number", "String value is not the magic number");
53     }
54     /* Change the data, and check the XML */
55     {
56           string xml = `<?xml version="1.0" encoding="UTF-8"?>
57 <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
58 <plist version="1.0">
59     <string>battery correct horse staple</string>
60 </plist>`;
61           string expected = `<?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     <string>3 is the magic number</string>
65 </plist>`;
66 
67           Plist parse = new Plist();
68           parse.read(xml);
69           assert(parse.length == 1, "Expected length of 1");
70           assert(parse.write() == xml, "Writing doesn't output the same what we put in");
71           assert(parse[0].type() == PlistElementType.PLIST_ELEMENT_STRING, "Expected string at index 0");
72           PlistElementString str = cast(PlistElementString)parse[0];
73           assert(str.value == "battery correct horse staple", "String value is not the magic number");
74           str.value = "3 is the magic number";
75           assert(str.value == "3 is the magic number", "String value is not the magic number");
76 
77           assert(parse.write() == expected, "Expected different write");
78     }
79     /* Try something that should obviously fail */
80     {
81           string xml = `<?xml version="1.0" encoding="UTF-8"?>
82 <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
83 <plist version="1.0">
84     <string><true/></string>
85 </plist>`;
86           bool fail = false;
87           Plist parse = new Plist();
88           try { 
89               parse.read(xml);
90           } catch(PlistParsingException e) {
91               fail = true;
92           }
93           assert(fail, "Operation should not have succeeded");
94     }
95     /* Try another thing that should obviously fail */
96     {
97           string xml = `<?xml version="1.0" encoding="UTF-8"?>
98 <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
99 <plist version="1.0">
100     <string><true/><true/></string>
101 </plist>`;
102           bool fail = false;
103           Plist parse = new Plist();
104           try { 
105               parse.read(xml);
106           } catch(PlistParsingException e) {
107               fail = true;
108           }
109           assert(fail, "Operation should not have succeeded");
110     }
111     /* Test key reading */
112     {
113           string xml = `<?xml version="1.0" encoding="UTF-8"?>
114 <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
115 <plist version="1.0">
116     <key>battery correct horse staple</key>
117 </plist>`;
118           Plist parse = new Plist();
119           parse.read(xml);
120           assert(parse.length == 1, "Expected length of 1");
121           assert(parse.write() == xml, "Writing doesn't output the same what we put in");
122           assert(parse[0].type() == "string", "Expected string at index 0");
123           PlistElementString str = cast(PlistElementString)parse[0];
124           assert(str.value == "battery correct horse staple", "String value is not the magic number");
125     }
126 }
127 
128