1+
2+
13/**
24 * function-regex <https://github.com/tunnckoCore/function-regex>
35 *
810'use strict' ;
911
1012var assert = require ( 'assert' ) ;
11- var functionRegex = require ( './index' ) ;
13+ var fnRegex = require ( './index' ) ;
14+ var cleanupCoverageCode = require ( 'cleanup-coverage-code' ) ;
1215var fixtures = require ( './fixtures' ) ;
1316
1417function match ( str ) {
15- return str . toString ( ) . match ( functionRegex ( ) ) ;
18+ return str . toString ( ) . match ( fnRegex ( ) ) ;
19+ }
20+
21+ function hackExpected ( expected ) {
22+ expected = expected . split ( 'var' ) . filter ( Boolean ) ;
23+ expected = expected . map ( function ( val ) {
24+ return val . replace ( / \s + / g, '' ) ;
25+ } ) . map ( function ( val ) {
26+ return val . replace ( / ( f u n c t i o n | r e t u r n ) (?: t | f ) * ?(? ! \{ ) / g, '$1 ' ) ;
27+ } ) ;
28+ expected = expected . filter ( Boolean ) . join ( 'var ' ) ;
29+ expected = 'var ' + expected ;
30+
31+ return expected ;
1632}
1733
1834describe ( 'function-regex:' , function ( ) {
@@ -33,23 +49,37 @@ describe('function-regex:', function() {
3349 var actual = match ( fixtures . fixtureOne ( ) . fn ) [ 2 ]
3450 var expected = 'cmd, args, opts' ;
3551
52+ // hack, because the coverage tools remove whitespaces
53+ actual = actual . split ( ',' ) . join ( ', ' ) ;
3654 assert . strictEqual ( actual , expected ) ;
3755
56+ // fixtureTwo
3857 actual = match ( fixtures . fixtureTwo ( ) . fn ) [ 2 ]
3958 expected = 'val' ;
4059
4160 assert . strictEqual ( actual , expected ) ;
4261 done ( ) ;
4362 } ) ;
4463
45- it ( 'should match function body' , function ( done ) {
64+ it ( 'should match function body (fixtureOne) ' , function ( done ) {
4665 var actual = match ( fixtures . fixtureOne ( ) . fn ) [ 3 ]
47- var expected = fixtures . fixtureOne ( ) . body ;
66+ var expected = hackExpected ( fixtures . fixtureOne ( ) . body ) ;
67+
68+ // hack, because the coverage tools add some ugly code
69+ // like `__cov_Ejgcx$XN18CSfmeWn$f7vQ.f['2']++`
70+ actual = cleanupCoverageCode ( actual ) ;
4871
4972 assert . strictEqual ( actual , expected ) ;
73+ done ( ) ;
74+ } ) ;
75+
76+ it ( 'should match function body (fixtureTwo)' , function ( done ) {
77+ var actual = match ( fixtures . fixtureOne ( ) . fn ) [ 3 ]
78+ var expected = hackExpected ( fixtures . fixtureOne ( ) . body ) ;
5079
51- actual = match ( fixtures . fixtureTwo ( ) . fn ) [ 3 ]
52- expected = fixtures . fixtureTwo ( ) . body ;
80+ // hack, because the coverage tools add some ugly code
81+ // like `__cov_Ejgcx$XN18CSfmeWn$f7vQ.f['2']++`
82+ actual = cleanupCoverageCode ( actual ) ;
5383
5484 assert . strictEqual ( actual , expected ) ;
5585 done ( ) ;
0 commit comments