diff options
author | Mike Gerwitz <mike.gerwitz@rtspecialty.com> | 2019-09-17 15:22:52 -0400 |
---|---|---|
committer | Mike Gerwitz <mike.gerwitz@rtspecialty.com> | 2019-10-18 09:55:09 -0400 |
commit | fe105789498377b49e409b30c3e17f152750fed1 (patch) | |
tree | 76abb5f0e5f81f398cfd4be65ce8e50bd8b60424 /test/error | |
parent | 1f66a25658e32706b6fb6e80421f7f951475369a (diff) | |
download | liza-fe105789498377b49e409b30c3e17f152750fed1.tar.gz liza-fe105789498377b49e409b30c3e17f152750fed1.tar.bz2 liza-fe105789498377b49e409b30c3e17f152750fed1.zip |
{Context,Chained}Error: New modules
This will allow us to provide additional useful information for structure
logging.
Diffstat (limited to 'test/error')
-rw-r--r-- | test/error/ChainedErrorTest.ts | 67 | ||||
-rw-r--r-- | test/error/ContextErrorTest.ts | 68 |
2 files changed, 135 insertions, 0 deletions
diff --git a/test/error/ChainedErrorTest.ts b/test/error/ChainedErrorTest.ts new file mode 100644 index 0000000..d6d98ed --- /dev/null +++ b/test/error/ChainedErrorTest.ts @@ -0,0 +1,67 @@ +/** + * Tests error chaining + * + * Copyright (C) 2010-2019 R-T Specialty, LLC. + * + * This file is part of the Liza Data Collection Framework. + * + * liza is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + */ + +import * as sut from "../../src/error/ChainedError"; +import { expect } from 'chai'; + + +describe( 'ChainedError', () => +{ + it( "can be created with generic error", () => + { + const eprev = new Error( "previous error" ); + + expect( sut.chain( new Error( "new error" ), eprev ).chain ) + .to.equal( eprev ); + } ); + + + it( "can be chained to arbitrary depth", () => + { + const e1 = new Error( "lower" ); + const e2 = sut.chain( new Error( "mid" ), e1 ); + const e3 = sut.chain( new Error( "outer" ), e2 ); + + expect( sut.isChained( e3 ) ).to.be.true; + expect( sut.isChained( e2 ) ).to.be.true; + expect( sut.isChained( e1 ) ).to.be.false; + } ); + + + it( "provides type predicate for TypeScript", () => + { + const inner = new Error( "inner" ); + + // force to Error to discard ChainedError type + const outer: Error = sut.chain( new Error( "outer" ), inner ); + + if ( sut.isChained( outer ) ) + { + // if isChained was properly defined, then outer should now + // have type ChainedError, and so this should compile + expect( outer.chain ).to.equal( inner ); + } + else + { + expect.fail(); + } + } ); +} ); diff --git a/test/error/ContextErrorTest.ts b/test/error/ContextErrorTest.ts new file mode 100644 index 0000000..c02c3f2 --- /dev/null +++ b/test/error/ContextErrorTest.ts @@ -0,0 +1,68 @@ +/** + * Tests error context + * + * Copyright (C) 2010-2019 R-T Specialty, LLC. + * + * This file is part of the Liza Data Collection Framework. + * + * liza is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + */ + +import * as sut from "../../src/error/ContextError"; +import { expect } from 'chai'; + + +describe( 'ContextError', () => +{ + it( "can be created with generic error", () => + { + const context = { foo: "context" }; + + expect( sut.context( new Error( "test error" ), context ).context ) + .to.equal( context ); + } ); + + + it( "provides type predicate for TypeScript", () => + { + const context = { bar: "baz context" }; + + // force to Error to discard ContextError type + const e: Error = sut.context( new Error( "test error" ), context ); + + if ( sut.hasContext( e ) ) + { + // if isChained was properly defined, then outer should now + // have type ChainedError, and so this should compile + expect( e.context ).to.equal( context ); + } + else + { + expect.fail(); + } + } ); + + + it( "can create typed contexts", () => + { + type FooErrorContext = { foo: string }; + + // this is the actual test + const e: sut.ContextError<FooErrorContext> = + sut.context( new Error( "test error" ), { foo: "context" } ); + + // contravariance check (would fail to compile) + expect( sut.hasContext( e ) ).to.be.true; + } ); +} ); |