1 module dud.semver.setoperationtest1; 2 3 @safe pure private: 4 import std.exception : assertThrown, assertNotThrown; 5 import std.stdio; 6 import std.format : format; 7 8 import dud.semver.checks; 9 import dud.semver.parse; 10 import dud.semver.semver; 11 import dud.semver.versionrange; 12 import dud.semver.versionunion; 13 import dud.semver.setoperation; 14 15 immutable SemVer v1 = SemVer(1,0,0); 16 immutable SemVer v2 = SemVer(2,0,0); 17 immutable SemVer v3 = SemVer(3,0,0); 18 immutable SemVer v4 = SemVer(4,0,0); 19 immutable SemVer v5 = SemVer(5,0,0); 20 21 immutable VersionRange vr1 = VersionRange(v1, Inclusive.yes, v2, Inclusive.yes); 22 immutable VersionRange vr2 = VersionRange(v2, Inclusive.yes, v3, Inclusive.yes); 23 immutable VersionRange vr3 = VersionRange(v3, Inclusive.yes, v4, Inclusive.yes); 24 immutable VersionRange vr4 = VersionRange(v1, Inclusive.yes, v3, Inclusive.yes); 25 immutable VersionRange vr5 = VersionRange(v2, Inclusive.yes, v5, Inclusive.yes); 26 27 // 28 // intersectionOf 29 // 30 31 // SemVer, SemVer 32 unittest { 33 SemVer r = intersectionOf(v1, v1); 34 assert(r == v1); 35 36 r = intersectionOf(v1, v2); 37 assert(r == SemVer.init); 38 } 39 40 // VersionRange, SemVer 41 unittest { 42 SemVer r = intersectionOf(vr1, v1); 43 assert(r == v1); 44 45 r = intersectionOf(vr1, v3); 46 assert(r == SemVer.init); 47 48 assert(intersectionOf(v3, vr1) == r); 49 } 50 51 // VersionUnion, SemVer 52 unittest { 53 const vu = VersionUnion([vr1, vr2]); 54 SemVer r = intersectionOf(vu, v1); 55 assert(r == v1); 56 assert(r == intersectionOf(v1, vu)); 57 58 r = intersectionOf(vu, v4); 59 assert(r == SemVer.init); 60 61 } 62 63 // VersionRange, VersionRange 64 unittest { 65 const VersionRange r = intersectionOf(vr4, vr5); 66 assert(r == vr2); 67 } 68 69 unittest { 70 const vr6 = VersionRange(v2, Inclusive.no, v5, Inclusive.yes); 71 const VersionRange r = intersectionOf(vr5, vr6); 72 assert(r == vr6); 73 } 74 75 unittest { 76 const vr6 = VersionRange(v2, Inclusive.yes, v5, Inclusive.no); 77 const VersionRange r = intersectionOf(vr5, vr6); 78 assert(r == vr6); 79 } 80 81 unittest { 82 const VersionRange r = intersectionOf(vr1, vr2); 83 assert(r != VersionRange.init); 84 assert( allowsAll(r, v2)); 85 assert(!allowsAll(r, v1)); 86 assert(!allowsAll(r, v3)); 87 88 const VersionRange r2 = intersectionOf(vr2, vr1); 89 assert(r != VersionRange.init); 90 assert( allowsAll(r, v2)); 91 assert(!allowsAll(r, v1)); 92 assert(!allowsAll(r, v3)); 93 assert(r == r2); 94 } 95 96 unittest { 97 const VersionRange r = intersectionOf(vr1, vr3); 98 assert(r == VersionRange.init); 99 } 100 101 unittest { 102 const VersionRange a = parseVersionRange(">=1.0.0 <2.0.0").get(); 103 const VersionRange b = parseVersionRange(">=1.0.0 <=3.0.0").get(); 104 const VersionRange c = intersectionOf(a, b); 105 const VersionRange r = parseVersionRange(">=1.0.0 <2.0.0").get(); 106 assert(c == r, format("%s", c)); 107 } 108 109 // VersionUnion, VersionRange 110 unittest { 111 const v6 = SemVer(2,5,0); 112 const v7 = SemVer(4,5,0); 113 114 const vr6 = VersionRange(v6, Inclusive.yes, v7, Inclusive.yes); 115 const vr7 = VersionRange(v4, Inclusive.yes, v5, Inclusive.yes); 116 117 const vu = VersionUnion([vr1, vr2, vr7]); 118 const r = intersectionOf(vu, vr6); 119 120 assert(r.ranges.length == 2, format("\n%s", r)); 121 assert(r.ranges[0] == VersionRange(v6, Inclusive.yes, v3, Inclusive.yes)); 122 assert(r.ranges[1] == VersionRange(v4, Inclusive.yes, v7, Inclusive.yes)); 123 124 assert(r == intersectionOf(vr6, vu)); 125 } 126 127 unittest { 128 const VersionRange a = parseVersionRange(">=1.0.0 <2.0.0").get(); 129 const VersionRange b = parseVersionRange(">2.0.0 <=3.0.0").get(); 130 131 const VersionUnion ab = VersionUnion([a, b]); 132 assert(ab.ranges.length == 2); 133 assert(ab.ranges[0] == a); 134 assert(ab.ranges[1] == b); 135 136 const VersionRange c = parseVersionRange(">=1.0.0 <=3.0.0").get(); 137 const VersionUnion abc = intersectionOf(ab, c); 138 assert(abc.ranges.length == 2, format("%s", abc.ranges.length)); 139 assert(ab.ranges[0] == a); 140 assert(ab.ranges[1] == b); 141 } 142 143 // VersionUnion, VersionUnion 144 unittest { 145 const vu1 = VersionUnion([vr1, vr3]); 146 assert(vu1.ranges.length == 2); 147 const vu2 = VersionUnion([vr2]); 148 149 const r = intersectionOf(vu1, vu2); 150 assert(r.ranges.length == 2); 151 assert(r.ranges[0] == VersionRange(v2, Inclusive.yes, v2, Inclusive.yes)); 152 assert(r.ranges[1] == VersionRange(v3, Inclusive.yes, v3, Inclusive.yes)); 153 }