UNPKG

showandtell

Version:

A Javascript library providing debugger-like command-line interactivity for program state inspection and modification

114 lines (101 loc) 3.65 kB
'use strict' /* globals describe, it */ const expect = require('chai').expect const command = require('../../lib/command') describe('command.Command', () => { describe('the execute method', () => { it('invokes a state transition function with arguments parsed from an input string', (done) => { const cmd = new command.Command({ name: 'TEST', help: 'TEST <argument1: int> <argument2: int>', args: [ {name: 'argument1', parser: parseInt}, {name: 'argument2', parser: parseInt} ], func: function (state, args, next) { expect(state).to.have.property('testValue').and.equal(3) expect(args).to.have.property('argument1').and.equal(10) expect(args).to.have.property('argument2').and.equal(12) // Set testValue to a1 * a2 + t which is 10 * 12 + 3 = 123 next(null, {testValue: args.argument1 * args.argument2 + state.testValue}) } }) cmd.execute({testValue: 3}, '10 12', (err, newState) => { expect(err).to.be.null expect(newState).to.have.property('testValue').and.equal(123) done() }) }) it('uses default values for parameters for which no value is found', (done) => { const cmd = new command.Command({ name: 'TEST', help: 'TEST <argument1: int> <argument2: int>', args: [ {name: 'argument1', parser: parseInt, default: 10}, {name: 'argument2', parser: parseInt, default: 12} ], func: function (state, args, next) { expect(args).to.have.property('argument1').and.equal(130) expect(args).to.have.property('argument2').and.equal(12) next(null, {}) } }) cmd.execute({}, '130', (err, newState) => { expect(err).to.be.null done() }) }) }) describe('the help method', () => { it('produces a string describing the command and its arguments', () => { const cmd = new command.Command({ name: 'TEST', help: 'TEST <arg1> <arg2> <arg3>', args: [ {name: 'argument1'}, {name: 'argument2', default: 42}, {name: 'argument3', help: 'third argument'} ], func: function () {} // Not going to get called }) const help = cmd.help() expect(help).to.equal(`TEST <arg1> <arg2> <arg3> argument1 argument2 - \tdefault: 42 argument3 - \tthird argument`) }) }) }) describe('command.commands.set', () => { it('is a command that allows an arbitrary state value to be set', (done) => { command.commands.set.execute( {test: {values: {number: 32}}}, 'test.values.number 10', function (err, newState) { expect(err).to.be.null expect(newState).to.have.deep.property('test.values.number').and.equal('10') done() }) }) it('nests objects to establish a path to the variable where one does not exist', (done) => { command.commands.set.execute({}, 'test.values.number 32', (err, newState) => { expect(err).to.be.null expect(newState).to.have.deep.property('test.values.number').and.equal('32') done() }) }) }) describe('command.commands.show', () => { it('logs a value at an arbitrarily nested depth', (done) => { command.commands.show.execute({test: {values: {number: 32}}}, 'test.values.number', (err, s) => { expect(err).to.be.null done() }) }) it('produces an error if the variable does not exist', (done) => { command.commands.show.execute({test: 32}, 'nottest', (err, s) => { expect(err).to.not.be.null done() }) }) })