Skip to content

Commit 18dc46f

Browse files
committed
test(units): increase coverage
1 parent 4799fcc commit 18dc46f

File tree

2 files changed

+98
-1
lines changed

2 files changed

+98
-1
lines changed

src/units.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -337,7 +337,7 @@ export function addQuantities(q1: Quantity, q2: Quantity): Quantity {
337337
return addQuantityValuesAndSetUnit(v1, v2, q1.unit);
338338
}
339339

340-
// Case 4: the two quantities do not have the same unit
340+
// Case 4: the two quantities have different units of known type
341341
if (unit1Def && unit2Def) {
342342
// Case 4.1: different unit type => we can't add quantities
343343

@@ -373,5 +373,6 @@ export function addQuantities(q1: Quantity, q2: Quantity): Quantity {
373373
);
374374
}
375375

376+
// Case 5: the two quantities have different units of unknown type
376377
throw new IncompatibleUnitsError(q1.unit, q2.unit);
377378
}

test/units.test.ts

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,90 @@ describe("addQuantities", () => {
141141
});
142142
});
143143

144+
it("should also work when at least one value is a range", () => {
145+
expect(
146+
addQuantities(
147+
{
148+
value: {
149+
type: "range",
150+
min: { type: "decimal", value: 100 },
151+
max: { type: "decimal", value: 200 },
152+
},
153+
unit: "g",
154+
},
155+
{
156+
value: {
157+
type: "range",
158+
min: { type: "decimal", value: 10 },
159+
max: { type: "decimal", value: 20 },
160+
},
161+
unit: "g",
162+
},
163+
),
164+
).toEqual({
165+
value: {
166+
type: "range",
167+
min: { type: "decimal", value: 110 },
168+
max: { type: "decimal", value: 220 },
169+
},
170+
unit: "g",
171+
});
172+
173+
expect(
174+
addQuantities(
175+
{
176+
value: {
177+
type: "fixed",
178+
value: { type: "decimal", value: 100 },
179+
},
180+
unit: "g",
181+
},
182+
{
183+
value: {
184+
type: "range",
185+
min: { type: "decimal", value: 10 },
186+
max: { type: "decimal", value: 20 },
187+
},
188+
unit: "g",
189+
},
190+
),
191+
).toEqual({
192+
value: {
193+
type: "range",
194+
min: { type: "decimal", value: 110 },
195+
max: { type: "decimal", value: 120 },
196+
},
197+
unit: "g",
198+
});
199+
200+
expect(
201+
addQuantities(
202+
{
203+
value: {
204+
type: "range",
205+
min: { type: "decimal", value: 10 },
206+
max: { type: "decimal", value: 20 },
207+
},
208+
unit: "g",
209+
},
210+
{
211+
value: {
212+
type: "fixed",
213+
value: { type: "decimal", value: 100 },
214+
},
215+
unit: "g",
216+
},
217+
),
218+
).toEqual({
219+
value: {
220+
type: "range",
221+
min: { type: "decimal", value: 110 },
222+
max: { type: "decimal", value: 120 },
223+
},
224+
unit: "g",
225+
});
226+
});
227+
144228
it("should add compatible metric units and convert to largest", () => {
145229
expect(
146230
addQuantities(
@@ -271,6 +355,18 @@ describe("addQuantities", () => {
271355
},
272356
),
273357
).toThrow(IncompatibleUnitsError);
358+
expect(() =>
359+
addQuantities(
360+
{
361+
value: { type: "fixed", value: { type: "decimal", value: 100 } },
362+
unit: "g",
363+
},
364+
{
365+
value: { type: "fixed", value: { type: "decimal", value: 1 } },
366+
unit: "bag",
367+
},
368+
),
369+
).toThrow(IncompatibleUnitsError);
274370
});
275371

276372
it("should add quantities defined as ranges", () => {

0 commit comments

Comments
 (0)