-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathexample.js
More file actions
119 lines (92 loc) · 3.43 KB
/
example.js
File metadata and controls
119 lines (92 loc) · 3.43 KB
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
'use strict';
var chado = require('../lib/chado');
var createDouble = chado.createDouble;
var assume = chado.assume; // with assume you describe your assumption
var verify = chado.verify; // with verify you can check your assumptions against a real object
describe('pizza restaurant', function () {
describe('a customer', function () {
var waiter = createDouble('waiter');
it('can successfully order a pizza tonno', function customer() {
assume(waiter).canHandle('order').withArgs('pizza tonno').andReturns('pizza tonno'); // will never be called here
});
it('will receive an "Error" if the pizza tonno is not available', function customer() {
assume(waiter).canHandle('order').withArgs('pizza tonno').andThrowsError('Sorry. Maybe you want to order something else?'); // will never be called here
});
});
describe('the waiter', function () {
var chef = createDouble('chef');
var waiter = createWaiter(chef);
it('passes the translated order to the chef', function () {
assume(chef).canHandle('make').withArgs('143').andReturns('pizza tonno');
verify('waiter').canHandle('order').withArgs('pizza tonno').andReturns('pizza tonno').on(waiter);
});
it('throws an error, when the chef throws the error "Not available"', function () {
assume(chef).canHandle('make').withArgs('143').andThrowsError('Not available');
verify('waiter').canHandle('order').withArgs('pizza tonno').andThrowsError('Sorry. Maybe you want to order something else?').on(waiter);
});
});
describe('the chef', function () {
it('can make pizzas, if the pantry has everything needed to produce an order', function () {
var fullPantry = createPantry({
dough: ['dough'],
cheese: ['cheese'],
'tomato sauce': ['tomato sauce'],
tuna: ['tuna']
});
var chef = createChef(fullPantry);
verify('chef').canHandle('make').withArgs('143').andReturns('pizza tonno').on(chef);
});
it('throws an error, when pantry is empty', function () {
var emptyPantry = createPantry();
var chef = createChef(emptyPantry);
verify('chef').canHandle('make').withArgs('143').andThrowsError('Not available').on(chef);
});
});
});
//====== "PRODUCTION" CODDE BELOW
function createWaiter(chef) {
var internalmenu = {'pizza tonno': '143'};
function order(meal) {
try {
return chef.make(internalmenu[meal]);
}
catch (e) {
throw new Error('Sorry. Maybe you want to order something else?');
}
}
return {
order: order
};
}
function createChef(pantry) {
var recipes = {'143': ['dough', 'cheese', 'tomato sauce', 'tuna']};
var externalmenu = {'143': 'pizza tonno'};
function make(order) {
var ingredients = recipes[order];
if (!pantry.has(ingredients)) {
throw new Error('Not available');
}
ingredients.forEach(function (ingredient) {
pantry.take(ingredient);
});
return externalmenu[order];
}
return {
make: make
};
}
function createPantry(initialFood) {
var storedFood = initialFood || {};
function has(foods) {
return foods.every(function (food) {
return storedFood[food] && storedFood[food].length > 0;
});
}
function take(food) {
return storedFood[food].pop();
}
return {
has: has,
take: take
};
}