fd-gulp-encodingfilter
Version:
filt the assigned encoding file
176 lines (132 loc) • 4.46 kB
JavaScript
;
var encodingFilter = require('../');
var should = require('should');
var File = require('gulp-util').File;
var Buffer = require('buffer').Buffer;
var fs = require('fs');
var Readable = require('stream').Readable;
var Path = require('path');
require('mocha');
describe('fd-gulp-encodingFilter', function() {
describe('encodingFilter()', function() {
it('should emit error if encoding param is empty', function(done) {
try {
encodingFilter();
done();
} catch(err) {
err.message.should.equal('encoding is empty');
done();
}
});
it('should emit error for stream files', function(done) {
var streamFile = {
isNull: function () { return false; },
isStream: function () { return true; }
};
var stream = encodingFilter('utf-8');
stream.on('error', function (err) {
err.message.should.equal('Streaming not supported');
done();
});
stream.write(streamFile);
});
it('should throw error if you except utf-8 but the file\'s encoding is not', function(done) {
var testDirectory = './source/qingguo';
var assets = Path.join(testDirectory, 'js/amdtest');
var stream = encodingFilter('utf-8');
var fileAdata = fs.readFileSync(Path.join(assets, 'hello_world.js'));
var fileA = new File({
path: Path.join(assets, 'hello_world.js'),
contents: fileAdata
});
stream.on('end', function() {
done();
});
try {
stream.write(fileA);
stream.end();
done();
}catch(err) {
err.message.should.startWith('STYLE_BUILD_ERR the file encoding should utf-8');
stream.end();
done();
}
});
it('should throw error if you except gb2312 but the file\'s encoding is not', function(done) {
var testDirectory = './source/style-m-lofty';
var assets = Path.join(testDirectory, 'ui/button/1.0');
var stream = encodingFilter('gb2312');
var fileAdata = fs.readFileSync(Path.join(assets, 'button.less'));
var fileA = new File({
path: Path.join(assets, 'button.less'),
contents: fileAdata
});
stream.on('end', function() {
done();
});
try {
stream.write(fileA);
stream.end();
done();
}catch(err) {
err.message.should.startWith('STYLE_BUILD_ERR the file encoding should gb2312');
stream.end();
done();
}
});
it('should pass if you except utf-8 and the file\'s encoding is utf-8', function(done) {
var testDirectory = './source/style-m-lofty';
var assets = Path.join(testDirectory, 'ui/button/1.0');
var stream = encodingFilter('utf-8');
var fileAdata = fs.readFileSync(Path.join(assets, 'button.less'));
var fileA = new File({
path: Path.join(assets, 'button.less'),
contents: fileAdata
});
stream.on('end', function() {
stream.end();
});
stream.write(fileA);
try {
stream.write(fileA);
stream.end();
done();
}catch(err) {
err.message.should.equal('');
stream.end();
done();
}
});
it('should pass if you except gb2312 and the file\'s encoding is gb2312', function(done) {
var testDirectory = './source/qingguo';
var assets = Path.join(testDirectory, 'js/app/gongsi/module/filter');
var stream = encodingFilter('gb2312');
var fileAdata = fs.readFileSync(Path.join(assets, 'area.js'));
var fileA = new File({
path: Path.join(assets, 'area.js'),
contents: fileAdata
});
stream.on('end', function() {
stream.end();
});
stream.write(fileA);
try {
stream.write(fileA);
stream.end();
done();
}catch(err) {
err.message.should.equal('');
stream.end();
done();
}
});
});
});
function stringStream() {
var stream = new Readable();
stream._read = function() {
this.push('terin');
this.push(null);
};
return stream;
}