-
Notifications
You must be signed in to change notification settings - Fork 1
/
test.js
200 lines (161 loc) · 6.58 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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
let assert = require('assert');
let chromabrew = require('./index.js');
const ERROR_CORRECTION_FACTOR = 1e3;
describe('chromabrew', function () {
it('has Batch property', function () {
assert(chromabrew.Batch instanceof Function);
});
it('is a function which returns a Batch instance', function () {
assert(chromabrew instanceof Function);
let batch = chromabrew({});
assert(batch instanceof chromabrew.Batch);
});
});
describe('Batch', function () {
describe('Constructor', function () {
let batches = [
{ in: { srm: 10 }, out: { srm: 10, ebc: 10 * 1.97, l: (10 + 0.6) / 1.35 } },
{ in: { ebc: 10 }, out: { srm: 10 / 1.97, ebc: 10, l: (10 / 1.97 + 0.6) / 1.35 } },
{ in: { srm: 4, calculator: 'daniels' }, out: { 'srm("daniels")': 8.4, mcu: 0 }, msg: 'minimum value for selected calculator' },
{ in: { srm: 4, calculator: 'mosher' }, out: { 'srm("mosher")': 4.7, mcu: 0 }, msg: 'minimum value for selected calculator' },
{ in: { batch: { volume: 1, ingredients: [{ l: 10, w: 1 }, { l: 20, w: 1 }] } } },
{ in: { batch: { volume: 1, ingredients: [{ l: 10, w: 1 }, { l: 20, w: 1 }], litres: true } } },
{ in: { batch: { volume: 1, ingredients: [{ l: 10, w: 1 }, { l: 20, w: 1 }], kilograms: true } } },
{ in: { batch: { volume: 1, ingredients: [{ l: 10, w: 1 }, { l: 20, w: 1 }], metric: true } } },
];
batches.forEach(function (batch) {
describe(`given input ${ JSON.stringify(batch.in) }`, function () {
let instance = chromabrew(batch.in);
it('mcu is a non-negative number', function () {
// this is a test of internals (mcu must always be defined), but is more clear than a test of exact output values
let result = instance.mcu();
assert.equal(typeof result, 'number');
assert(result >= 0);
});
if (batch.out) {
if (Object.keys(batch.out).length) {
Object.keys(batch.out).forEach(function (key) {
it(`${ key } is ${ batch.out[key] }${ batch.msg ? ` [${ batch.msg }]` : '' }`, function () {
let actual = eval(`instance.${ key + (key.endsWith(')') ? '' : '()') }`);
let expected = batch.out[key];
if (typeof actual === 'number' && typeof expected === 'number') {
// correct floating point errors
actual = Math.round(actual * ERROR_CORRECTION_FACTOR) / ERROR_CORRECTION_FACTOR;
expected = Math.round(expected * ERROR_CORRECTION_FACTOR) / ERROR_CORRECTION_FACTOR;
}
assert.equal(actual, expected);
});
});
} else {
it('has specified output');
}
}
});
})
});
describe('#mcu', function () {
let instance = chromabrew({ srm: 4 });
it('returns number', function () {
assert.equal(typeof instance.mcu(), 'number');
});
});
describe('#srm', function () {
let instance = chromabrew({ srm: 4 });
it('returns number', function () {
assert.equal(typeof instance.srm(), 'number');
});
it('accepts optional calculator which defaults to "morey"', function () {
assert.doesNotThrow(instance.srm.bind(instance));
Object.keys(chromabrew.Batch.mcu).forEach(function (calculator) {
assert[calculator === 'morey' ? 'equal' : 'notEqual'](instance.srm(calculator), instance.srm());
});
});
});
describe('#ebc', function () {
let instance = chromabrew({ srm: 4 });
it('returns number', function () {
assert.equal(typeof instance.ebc(), 'number');
});
it('accepts optional calculator which defaults to "morey"', function () {
assert.doesNotThrow(instance.ebc.bind(instance));
Object.keys(chromabrew.Batch.mcu).forEach(function (calculator) {
assert[calculator === 'morey' ? 'equal' : 'notEqual'](instance.ebc(calculator), instance.ebc());
});
});
});
describe('#lovibond', function () {
let instance = chromabrew({ srm: 4 });
it('returns number', function () {
assert.equal(typeof instance.lovibond(), 'number');
});
it('accepts optional calculator which defaults to "morey"', function () {
assert.doesNotThrow(instance.lovibond.bind(instance));
Object.keys(chromabrew.Batch.mcu).forEach(function (calculator) {
assert[calculator === 'morey' ? 'equal' : 'notEqual'](instance.lovibond(calculator), instance.lovibond());
});
});
});
describe('#hex', function () {
let instance = chromabrew({ srm: 4 });
it('returns hex string', function () {
let result = instance.hex();
assert.equal(typeof result, 'string');
assert(result.match(/^#[A-F0-9]{6}$/));
});
it('accepts optional calculator which defaults to "morey"', function () {
assert.doesNotThrow(instance.hex.bind(instance));
Object.keys(chromabrew.Batch.mcu).forEach(function (calculator) {
assert[calculator === 'morey' ? 'equal' : 'notEqual'](instance.hex(calculator), instance.hex());
});
});
});
describe('#l', function () {
it('aliases #lovibond', function () {
assert.equal(chromabrew.Batch.prototype.l, chromabrew.Batch.prototype.lovibond);
});
});
describe('::mcu', function () {
describe('#morey', function () {
it('returns number', function () {
assert.equal(typeof chromabrew.Batch.mcu.morey(30), 'number');
});
});
describe('#daniels', function () {
it('returns number', function () {
assert.equal(typeof chromabrew.Batch.mcu.daniels(30), 'number');
});
});
describe('#mosher', function () {
it('returns number', function () {
assert.equal(typeof chromabrew.Batch.mcu.mosher(30), 'number');
});
});
describe('#barry', function () {
it('returns number', function () {
assert.equal(typeof chromabrew.Batch.mcu.barry(30), 'number');
});
});
});
describe('::srm', function () {
describe('#morey', function () {
it('returns number', function () {
assert.equal(typeof chromabrew.Batch.srm.morey(30), 'number');
});
});
describe('#daniels', function () {
it('returns number', function () {
assert.equal(typeof chromabrew.Batch.srm.daniels(30), 'number');
});
});
describe('#mosher', function () {
it('returns number', function () {
assert.equal(typeof chromabrew.Batch.srm.mosher(30), 'number');
});
});
describe('#barry', function () {
it('returns number', function () {
assert.equal(typeof chromabrew.Batch.srm.barry(30), 'number');
});
});
});
});