Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to pass multiple arguments to solutions? #32

Open
NickHeiner opened this issue Dec 23, 2013 · 3 comments
Open

How to pass multiple arguments to solutions? #32

NickHeiner opened this issue Dec 23, 2013 · 3 comments

Comments

@NickHeiner
Copy link

I see the from the examples that you can pass a single array of arguments to the solutions to verify them:

setup.js

function rndint () {
  return Math.ceil(Math.random() * 100)
}

module.exports = function () {
  var args = [ rndint(), rndint() ]
  while (Math.random() > 0.3)
    args.push(rndint())

  return { args: args, stdin: null }
}

Is it possible to pass multiple sets of arguments, so you can have multiple test cases? I looked around in the code and other examples but didn't find anything conclusive.

@rvagg
Copy link
Contributor

rvagg commented Dec 23, 2013

yeah, not so easy with the current API given that it's focused on a single exec. If you can make your solution about making a "module" rather than a "program" then it's easier to call it to verify in different ways. Check out levelmeup for lots of examples of this kind; once you have the module you can do what you want with it and call it as many times as you like and verification can involve printing out your test output to stdout.

@NickHeiner
Copy link
Author

Interesting. So you're using process.argv to find the solution file, require it yourself, and make assertions about it? That seems like coupling to an implementation detail of workshopper. Is there a higher level API available?

const
// ...
    , file     = process.argv[3]
    , solution = require(path.resolve(process.cwd(), file))

@NickHeiner
Copy link
Author

This seems to work:

setup.js

module.exports = function() {

    var file     = process.argv[3],
        solution = require('./solution'),
        submission = require(path.resolve(process.cwd(), file)),
        args = [1, 4, 6],

        streamA = new readable(),
        streamB = new readable();

  streamA.push(JSON.stringify(solution(args)));
  streamA.push(null);

  streamB.push(JSON.stringify(submission(args)));
  streamB.push(null);

  return {
    a: streamA,
    b: streamB
  };
}

It's a little clunkier than I was hoping - am I doing something wrong? Would you accept a PR to make this cleaner? I think ideally it would look something like:

module.exports = function(run, callback, solution, submission) {

  var args = [1, 4, 6];

  return {
    a: solution(args),
    b: submission(args)
  };
};

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants