1 module dcorelib.valueobjects.id; 2 3 import dcorelib.valueobjects.constrainedstring; 4 5 import std.string; 6 import std.conv; 7 import std.stdio; 8 9 class Id : ConstrainedString 10 { 11 private ubyte colonPos; 12 13 this(string newValue, string identifier = "") @safe 14 { 15 if (newValue.length <= 10) { 16 throw new Exception("An Id field may not have a length of less than 10 characters"); 17 } 18 19 this.colonPos = to!ubyte(newValue.indexOf(':')); 20 21 if (this.colonPos < 1) { 22 throw new Exception("An Id field must contain a : in the ID value"); 23 } 24 25 if ((newValue.length - this.colonPos) < 10) { 26 throw new Exception("An Id field must contain a suffix of at least 10 characters"); 27 } 28 29 // Ids must be at most 45 characters long 30 super(newValue, 45, true, identifier != "" ? identifier : "id"); 31 } 32 33 public string getPrefix() @safe 34 { 35 return this.value[0 .. this.colonPos]; 36 } 37 } 38 39 unittest { 40 /********************** UNIT TESTS THAT SHOULD FAIL ********************/ 41 try { 42 auto id = new Id(""); 43 assert(false, "Was able to create an Id from a blank value"); 44 } catch(Exception e) { 45 46 } 47 48 try { 49 auto id = new Id("APPLE"); 50 assert(false, "Was able to create an Id from a value that was too short"); 51 } catch(Exception e) { 52 53 } 54 55 try { 56 auto id = new Id("APPLEORANGEPEARBANANAAPPLEORANGEPEARBANANAAPPLEORANGEPEARBANANA"); 57 assert(false, "Was able to create an Id from a value that was too long"); 58 } catch(Exception e) { 59 60 } 61 62 try { 63 auto id = new Id("APPLEORANGEPEARBANANAAPPLEORANGEPEAR"); 64 assert(false, "Was able to create an Id from a value that had no semi colon"); 65 } catch(Exception e) { 66 67 } 68 69 try { 70 auto id = new Id("APPLEORANGEPEARBANANAAPPLEORANGEPEAR:123456789"); 71 assert(false, "Was able to create an Id without a sifficient suffix length"); 72 } catch(Exception e) { 73 74 } 75 76 /********************** UNIT TESTS THAT SHOULD PASS ********************/ 77 try { 78 auto id = new Id("02DD6A2A:1509060290"); 79 } catch(Exception e) { 80 assert(false, "Failed to create a valid Id"); 81 } 82 83 auto id = new Id("02DD6A2A:1509060290"); 84 assert(id.toString() == "02DD6A2A:1509060290"); 85 assert(id.getPrefix() == "02DD6A2A"); 86 }