1 module dshould.contain; 2 3 import dshould.ShouldType; 4 5 unittest 6 { 7 import dshould.basic : not, should; 8 9 [2, 3, 4].should.contain([3]); 10 [2, 3, 4].should.contain([4, 3]); 11 [2, 3, 4].should.not.contain([5]); 12 [3, 4].should.only.contain([4, 3]); 13 [3, 4].should.only.contain([1, 2, 3, 4]); 14 [3, 4].should.contain.only([4, 3]); 15 [2, 3, 4].should.not.only.contain([4, 3]); 16 } 17 18 auto only(Should)(Should should) 19 if (isInstanceOf!(ShouldType, Should)) 20 { 21 should.allowOnlyWordsBefore!(["not"], "only"); 22 23 return should.addWord!"only"; 24 } 25 26 void contain(Should, T)(Should should, T set, string file = __FILE__, size_t line = __LINE__) 27 if (isInstanceOf!(ShouldType, Should)) 28 { 29 should.allowOnlyWordsBefore!(["not", "only"], "contain"); 30 31 should.addWord!"contain".addData!"rhs"(set).checkContain(file, line); 32 } 33 34 auto contain(Should)(Should should) 35 if (isInstanceOf!(ShouldType, Should)) 36 { 37 should.allowOnlyWordsBefore!(["not"], "contain"); 38 39 return should.addWord!"contain"; 40 } 41 42 void only(Should, T)(Should should, T set, string file = __FILE__, size_t line = __LINE__) 43 if (isInstanceOf!(ShouldType, Should)) 44 { 45 should.requireWord!("contain", "only"); 46 should.allowOnlyWordsBefore!(["not", "contain"], "only"); 47 48 should.addWord!"only".addData!"rhs"(set).checkContain(file, line); 49 } 50 51 void checkContain(Should)(Should should, string file, size_t line) 52 if (isInstanceOf!(ShouldType, Should)) 53 { 54 import std.algorithm : any, all, canFind; 55 import std.range : ElementType; 56 57 with (should) 58 { 59 auto lhsValue = data.lhs(); 60 enum rhsIsValue = is(typeof(data.rhs) == ElementType!(typeof(lhsValue))); 61 62 static if (rhsIsValue) 63 { 64 static if (Should.hasWord!"only") 65 { 66 static if (Should.hasWord!"not") 67 { 68 mixin(gencheck!("%s.any!(a => a != %s)", ["lhsValue", "rhs"])); 69 } 70 else 71 { 72 mixin(gencheck!("%s.all!(a => a == %s)", ["lhsValue", "rhs"])); 73 } 74 } 75 else 76 { 77 static if (Should.hasWord!"not") 78 { 79 mixin(gencheck!("!%s.canFind(%s)", ["lhsValue", "rhs"])); 80 } 81 else 82 { 83 mixin(gencheck!("%s.canFind(%s)", ["lhsValue", "rhs"])); 84 } 85 } 86 } 87 else 88 { 89 static if (Should.hasWord!"only") 90 { 91 static if (Should.hasWord!"not") 92 { 93 mixin(gencheck!("%s.any!(a => !%s.canFind(a))", ["lhsValue", "rhs"])); 94 } 95 else 96 { 97 mixin(gencheck!("%s.all!(a => %s.canFind(a))", ["lhsValue", "rhs"])); 98 } 99 } 100 else 101 { 102 static if (Should.hasWord!"not") 103 { 104 mixin(gencheck!("%s.all!(a => !%s.canFind(a))", ["rhs", "lhsValue"])); 105 } 106 else 107 { 108 mixin(gencheck!("%s.all!(a => %s.canFind(a))", ["rhs", "lhsValue"])); 109 } 110 } 111 } 112 } 113 }