Skip to content

Commit

Permalink
Reduce test code duplication (#31)
Browse files Browse the repository at this point in the history
  • Loading branch information
Alvin Smith authored and sindresorhus committed Nov 29, 2018
1 parent bcd9a70 commit 30c5652
Showing 1 changed file with 17 additions and 55 deletions.
72 changes: 17 additions & 55 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@ const yosay = require('../');

const getFixturePath = testName => path.join(__dirname, 'fixture', `${testName}.json`);

const getAssertResult = (testName, expected, done) => fs.readFile(getFixturePath(testName), (err, data) => {
assert.ifError(err);
assert.equal(JSON.parse(data), expected);
done();
});

console.log(yosay(chalk.red('WHAT DOES THE YO SAY??? ') + chalk.yellow('\'ALLO \'ALLO')));

describe('yosay', () => {
Expand All @@ -31,122 +37,78 @@ describe('yosay', () => {
const testName = 'correctly-formatted';
const expected = yosay('Hi');

fs.readFile(getFixturePath(testName), (err, data) => {
assert.ifError(err);
assert.equal(JSON.parse(data), expected);
done();
});
getAssertResult(testName, expected, done);
});

it('should return correctly formatted string in two lines', done => {
const testName = 'correctly-formatted-two-lines';
const expected = yosay('Welcome to Yeoman, ladies and gentlemen!');

fs.readFile(getFixturePath(testName), (err, data) => {
assert.ifError(err);
assert.equal(JSON.parse(data), expected);
done();
});
getAssertResult(testName, expected, done);
});

it('should allow customization of line length', done => {
const testName = 'length-customization';
const expected = yosay('Hi', {maxLength: 8});

fs.readFile(getFixturePath(testName), (err, data) => {
assert.ifError(err);
assert.equal(JSON.parse(data), expected);
done();
});
getAssertResult(testName, expected, done);
});

it('should override a maxLength setting that is too short', done => {
const testName = 'override-maxLength';
const expected = yosay('Hello, buddy!', {maxLength: 4});

fs.readFile(getFixturePath(testName), (err, data) => {
assert.ifError(err);
assert.equal(JSON.parse(data), expected);
done();
});
getAssertResult(testName, expected, done);
});

describe('ansi', () => {
it('should display ansi styling correctly', done => {
const testName = 'ansi';
const expected = yosay(chalk.red.bgWhite('Hi'));

fs.readFile(getFixturePath(testName), (err, data) => {
assert.ifError(err);
assert.equal(JSON.parse(data), expected);
done();
});
getAssertResult(testName, expected, done);
});

it('should handle part ansi and part not-ansi', done => {
const testName = 'half-ansi';
const expected = yosay(chalk.red.bgWhite('Hi') + ' there, sir!');

fs.readFile(getFixturePath(testName), (err, data) => {
assert.ifError(err);
assert.equal(JSON.parse(data), expected);
done();
});
getAssertResult(testName, expected, done);
});

it('should wrap ansi styling to the next line properly', done => {
const testName = 'wrap-ansi-styles';
const expected = yosay(chalk.red.bgWhite('Hi') + ' there, sir! ' + chalk.bgBlue.white('you are looking') + ' swell today!');

fs.readFile(getFixturePath(testName), (err, data) => {
assert.ifError(err);
assert.equal(JSON.parse(data), expected);
done();
});
getAssertResult(testName, expected, done);
});

it('should handle new line properly', done => {
const testName = 'handle-new-line';
const expected = yosay('first line\nsecond line\n\nfourth line');

fs.readFile(getFixturePath(testName), (err, data) => {
assert.ifError(err);
assert.equal(JSON.parse(data), expected);
done();
});
getAssertResult(testName, expected, done);
});

it('should handle fullwidth characters', done => {
const testName = 'handle-fullwidth';
const expected = yosay('项目可以更新了');

fs.readFile(getFixturePath(testName), (err, data) => {
assert.ifError(err);
assert.equal(JSON.parse(data), expected);
done();
});
getAssertResult(testName, expected, done);
});

it('should display long words correctly', done => {
const testName = 'long-words';
const expected = yosay('iloveunicornsiloveunicornsiloveunicornsiloveunicornsiloveunicornsiloveunicorns');

fs.readFile(getFixturePath(testName), (err, data) => {
assert.ifError(err);
assert.equal(JSON.parse(data), expected);
done();
});
getAssertResult(testName, expected, done);
});

it('should overflow when lines exceed the default greeting', done => {
const testName = 'overflow';
const expected = yosay('Lie on your belly and purr when you are asleep shove bum in owner’s face like camera lens. Cough furball.', {maxLength: 11});

fs.readFile(getFixturePath(testName), (err, data) => {
assert.ifError(err);
assert.equal(JSON.parse(data), expected);
done();
});
getAssertResult(testName, expected, done);
});
});
});

0 comments on commit 30c5652

Please sign in to comment.