Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
with
146 additions
and 16 deletions.
- +2 −0 integration/testing-tools/src/expect/index.ts
- +87 −0 packages/assertions/spec/Check.spec.ts
- +43 −0 packages/assertions/src/Check.ts
- +7 −7 packages/assertions/src/Ensure.ts
- +1 −0 packages/assertions/src/index.ts
- +0 −5 packages/core/src/screenplay/Task.ts
- +6 −4 packages/core/src/stage/crew/serenity-bdd-reporter/reports/ErrorRenderer.ts
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@@ -1,8 +1,10 @@ | ||
import * as chai from 'chai'; | ||
import * as chaiAsPromised from 'chai-as-promised'; | ||
import * as sinonChai from 'sinon-chai'; | ||
import { assertions } from './tiny-types'; | ||
|
||
chai.use(chaiAsPromised); | ||
chai.use(sinonChai); | ||
chai.use(assertions); | ||
|
||
export const expect = chai.expect; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@@ -0,0 +1,87 @@ | ||
import 'mocha'; | ||
|
||
import { expect } from '@integration/testing-tools'; | ||
import { Actor, Interaction } from '@serenity-js/core'; | ||
import * as sinon from 'sinon'; | ||
import { Check, startsWith } from '../src'; | ||
|
||
/** @test {Check} */ | ||
describe('Check', () => { | ||
|
||
const Enrique = Actor.named('Enrique'); | ||
|
||
const Call = (fn: () => void) => Interaction.where(`#actor calls a function`, actor => fn()); | ||
|
||
let spy: sinon.SinonSpy; | ||
beforeEach(() => spy = sinon.spy()); | ||
|
||
describe('(if branch)', () => { | ||
|
||
/** @test {Check.whether} */ | ||
/** @test {Check#andIfSo} */ | ||
it('makes the actor execute the activities when the expectation is met', () => | ||
expect(Enrique.attemptsTo( | ||
Check.whether('Hello World', startsWith('Hello')) | ||
.andIfSo( | ||
Call(() => spy(true)), | ||
Call(() => spy(true)), | ||
), | ||
)).to.be.fulfilled. | ||
then(() => { | ||
expect(spy).to.have.been.calledWith(true).callCount(2); | ||
}), | ||
); | ||
|
||
/** @test {Check.whether} */ | ||
/** @test {Check#andIfSo} */ | ||
it('makes the actor ignore the activities when the expectation is not met', () => | ||
expect(Enrique.attemptsTo( | ||
Check.whether('Hello World', startsWith('¡Hola')) | ||
.andIfSo( | ||
Call(() => spy(true)), | ||
), | ||
)).to.be.fulfilled. | ||
then(() => { | ||
expect(spy).to.not.have.been.called; // tslint:disable-line:no-unused-expression | ||
}), | ||
); | ||
}); | ||
|
||
describe('(if/else branches)', () => { | ||
/** @test {Check.whether} */ | ||
/** @test {Check#andIfSo} */ | ||
/** @test {Check#otherwise} */ | ||
it('makes the actor execute the activities when the expectation is met', () => | ||
expect(Enrique.attemptsTo( | ||
Check.whether('Hello World', startsWith('Hello')) | ||
.andIfSo( | ||
Call(() => spy(true)), | ||
) | ||
.otherwise( | ||
Call(() => spy(false)), | ||
), | ||
)).to.be.fulfilled. | ||
then(() => { | ||
expect(spy).to.have.been.calledWith(true).callCount(1); | ||
}), | ||
); | ||
|
||
/** @test {Check.whether} */ | ||
/** @test {Check#andIfSo} */ | ||
/** @test {Check#otherwise} */ | ||
it('makes the actor execute the alternative activities when the expectation is not met', () => | ||
expect(Enrique.attemptsTo( | ||
Check.whether('Hello World', startsWith('¡Hola')) | ||
.andIfSo( | ||
Call(() => spy(true)), | ||
) | ||
.otherwise( | ||
Call(() => spy(false)), | ||
), | ||
)).to.be.fulfilled. | ||
then(() => { | ||
expect(spy).to.have.been.calledWith(false).callCount(1); | ||
}), | ||
); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@@ -0,0 +1,43 @@ | ||
import { Activity, AnswersQuestions, KnowableUnknown, PerformsTasks, Task } from '@serenity-js/core'; | ||
import { formatted } from '@serenity-js/core/lib/io'; | ||
import { match } from 'tiny-types'; | ||
import { Expectation } from './Expectation'; | ||
import { ExpectationMet, ExpectationNotMet, Outcome } from './outcomes'; | ||
|
||
export class Check<Actual> implements Task { | ||
static whether<A>(actual: KnowableUnknown<A>, expectation: Expectation<any, A>) { | ||
return { | ||
andIfSo: (...activities: Activity[]) => new Check(actual, expectation, activities), | ||
}; | ||
} | ||
|
||
constructor( | ||
private readonly actual: KnowableUnknown<Actual>, | ||
private readonly expectation: Expectation<any, Actual>, | ||
private readonly activities: Activity[], | ||
private readonly alternativeActivities: Activity[] = [], | ||
) { | ||
} | ||
|
||
otherwise(...alternativeActivities: Activity[]) { | ||
return new Check<Actual>(this.actual, this.expectation, this.activities, alternativeActivities); | ||
} | ||
|
||
performAs(actor: AnswersQuestions & PerformsTasks): PromiseLike<void> { | ||
return Promise.all([ | ||
actor.answer(this.actual), | ||
actor.answer(this.expectation), | ||
]).then(([actual, expectation]) => | ||
expectation(actual).then(outcome => | ||
match<Outcome<any, Actual>, void>(outcome) | ||
.when(ExpectationMet, o => actor.attemptsTo(...this.activities)) | ||
.when(ExpectationNotMet, o => actor.attemptsTo(...this.alternativeActivities)) | ||
.else(_ => void 0), | ||
), | ||
); | ||
} | ||
|
||
toString(): string { | ||
return formatted `#actor ensures that ${ this.actual } does ${ this.expectation }`; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@@ -1,3 +1,4 @@ | ||
export * from './Check'; | ||
export * from './Expectation'; | ||
export * from './expectations'; | ||
export * from './Ensure'; | ||
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters