import { AnswersQuestions } from '@serenity-js/core'; import { match } from 'tiny-types'; import { Expectation } from '../Expectation'; import { ExpectationMet, ExpectationNotMet, Outcome } from '../outcomes'; export function not(assertion: Expectation): Expectation { return new Not(assertion); } /** * @package */ class Not extends Expectation { constructor(private readonly expectation: Expectation) { super(); } answeredBy(actor: AnswersQuestions): (actual: Actual) => Promise> { return (actual: any) => this.expectation.answeredBy(actor)(actual) .then((outcome: Outcome) => match, Outcome>(outcome) .when(ExpectationMet, o => new ExpectationNotMet(this.flipped(this.expectation.toString()), o.expected, o.actual)) .else(o => new ExpectationMet(this.flipped(this.expectation.toString()), o.expected, o.actual))); } toString(): string { return this.flipped(this.expectation.toString()); } private flipped(message: string): string { return message.startsWith('not ') ? message.substring(4) : `not ${ message }`; } }