Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGES_NEXT_RELEASE
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
- Add variables for the intepreter (#10).
57 changes: 50 additions & 7 deletions lib/command-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -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': {
Expand All @@ -50,6 +60,7 @@ var readline = require('readline'),
handler: function() {}
}
},
variables = {},
rl;

/**
Expand Down Expand Up @@ -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.
Expand All @@ -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 {
Expand Down Expand Up @@ -278,6 +320,7 @@ function initialize(commands, promptString) {
prompt();
}
});

if (process.argv.length === 3) {
var lines = fs.readFileSync(process.argv[2], 'utf8').split('\n');

Expand Down
26 changes: 24 additions & 2 deletions test/unit/command-node_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -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();
});
});
});