-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.js
140 lines (118 loc) · 2.79 KB
/
test.js
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
const test = require('tape')
const {compute, StateError} = require('./')
test('is working', t => {
t.plan(1)
let state = compute({
init () {
return {
fruits: {},
nutrientsEaten: {},
fruitCount: 0
}
},
reducers: {
buy (state, line) {
let [name, quantity = 1] = line.args
let nutrients = line.kwargs
var fruit = state.fruits[name] || {
name,
nutrients,
quantity: 0
}
fruit.quantity += quantity
state.fruits[name] = fruit
},
sell (state, line) {
let [name, quantity = 1] = line.args
var fruit = state.fruits[name]
if (!fruit) {
throw StateError(`${name} does not exist!`)
}
if (fruit.quantity < quantity) {
throw StateError(`${name} quantity ${fruit.quantity} is less than the amount you're trying to sell!`)
}
fruit.quantity -= quantity
state.fruits[name] = fruit
},
eat (state, line) {
let [name, quantity = 1] = line.args
var fruit = state.fruits[name]
if (!fruit) {
throw StateError(`${name} does not exist!`)
}
if (fruit.quantity < quantity) {
throw StateError(`${name} quantity ${fruit.quantity} is less than the amount you're trying to eat!`)
}
fruit.quantity -= quantity
state.fruits[name] = fruit
for (let n in fruit.nutrients) {
var neat = state.nutrientsEaten[n] || 0
neat += fruit.nutrients[n] * quantity
state.nutrientsEaten[n] = neat
}
}
},
end (state) {
state.fruitCount = Object.keys(state.fruits)
.map(f => state.fruits[f].quantity)
.reduce((a, b) => a + b, 0)
},
journal: `
2016-01-01
buy: passionfruit, 2, vitamin=12, protein=7
buy: melon, 3, vitamin=5, zinc=8
2016-02-15
sell: passionfruit, 1
eat: passionfruit, 1
include melon-bought.txt
2016-03-01
buy: melon
buy: banana, 7, vitamin=4, protein=3
2016-04-15
eat: banana, 5
sell: melon: 1
`,
include (path) {
return path === 'melon-bought.txt'
? `
2016-03-01
eat: melon, 2
`
: ''
}
})
t.deepEqual(state, {
fruits: {
banana: {
name: 'banana',
nutrients: {
vitamin: 4,
protein: 3
},
quantity: 2
},
melon: {
name: 'melon',
nutrients: {
vitamin: 5,
zinc: 8
},
quantity: 1
},
passionfruit: {
name: 'passionfruit',
nutrients: {
vitamin: 12,
protein: 7
},
quantity: 0
}
},
nutrientsEaten: {
vitamin: 12 + (2 * 5) + (5 * 4),
protein: 7 + (5 * 3),
zinc: (2 * 8)
},
fruitCount: 3
})
})