|
| 1 | +/* |
| 2 | + * Copyright 2016 Wave Software |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +import { describe, expect, it } from "vitest"; |
| 18 | +import { |
| 19 | + EidIllegalArgumentException, |
| 20 | + EidIllegalStateException, |
| 21 | + EidIndexOutOfBoundsException, |
| 22 | + EidNullPointerException, |
| 23 | + EidRuntimeException, |
| 24 | +} from "../exceptions"; |
| 25 | + |
| 26 | +describe("EidRuntimeException", () => { |
| 27 | + const ex = new EidRuntimeException("20160110:230223", "A null pointer occured!"); |
| 28 | + |
| 29 | + it("should contain proper message", () => { |
| 30 | + expect(ex.toString()).toMatch( |
| 31 | + /EidRuntimeException: \[20160110:230223\]<[a-z0-9]+> A null pointer occured!/, |
| 32 | + ); |
| 33 | + }); |
| 34 | + |
| 35 | + it("should contain a stack trace", () => { |
| 36 | + expect(ex.stack).toBeTruthy(); |
| 37 | + }); |
| 38 | + |
| 39 | + it("should inherits Error", () => { |
| 40 | + expect(ex instanceof Error).toBe(true); |
| 41 | + }); |
| 42 | + |
| 43 | + it("should not inherits TypeError or other types of Eid*Exception", () => { |
| 44 | + expect(ex instanceof TypeError).toBe(false); |
| 45 | + expect(ex instanceof EidIllegalStateException).toBe(false); |
| 46 | + expect(ex instanceof EidIllegalArgumentException).toBe(false); |
| 47 | + expect(ex instanceof EidIndexOutOfBoundsException).toBe(false); |
| 48 | + expect(ex instanceof EidNullPointerException).toBe(false); |
| 49 | + }); |
| 50 | + |
| 51 | + describe("construction without passing eid object", () => { |
| 52 | + it("should fail with TypeError", () => { |
| 53 | + let caught: unknown; |
| 54 | + try { |
| 55 | + new EidRuntimeException(undefined); |
| 56 | + } catch (e) { |
| 57 | + caught = e; |
| 58 | + } |
| 59 | + expect(caught).toBeInstanceOf(TypeError); |
| 60 | + expect((caught as TypeError).message).toEqual( |
| 61 | + "You need to provide an valid Eid number to EidRuntimeExceptions, but given undefined", |
| 62 | + ); |
| 63 | + }); |
| 64 | + }); |
| 65 | +}); |
| 66 | + |
| 67 | +describe("EidNullPointerException", () => { |
| 68 | + const ex = new EidNullPointerException("20160110:223238", "A null pointer occured!"); |
| 69 | + |
| 70 | + it("should contain proper message", () => { |
| 71 | + expect(ex.toString()).toMatch( |
| 72 | + /EidNullPointerException: \[20160110:223238\]<[a-z0-9]+> A null pointer occured!/, |
| 73 | + ); |
| 74 | + }); |
| 75 | + |
| 76 | + it("should contain a stack trace", () => { |
| 77 | + expect(ex.stack).toBeTruthy(); |
| 78 | + }); |
| 79 | + |
| 80 | + it("should inherits Error and EidRuntimeException", () => { |
| 81 | + expect(ex instanceof EidNullPointerException).toBe(true); |
| 82 | + expect(ex instanceof Error).toBe(true); |
| 83 | + expect(ex instanceof EidRuntimeException).toBe(true); |
| 84 | + }); |
| 85 | + |
| 86 | + it("should not inherits TypeError or other types of Eid*Exception", () => { |
| 87 | + expect(ex instanceof TypeError).toBe(false); |
| 88 | + expect(ex instanceof EidIllegalStateException).toBe(false); |
| 89 | + expect(ex instanceof EidIllegalArgumentException).toBe(false); |
| 90 | + expect(ex instanceof EidIndexOutOfBoundsException).toBe(false); |
| 91 | + }); |
| 92 | +}); |
| 93 | + |
| 94 | +describe("EidIllegalArgumentException", () => { |
| 95 | + const ex = new EidIllegalArgumentException("20160110:230031", "A null pointer occured!"); |
| 96 | + |
| 97 | + it("should contain proper message", () => { |
| 98 | + expect(ex.toString()).toMatch( |
| 99 | + /EidIllegalArgumentException: \[20160110:230031\]<[a-z0-9]+> A null pointer occured!/, |
| 100 | + ); |
| 101 | + }); |
| 102 | + |
| 103 | + it("should contain a stack trace", () => { |
| 104 | + expect(ex.stack).toBeTruthy(); |
| 105 | + }); |
| 106 | + |
| 107 | + it("should inherits Error and EidRuntimeException", () => { |
| 108 | + expect(ex instanceof Error).toBe(true); |
| 109 | + expect(ex instanceof EidRuntimeException).toBe(true); |
| 110 | + expect(ex instanceof EidIllegalArgumentException).toBe(true); |
| 111 | + }); |
| 112 | + |
| 113 | + it("should not inherits TypeError or other types of Eid*Exception", () => { |
| 114 | + expect(ex instanceof TypeError).toBe(false); |
| 115 | + expect(ex instanceof EidIllegalStateException).toBe(false); |
| 116 | + expect(ex instanceof EidNullPointerException).toBe(false); |
| 117 | + expect(ex instanceof EidIndexOutOfBoundsException).toBe(false); |
| 118 | + }); |
| 119 | +}); |
| 120 | + |
| 121 | +describe("EidIllegalStateException", () => { |
| 122 | + const ex = new EidIllegalStateException("20160110:230112", "Illegal state!"); |
| 123 | + const regex = /EidIllegalStateException: \[20160110:230112\]<[a-z0-9]+> Illegal state!/; |
| 124 | + |
| 125 | + it("should contain proper message", () => { |
| 126 | + expect(ex.toString()).toMatch(regex); |
| 127 | + }); |
| 128 | + |
| 129 | + it("should contain a stack trace", () => { |
| 130 | + expect(ex.stack).toBeTruthy(); |
| 131 | + expect(ex.stack).toMatch(regex); |
| 132 | + }); |
| 133 | + |
| 134 | + it("should inherits Error and EidRuntimeException", () => { |
| 135 | + expect(ex instanceof Error).toBe(true); |
| 136 | + expect(ex instanceof EidRuntimeException).toBe(true); |
| 137 | + expect(ex instanceof EidIllegalStateException).toBe(true); |
| 138 | + }); |
| 139 | + |
| 140 | + it("should not inherits TypeError or other types of Eid*Exception", () => { |
| 141 | + expect(ex instanceof TypeError).toBe(false); |
| 142 | + expect(ex instanceof EidIllegalArgumentException).toBe(false); |
| 143 | + expect(ex instanceof EidNullPointerException).toBe(false); |
| 144 | + expect(ex instanceof EidIndexOutOfBoundsException).toBe(false); |
| 145 | + }); |
| 146 | +}); |
| 147 | + |
| 148 | +describe("EidIndexOutOfBoundsException", () => { |
| 149 | + const ex = new EidIndexOutOfBoundsException("20160110:230140", "Index is out of bounds!"); |
| 150 | + const regex = |
| 151 | + /EidIndexOutOfBoundsException: \[20160110:230140\]<[a-z0-9]+> Index is out of bounds!/; |
| 152 | + |
| 153 | + it("should contain proper message", () => { |
| 154 | + expect(ex.toString()).toMatch(regex); |
| 155 | + }); |
| 156 | + |
| 157 | + it("should contain a stack trace", () => { |
| 158 | + expect(ex.stack).toBeTruthy(); |
| 159 | + expect(ex.stack).toMatch(regex); |
| 160 | + }); |
| 161 | + |
| 162 | + it("should inherits Error and EidRuntimeException", () => { |
| 163 | + expect(ex instanceof Error).toBe(true); |
| 164 | + expect(ex instanceof EidRuntimeException).toBe(true); |
| 165 | + expect(ex instanceof EidIndexOutOfBoundsException).toBe(true); |
| 166 | + }); |
| 167 | + |
| 168 | + it("should not inherits TypeError or other types of Eid*Exception", () => { |
| 169 | + expect(ex instanceof TypeError).toBe(false); |
| 170 | + expect(ex instanceof EidIllegalArgumentException).toBe(false); |
| 171 | + expect(ex instanceof EidIllegalStateException).toBe(false); |
| 172 | + expect(ex instanceof EidNullPointerException).toBe(false); |
| 173 | + }); |
| 174 | +}); |
| 175 | + |
| 176 | +describe("cause support", () => { |
| 177 | + it("should store cause when provided to EidRuntimeException", () => { |
| 178 | + const originalError = new TypeError("original failure"); |
| 179 | + const ex = new EidRuntimeException("20260714:100000", "wrapped", originalError); |
| 180 | + expect(ex.cause).toBe(originalError); |
| 181 | + expect(ex.message).toMatch(/wrapped/); |
| 182 | + }); |
| 183 | + |
| 184 | + it("should have undefined cause when not provided", () => { |
| 185 | + const ex = new EidRuntimeException("20260714:100001", "no cause"); |
| 186 | + expect(ex.cause).toBeUndefined(); |
| 187 | + }); |
| 188 | + |
| 189 | + it("should propagate cause through subclasses", () => { |
| 190 | + const cause = new RangeError("out of range"); |
| 191 | + const ex = new EidIllegalStateException("20260714:100002", "state error", cause); |
| 192 | + expect(ex.cause).toBe(cause); |
| 193 | + expect(ex).toBeInstanceOf(EidRuntimeException); |
| 194 | + expect(ex).toBeInstanceOf(Error); |
| 195 | + }); |
| 196 | + |
| 197 | + it("should accept non-Error cause values", () => { |
| 198 | + const ex = new EidRuntimeException("20260714:100003", "string cause", "a string cause"); |
| 199 | + expect(ex.cause).toBe("a string cause"); |
| 200 | + }); |
| 201 | +}); |
0 commit comments