1 module dud.pkgdescription.testhelper; 2 3 import std.json; 4 import std.stdio; 5 import std.format; 6 import dud.pkgdescription; 7 8 @safe: 9 10 enum TestFailKind { 11 fromJsonOrig, 12 fromSDLOrig, 13 toJson, 14 toSDL, 15 fromJsonCopy, 16 fromSDLCopy, 17 validate, 18 cmp 19 } 20 21 package void unRollException(Exception e, string f) { 22 import std.stdio : writefln; 23 24 Throwable en = e; 25 writefln("%s", f); 26 while(en.next !is null) { 27 en = en.next; 28 } 29 writefln("%s", en.msg); 30 } 31 32 JSONValue testToJson(PackageDescription pkg, string f, 33 ref size_t[TestFailKind] failCnt) 34 { 35 import dud.pkgdescription.output : toJSON; 36 try { 37 return toJSON(pkg); 38 } catch(Exception e) { 39 unRollException(e, f); 40 incrementFailCnt(failCnt, TestFailKind.toJson); 41 } 42 return JSONValue.init; 43 } 44 45 void incrementFailCnt(ref size_t[TestFailKind] aa, TestFailKind tfk) pure { 46 size_t* p = tfk in aa; 47 if(p !is null) { 48 (*p)++; 49 } else { 50 aa[tfk] = 1; 51 } 52 } 53 54 bool fromJsonTest(JSONValue js, ref const(PackageDescription) pkg, 55 string f, ref size_t[TestFailKind] failCnt) { 56 try { 57 PackageDescription nPkg = jsonToPackageDescription(js); 58 assert(pkg == nPkg, 59 () @trusted { 60 return format("%s\nexp:\n%s\ngot:\n%s", f, pkg, nPkg); 61 }()); 62 return true; 63 } catch(Exception e) { 64 unRollException(e, f); 65 incrementFailCnt(failCnt, TestFailKind.fromJsonCopy); 66 } 67 return false; 68 } 69 70 PackageDescription ddupTest(ref const(PackageDescription) old, string f, 71 ref size_t[TestFailKind] failCnt) 72 { 73 import dud.pkgdescription.duplicate : ddup = dup; 74 import dud.pkgdescription.validation; 75 import dud.pkgdescription.exception; 76 PackageDescription copy = ddup(old); 77 try { 78 validate(copy); 79 } catch(ValidationException ve) { 80 unRollException(ve, f); 81 incrementFailCnt(failCnt, TestFailKind.validate); 82 } 83 return copy; 84 } 85 86 string testToSDL(PackageDescription pkg, string f, 87 ref size_t[TestFailKind] failCnt) 88 { 89 import dud.pkgdescription.output : toSDL; 90 try { 91 return toSDL(pkg); 92 } catch(Exception e) { 93 unRollException(e, f); 94 incrementFailCnt(failCnt, TestFailKind.toSDL); 95 } 96 return ""; 97 }