ignacio-chiazzo / Algorithms-Leetcode-Javascript

Algorithms resolution in Javascript. Leetcode - Geeksforgeeks - Careercup
https://ignacio-chiazzo.github.io/Algorithms-Leetcode-Javascript/
MIT License
608 stars 88 forks source link

Modifying Tests.js to handle multiple test exports #68

Closed hot9cups closed 1 year ago

hot9cups commented 1 year ago

This is in response to the comment from my last PR. I've been experimenting ever since, and I think there's a way it could be achieved.

What we want to do:

  1. For every problem, if we have multiple solutions, then we would like to have multiple test functions, one for each solution. This should help in debugging as well as is a cleaner and more maintainable way of writing code. We should also be free to name our test functions anything, as opposed to the rigid and undescriptive names(test or in some cases test1, test2, etc) we have right now,
  2. We want to modify Tests.js to be able to handle the test imports dynamically. Right now we hardcode it to invoke the test function as can be seen here. We want to get rid of this hardcoding as well as be able to invoke varying number of tests per test file as opposed to being able to invoke just 1 test per test file currently.

After having explored some of the options Javascript offers, I have found a solution to do this. I'll be happy to work on the PR if you'd like to assign the issue to me.

ignacio-chiazzo commented 1 year ago

After having explored some of the options Javascript offers, I have found a solution to do this.

Could you elaborate on the solution you propose?

I'm cool with doing:

function testSolution1() { }
function testSolution2() { }
function testSolution3() { }

function test() {
  testSolution1()
  testSolution2()
  testSolution3()
}

module.exports.test = test;

and then having a Test.js file that calls all the tests functions. If you have a solution to the hardcoding I would like to hear it. Thanks!

hot9cups commented 1 year ago

I'm on phone but I'll try to be elaborate.

So what you typed is kinda what we have already anyway, maybe with 1 degree of abstraction lesser where the code for both testSolution1 and testSolution2 for example is within the test method instead of being in separate functions, but either way this already exists. The problem is having to hardcode the method name to be test for instance instead of something more generic.

Again since this already exists, I thought your comment earlier was to help move to a state where we're allowed to have multiple functions with whatever names we'd like, have them all exported and run through the Tests.js file without having to hardcode the function names in Tests.js If this isn't what you intended to begin with, well then I suppose I misunderstood your comment and so this issue could be closed right away I guess.

Otherwise, the solution I have in mind is exporting all of the test methods in the test file. Then having these imported in the Tests.js file as an Object, and looping through the functions in this Object and calling them. This enables us to 'just call the function' without having to know any of their names. This should also help make debugging slightly easier as we'd get the exact method that failed right at the first layer of the stack trace.

ignacio-chiazzo commented 1 year ago

oh I see what you mean. Yeah, running all the functions in the test file would be fantastic. We can use existing frameworks for that.

hot9cups commented 1 year ago

Cool, I plan to do it with vanilla JavaScript instead of introducing a framework. Let me know if that works with you.

ignacio-chiazzo commented 1 year ago

Yes, that works for me

hot9cups commented 1 year ago

@ignacio-chiazzo Do you think we can close this issue now as we were able to achieve what we wanted?

Or do you have anything else in your mind that we could do to improve the test functionality?