-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrunner.dart
165 lines (130 loc) · 3.47 KB
/
runner.dart
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
prepareArbitrary(g) {
if (g is ArbitraryBuilderMarker)
return g.toArbitrary();
else
return g;
}
check(name, generator, callable) {
return new Property(name, prepareArbitrary(generator), callable);
}
Property property(name, generator, callable) {
Property p = new Property(name, prepareArbitrary(generator), callable);
Property.registerProperty(p);
return p;
}
void checkAll() {
run(Property.declaredProperties);
Property.declaredProperties = null;
}
//checkFails(name, generator, callable) =>
//new Property.negative(name, prepareArbitrary(generator), callable);
void run(List<Property<Object>> ps) {
final config = new Config();
(new Suite(ps, config)).run();
}
beVerbose() => Config.setVerbose(true);
shutUp() => Config.setVerbose(false);
class Config {
static int _numRuns = 100;
static bool _verbose = false;
Reporter report;
Config() {
report = new Reporter(_verbose);
}
int get numRuns() => _numRuns;
bool get verbose() => _verbose;
static bool setVerbose(bool flag) => _verbose = flag;
}
class Suite {
final List<Property<Object>> properties;
final Config config;
Suite(this.properties, this.config);
void run() {
var results = [];
properties.forEach((p) => results.add(_testOne(p)));
config.report.summary(results);
}
bool _testOne(Property<Object> p) {
config.report.testStart(p);
final Results rs = new Results();
for (int i = 0; i<config.numRuns; ++i) {
final Result<Object> r = p.check();
rs.add(r);
config.report.singleCheck(r);
if (! r.result) {
return _fail(p, rs);
}
}
return _success(p, rs);
}
bool _fail(Property p, Results rs) {
config.report.testFail(p, rs);
return false;
}
bool _success(Property p, Results rs) {
config.report.testSuccess(p, rs);
return true;
}
}
class Result<T> {
final Property<T> property;
final T input;
final bool result;
Result(this.property, this.input, this.result);
}
class Results {
List<Result<Object>> _results;
Results() {
_results = [];
}
void add(Result<Object> r) => _results.add(r);
int count() => _results.length;
Result<Object> latest() => _results.last();
}
class Property<T> {
final String name;
final Iterator<T> _generator;
final Function _callable;
Condition _condition;
Function _filter = null;
static List<Property> declaredProperties;
static registerProperty(p) {
if (declaredProperties == null)
declaredProperties = new List<Property>();
declaredProperties.add(p);
}
Property(this.name, this._generator, this._callable) {
_condition = new AcceptPositive();
}
Property.negative(this.name, this._generator, this._callable) {
_condition = new AcceptNegative();
}
Result<T> check() {
final T input = _takeWhile(_filter, _generator.next);
final bool checkResult = _callable(input);
final bool result = _condition.check(checkResult);
return new Result<T>(this, input, result);
}
Property<T> when(filter) {
_filter = filter;
return this;
}
_takeWhile(condition, f) {
// TODO: Report if too much tries.
var r = f();
if (condition == null) return r;
while (! condition(r)) {
r = f();
}
return r;
}
}
abstract class Condition {
abstract bool check(bool r);
}
class AcceptPositive extends Condition {
bool check(bool r) => r;
}
class AcceptNegative extends Condition {
bool check(bool r) => !r;
}