Skip to content

Commit 50dbc3c

Browse files
committed
Add pipe chaining test
1 parent 592f244 commit 50dbc3c

File tree

2 files changed

+39
-6
lines changed

2 files changed

+39
-6
lines changed

tests/tests/src/option_stdlib_optimization_test.mjs

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,22 @@
33
import * as Mocha from "mocha";
44
import * as Test_utils from "./test_utils.mjs";
55
import * as Stdlib_Option from "@rescript/runtime/lib/es6/Stdlib_Option.js";
6+
import * as Belt_MapString from "@rescript/runtime/lib/es6/Belt_MapString.js";
67
import * as Primitive_option from "@rescript/runtime/lib/es6/Primitive_option.js";
78

9+
function getIncidentCategoryName(incidents, categories, incidentId) {
10+
let incident = incidentId !== undefined ? Belt_MapString.get(incidents, incidentId) : undefined;
11+
let categoryId = incident !== undefined ? incident.categoryId : undefined;
12+
let category = categoryId !== undefined ? Belt_MapString.get(categories, categoryId) : undefined;
13+
if (category !== undefined) {
14+
return category.name;
15+
}
16+
}
17+
18+
let PipeChain = {
19+
getIncidentCategoryName: getIncidentCategoryName
20+
};
21+
822
function testPrimitive() {
923
console.log(42);
1024
}
@@ -348,12 +362,12 @@ Mocha.describe("Scope preservation in Option optimizations", () => {
348362
return _value => {};
349363
};
350364
Stdlib_Option.forEach(undefined, makeCallback());
351-
Test_utils.eq("File \"option_stdlib_optimization_test.res\", line 337, characters 7-14", invocations.contents, 1);
365+
Test_utils.eq("File \"option_stdlib_optimization_test.res\", line 481, characters 7-14", invocations.contents, 1);
352366
});
353367
Mocha.test("Option.forEach does not shadow surrounding bindings", () => {
354368
let result;
355369
result = 89 + 1 | 0;
356-
Test_utils.eq("File \"option_stdlib_optimization_test.res\", line 350, characters 7-14", result, 90);
370+
Test_utils.eq("File \"option_stdlib_optimization_test.res\", line 494, characters 7-14", result, 90);
357371
});
358372
Mocha.test("Option.map evaluates callback argument even when option is None", () => {
359373
let invocations = {
@@ -364,11 +378,11 @@ Mocha.describe("Scope preservation in Option optimizations", () => {
364378
return value => value;
365379
};
366380
Stdlib_Option.map(undefined, makeCallback());
367-
Test_utils.eq("File \"option_stdlib_optimization_test.res\", line 362, characters 7-14", invocations.contents, 1);
381+
Test_utils.eq("File \"option_stdlib_optimization_test.res\", line 506, characters 7-14", invocations.contents, 1);
368382
});
369383
Mocha.test("Option.map does not shadow surrounding bindings", () => {
370384
let result = 89 + 1 | 0;
371-
Test_utils.eq("File \"option_stdlib_optimization_test.res\", line 368, characters 7-14", result, 90);
385+
Test_utils.eq("File \"option_stdlib_optimization_test.res\", line 512, characters 7-14", result, 90);
372386
});
373387
Mocha.test("Option.flatMap evaluates callback argument even when option is None", () => {
374388
let invocations = {
@@ -379,18 +393,19 @@ Mocha.describe("Scope preservation in Option optimizations", () => {
379393
return value => Primitive_option.some(value);
380394
};
381395
Stdlib_Option.flatMap(undefined, makeCallback());
382-
Test_utils.eq("File \"option_stdlib_optimization_test.res\", line 380, characters 7-14", invocations.contents, 1);
396+
Test_utils.eq("File \"option_stdlib_optimization_test.res\", line 524, characters 7-14", invocations.contents, 1);
383397
});
384398
Mocha.test("Option.flatMap does not shadow surrounding bindings", () => {
385399
let result = 89 + 1 | 0;
386-
Test_utils.eq("File \"option_stdlib_optimization_test.res\", line 386, characters 7-14", result, 90);
400+
Test_utils.eq("File \"option_stdlib_optimization_test.res\", line 530, characters 7-14", result, 90);
387401
});
388402
});
389403

390404
let globalValue = 89;
391405

392406
export {
393407
globalValue,
408+
PipeChain,
394409
ForEach,
395410
$$Map,
396411
FlatMap,

tests/tests/src/option_stdlib_optimization_test.res

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,24 @@ open Test_utils
77

88
let globalValue = 89
99

10+
module PipeChain = {
11+
// Modeled after some real world code that chains a lot of
12+
// Option.flatMap/Option.map calls.
13+
type incident = {incidentId: string, categoryId: option<string>}
14+
type category = {categoryId: string, name: string}
15+
16+
let getIncidentCategoryName = (
17+
incidents: Belt.Map.String.t<incident>,
18+
categories: Belt.Map.String.t<category>,
19+
~incidentId,
20+
) =>
21+
incidentId
22+
->Option.flatMap(incidentId => incidents->Belt.Map.String.get(incidentId))
23+
->Option.flatMap(incident => incident.categoryId)
24+
->Option.flatMap(categoryId => categories->Belt.Map.String.get(categoryId))
25+
->Option.map(category => category.name)
26+
}
27+
1028
module ForEach = {
1129
let testPrimitive = () => {
1230
let opt = Some(42)

0 commit comments

Comments
 (0)