-
-
Notifications
You must be signed in to change notification settings - Fork 39
Expand file tree
/
Copy pathquantity.spec.js
More file actions
186 lines (170 loc) · 6.9 KB
/
Copy pathquantity.spec.js
File metadata and controls
186 lines (170 loc) · 6.9 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
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
const { QuantityType, DecimalType } = require('./openhab.mock');
const { BigDecimal } = require('./java.mock');
const { Unit } = require('./javax-measure.mock');
const { getQuantity, Quantity, QuantityError, _toBigDecimalOrQtyType, _toQtyType } = require('../src/quantity');
class Item {
constructor (rawState) {
this.rawState = rawState;
}
}
describe('quantity.js', () => {
const quantityTypeSpy = new QuantityType();
QuantityType.valueOf.mockImplementation(() => quantityTypeSpy);
const unitSpy = new Unit();
quantityTypeSpy.getUnit.mockImplementation(() => unitSpy);
describe('factory', () => {
it('creates Quantity from string', () => {
expect(getQuantity('5m')).toBeInstanceOf(Quantity);
});
it('creates Quantity from Quantity', () => {
expect(getQuantity(getQuantity('5m'))).toBeInstanceOf(Quantity);
});
it('creates Quantity from Java QuantityType', () => {
expect(getQuantity(quantityTypeSpy)).toBeInstanceOf(Quantity);
});
it('throws TypeError else', () => {
expect(() => getQuantity(5)).toThrowError(TypeError);
});
});
describe('member', () => {
it('dimension delegates & returns', () => {
quantityTypeSpy.getDimension.mockImplementation(() => '[L]');
const dimension = getQuantity('5 m').dimension;
expect(quantityTypeSpy.getDimension).toHaveBeenCalled();
expect(dimension).toBe('[L]');
});
it('unit delegates & returns', () => {
unitSpy.getName.mockImplementation(() => 'Metre');
let unit = getQuantity('5 m').unit;
expect(unit).toBe('Metre');
unitSpy.getName.mockImplementation(() => null);
unitSpy.toString.mockImplementation(() => 'Metre');
unit = getQuantity('5 m').unit;
expect(unit).toBe('Metre');
expect(unitSpy.getName).toHaveBeenCalledTimes(2);
expect(quantityTypeSpy.getUnit).toHaveBeenCalledTimes(2);
});
describe('symbol returns', () => {
it.each([
['° C', '56.7 ° C'],
['mm', '575 mm'],
[null, '4732']
])('%s for %s', (symbol, value) => {
quantityTypeSpy.toString.mockImplementation(() => value);
expect(getQuantity(value).symbol).toBe(symbol);
});
quantityTypeSpy.toString.mockClear();
});
it('float delegates & returns', () => {
quantityTypeSpy.doubleValue.mockImplementation(() => 1.5);
const float = getQuantity('1.5 m').float;
expect(quantityTypeSpy.doubleValue).toHaveBeenCalled();
expect(float).toBe(1.5);
});
it('int delegates & returns', () => {
quantityTypeSpy.longValue.mockImplementation(() => 5);
const int = getQuantity('5 m').int;
expect(quantityTypeSpy.longValue).toHaveBeenCalled();
expect(int).toBe(5);
});
});
describe('method', () => {
it.each([
['add'],
['divide'],
['multiply'],
['subtract']
])('%s delegates & returns Quantity', (method) => {
const qty = getQuantity('5 m')[method]('5 m');
expect(quantityTypeSpy[method]).toHaveBeenCalled();
expect(qty).toBeInstanceOf(Quantity);
});
describe('toUnit', () => {
it('delegates & returns Quantity', () => {
const unit = 'cm';
const qty = getQuantity('5 m').toUnit(unit);
expect(quantityTypeSpy.toUnit).toHaveBeenCalledWith(unit);
expect(qty).toBeInstanceOf(Quantity);
});
it('delegates & returns null', () => {
quantityTypeSpy.toUnit.mockImplementation(() => null);
const unit = 'cm';
const qty = getQuantity('5 m').toUnit(unit);
expect(quantityTypeSpy.toUnit).toHaveBeenCalledWith(unit);
expect(qty).toBe(null);
quantityTypeSpy.toUnit.mockImplementation(() => new QuantityType());
});
it('wraps exceptions in QuantityError', () => {
quantityTypeSpy.toUnit.mockImplementation(() => { throw new Error(); });
expect(() => getQuantity('5 m').toUnit('cm')).toThrowError(QuantityError);
quantityTypeSpy.toUnit.mockImplementation(() => new QuantityType());
});
});
// method name | compareTo returns for true | compareTo returns for false
it.each([
['equal', 0, 1],
['greaterThan', 1, 0],
['greaterThanOrEqual', 0, -1],
['lessThan', -1, 0],
['lessThanOrEqual', 0, 1]
])('%s delegates, compares & returns correct boolean', (name, mock1, mock2) => {
quantityTypeSpy.compareTo.mockImplementation(() => mock1);
let equals = getQuantity('5 m')[name]('500 cm');
expect(equals).toBe(true);
quantityTypeSpy.compareTo.mockImplementation(() => mock2);
equals = getQuantity('5 m')[name]('500 cm');
expect(equals).toBe(false);
expect(quantityTypeSpy.compareTo).toHaveBeenCalledTimes(2);
});
it('toString delegates & returns string', () => {
const str = getQuantity('5 m').toString();
expect(quantityTypeSpy.toString).toHaveBeenCalled();
expect(typeof str).toBe('string');
});
});
describe('private method', () => {
describe('_toBigDecimalOrQtyType', () => {
it('extracts BigDecimal from DecimalType Item state', () => {
const item = new Item(new DecimalType());
const value = _toBigDecimalOrQtyType(item);
expect(value).toBeInstanceOf(BigDecimal);
});
it('parses number to BigDecimal', () => {
const value = _toBigDecimalOrQtyType(10);
expect(BigDecimal.valueOf).toHaveBeenCalledWith(10);
expect(value).toBeInstanceOf(BigDecimal);
});
it('wraps exceptions from BigDecimal in QuantityError', () => {
BigDecimal.valueOf.mockImplementation(() => { throw new Error(); });
expect(() => _toBigDecimalOrQtyType(10)).toThrowError(QuantityError);
BigDecimal.valueOf.mockImplementation(() => new BigDecimal());
});
it('throws TypeError for wrong argument types', () => {
expect(() => _toBigDecimalOrQtyType(true)).toThrowError(TypeError);
});
});
describe('_toQtyType', () => {
it('extracts QuantityType from QuantityType Item state', () => {
const value = _toQtyType(new Item(new QuantityType()));
expect(value).toBeInstanceOf(QuantityType);
});
it('parses string to QuantityType', () => {
const value = _toQtyType('10 m');
expect(QuantityType.valueOf).toHaveBeenCalledWith('10 m');
expect(value).toBeInstanceOf(QuantityType);
});
it('extracts QuantityType from Quantity', () => {
const value = _toQtyType(getQuantity('10 m'));
expect(value).toBeInstanceOf(QuantityType);
});
it('wraps exceptions from QuantityType in QuantityError', () => {
QuantityType.valueOf.mockImplementation(() => { throw new Error(); });
expect(() => _toQtyType('10 m')).toThrowError(QuantityError);
QuantityType.valueOf.mockImplementation(() => new QuantityType());
});
it('else throws TypeError', () => {
expect(() => _toQtyType(10)).toThrowError(TypeError);
});
});
});
});