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 tests.Util; 8 9 import std.algorithm; 10 import std.array; 11 import std.xml; 12 13 /** 14 * Returns $(D_KEYWORD true) if the array contains the given pattern. 15 * 16 * Params: 17 * arr = the array to check if it contains the element 18 * pattern = the pattern whose presence in the array is to be tested 19 * 20 * Returns: $(D_KEYWORD true) if the array contains the given pattern 21 */ 22 bool contains (T) (T[] arr, T[] pattern) 23 { 24 return !arr.find(pattern).empty; 25 } 26 27 bool equalToXml(string expected, string actual) 28 { 29 import std.string; 30 31 return new Document(expected.strip).isEqual(new Document(actual.strip)); 32 } 33 34 private: 35 36 T toType(T)(Object o) 37 { 38 auto t = cast(T)(o); 39 40 if (t is null) 41 throw new Exception("Attempt to compare a " ~ T.stringof ~ " with an instance of another type"); 42 43 return t; 44 } 45 46 bool isEqual(Document lhs, Object rhs) 47 { 48 auto doc = toType!(Document)(rhs); 49 return 50 (lhs.prolog != doc.prolog ) ? false : ( 51 (!isEqual(cast(Element) lhs, doc) ) ? false : ( 52 (lhs.epilog != doc.epilog ) ? false : ( 53 true ))); 54 } 55 56 bool isEqual(Element lhs, Object rhs) 57 { 58 import std.algorithm; 59 import std.range; 60 61 auto element = toType!(Element)(rhs); 62 63 if (lhs.tag != element.tag) 64 return false; 65 66 return lhs.items.zip(element.items).all!(e => e[0].isEqual(e[1])); 67 } 68 69 bool isEqual(Item lhs, Object rhs) 70 { 71 if (auto o = cast(Document) lhs) return o.isEqual(rhs); 72 if (auto o = cast(Element) lhs) return o.isEqual(rhs); 73 else return lhs == rhs; 74 }