Coder Social home page Coder Social logo

leetsolve's Introduction

Build Status Maintainability Node.js LeetCode

๐Ÿฐ simple & light testing frame for LeetCode solutions with JavaScript

Feature

optimized for the algorithms question pattern, test output for input, such as LeetCode

  • easy to describe a problem and testcase
  • lightweight testing frame which a bit like mocha, but more features which are mocha NOT support
  • using multiprocessing, speed up testcase
  • canable to stop timeout testing with infinite loop
  • direct to run a single problem solution for test
  • also support hooks like beforEach before after afterEach

Style preview

output reporters style adjust to like mocha spec, preview some example:

testing-output-style

Unit test

chone it, then run the unit test

git clone [email protected]:zthxxx/leetsolve.git
cd leetsolve
npm i
npm run unit

you will get the result as shown above, those examples problem in test/examples

Usage

replace your problems and solutions in problems/

to define a problem, as the same structure of unit test examples,

a problem is a folder which named its description, with two files in it, index.js as solution, testcase.js as test case.

problems/
โ”‚
โ””โ”€ problem1-description/  # define problem 1
   โ”‚
   โ”œโ”€ index.js            # solution file
   โ””โ”€ testcase.js         # problem testcase file

in index.js, you may want export one or more solutions, for example:

function solution1 () {}
function solution2 () {}
/**
 * @type {(function | function[])} - exports
 */
// export one solution
module.exports = solution1
// or export some more solutions
module.exports = [solution1, solution2]

it's means test the specified solution or test all.

solutions will gain input arguments with testcase input field, and compare its output to testcase expect field.

so, in testcase.js, should export list of input and expect:

/**
 * @type {{input: any[], expect: any}[]} - exports
 */
module.exports = [
  {
    input: ['a', 'b'],
    expect: 1
  }
]

see the example code that you will understand immediately, I promise.

hooks

lifecycle hooks

support to use beforeEach afterEach hooks on exports, if the exports is array,

and use before after hooks on each solutions.

hook of before deal with input arguments and output processed 'input args' for next hook,

oppositely, hook of after deal with output result, origin in, processed out.

the full resolution flow:

beforeEachs -> befores -> solution -> afters -> afterEachs
// register beforeEach or afterEachs hook
module.exports = [solution1, solution2]
/**
 * @type {(function | function[])} - beforeEach | afterEachs
 */
module.exports.beforeEach = input => input
// or
module.exports.beforeEach = [() => {}, () => {}]
// register before or after hook
module.exports = [solution1, solution2]
/**
 * @type {(function | function[])} - before | after
 */
solution1.before = () => {}
// or
solution2.after = [() => {}, () => {}]

timeout setting hook

support to set different timelimit to specific problem or solution. By ues timeout property as lifecycle hooks.

direct running hook

you may want to direct run a solution file for test as node ./problem-1/index.js or node ./problem-1

all right, only ensure index.js require this hook module libs/runDirect.js

// problems/problem-for-unit-test/index.js
require('../../libs/runDirect')

configure

you can also adjust problems path at problemBase and testcase file name at casefile in config.js

more options description see config.js

ok, come on, try out!

Author

LeetSolve ยฉ zthxxx, Released under the MIT License.

Blog @zthxxx ยท GitHub @zthxxx

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.