1 module text.xml.ValidationTest; 2 3 import dshould; 4 import std.algorithm; 5 import std.array; 6 import text.xml.Parser; 7 import text.xml.Tree; 8 import text.xml.Validation; 9 import text.xml.XmlException; 10 11 @("passes when element has required name") 12 unittest 13 { 14 // given 15 XmlNode root = parse("<Element/>"); 16 17 // when/then 18 root.enforceName("Element"); 19 } 20 21 @("fails when element does not have required name") 22 unittest 23 { 24 // given 25 XmlNode root = parse("<element/>"); 26 27 // when/then 28 root.enforceName("Element").should.throwAn!XmlException; 29 } 30 31 @("passes when required attribute has fixed value") 32 unittest 33 { 34 // given 35 XmlNode root = parse("<Element name='expected'/>"); 36 37 // when/then 38 root.enforceFixed("name", "expected"); 39 } 40 41 @("fails when required attribute does not have fixed value") 42 unittest 43 { 44 // given 45 XmlNode root = parse("<Element name='unexpected'/>"); 46 47 // when/then 48 root.enforceFixed("name", "expected").should.throwAn!XmlException; 49 } 50 51 @("gets required descendant") 52 unittest 53 { 54 // given 55 XmlNode root = parse("<Element><Child><Descendant/></Child></Element>"); 56 57 // when 58 XmlNode descendant = root.requireDescendant("Descendant"); 59 60 // then 61 descendant.tag.should.equal("Descendant"); 62 } 63 64 @("fails to get missing descendant") 65 unittest 66 { 67 // given 68 XmlNode root = parse("<Element/>"); 69 70 // when/then 71 root.requireDescendant("Descendant").should.throwAn!XmlException; 72 } 73 74 @("fails to get non-specific descendant") 75 unittest 76 { 77 // given 78 XmlNode root = parse("<Element><Child><Descendant/></Child><Descendant/></Element>"); 79 80 // when/then 81 root.requireDescendant("Descendant").should.throwAn!XmlException; 82 83 } 84 85 @("gets required child by name") 86 unittest 87 { 88 // given 89 XmlNode root = parse("<Element><Child/></Element>"); 90 91 // when 92 XmlNode child = root.requireChild("Child"); 93 94 // then 95 child.tag.should.equal("Child"); 96 } 97 98 @("fails to get missing child by name") 99 unittest 100 { 101 // given 102 XmlNode root = parse("<Element/>"); 103 104 // when/then 105 root.requireChild("Child").should.throwAn!XmlException; 106 } 107 108 @("fails to get non-specific child by name") 109 unittest 110 { 111 // given 112 XmlNode root = parse("<Element><Child/><Child/></Element>"); 113 114 // when/then 115 root.requireChild("Child").should.throwAn!XmlException; 116 117 } 118 119 @("gets required child") 120 unittest 121 { 122 // given 123 XmlNode root = parse("<Element><Child/></Element>"); 124 125 // when 126 XmlNode child = root.requireChild; 127 128 // then 129 child.tag.should.equal("Child"); 130 } 131 132 @("fails to get missing child") 133 unittest 134 { 135 // given 136 XmlNode root = parse("<Element/>"); 137 138 // when/then 139 root.requireChild.should.throwAn!XmlException; 140 } 141 142 @("fails to get non-specific child") 143 unittest 144 { 145 // given 146 XmlNode root = parse("<Element><Child/><Child/></Element>"); 147 148 // when/then 149 root.requireChild.should.throwAn!XmlException; 150 151 } 152 153 @("gets required content") 154 unittest 155 { 156 // given 157 XmlNode root = parse("<Element> -1 </Element>"); 158 159 // when 160 long value = root.require!long; 161 162 // then 163 value.should.equal(-1); 164 } 165 166 @("fails to get invalid content") 167 unittest 168 { 169 // given 170 XmlNode root = parse("<Element> 0xab </Element>"); 171 172 // when/then 173 root.require!int.should.throwAn!XmlException; 174 } 175 176 @("gets required value") 177 unittest 178 { 179 // given 180 XmlNode root = parse("<Element name=' -1 '/>"); 181 182 // when 183 long value = root.require!long("name"); 184 185 // then 186 value.should.equal(-1); 187 } 188 189 @("fails to get missing value") 190 unittest 191 { 192 // given 193 XmlNode root = parse("<Element Name='42'/>"); 194 195 // when/then 196 root.require!int("name").should.throwAn!XmlException; 197 } 198 199 @("fails to get invalid value") 200 unittest 201 { 202 // given 203 XmlNode root = parse("<Element name='0xab'/>"); 204 205 // when/then 206 root.require!int("name").should.throwAn!XmlException; 207 } 208 209 @("gets required value instead of fallback") 210 unittest 211 { 212 // given 213 XmlNode root = parse("<Element name=' -1 '/>"); 214 215 // when 216 long value = root.require!long("name", 42); 217 218 // then 219 value.should.equal(-1); 220 } 221 222 @("gets fallback when required value is missing") 223 unittest 224 { 225 //given 226 XmlNode root = parse("<Element Name=' -1 '/>"); 227 228 // when 229 long value = root.require!long("name", 42); 230 231 // then 232 value.should.equal(42); 233 } 234 235 @("fails to get invalid value instead of fallback") 236 unittest 237 { 238 // given 239 XmlNode root = parse("<Element name='0xab'/>"); 240 241 // when/then 242 root.require!int("name", 42).should.throwAn!XmlException; 243 }