22/* eslint-disable no-undef */
33const fs = require ( "fs" ) ;
44
5+ const PROBLEMS_FOLDERS = [
6+ "./LeetcodeProblems/Algorithms/easy/" ,
7+ "./LeetcodeProblems/Algorithms/medium/" ,
8+ "./LeetcodeProblems/Algorithms/hard/"
9+ ] ;
10+
511const TESTS_FOLDERS = [
612 "./LeetcodeProblemsTests/Algorithms/easy/" ,
713 "./LeetcodeProblemsTests/Algorithms/medium/" ,
814 "./LeetcodeProblemsTests/Algorithms/hard/"
9- ]
15+ ] ;
1016
1117const REGEX_PATTERN_HIDDEN_FILES = / ( ^ | \/ ) \. [ ^ \/ \. ] / g;
1218
13- var test_all = async function ( ) {
19+ const getAllTests = async function ( paths ) {
20+ let problems = [ ] ;
21+ for ( const i in paths ) {
22+ const folder = paths [ i ] ;
23+ const newProblems = await loadProblemsFiles ( folder ) ; // await
24+ problems = problems . concat ( newProblems ) ;
25+ }
26+ return problems ;
27+ } ;
28+
29+ const runAllTests = async function ( problems ) {
1430 try {
15- var problems = [ ] ;
16- for ( const i in TESTS_FOLDERS ) {
17- var folder = TESTS_FOLDERS [ i ] ;
18- var new_problems = await loadProblemsFiles ( folder ) ; // await
19- problems = problems . concat ( new_problems ) ;
20- } ;
2131 console . log ( problems ) ;
22-
23- var solvePromises = problems . map ( solve ) ;
32+ var solvePromises = problems . map ( solveProblem ) ;
2433
25- await Promise . all ( solvePromises )
34+ await Promise . all ( solvePromises ) ;
2635 } catch ( error ) {
2736 console . log ( error ) ;
2837 throw new Error ( error ) ;
2938 }
3039} ;
3140
32- var solve = ( problem ) => {
41+ const solveProblem = ( problem ) => {
3342 try {
3443 console . log ( "Solving: " + problem ) ;
3544
@@ -47,28 +56,61 @@ var solve = (problem) => {
4756 console . log ( error ) ;
4857 throw new Error ( error ) ;
4958 }
50- }
59+ } ;
5160
52- var loadProblemsFiles = ( folder ) => {
61+ const loadProblemsFiles = ( folder ) => {
5362 return new Promise ( function ( resolve , reject ) {
5463 fs . readdir ( folder , ( error , files ) => {
5564 if ( error ) {
5665 reject ( error ) ;
5766 } else {
5867 console . log ( folder ) ;
59- new_problems = files . filter ( ( item ) => ! REGEX_PATTERN_HIDDEN_FILES . test ( item ) ) ;
60- new_problems = new_problems . map ( ( item ) => folder + item ) ;
68+ newProblems = files . filter ( ( item ) => ! REGEX_PATTERN_HIDDEN_FILES . test ( item ) ) ;
69+ newProblems = newProblems . map ( ( item ) => folder + item ) ;
6170
62- resolve ( new_problems ) ;
71+ resolve ( newProblems ) ;
6372 }
6473 } ) ;
6574 } ) ;
6675} ;
6776
68- if ( process . argv . length > 2 ) {
69- const path = process . argv . pop ( ) ;
70- solve ( path ) ;
71- } else {
72- test_all ( ) ;
77+ const getMissingTests = async function ( tests , problems ) {
78+ const hasTestStatus = problems . reduce ( ( status , problemPath ) => {
79+ const baseIndex = PROBLEMS_FOLDERS . findIndex ( ( basePath ) =>
80+ problemPath . startsWith ( basePath )
81+ ) ;
82+
83+ let testPath = problemPath
84+ . replace ( PROBLEMS_FOLDERS [ baseIndex ] , TESTS_FOLDERS [ baseIndex ] )
85+ . replace ( / \. j s $ / , "_Test.js" ) ;
86+
87+ status . push ( {
88+ problem : problemPath ,
89+ hasTest : tests . includes ( testPath )
90+ } ) ;
91+
92+ return status ;
93+ } , [ ] ) ;
94+ const missingTests = hasTestStatus . filter ( ( stat ) => ! stat . hasTest ) ;
95+ console . log ( "Total Problems:" , problems . length ) ;
96+ console . log ( "Missing Tests:" , missingTests . length ) ;
97+
98+ if ( missingTests . length ) {
99+ console . table ( missingTests ) ;
100+ }
101+ } ;
102+
103+ async function runScript ( ) {
104+ if ( process . argv . length > 2 ) {
105+ const path = process . argv . pop ( ) ;
106+ solveProblem ( path ) ;
107+ } else {
108+ const problems = await getAllTests ( PROBLEMS_FOLDERS ) ;
109+ const tests = await getAllTests ( TESTS_FOLDERS ) ;
110+
111+ await runAllTests ( tests ) ;
112+ await getMissingTests ( tests , problems ) ;
113+ }
73114}
74-
115+
116+ runScript ( ) ;
0 commit comments