@@ -73,3 +73,56 @@ expect(function (a, b) {
73
73
return (a + b).length === a .length + b .length ;
74
74
}, ' to be valid for all' , g .word , g .word );
75
75
```
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