test.js now accepts args correctly + minor tweaking...

Signed-off-by: Alex A. Naanou <alex.nanou@gmail.com>
This commit is contained in:
Alex A. Naanou 2020-06-02 15:56:03 +03:00
parent 4bb18a769f
commit 507fa5f80f
2 changed files with 80 additions and 62 deletions

View File

@ -25,8 +25,8 @@
var TAB_SIZE =
module.TAB_SIZE = 4
var KEEP_TABS =
module.KEEP_TABS = 1
var KEEP_INDENT =
module.KEEP_INDENT = 1
// Normalize code indent...
@ -42,8 +42,8 @@ module.KEEP_TABS = 1
// This will ignore the indent of the first line.
//
// If the last line is indented higher or equal to the rest of the text
// we will user keep_tabs (defaults to KEEP_TABS) to indent the rest of
// the text.
// we will user keep_indent (defaults to KEEP_INDENT) to indent the rest
// of the text.
// This will indent the following styles correctnly:
//
// |function(a, b){ |function(a, b){
@ -56,13 +56,13 @@ module.KEEP_TABS = 1
// ...when moving take care that ImageGrid's core.doc uses this...
var normalizeIndent =
module.normalizeIndent =
function(text, tab_size, keep_tabs){
function(text, tab_size, keep_indent){
tab_size = tab_size == null ?
TAB_SIZE
: tab_size
keep_tabs = (keep_tabs == null ?
KEEP_TABS
: keep_tabs)
keep_indent = (keep_indent == null ?
KEEP_INDENT
: keep_indent)
* tab_size
// prepare text...
var tab = ' '.repeat(tab_size)
@ -78,9 +78,9 @@ function(text, tab_size, keep_tabs){
// ignore 0 indent of first line...
|| (i == 0 && indent == 0) ?
l
// last line -- ignore keep_tabs if lower indent...
// last line -- ignore keep_indent if lower indent...
: i == lines.length-1 && indent > l ?
Math.max(l-keep_tabs, 0)
Math.max(l-keep_indent, 0)
// initial state...
: l < 0 ?
indent
@ -99,8 +99,8 @@ function(text, tab_size, keep_tabs){
// shorthand more suted for text...
var normalizeTextIndent =
module.normalizeTextIndent =
function(text, tab_size, keep_tabs){
return module.normalizeIndent(text, tab_size, keep_tabs || 0) }
function(text, tab_size, keep_indent){
return module.normalizeIndent(text, tab_size, keep_indent || 0) }
// Match two objects...

76
test.js
View File

@ -107,7 +107,7 @@ module.setups = {
var X, Y, A, B, C
return {
X: X = assert(object.Constructor('A'), `Constructor`),
Y: Y = assert(object.C('Y', { }), ` C`),
Y: Y = assert(object.C('Y', { }), `C`),
A: A = assert(object.C('A', Y, { }), `inherit (gen1)`),
B: B = assert(object.C('B', A, { }), `inherit (gen2)`),
@ -356,8 +356,10 @@ module.cases = {
// Test runner...
//
// runner()
// runner('*')
// -> stats
//
// runner('case')
// runner('setup:test')
// runner('setup:mod:test')
// -> stats
@ -375,9 +377,11 @@ module.cases = {
// NOTE: chaining more than one modifier is not yet supported (XXX)
var runner =
module.runner =
function(chain){
function(chain, stats){
// parse chain...
chain = chain || []
chain = (chain == '*' || chain == null) ?
[]
: chain
chain = chain instanceof Array ?
chain
: chain.split(/:/)
@ -385,16 +389,17 @@ function(chain){
var setup = chain.shift() || '*'
var test = chain.pop() || '*'
var mod = chain.pop() || '*'
// XXX add case support...
// prep...
// stats...
stats = stats || {}
Object.assign(stats, {
tests: stats.tests || 0,
assertions: stats.assertions || 0,
failures: stats.failures || 0,
time: stats.time || 0,
})
var started = Date.now()
var stats = {
tests: 0,
assertions: 0,
failures: 0,
time: 0,
}
// tests...
chain_length != 1
&& Object.keys(tests)
@ -416,7 +421,7 @@ function(chain){
// run the test...
stats.tests += 1
// XXX revise order...
var _assert = makeAssert(`test:${s}:${m}:${t}`, stats)
var _assert = makeAssert(`test: ${s}:${m}:${t}`, stats)
tests[t](_assert,
modifiers[m](_assert,
setups[s](_assert))) }) }) })
@ -427,14 +432,9 @@ function(chain){
return setup == '*' || setup == s })
.forEach(function(c){
stats.tests += 1
cases[c]( makeAssert(`case:${c}`, stats) ) })
cases[c]( makeAssert(`case: ${c}`, stats) ) })
// runtime...
stats.time = Date.now() - started
// stats...
console.log('Tests run:', stats.tests,
'Assertions:', stats.assertions,
'Failures:', stats.failures,
` (${stats.time}ms)`)
stats.time += Date.now() - started
return stats }
@ -453,20 +453,27 @@ if(typeof(__filename) != 'undefined'
// parse args...
var args = process.argv.slice(2)
var arg
var chains = []
while(args.length > 0){
var arg = args.shift()
arg = args.shift()
// options...
if(/^--?[a-zA-Z-]*/.test(arg)){
arg = arg.replace(/^--?/, '')
// verbose...
if(arg == '-v' || arg == '--verbose'){
if(arg == 'v' || arg == 'verbose'){
module.VERBOSE=true
// help...
// XXX format the lists better... word-wrap??
} else if(arg == '-h' || arg == '--help'){
} else if(arg == 'h' || arg == 'help'){
console.log(object.normalizeTextIndent(
`Usage: ${ process.argv[1].split(/[\\\/]/).pop() } [OPTIONS] [CHAIN]
`Usage: ${ process.argv[1].split(/[\\\/]/).pop() } [OPTIONS] [CHAIN] ...
Chain format:
<case>
<setup>:<test>
<setup>:<modifier>:<test>
@ -487,15 +494,26 @@ if(typeof(__filename) != 'undefined'
-v | --verbose verbose mode
`))
process.exit()
} }
process.exit() }
continue }
// collect chains...
chains.push(arg) }
// run the tests...
var stats = module.stats =
arg ?
runner(arg)
: runner()
var stats = {}
chains.length > 0 ?
chains
.forEach(function(chain){
runner(chain, stats) })
: runner('*', stats)
// print stats...
console.log('Tests run:', stats.tests,
'Assertions:', stats.assertions,
'Failures:', stats.failures,
` (${stats.time}ms)`)
// report error status to the OS...
process.exit(stats.failures)