Skip to content
Draft
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Add pipe chaining test
  • Loading branch information
cknitt committed Sep 24, 2025
commit 50dbc3c8100fc32ac89337eeb2a7aa23a417e6e5
27 changes: 21 additions & 6 deletions tests/tests/src/option_stdlib_optimization_test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,22 @@
import * as Mocha from "mocha";
import * as Test_utils from "./test_utils.mjs";
import * as Stdlib_Option from "@rescript/runtime/lib/es6/Stdlib_Option.js";
import * as Belt_MapString from "@rescript/runtime/lib/es6/Belt_MapString.js";
import * as Primitive_option from "@rescript/runtime/lib/es6/Primitive_option.js";

function getIncidentCategoryName(incidents, categories, incidentId) {
let incident = incidentId !== undefined ? Belt_MapString.get(incidents, incidentId) : undefined;
let categoryId = incident !== undefined ? incident.categoryId : undefined;
let category = categoryId !== undefined ? Belt_MapString.get(categories, categoryId) : undefined;
if (category !== undefined) {
return category.name;
}
}

let PipeChain = {
getIncidentCategoryName: getIncidentCategoryName
};

function testPrimitive() {
console.log(42);
}
Expand Down Expand Up @@ -348,12 +362,12 @@ Mocha.describe("Scope preservation in Option optimizations", () => {
return _value => {};
};
Stdlib_Option.forEach(undefined, makeCallback());
Test_utils.eq("File \"option_stdlib_optimization_test.res\", line 337, characters 7-14", invocations.contents, 1);
Test_utils.eq("File \"option_stdlib_optimization_test.res\", line 481, characters 7-14", invocations.contents, 1);
});
Mocha.test("Option.forEach does not shadow surrounding bindings", () => {
let result;
result = 89 + 1 | 0;
Test_utils.eq("File \"option_stdlib_optimization_test.res\", line 350, characters 7-14", result, 90);
Test_utils.eq("File \"option_stdlib_optimization_test.res\", line 494, characters 7-14", result, 90);
});
Mocha.test("Option.map evaluates callback argument even when option is None", () => {
let invocations = {
Expand All @@ -364,11 +378,11 @@ Mocha.describe("Scope preservation in Option optimizations", () => {
return value => value;
};
Stdlib_Option.map(undefined, makeCallback());
Test_utils.eq("File \"option_stdlib_optimization_test.res\", line 362, characters 7-14", invocations.contents, 1);
Test_utils.eq("File \"option_stdlib_optimization_test.res\", line 506, characters 7-14", invocations.contents, 1);
});
Mocha.test("Option.map does not shadow surrounding bindings", () => {
let result = 89 + 1 | 0;
Test_utils.eq("File \"option_stdlib_optimization_test.res\", line 368, characters 7-14", result, 90);
Test_utils.eq("File \"option_stdlib_optimization_test.res\", line 512, characters 7-14", result, 90);
});
Mocha.test("Option.flatMap evaluates callback argument even when option is None", () => {
let invocations = {
Expand All @@ -379,18 +393,19 @@ Mocha.describe("Scope preservation in Option optimizations", () => {
return value => Primitive_option.some(value);
};
Stdlib_Option.flatMap(undefined, makeCallback());
Test_utils.eq("File \"option_stdlib_optimization_test.res\", line 380, characters 7-14", invocations.contents, 1);
Test_utils.eq("File \"option_stdlib_optimization_test.res\", line 524, characters 7-14", invocations.contents, 1);
});
Mocha.test("Option.flatMap does not shadow surrounding bindings", () => {
let result = 89 + 1 | 0;
Test_utils.eq("File \"option_stdlib_optimization_test.res\", line 386, characters 7-14", result, 90);
Test_utils.eq("File \"option_stdlib_optimization_test.res\", line 530, characters 7-14", result, 90);
});
});

let globalValue = 89;

export {
globalValue,
PipeChain,
ForEach,
$$Map,
FlatMap,
Expand Down
18 changes: 18 additions & 0 deletions tests/tests/src/option_stdlib_optimization_test.res
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,24 @@ open Test_utils

let globalValue = 89

module PipeChain = {
// Modeled after some real world code that chains a lot of
// Option.flatMap/Option.map calls.
type incident = {incidentId: string, categoryId: option<string>}
type category = {categoryId: string, name: string}

let getIncidentCategoryName = (
incidents: Belt.Map.String.t<incident>,
categories: Belt.Map.String.t<category>,
~incidentId,
) =>
incidentId
->Option.flatMap(incidentId => incidents->Belt.Map.String.get(incidentId))
->Option.flatMap(incident => incident.categoryId)
->Option.flatMap(categoryId => categories->Belt.Map.String.get(categoryId))
->Option.map(category => category.name)
}

module ForEach = {
let testPrimitive = () => {
let opt = Some(42)
Expand Down
Loading