aboutsummaryrefslogtreecommitdiff
path: root/test/js
blob: c29673285d20437b1b588db65558dd90e4ee52dd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#! /usr/bin/env node

let bqn = require(__dirname+'/../docs/bqn.js');
let read = require('fs').readFileSync;
let cases = f=>read(__dirname+'/cases/'+f+'.bqn','utf8').split('\n')
               .filter(x=>x.length>0 && x.charAt(0)!=='#')
               .map(x=>x.indexOf('%')===-1?[1,x]:x.split(' % '));
let args = process.argv.slice(2);
let onfiles = args[0]==='--';
let files = onfiles ? args.slice(1) : ['simple','syntax'];

if (onfiles || args.length==0) {
  let t = [].concat.apply([],files.map(cases));
  let test = t.map(e=>e[1]);
  let expt = t.map(e=>e[0]==='!'?null:+e[0]);

  let rslt = test.map(t => {try{return bqn(t);}catch(e){return null;}});
  let pass = rslt.map((r,i)=>r===expt[i]);
  let fail = pass.map((p,i)=>p?-1:i).filter(i=>i>=0);
  console.log(
      fail.length
      ? fail.map(i=>'"'+test[i]+'": expected '
                   +(e=>e===null?'to fail':e)(expt[i])
                   +' but '
                   +(r=>r===null?'evaluation failed':'received '+r)(rslt[i]))
      : "All "+test.length+" passed!"
  );
} else {
  args.map(a=>{
    try {
      console.log(bqn.fmt(bqn(a)))
    } catch(e) {
      console.error(''+bqn.fmtErr(Array.from(a),e)+'');
    }
  });
}