mirror of https://github.com/lukechilds/node.git
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
78 lines
1.9 KiB
78 lines
1.9 KiB
13 years ago
|
var assert = require('assert');
|
||
|
var util = require('util');
|
||
|
var join = require('path').join;
|
||
|
var fs = require('fs');
|
||
|
var common = require('../common');
|
||
|
|
||
|
var repl = require('repl');
|
||
|
|
||
|
// A stream to push an array into a REPL
|
||
|
function ArrayStream() {
|
||
13 years ago
|
this.run = function(data) {
|
||
13 years ago
|
var self = this;
|
||
13 years ago
|
data.forEach(function(line) {
|
||
13 years ago
|
self.emit('data', line + '\n');
|
||
13 years ago
|
});
|
||
|
}
|
||
|
}
|
||
|
util.inherits(ArrayStream, require('stream').Stream);
|
||
|
ArrayStream.prototype.readable = true;
|
||
|
ArrayStream.prototype.writable = true;
|
||
13 years ago
|
ArrayStream.prototype.resume = function() {};
|
||
|
ArrayStream.prototype.write = function() {};
|
||
13 years ago
|
|
||
13 years ago
|
var works = [['inner.one'], 'inner.o'];
|
||
13 years ago
|
|
||
|
var putIn = new ArrayStream();
|
||
|
var testMe = repl.start('', putIn);
|
||
|
|
||
|
|
||
|
var testFile = [
|
||
|
'var top = function () {',
|
||
|
'var inner = {one:1};'
|
||
|
];
|
||
|
var saveFileName = join(common.tmpDir, 'test.save.js');
|
||
|
|
||
|
// input some data
|
||
|
putIn.run(testFile);
|
||
|
|
||
|
// save it to a file
|
||
|
putIn.run(['.save ' + saveFileName]);
|
||
|
|
||
|
// the file should have what I wrote
|
||
13 years ago
|
assert.equal(fs.readFileSync(saveFileName, 'utf8'), testFile.join('\n') + '\n');
|
||
13 years ago
|
|
||
|
// make sure that the REPL data is "correct"
|
||
|
// so when I load it back I know I'm good
|
||
13 years ago
|
testMe.complete('inner.o', function(error, data) {
|
||
13 years ago
|
assert.deepEqual(data, works);
|
||
|
});
|
||
|
|
||
|
// clear the REPL
|
||
|
putIn.run(['.clear']);
|
||
|
|
||
|
// Load the file back in
|
||
|
putIn.run(['.load ' + saveFileName]);
|
||
|
|
||
|
// make sure that the REPL data is "correct"
|
||
13 years ago
|
testMe.complete('inner.o', function(error, data) {
|
||
13 years ago
|
assert.deepEqual(data, works);
|
||
|
});
|
||
|
|
||
|
// clear the REPL
|
||
|
putIn.run(['.clear']);
|
||
|
|
||
|
var loadFile = join(common.tmpDir, 'file.does.not.exist');
|
||
|
|
||
12 years ago
|
// should not break
|
||
13 years ago
|
putIn.write = function(data) {
|
||
|
// make sure I get a failed to load message and not some crazy error
|
||
13 years ago
|
assert.equal(data, 'Failed to load:' + loadFile + '\n');
|
||
13 years ago
|
// eat me to avoid work
|
||
|
putIn.write = function() {};
|
||
|
};
|
||
13 years ago
|
putIn.run(['.load ' + loadFile]);
|
||
13 years ago
|
|
||
|
|
||
|
//TODO how do I do a failed .save test?
|