From b10c436985a638350a10122e8996ce413c3be144 Mon Sep 17 00:00:00 2001 From: Daniel Moran Date: Fri, 22 Jul 2016 16:54:58 +0200 Subject: [PATCH] ADD Variables for the command interpreters --- CHANGES_NEXT_RELEASE | 1 + lib/command-node.js | 57 +++++++++++++++++++++++++++++----- test/unit/command-node_test.js | 26 ++++++++++++++-- 3 files changed, 75 insertions(+), 9 deletions(-) diff --git a/CHANGES_NEXT_RELEASE b/CHANGES_NEXT_RELEASE index e69de29..da8343b 100644 --- a/CHANGES_NEXT_RELEASE +++ b/CHANGES_NEXT_RELEASE @@ -0,0 +1 @@ +- Add variables for the intepreter (#10). diff --git a/lib/command-node.js b/lib/command-node.js index 9ff5ced..bb1bebf 100644 --- a/lib/command-node.js +++ b/lib/command-node.js @@ -38,10 +38,20 @@ var readline = require('readline'), 'stressCommit': { parameters: ['delay', 'times', 'threads', 'initTime'], description: '\tExecutes the recorded batch as many times as requested, with delay (ms) ' + - 'between commands. \n' + - '\tThe "threads" parameter indicates how many agents will repeat that same sequence. ' + - 'The "initTime" (ms)\n' + - '\tparameter indicates the mean of the random initial waiting times for each agent.', + 'between commands. \n' + + '\tThe "threads" parameter indicates how many agents will repeat that same sequence. ' + + 'The "initTime" (ms)\n' + + '\tparameter indicates the mean of the random initial waiting times for each agent.', + handler: function() {} + }, + 'set': { + parameters: ['name', 'value'], + description: '\tSets a new variable or update the contents of an existing one.', + handler: function() {} + }, + 'vars': { + parameters: [], + description: '\tList all the variables currently set.', handler: function() {} }, 'exit': { @@ -50,6 +60,7 @@ var readline = require('readline'), handler: function() {} } }, + variables = {}, rl; /** @@ -196,6 +207,33 @@ function doExit() { toExit = true; } } + +function executeSet(commands) { + variables[commands[1]] = commands[2]; +} + +function executeVars() { + writer.log('Variable list:\n'); + + for (var i in variables) { + if (variables.hasOwnProperty(i)) { + writer.log('- %s=%s\n', i, variables[i]); + } + } +} + +function applyVariables(command) { + for (var i in command) { + for (var j in variables) { + if (variables.hasOwnProperty(j)) { + command[i] = command[i].replace('@' + j, variables[j]); + } + } + } + + return command; +} + /** * Executes the given command (the list of words parsed from the command line input) in the context of the commands * describe by the object Commands. @@ -208,14 +246,18 @@ function executeCommander(command, commands) { showHelp(commands); } else if (command[0] === 'exit') { doExit(); + } else if (command[0] === 'set') { + executeSet(command); + } else if (command[0] === 'vars') { + executeVars(); } else if (command[0] === 'stressInit') { stressInit(); } else if (command[0] === 'stressCommit') { - stressCommit(command, commands); + stressCommit(applyVariables(command), commands); } else if (stressTest) { - stressAdd(command); + stressAdd(applyVariables(command)); } else if (commands[command[0]]) { - execute(command, commands); + execute(applyVariables(command), commands); } else if (command[0] === '') { writer.log('\n'); } else { @@ -278,6 +320,7 @@ function initialize(commands, promptString) { prompt(); } }); + if (process.argv.length === 3) { var lines = fs.readFileSync(process.argv[2], 'utf8').split('\n'); diff --git a/test/unit/command-node_test.js b/test/unit/command-node_test.js index 652d90e..549d611 100644 --- a/test/unit/command-node_test.js +++ b/test/unit/command-node_test.js @@ -57,9 +57,11 @@ describe('Command-line tool', function() { var writer = new StringWriter(); beforeEach(function() { - commandNode.initialize(commands, 'Test>', process.stdin, process.stdout); + commandNode.initialize(commands, 'Test>'); commandNode.setWriter(writer); - commands.create.handler = function() {}; + commands.create.handler = function(command) { + commandNode.getWriter().log('Creating %s with %s', command[0], command[1]); + }; }); afterEach(function() { @@ -161,4 +163,24 @@ describe('Command-line tool', function() { }, 800); }); }); + describe('When two variables are set and the variable list is listed', function() { + it('should show both variables with their values', function(done) { + process.stdin.push('set testVar1 18\n'); + process.stdin.push('set testVar2 6324\n'); + writer.reset(); + process.stdin.push('vars\n'); + writer.get().should.match(/testVar1=18[.\s\S]*testVar2=6324/); + done(); + }); + }); + describe('When a command is executed with a variable name "@testVar1"', function() { + it('should replace the variable names with their values', function(done) { + process.stdin.push('set testVar1 18\n'); + process.stdin.push('set testVar2 6324\n'); + writer.reset(); + process.stdin.push('create @testVar1 @testVar2\n'); + writer.get().should.match(/Creating.*18.*6324/); + done(); + }); + }); });