Skip to content

Tool to test javascript scripts

jonathan poelen requested to merge work/jpoelen/js-framework into master

I'm proposing a utility for complete testing of JS commands and APIs. The aim is that it should also be usable by Kate users who want to add their own commands.

By complete test, I mean tests on the document text, the position of cursors and selections (primary and secondary) and the block selection mode. All in one line of code, because otherwise it's too complicated 😄.

What I've done isn't complete (no selection by block, no multi-cursors), but it works pretty well. To test it, you'll have to change the hard path of the js file in main, which will be embedded with the executable. But for the moment, it saves me compilation time when I change something. The code is rough and not much commented.

It currently looks like this:

$ ./bin/kate-script-tester 'testCase("a test", (c) => {
  c.cmd("view.executeCommand(\"duplicateLinesDown\")", "abc", "abc\nabcc");
  c.cmd([view.executeCommand, "duplicateLinesDown"], "a[b]c", "abc\nab|c", {result: {status:"ok",ok:true}});
  c.cmd("throw Error(\"babar\")", "a|[b]c", "abc\nabc", {result: {status:"",ok:true}});
  //c.cmd(()=>{}, "abc", "abc\nabc", {result: {status:"",ok:true}});
})'

With

cmd(command: String | Array[func, params...],
    input: String,
    expectedOutput: String,
    msgOrOptions: undefined | String | {[message: String, result: Any, error: String | bool]})

(error will be changed with exception). The | and [/] characters represent the cursor and selection (configurable).

which displays:

kate-script-tester

The colors will be configurable, by default they are basic ANSI codes which in my case give:

  • blue: file name (as currently the content is passed on the command line, I have myfile.js)
  • purple: line number
  • red: error
  • green: command or javascript code (here are js function calls, but cmd should be Kate's command-line equivalent)
  • gray: an error on the function result, but not on the input/output text
  • orange: cursors and selections start/end (bold for primary)
  • underline: the selection
  • black background: to see spaces in inputs/outputs
  • cyan: special characters (tab and new line) written in literal form. Much easier to spot differences when everything is on the same line. There are 5 formats, but no option to change it yet...
  • white background (more precisely, a color inversion): start of the difference in results.

The interface will be fairly minimalist, with just a few functions:

.cmd(command, input, expectedOutput, msgOrOptions)
.xcmd(command, input, currentOutput, expectedOutput, currentOptions, msgOrOptions) // for failed tests

.eq(command, input, expected, msg: String | undefined) // for strict equality tests on js function returns
.xeq .ne .lt etc .typeOf .any .all .none // (for simple tests on sequences)
.exception(command, input, error) // test on exceptions

// + some display-related functions

.setConfig(Object) // tabWidth, replaceTabs, syntax, etc

.testCase(name, func: (ctx) -> void) // what's on the picture
.withInput(name, input, func: (ctx) -> void) // like testCase, but ctx.cmd, eq, etc. no longer have an `input` parameter.
.sequence(name, input, func: (ctx) -> void) // same as withInput, but next input is previous expected value

It'll be a while before I finish, but I'd appreciate any feedback. For information, this has already helped detect a bug in view.executeCommand that always returned undefined (see first commit).

Merge request reports

Loading