Skip to content

Commit 90000df

Browse files
committed
Rename assertAndReturn ~> ensure
This is shorter.
1 parent c1d628a commit 90000df

File tree

9 files changed

+60
-78
lines changed

9 files changed

+60
-78
lines changed

lib/browser-runtime.ts

Lines changed: 29 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import { v4 as uuid } from "uuid";
1313

1414
import random from "seedrandom";
1515

16-
import { assert, assertAndReturn, fail } from "./helpers/assertions";
16+
import { assert, ensure, fail } from "./helpers/assertions";
1717

1818
import DataTable from "./data_table";
1919

@@ -293,7 +293,7 @@ function emitSkippedPickle(
293293
}
294294

295295
function findPickleById(context: CompositionContext, astId: string) {
296-
return assertAndReturn(
296+
return ensure(
297297
context.pickles.find(
298298
(pickle) => pickle.astNodeIds && pickle.astNodeIds.includes(astId),
299299
),
@@ -304,11 +304,8 @@ function findPickleById(context: CompositionContext, astId: string) {
304304
function collectExampleIds(examples: readonly messages.Examples[]) {
305305
return examples
306306
.map((examples) => {
307-
return assertAndReturn(
308-
examples.tableBody,
309-
"Expected to find a table body",
310-
).map((row) =>
311-
assertAndReturn(row.id, "Expected table row to have an id"),
307+
return ensure(examples.tableBody, "Expected to find a table body").map(
308+
(row) => ensure(row.id, "Expected table row to have an id"),
312309
);
313310
})
314311
.reduce((acum, el) => acum.concat(el), []);
@@ -346,8 +343,8 @@ function getTestStepId(options: {
346343
}) {
347344
const { context, pickleId, hookIdOrPickleStepId } = options;
348345

349-
return assertAndReturn(
350-
assertAndReturn(
346+
return ensure(
347+
ensure(
351348
context.testStepIds.get(pickleId),
352349
"Expected to find test step IDs for pickle = " + pickleId,
353350
).get(hookIdOrPickleStepId),
@@ -422,7 +419,7 @@ function createRule(context: CompositionContext, rule: messages.Rule) {
422419
return findPickleById(context, exampleId);
423420
});
424421
} else {
425-
const scenarioId = assertAndReturn(
422+
const scenarioId = ensure(
426423
scenario.id,
427424
"Expected scenario to have an id",
428425
);
@@ -473,10 +470,7 @@ function createScenario(
473470
createPickle(context, { ...pickle, name: exampleName });
474471
}
475472
} else {
476-
const scenarioId = assertAndReturn(
477-
scenario.id,
478-
"Expected scenario to have an id",
479-
);
473+
const scenarioId = ensure(scenario.id, "Expected scenario to have an id");
480474

481475
const pickle = findPickleById(context, scenarioId);
482476

@@ -521,12 +515,9 @@ function createPickle(context: CompositionContext, pickle: messages.Pickle) {
521515
[INTERNAL_SPEC_PROPERTIES]: internalProperties,
522516
};
523517

524-
const scenario = assertAndReturn(
518+
const scenario = ensure(
525519
context.astIdsMap.get(
526-
assertAndReturn(
527-
pickle.astNodeIds?.[0],
528-
"Expected to find at least one astNodeId",
529-
),
520+
ensure(pickle.astNodeIds?.[0], "Expected to find at least one astNodeId"),
530521
),
531522
`Expected to find scenario associated with id = ${pickle.astNodeIds?.[0]}`,
532523
);
@@ -624,7 +615,7 @@ function createPickle(context: CompositionContext, pickle: messages.Pickle) {
624615
remainingSteps.shift();
625616

626617
for (const skippedStep of remainingSteps) {
627-
const hookIdOrPickleStepId = assertAndReturn(
618+
const hookIdOrPickleStepId = ensure(
628619
skippedStep.hook?.id ?? skippedStep.pickleStep?.id,
629620
"Expected a step to either be a hook or a pickleStep",
630621
);
@@ -729,14 +720,14 @@ function createPickle(context: CompositionContext, pickle: messages.Pickle) {
729720
hookIdOrPickleStepId: pickleStep.id,
730721
});
731722

732-
const text = assertAndReturn(
723+
const text = ensure(
733724
pickleStep.text,
734725
"Expected pickle step to have a text",
735726
);
736727

737-
const scenarioStep = assertAndReturn(
728+
const scenarioStep = ensure(
738729
context.astIdsMap.get(
739-
assertAndReturn(
730+
ensure(
740731
pickleStep.astNodeIds?.[0],
741732
"Expected to find at least one astNodeId",
742733
),
@@ -797,7 +788,7 @@ function createPickle(context: CompositionContext, pickle: messages.Pickle) {
797788
return beforeHooksChain.then(() => {
798789
try {
799790
return runStepWithLogGroup({
800-
keyword: assertAndReturn(
791+
keyword: ensure(
801792
"keyword" in scenarioStep && scenarioStep.keyword,
802793
"Expected to find a keyword in the scenario step",
803794
),
@@ -935,12 +926,9 @@ function afterEachHandler(this: Mocha.Context, context: CompositionContext) {
935926

936927
if (remainingSteps.length > 0) {
937928
if (this.currentTest?.state === "failed") {
938-
const error = assertAndReturn(
939-
this.currentTest?.err,
940-
"Expected to find an error",
941-
);
929+
const error = ensure(this.currentTest?.err, "Expected to find an error");
942930

943-
const message = assertAndReturn(
931+
const message = ensure(
944932
error.message,
945933
"Expected to find an error message",
946934
);
@@ -949,12 +937,12 @@ function afterEachHandler(this: Mocha.Context, context: CompositionContext) {
949937
return;
950938
}
951939

952-
const failedStep = assertAndReturn(
940+
const failedStep = ensure(
953941
remainingSteps.shift(),
954942
"Expected there to be a remaining step",
955943
);
956944

957-
const hookIdOrPickleStepId = assertAndReturn(
945+
const hookIdOrPickleStepId = ensure(
958946
failedStep.hook?.id ?? failedStep.pickleStep?.id,
959947
"Expected a step to either be a hook or a pickleStep",
960948
);
@@ -998,7 +986,7 @@ function afterEachHandler(this: Mocha.Context, context: CompositionContext) {
998986
message,
999987
}),
1000988
duration: duration(
1001-
assertAndReturn(
989+
ensure(
1002990
currentStepStartedAt,
1003991
"Expected there to be a timestamp for current step",
1004992
),
@@ -1021,7 +1009,7 @@ function afterEachHandler(this: Mocha.Context, context: CompositionContext) {
10211009
taskTestStepFinished(context, failedTestStepFinished);
10221010

10231011
for (const skippedStep of remainingSteps) {
1024-
const hookIdOrPickleStepId = assertAndReturn(
1012+
const hookIdOrPickleStepId = ensure(
10251013
skippedStep.hook?.id ?? skippedStep.pickleStep?.id,
10261014
"Expected a step to either be a hook or a pickleStep",
10271015
);
@@ -1053,12 +1041,12 @@ function afterEachHandler(this: Mocha.Context, context: CompositionContext) {
10531041
}
10541042
} else if (this.currentTest?.state === "pending") {
10551043
if (currentStepStartedAt) {
1056-
const skippedStep = assertAndReturn(
1044+
const skippedStep = ensure(
10571045
remainingSteps.shift(),
10581046
"Expected there to be a remaining step",
10591047
);
10601048

1061-
const hookIdOrPickleStepId = assertAndReturn(
1049+
const hookIdOrPickleStepId = ensure(
10621050
skippedStep.hook?.id ?? skippedStep.pickleStep?.id,
10631051
"Expected a step to either be a hook or a pickleStep",
10641052
);
@@ -1081,7 +1069,7 @@ function afterEachHandler(this: Mocha.Context, context: CompositionContext) {
10811069
}
10821070

10831071
for (const remainingStep of remainingSteps) {
1084-
const hookIdOrPickleStepId = assertAndReturn(
1072+
const hookIdOrPickleStepId = ensure(
10851073
remainingStep.hook?.id ?? remainingStep.pickleStep?.id,
10861074
"Expected a step to either be a hook or a pickleStep",
10871075
);
@@ -1113,7 +1101,7 @@ function afterEachHandler(this: Mocha.Context, context: CompositionContext) {
11131101
}
11141102
} else {
11151103
for (const remainingStep of remainingSteps) {
1116-
const hookIdOrPickleStepId = assertAndReturn(
1104+
const hookIdOrPickleStepId = ensure(
11171105
remainingStep.hook?.id ?? remainingStep.pickleStep?.id,
11181106
"Expected a step to either be a hook or a pickleStep",
11191107
);
@@ -1146,12 +1134,12 @@ function afterEachHandler(this: Mocha.Context, context: CompositionContext) {
11461134
}
11471135
}
11481136

1149-
const currentRetry = assertAndReturn(
1137+
const currentRetry = ensure(
11501138
(this.currentTest as any)?._currentRetry,
11511139
"Expected to find an attribute _currentRetry",
11521140
);
11531141

1154-
const retries = assertAndReturn(
1142+
const retries = ensure(
11551143
(this.currentTest as any)?._retries,
11561144
"Expected to find an attribute _retries",
11571145
);
@@ -1318,10 +1306,7 @@ export default function createTests(
13181306
specEnvelopes.push({
13191307
source: {
13201308
data: source,
1321-
uri: assertAndReturn(
1322-
gherkinDocument.uri,
1323-
"Expected gherkin document to have URI",
1324-
),
1309+
uri: ensure(gherkinDocument.uri, "Expected gherkin document to have URI"),
13251310
mediaType: SourceMediaType.TEXT_X_CUCUMBER_GHERKIN_PLAIN,
13261311
},
13271312
});
@@ -1495,7 +1480,7 @@ function createMissingStepDefinitionMessage(
14951480
.map((expression) =>
14961481
generateSnippet(
14971482
expression,
1498-
assertAndReturn(pickleStep.type, "Expected pickleStep to have a type"),
1483+
ensure(pickleStep.type, "Expected pickleStep to have a type"),
14991484
parameter,
15001485
),
15011486
)

lib/data_table.ts

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import type * as messages from "@cucumber/messages";
22

3-
import { assert, assertAndReturn } from "./helpers/assertions";
3+
import { assert, ensure } from "./helpers/assertions";
44

55
function zip<A, B>(collectionA: A[], collectionB: B[]) {
66
return collectionA.map<[A, B]>((element, index) => [
@@ -16,18 +16,17 @@ export default class DataTable {
1616
if (sourceTable instanceof Array) {
1717
this.rawTable = sourceTable;
1818
} else {
19-
this.rawTable = assertAndReturn(
19+
this.rawTable = ensure(
2020
sourceTable.rows,
2121
"Expected a PicleTable to have rows",
2222
).map((row) =>
23-
assertAndReturn(
24-
row.cells,
25-
"Expected a PicleTableRow to have cells",
26-
).map((cell) => {
27-
const { value } = cell;
28-
assert(value != null, "Expected a PicleTableCell to have a value");
29-
return value;
30-
}),
23+
ensure(row.cells, "Expected a PicleTableRow to have cells").map(
24+
(cell) => {
25+
const { value } = cell;
26+
assert(value != null, "Expected a PicleTableCell to have a value");
27+
return value;
28+
},
29+
),
3130
);
3231
}
3332
}

lib/helpers/assertions.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ export function assert(value: unknown, message: string): asserts value {
1818
fail(message);
1919
}
2020

21-
export function assertAndReturn<T>(
21+
export function ensure<T>(
2222
value: T,
2323
message: string,
2424
): Exclude<T, false | null | undefined> {

lib/helpers/ast.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import type * as messages from "@cucumber/messages";
22

3-
import { assertAndReturn } from "./assertions";
3+
import { ensure } from "./assertions";
44

55
export function* traverseGherkinDocument(
66
gherkinDocument: messages.GherkinDocument,
@@ -190,9 +190,7 @@ export function collectTagNames(
190190
tags: readonly (messages.Tag | messages.PickleTag)[] | null | undefined,
191191
) {
192192
return (
193-
tags?.map((tag) =>
194-
assertAndReturn(tag.name, "Expected tag to have a name"),
195-
) ?? []
193+
tags?.map((tag) => ensure(tag.name, "Expected tag to have a name")) ?? []
196194
);
197195
}
198196

lib/helpers/merge.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import {
1010
orderMessages,
1111
} from "./messages";
1212

13-
import { assertAndReturn } from "./assertions";
13+
import { ensure } from "./assertions";
1414

1515
import { CypressCucumberError } from "./error";
1616

@@ -46,12 +46,12 @@ export function mergeMessages(
4646
}
4747
}
4848

49-
const meta = assertAndReturn(
49+
const meta = ensure(
5050
messages.map((message) => message.meta).find(notNull),
5151
"Expected to find a meta envelope",
5252
);
5353

54-
const testRunStarted = assertAndReturn(
54+
const testRunStarted = ensure(
5555
messages
5656
.map((message) => message.testRunStarted)
5757
.filter(notNull)
@@ -60,7 +60,7 @@ export function mergeMessages(
6060
"Expected to find a testRunStarted envelope",
6161
);
6262

63-
const testRunFinished = assertAndReturn(
63+
const testRunFinished = ensure(
6464
messages
6565
.map((message) => message.testRunFinished)
6666
.filter(notNull)

lib/plugin-event-handlers.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ import debug from "./helpers/debug";
4646

4747
import { CypressCucumberError, createError, homepage } from "./helpers/error";
4848

49-
import { assert, assertAndReturn, assertIsString } from "./helpers/assertions";
49+
import { assert, ensure, assertIsString } from "./helpers/assertions";
5050

5151
import {
5252
createHtmlStream,
@@ -906,42 +906,42 @@ export async function testStepFinishedHandler(
906906

907907
const { testCaseStartedId, testStepId } = testStepFinished;
908908

909-
const { testCaseId: pickleId } = assertAndReturn(
909+
const { testCaseId: pickleId } = ensure(
910910
state.messages.current
911911
.map((message) => message.testCaseStarted)
912912
.filter(notNull)
913913
.find((testCaseStarted) => testCaseStarted.id === testCaseStartedId),
914914
"Expected to find a testCaseStarted",
915915
);
916916

917-
const testCase = assertAndReturn(
917+
const testCase = ensure(
918918
state.messages.current
919919
.map((message) => message.testCase)
920920
.filter(notNull)
921921
.find((testCase) => testCase.id === pickleId),
922922
"Expected to find a testCase",
923923
);
924924

925-
const { pickleStepId, hookId } = assertAndReturn(
925+
const { pickleStepId, hookId } = ensure(
926926
testCase.testSteps.find((testStep) => testStep.id === testStepId),
927927
"Expected to find a testStep",
928928
);
929929

930930
if (pickleStepId != null) {
931-
const pickle = assertAndReturn(
931+
const pickle = ensure(
932932
state.messages.current
933933
.map((message) => message.pickle)
934934
.filter(notNull)
935935
.find((pickle) => pickle.id === pickleId),
936936
"Expected to find a pickle",
937937
);
938938

939-
const pickleStep = assertAndReturn(
939+
const pickleStep = ensure(
940940
pickle.steps.find((step) => step.id === pickleStepId),
941941
"Expected to find a pickleStep",
942942
);
943943

944-
const gherkinDocument = assertAndReturn(
944+
const gherkinDocument = ensure(
945945
state.messages.current
946946
.map((message) => message.gherkinDocument)
947947
.filter(notNull)

0 commit comments

Comments
 (0)