Skip to content

Commit

Permalink
Added test for replacing blank lines, related to #74
Browse files Browse the repository at this point in the history
  • Loading branch information
lazd committed Sep 21, 2016
1 parent 65b5deb commit 72067c6
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 37 deletions.
3 changes: 3 additions & 0 deletions test/expected/blank.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
I have text, but the line above me doesn't
And
Neither do the two lines above me or the one below!
8 changes: 8 additions & 0 deletions test/fixtures/blank.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@


I have text, but the line above me doesn't

And


Neither do the two lines above me or the one below!
101 changes: 64 additions & 37 deletions test/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,28 @@ describe('gulp-replace', function() {
describe('replacePlugin()', function() {
var replacements;

var checkBuffered = function (file, stream, done, cb) {
stream.on('data', function (newFile) {
cb(newFile);
done();
});

stream.write(file);
stream.end();
};

var checkStreamed = function (file, stream, done, cb) {
stream.on('data', function(newFile) {
newFile.contents.pipe(concatStream({encoding: 'string'}, function(data) {
cb(data);
done();
}));
});

stream.write(file);
stream.end();
};

beforeEach(function () {
replacements = [
'cow',
Expand All @@ -20,62 +42,51 @@ describe('gulp-replace', function() {
});

describe('buffered input', function () {
var file, check;
var file;

beforeEach(function () {
file = new File({
path: 'test/fixtures/helloworld.txt',
contents: fs.readFileSync('test/fixtures/helloworld.txt')
});

check = function (stream, done, cb) {
stream.on('data', function (newFile) {
cb(newFile);
done();
});

stream.write(file);
stream.end();
};
});

it('should replace string on a buffer', function(done) {
var stream = replacePlugin('world', 'person');

check(stream, done, function (newFile) {
checkBuffered(file, stream, done, function (newFile) {
String(newFile.contents).should.equal(fs.readFileSync('test/expected/helloworld.txt', 'utf8'));
});
});

it('should replace regex on a buffer', function(done) {
var stream = replacePlugin(/world/g, 'person');

check(stream, done, function (newFile) {
checkBuffered(file, stream, done, function (newFile) {
String(newFile.contents).should.equal(fs.readFileSync('test/expected/helloworld.txt', 'utf8'));
});
});

it('should replace regex on a buffer with a function', function(done) {
var stream = replacePlugin(/world/g, function() { return 'person'; });

check(stream, done, function (newFile) {
checkBuffered(file, stream, done, function (newFile) {
String(newFile.contents).should.equal(fs.readFileSync('test/expected/helloworld.txt', 'utf8'));
});
});

it('should replace string on a buffer with a function', function(done) {
var stream = replacePlugin('world', function() { return 'person'; });

check(stream, done, function (newFile) {
checkBuffered(file, stream, done, function (newFile) {
String(newFile.contents).should.equal(fs.readFileSync('test/expected/helloworld.txt', 'utf8'));
});
});


it('should call function once for each replacement when replacing a string on a buffer', function(done) {
var stream = replacePlugin('world', function() { return replacements.shift(); });
check(stream, done, function (newFile) {

checkBuffered(file, stream, done, function (newFile) {
String(newFile.contents).should.equal(fs.readFileSync('test/expected/hellofarm.txt', 'utf8'));
});
});
Expand All @@ -84,7 +95,7 @@ describe('gulp-replace', function() {
it('should call function once for each replacement when replacing a regex on a buffer', function(done) {
var stream = replacePlugin(/world/g, function() { return replacements.shift(); });

check(stream, done, function (newFile) {
checkBuffered(file, stream, done, function (newFile) {
String(newFile.contents).should.equal(fs.readFileSync('test/expected/hellofarm.txt', 'utf8'));
});
});
Expand All @@ -102,70 +113,86 @@ describe('gulp-replace', function() {
});

describe('streamed input', function () {
var file, check;
var file;

beforeEach(function () {
file = new File({
path: 'test/fixtures/helloworld.txt',
contents: fs.createReadStream('test/fixtures/helloworld.txt')
});

check = function (stream, done, cb) {
stream.on('data', function(newFile) {
newFile.contents.pipe(concatStream({encoding: 'string'}, function(data) {
cb(data);
done();
}));
});

stream.write(file);
stream.end();
};
});

it('should replace string on a stream', function(done) {
var stream = replacePlugin('world', 'person');
check(stream, done, function (data) {
checkStreamed(file, stream, done, function (data) {
data.should.equal(fs.readFileSync('test/expected/helloworld.txt', 'utf8'));
});
});

it('should replace regex on a stream', function(done) {
var stream = replacePlugin(/world/g, 'person');
check(stream, done, function (data) {
checkStreamed(file, stream, done, function (data) {
data.should.equal(fs.readFileSync('test/expected/helloworld.txt', 'utf8'));
});
});

it('should replace regex on a stream with a function', function(done) {
var stream = replacePlugin(/world/g, function() { return 'person'; });
check(stream, done, function (data) {
checkStreamed(file, stream, done, function (data) {
data.should.equal(fs.readFileSync('test/expected/helloworld.txt', 'utf8'));
});
});

it('should replace string on a stream with a function', function(done) {
var stream = replacePlugin('world', function() { return 'person'; });
check(stream, done, function (data) {
checkStreamed(file, stream, done, function (data) {
data.should.equal(fs.readFileSync('test/expected/helloworld.txt', 'utf8'));
});
});

it('should call function once for each replacement when replacing a string on a stream', function(done) {
var stream = replacePlugin('world', function() { return replacements.shift(); });
check(stream, done, function (data) {
checkStreamed(file, stream, done, function (data) {
data.should.equal(fs.readFileSync('test/expected/hellofarm.txt', 'utf8'));
});
});

it('should call function once for each replacement when replacing a regex on a stream', function(done) {
var stream = replacePlugin(/world/g, function() { return replacements.shift(); });
check(stream, done, function (data) {
checkStreamed(file, stream, done, function (data) {
data.should.equal(fs.readFileSync('test/expected/hellofarm.txt', 'utf8'));
});
});
});

describe('special cases', function() {
it('should replace empty lines on a buffer', function(done) {
var stream = replacePlugin(/^\s*\n/gm, '\n');

var file = new File({
path: 'test/fixtures/blank.txt',
contents: fs.readFileSync('test/fixtures/blank.txt')
});

checkBuffered(file, stream, done, function (newFile) {
String(newFile.contents).should.equal(fs.readFileSync('test/expected/blank.txt', 'utf8'));
});
});

it('should replace empty lines on a stream', function(done) {
var stream = replacePlugin(/^\s*\n/gm, '');

var file = new File({
path: 'test/fixtures/blank.txt',
contents: fs.createReadStream('test/fixtures/blank.txt')
});

checkStreamed(file, stream, done, function (data) {
data.should.equal(fs.readFileSync('test/expected/blank.txt', 'utf8'));
});
});
});

describe('options', function () {
describe('skipBinary', function () {
var stream;
Expand Down

0 comments on commit 72067c6

Please sign in to comment.