1 module dmocks.interval; 2 3 import dmocks.util; 4 import std.conv; 5 import std.exception; 6 7 package: 8 9 struct Interval 10 { 11 bool Valid () { return Min <= Max; } 12 13 private int _min; 14 private int _max; 15 16 @property int Min() 17 { 18 return _min; 19 } 20 21 @property int Max() 22 { 23 return _max; 24 } 25 26 string toString () const 27 { 28 if (_min == _max) 29 return std.conv.to!string(_min); 30 return std.conv.to!string(_min) ~ ".." ~ std.conv.to!string(_max); 31 } 32 33 this (int min, int max) 34 { 35 this._min = min; 36 this._max = max; 37 enforceValid(); 38 } 39 40 void enforceValid() 41 { 42 enforceEx!MocksSetupException(Valid, "Interval: invalid interval range: "~ toString()); 43 } 44 } 45 46 unittest 47 { 48 Interval t = Interval(1, 2); 49 assert (to!string(t) == "1..2"); 50 } 51 52 unittest 53 { 54 Interval t = Interval(1, 1); 55 assert (to!string(t) == "1"); 56 }