From 4e67868284489c7320564bce6a5ddc411b79194f Mon Sep 17 00:00:00 2001 From: Michael Haufe Date: Wed, 25 Sep 2019 20:09:37 -0500 Subject: [PATCH] Implemented Unit test --- src/InvariantDecorator.test.ts | 47 ++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) diff --git a/src/InvariantDecorator.test.ts b/src/InvariantDecorator.test.ts index 5213f84..b23519f 100644 --- a/src/InvariantDecorator.test.ts +++ b/src/InvariantDecorator.test.ts @@ -663,4 +663,51 @@ describe('An invariant decorator must accept multiple assertions', () => { return Stack; }).not.toThrow(); }); +}); + +/** + * Requirement 187 + * https://dev.azure.com/thenewobjective/decorator-contracts/_workitems/edit/187 + */ +describe('A subclass with its own invariants must enforce all ancestor invariants', () => { + test('Debug Mode', () => { + let {invariant} = new Contracts(true); + + expect(() => { + @invariant( + self => self instanceof Base, + self => self != null + ) + class Base {} + + @invariant(self => self instanceof Sub) + class Sub extends Base {} + + return new Sub(); + }).not.toThrow(); + + expect(() => { + @invariant(self => self instanceof Array) + class Base {} + + @invariant(self => self instanceof Sub) + class Sub extends Base {} + + return new Sub(); + }).toThrow(AssertionError); + + expect(() => { + @invariant( + self => self instanceof Base, + 'I thought I was a Base!' + ) + class Base {} + + @invariant(self => self instanceof Array) + // @ts-ignore : Ignore unused error + class Sub extends Base {} + + return new Base(); + }).not.toThrow(); + }); }); \ No newline at end of file