1 module mocked.tests.readme; 2 3 import mocked; 4 5 unittest 6 { 7 static class Dependency 8 { 9 string authorOf(string phrase) 10 { 11 return null; 12 } 13 } 14 15 enum string phrase = "[T]he meaning of a word is its use in the language."; 16 enum string expected = "L. Wittgenstein"; 17 18 Mocker mocker; 19 auto builder = mocker.mock!Dependency; 20 21 builder.expect 22 .authorOf("[T]he meaning of a word is its use in the language.") 23 .returns(expected); 24 25 auto dependency = builder.get; 26 27 assert(dependency.authorOf(phrase) == expected); 28 } 29 30 unittest 31 { 32 import std.math : fabs; 33 34 static class Dependency 35 { 36 public void call(float) 37 { 38 } 39 } 40 41 // This function is used to compare two floating point numbers that don't 42 // match exactly. 43 alias approxComparator = (float a, float b) { 44 return fabs(a - b) <= 0.1; 45 }; 46 auto mocker = configure!(Comparator!approxComparator); 47 auto builder = mocker.mock!Dependency; 48 49 builder.expect.call(1.01); 50 51 auto mock = builder.get; 52 53 mock.call(1.02); 54 55 mocker.verify; 56 } 57 58 unittest 59 { 60 static class Dependency 61 { 62 bool isTrue() 63 { 64 return true; 65 } 66 } 67 Mocker mocker; 68 auto mock = mocker.mock!Dependency; 69 mock.expect.isTrue.passThrough; 70 71 assert(mock.get.isTrue); 72 } 73 74 unittest 75 { 76 static class Dependency 77 { 78 } 79 Mocker mocker; 80 auto mock = mocker.mock!Dependency; 81 mock.expect.toString.returns("in abstracto"); 82 83 assert(mock.get.toString == "in abstracto"); 84 }