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

118
test.js
View File

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