|
| 1 | +import { DataTable, Then } from '../../' |
| 2 | +import { World } from '../support/world' |
| 3 | +import messages from '@cucumber/messages' |
| 4 | +import { expect } from 'chai' |
| 5 | + |
| 6 | +function getSetsOfPicklesRunningAtTheSameTime( |
| 7 | + envelopes: messages.Envelope[] |
| 8 | +): string[] { |
| 9 | + const pickleIdToName: Record<string, string> = {} |
| 10 | + const testCaseIdToPickleId: Record<string, string> = {} |
| 11 | + const testCaseStarteIdToPickleId: Record<string, string> = {} |
| 12 | + let currentRunningPickleIds: string[] = [] |
| 13 | + const result: string[] = [] |
| 14 | + envelopes.forEach((envelope) => { |
| 15 | + if (envelope.pickle != null) { |
| 16 | + pickleIdToName[envelope.pickle.id] = envelope.pickle.name |
| 17 | + } else if (envelope.testCase != null) { |
| 18 | + testCaseIdToPickleId[envelope.testCase.id] = envelope.testCase.pickleId |
| 19 | + } else if (envelope.testCaseStarted != null) { |
| 20 | + const pickleId = testCaseIdToPickleId[envelope.testCaseStarted.testCaseId] |
| 21 | + testCaseStarteIdToPickleId[envelope.testCaseStarted.id] = pickleId |
| 22 | + currentRunningPickleIds.push(pickleId) |
| 23 | + if (currentRunningPickleIds.length > 1) { |
| 24 | + const setOfPickleNames = currentRunningPickleIds |
| 25 | + .map((x) => pickleIdToName[x]) |
| 26 | + .sort() |
| 27 | + .join(', ') |
| 28 | + result.push(setOfPickleNames) |
| 29 | + } |
| 30 | + } else if (envelope.testCaseFinished != null) { |
| 31 | + const pickleId = |
| 32 | + testCaseStarteIdToPickleId[envelope.testCaseFinished.testCaseStartedId] |
| 33 | + currentRunningPickleIds = currentRunningPickleIds.filter( |
| 34 | + (x) => x != pickleId |
| 35 | + ) |
| 36 | + } |
| 37 | + }) |
| 38 | + return result |
| 39 | +} |
| 40 | + |
| 41 | +Then('no pickles run at the same time', function (this: World) { |
| 42 | + const actualSets = getSetsOfPicklesRunningAtTheSameTime( |
| 43 | + this.lastRun.envelopes |
| 44 | + ) |
| 45 | + expect(actualSets).to.eql([]) |
| 46 | +}) |
| 47 | + |
| 48 | +Then( |
| 49 | + 'the following sets of pickles execute at the same time:', |
| 50 | + function (this: World, dataTable: DataTable) { |
| 51 | + const expectedSets = dataTable.raw().map((row) => row[0]) |
| 52 | + const actualSets = getSetsOfPicklesRunningAtTheSameTime( |
| 53 | + this.lastRun.envelopes |
| 54 | + ) |
| 55 | + expect(actualSets).to.eql(expectedSets) |
| 56 | + } |
| 57 | +) |
0 commit comments