11import type { Simplify } from 'type-fest' ;
22import { describe , expect , expectTypeOf , it , test } from 'vitest' ;
33
4- import { BaseException , ExceptionBuilder } from '../exception.js' ;
4+ import { BaseException , ExceptionBuilder , OutOfRangeException , ValueException } from '../exception.js' ;
55
6- import type { ExceptionConstructor , ExceptionInstance } from '../exception.js' ;
6+ import type { ExceptionConstructor } from '../exception.js' ;
77
88type ExceptionOptionsWithCode = Simplify < ErrorOptions & { details : { code : number } } > ;
99type ExceptionOptionsWithCause = Simplify < ErrorOptions & { cause : Error } > ;
@@ -13,31 +13,28 @@ type ExceptionParams = { name: 'TestException' };
1313type ExceptionParamsWithMessage = Simplify < ExceptionParams & { message : string } > ;
1414
1515test ( 'ExceptionConstructor' , ( ) => {
16- expectTypeOf < ExceptionConstructor < ExceptionParams , ErrorOptions > > ( ) . toEqualTypeOf <
17- new ( message ?: string , options ?: ErrorOptions ) => ExceptionInstance < ExceptionParams , ErrorOptions >
16+ expectTypeOf < ExceptionConstructor < ExceptionParams , ErrorOptions > > ( ) . toMatchTypeOf <
17+ new ( message ?: string , options ?: ErrorOptions ) => BaseException < ExceptionParams , ErrorOptions >
1818 > ( ) ;
19- expectTypeOf < ExceptionConstructor < ExceptionParams , ExceptionOptionsWithCode > > ( ) . toEqualTypeOf <
20- new (
21- message : string ,
22- options : ExceptionOptionsWithCode
23- ) => ExceptionInstance < ExceptionParams , ExceptionOptionsWithCode >
19+ expectTypeOf < ExceptionConstructor < ExceptionParams , ExceptionOptionsWithCode > > ( ) . toMatchTypeOf <
20+ new ( message : string , options : ExceptionOptionsWithCode ) => BaseException < ExceptionParams , ExceptionOptionsWithCode >
2421 > ( ) ;
25- expectTypeOf < ExceptionConstructor < ExceptionParams , ExceptionOptionsWithCause > > ( ) . toEqualTypeOf <
22+ expectTypeOf < ExceptionConstructor < ExceptionParams , ExceptionOptionsWithCause > > ( ) . toMatchTypeOf <
2623 new (
2724 message : string ,
2825 options : ExceptionOptionsWithCause
29- ) => ExceptionInstance < ExceptionParams , ExceptionOptionsWithCause >
26+ ) => BaseException < ExceptionParams , ExceptionOptionsWithCause >
3027 > ( ) ;
31- expectTypeOf < ExceptionConstructor < ExceptionParams , ExceptionOptionsWithCodeAndCause > > ( ) . toEqualTypeOf <
28+ expectTypeOf < ExceptionConstructor < ExceptionParams , ExceptionOptionsWithCodeAndCause > > ( ) . toMatchTypeOf <
3229 new (
3330 message : string ,
3431 options : ExceptionOptionsWithCodeAndCause
35- ) => ExceptionInstance < ExceptionParams , ExceptionOptionsWithCodeAndCause >
32+ ) => BaseException < ExceptionParams , ExceptionOptionsWithCodeAndCause >
3633 > ( ) ;
37- expectTypeOf < ExceptionConstructor < ExceptionParamsWithMessage , ExceptionOptionsWithCodeAndCause > > ( ) . toEqualTypeOf <
34+ expectTypeOf < ExceptionConstructor < ExceptionParamsWithMessage , ExceptionOptionsWithCodeAndCause > > ( ) . toMatchTypeOf <
3835 new (
3936 options : ExceptionOptionsWithCodeAndCause
40- ) => ExceptionInstance < ExceptionParamsWithMessage , ExceptionOptionsWithCodeAndCause >
37+ ) => BaseException < ExceptionParamsWithMessage , ExceptionOptionsWithCodeAndCause >
4138 > ( ) ;
4239} ) ;
4340
@@ -58,12 +55,12 @@ describe('BaseException', () => {
5855
5956describe ( 'ExceptionBuilder' , ( ) => {
6057 it ( 'should return never for the build method if no name is specified' , ( ) => {
61- const fn = ( ) => new ExceptionBuilder ( ) . build ( ) ;
58+ const fn = ( ) : never => new ExceptionBuilder ( ) . build ( ) ;
6259 expect ( fn ) . toThrow ( 'Cannot build exception: params is undefined' ) ;
6360 expectTypeOf < ReturnType < typeof fn > > ( ) . toBeNever ( ) ;
6461 } ) ;
6562 it ( 'should build an exception with the provided name and message' , ( ) => {
66- const TestException = new ExceptionBuilder ( ) . setParams ( { name : 'TestException' } ) . build ( ) ;
63+ const { TestException } = new ExceptionBuilder ( ) . setParams ( { name : 'TestException' } ) . build ( ) ;
6764 expect ( Object . getPrototypeOf ( TestException ) ) . toBe ( BaseException ) ;
6865 expectTypeOf < Pick < InstanceType < typeof TestException > , 'name' > > ( ) . toEqualTypeOf < { name : 'TestException' } > ( ) ;
6966 const error = new TestException ( 'This is a test' ) ;
@@ -73,8 +70,8 @@ describe('ExceptionBuilder', () => {
7370 } ) ;
7471
7572 it ( 'should create distinct constructors' , ( ) => {
76- const TestException = new ExceptionBuilder ( ) . setParams ( { name : 'TestException' } ) . build ( ) ;
77- const OtherException = new ExceptionBuilder ( ) . setParams ( { name : 'OtherException' } ) . build ( ) ;
73+ const { TestException } = new ExceptionBuilder ( ) . setParams ( { name : 'TestException' } ) . build ( ) ;
74+ const { OtherException } = new ExceptionBuilder ( ) . setParams ( { name : 'OtherException' } ) . build ( ) ;
7875 const e1 = new TestException ( 'This is a test' ) ;
7976 const e2 = new OtherException ( 'This is a test' ) ;
8077 expect ( e1 ) . toBeInstanceOf ( BaseException ) ;
@@ -84,7 +81,7 @@ describe('ExceptionBuilder', () => {
8481 } ) ;
8582
8683 it ( 'should allow creating an exception with additional details' , ( ) => {
87- const TestException = new ExceptionBuilder ( )
84+ const { TestException } = new ExceptionBuilder ( )
8885 . setParams ( { name : 'TestException' } )
8986 . setOptionsType < { details : { code : number } } > ( )
9087 . build ( ) ;
@@ -96,7 +93,7 @@ describe('ExceptionBuilder', () => {
9693 } ) ;
9794
9895 it ( 'should allow creating an error with a custom cause' , ( ) => {
99- const TestException = new ExceptionBuilder ( )
96+ const { TestException } = new ExceptionBuilder ( )
10097 . setParams ( { name : 'TestException' } )
10198 . setOptionsType < { cause : Error } > ( )
10299 . build ( ) ;
@@ -108,7 +105,7 @@ describe('ExceptionBuilder', () => {
108105 } ) ;
109106
110107 it ( 'should allow creating an error with a default message' , ( ) => {
111- const TestException = new ExceptionBuilder ( )
108+ const { TestException } = new ExceptionBuilder ( )
112109 . setParams ( { message : 'Custom message' , name : 'TestException' } )
113110 . setOptionsType < { cause : Error } > ( )
114111 . build ( ) ;
@@ -117,3 +114,33 @@ describe('ExceptionBuilder', () => {
117114 expectTypeOf < ConstructorParameters < typeof TestException > > ( ) . toEqualTypeOf < [ options : { cause : Error } ] > ( ) ;
118115 } ) ;
119116} ) ;
117+
118+ describe ( 'ValueException' , ( ) => {
119+ it ( 'should have the correct prototype' , ( ) => {
120+ expect ( Object . getPrototypeOf ( ValueException ) ) . toBe ( BaseException ) ;
121+ } ) ;
122+ } ) ;
123+
124+ describe ( 'OutOfRangeException' , ( ) => {
125+ it ( 'should have the correct prototype' , ( ) => {
126+ expect ( Object . getPrototypeOf ( OutOfRangeException ) ) . toBe ( ValueException ) ;
127+ } ) ;
128+ describe ( 'constructor' , ( ) => {
129+ it ( 'should create the correct message' , ( ) => {
130+ const error = new OutOfRangeException ( {
131+ details : {
132+ max : Infinity ,
133+ min : 0 ,
134+ value : - 1
135+ }
136+ } ) ;
137+ expect ( error . message ) . toBe ( 'Value -1 is out of range (0 - Infinity)' ) ;
138+ } ) ;
139+ } ) ;
140+ describe ( 'ForNonPositive' , ( ) => {
141+ it ( 'should create the correct message' , ( ) => {
142+ const error = OutOfRangeException . forNonPositive ( - 1 ) ;
143+ expect ( error . message ) . toBe ( 'Value -1 is out of range (0 - Infinity)' ) ;
144+ } ) ;
145+ } ) ;
146+ } ) ;
0 commit comments