@@ -4,66 +4,63 @@ import { resolve, dirname } from 'path';
44// TODO: test approximate size (need ability to convert Unit, e.g kb to KB)
55
66function inLoopExpect ( array , prop , isNot = false ) {
7- return {
8- get not ( ) {
9- return inLoopExpect ( array , prop , true ) ;
10- } ,
11-
12- toContain ( expected ) {
7+ // #region sphagetti helpers 😋 (need Typescript!!!)
8+ const an = ( expected , stat ) => typeof expected === 'function' ? expected ( stat ) : expected ;
9+ const call = ( func , expected ) => {
10+ if ( expected ) {
1311 for ( const element of array ) {
1412 if ( isNot ) {
15- if ( prop ) expect ( element [ prop ] ) . not . toContain ( expected ) ;
16- else expect ( element ) . not . toContain ( expected ) ;
17- } else if ( prop ) expect ( element [ prop ] ) . toContain ( expected ) ;
18- else expect ( element ) . toContain ( expected ) ;
13+ if ( prop ) expect ( element [ prop ] ) . not [ func ] ( an ( expected , element ) ) ;
14+ else expect ( element ) . not [ func ] ( an ( expected , element ) ) ;
15+ } else if ( prop ) expect ( element [ prop ] ) [ func ] ( an ( expected , element ) ) ;
16+ else expect ( element ) [ func ] ( an ( expected , element ) ) ;
1917 }
20- } ,
21-
22- toBeLessThan ( expected ) {
18+ } else {
2319 for ( const element of array ) {
2420 if ( isNot ) {
25- if ( prop ) expect ( element [ prop ] ) . not . toBeLessThan ( expected ) ;
26- else expect ( element ) . not . toBeLessThan ( expected ) ;
27- } else if ( prop ) expect ( element [ prop ] ) . toBeLessThan ( expected ) ;
28- else expect ( element ) . toBeLessThan ( expected ) ;
21+ if ( prop ) expect ( element [ prop ] ) . not [ func ] ( ) ;
22+ else expect ( element ) . not [ func ] ( ) ;
23+ } else if ( prop ) expect ( element [ prop ] ) [ func ] ( ) ;
24+ else expect ( element ) [ func ] ( ) ;
2925 }
30- } ,
26+ }
27+ } ;
28+ // #endregion
3129
32- toMatchSnapshot ( ) {
33- for ( const element of array ) {
34- if ( isNot ) {
35- if ( prop ) expect ( element [ prop ] ) . not . toMatchSnapshot ( ) ;
36- else expect ( element ) . not . toMatchSnapshot ( ) ;
37- } else if ( prop ) expect ( element [ prop ] ) . toMatchSnapshot ( ) ;
38- else expect ( element ) . toMatchSnapshot ( ) ;
39- }
40- } ,
30+ return {
31+ get not ( ) { return inLoopExpect ( array , prop , true ) } ,
32+ toContain : expected => call ( 'toContain' , expected ) ,
33+ toBeLessThan : expected => call ( 'toBeLessThan' , expected ) ,
34+ toBeLessThanOrEqual : expected => call ( 'toBeLessThanOrEqual' , expected ) ,
35+ toBeGreaterThan : expected => call ( 'toBeGreaterThan' , expected ) ,
36+ toBeGreaterThanOrEqual : expected => call ( 'toBeGreaterThanOrEqual' , expected ) ,
37+ toMatchSnapshot : ( ) => call ( 'toMatchSnapshot' ) ,
4138 } ;
4239}
4340
41+ /* eslint no-dupe-keys: "off", no-param-reassign: "off"*/
4442function chainer ( statModules ) {
43+ statModules . forEach ( stat => {
44+ const { issuer, name } = stat ;
45+ stat . originSize = issuer ? statSync ( resolve ( dirname ( issuer ) , name ) ) . size : null ;
46+ } ) ;
47+
4548 return {
49+ get get ( ) { return statModules } ,
50+
4651 get : prop => inLoopExpect ( statModules , prop ) ,
4752 get source ( ) { return inLoopExpect ( statModules , 'source' ) } ,
4853 get providedExports ( ) { return inLoopExpect ( statModules , 'providedExports' ) } ,
54+ get size ( ) { return inLoopExpect ( statModules , 'size' ) } ,
4955
5056 get originSize ( ) {
5157 return inLoopExpect (
5258 statModules . map ( ( { issuer, name } ) => statSync ( resolve ( dirname ( issuer ) , name ) ) . size )
5359 ) ;
5460 } ,
5561
56- withExtension ( extension ) {
57- return chainer (
58- statModules . filter ( ( { name } ) => name . includes ( extension ) )
59- ) ;
60- } ,
61-
62- withoutExtension ( extension ) {
63- return chainer (
64- statModules . filter ( ( { name } ) => ! name . includes ( extension ) )
65- ) ;
66- } ,
62+ withExtension : extension => chainer ( statModules . filter ( ( { name } ) => name . includes ( extension ) ) ) ,
63+ withoutExtension : extension => chainer ( statModules . filter ( ( { name } ) => ! name . includes ( extension ) ) ) ,
6764 } ;
6865}
6966
0 commit comments