Skip to content

Commit 1df8bd8

Browse files
committed
Enqueue dequeue example
1 parent 55f9aa2 commit 1df8bd8

File tree

2 files changed

+57
-0
lines changed

2 files changed

+57
-0
lines changed

bootstrap.js

+4
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,8 @@ unexpected.addAssertion('<array> last item <assertion>', function (expect, subje
1919
return expect.shift(subject[subject.length - 1]);
2020
});
2121

22+
unexpected.addAssertion('<any> to be contained by <array>', function (expect, item, array) {
23+
expect(array.indexOf(item) !== -1, 'to be true');
24+
});
25+
2226
expect = unexpected.clone();

documentation/assertions/function/to-be-valid-for-all.md

+53
Original file line numberDiff line numberDiff line change
@@ -73,3 +73,56 @@ expect(function (a, b) {
7373
return (a + b).length === a.length + b.length;
7474
}, 'to be valid for all', g.word, g.word);
7575
```
76+
77+
Another example could be to generate actions.
78+
79+
Let's create a simple queue:
80+
81+
```js
82+
function Queue() {
83+
this.buffer = [];
84+
}
85+
Queue.prototype.enqueue = function (value) {
86+
this.buffer.push(value);
87+
};
88+
Queue.prototype.dequeue = function () {
89+
return this.buffer.shift(1);
90+
};
91+
Queue.prototype.isEmpty = function () {
92+
return this.buffer.length === 0;
93+
};
94+
Queue.prototype.drainTo = function (array) {
95+
while (!this.isEmpty()) {
96+
array.push(this.dequeue());
97+
}
98+
};
99+
```
100+
101+
Now let's test that items enqueued always comes out in the right order:
102+
103+
```js
104+
var action = g.pick([
105+
{ name: 'enqueue', value: g.string },
106+
{ name: 'dequeue' }
107+
]);
108+
109+
var actions = g.n(action, 200);
110+
111+
expect(function (actions) {
112+
var queue = new Queue();
113+
var enqueued = [];
114+
var dequeued = [];
115+
actions.forEach(function (action) {
116+
if (action.name === 'enqueue') {
117+
enqueued.push(action.value);
118+
queue.enqueue(action.value);
119+
} else if (!queue.isEmpty()) {
120+
dequeued.push(queue.dequeue());
121+
}
122+
});
123+
124+
queue.drainTo(dequeued);
125+
126+
expect(dequeued, 'to equal', enqueued);
127+
}, 'to be valid for all', actions);
128+
```

0 commit comments

Comments
 (0)