/
Testing.java
142 lines (121 loc) · 3.26 KB
/
Testing.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
package org.quackery.testing;
import static java.lang.String.format;
import static java.util.Objects.deepEquals;
import static java.util.Objects.hash;
import org.quackery.Case;
import org.quackery.Case.Body;
import org.quackery.Contract;
import org.quackery.Test;
public class Testing {
public static void assertTrue(boolean condition) {
if (!condition) {
throw new AssertionError();
}
}
public static void assertEquals(Object actual, Object expected) {
if (!deepEquals(actual, expected)) {
throw new AssertionError(format(""
+ "\n"
+ " expected equal to\n"
+ " %s\n"
+ " but was\n"
+ " %s\n",
expected,
actual));
}
}
public static void assertNotEquals(Object actual, Object expected) {
if (deepEquals(actual, expected)) {
throw new AssertionError(format(""
+ "\n"
+ " expected not equal to\n"
+ " %s\n"
+ " but was\n"
+ " %s\n",
expected,
actual));
}
}
public static void fail() {
throw new AssertionError();
}
public static Object mockObject(final String name) {
return new Object() {
public String toString() {
return name;
}
};
}
public static Body mockBody() {
return new Body() {
public void run() {}
};
}
public static Case mockCase(String name) {
return new Case(name) {
public void run() {}
};
}
public static Case mockCase(String name, final Throwable throwable) {
return new Case(name) {
public void run() throws Throwable {
throw throwable.fillInStackTrace();
}
};
}
public static <T> Contract<T> mockContract(final String name) {
return new Contract<T>() {
public Test test(T item) {
return new MockTest(item, this);
}
public String toString() {
return format("mockContract(%s)", name);
}
};
}
private static class MockTest extends Test {
public final Object item;
public final Contract<?> contract;
public MockTest(Object item, Contract<?> contract) {
super(format("%s.test(%s)", contract, item));
this.item = item;
this.contract = contract;
}
public boolean equals(Object object) {
return object instanceof MockTest
&& equals((MockTest) object);
}
public boolean equals(MockTest that) {
return item.equals(that.item)
&& contract.equals(that.contract);
}
public int hashCode() {
return hash(item, contract);
}
public String toString() {
return name;
}
}
public static void interruptMeAfter(final double time) {
final Thread caller = Thread.currentThread();
Thread interrupter = new Thread(new Runnable() {
public void run() {
try {
sleep(time);
} catch (InterruptedException e) {
throw new Error(e);
}
caller.interrupt();
}
});
interrupter.start();
}
public static void sleep(double time) throws InterruptedException {
Thread.sleep((long) (time * 1e3));
}
public static void sleepBusy(double time) {
long nanos = (long) (time * 1e9);
long start = System.nanoTime();
while (System.nanoTime() - start < nanos) {}
}
}