Skip to content

Commit 8355b5a

Browse files
authored
Switch to yargs. (#189)
1 parent 26f213c commit 8355b5a

File tree

3 files changed

+26
-12
lines changed

3 files changed

+26
-12
lines changed

translate/system-test/translate.test.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ describe('translate:translate', function () {
2828
assert.ifError(err);
2929
assert(result, 'should have received a result');
3030
assert.equal(result.language, 'en', 'should have detected english');
31-
assert(console.log.calledWith('Detected %s with confidence %d', 'English', result.confidence));
31+
assert(console.log.calledWith('Detected %s (%s) with confidence %d', 'English', 'en', result.confidence));
3232
done();
3333
});
3434
});
@@ -58,7 +58,7 @@ describe('translate:translate', function () {
5858
program.translateText(options, function (err, translation) {
5959
assert.ifError(err);
6060
assert.equal(translation, expected);
61-
assert(console.log.calledWith('Translated text to %s', 'Russian'));
61+
assert(console.log.calledWith('Translated text to %s:', 'Russian'));
6262
done();
6363
});
6464
});

translate/test/translate.test.js

+13-5
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ describe('translate:translate', function () {
6565
assert.equal(callback.firstCall.args.length, 2, 'callback received 2 arguments');
6666
assert.ifError(callback.firstCall.args[0], 'callback did not receive error');
6767
assert.strictEqual(callback.firstCall.args[1], sample.mocks.result, 'callback received result');
68-
assert(console.log.calledWith('Detected %s with confidence %d', 'English', sample.mocks.result.confidence));
68+
assert(console.log.calledWith('Detected %s (%s) with confidence %d', 'English', 'en', sample.mocks.result.confidence));
6969
});
7070

7171
it('should handle error', function () {
@@ -137,7 +137,7 @@ describe('translate:translate', function () {
137137
assert.equal(callback.firstCall.args.length, 2, 'callback received 2 arguments');
138138
assert.ifError(callback.firstCall.args[0], 'callback did not receive error');
139139
assert.strictEqual(callback.firstCall.args[1], sample.mocks.translation, 'callback received result');
140-
assert(console.log.calledWith('Translated text to %s', 'Russian'));
140+
assert(console.log.calledWith('Translated text to %s:', 'Russian'));
141141
});
142142

143143
it('should handle error', function () {
@@ -166,23 +166,31 @@ describe('translate:translate', function () {
166166

167167
sinon.stub(program, 'detectLanguage');
168168
program.main(['detect', text, '-k', apiKey]);
169-
assert(program.detectLanguage.calledOnce);
169+
assert.equal(program.detectLanguage.calledOnce, true);
170+
assert.deepEqual(program.detectLanguage.firstCall.args.slice(0, -1), [text, apiKey]);
170171
});
171172

172173
it('should call listLanguages', function () {
173174
var program = getSample().program;
174175

175176
sinon.stub(program, 'listLanguages');
176177
program.main(['list', '-k', apiKey]);
177-
assert(program.listLanguages.calledOnce);
178+
assert.equal(program.listLanguages.calledOnce, true);
179+
assert.deepEqual(program.listLanguages.firstCall.args.slice(0, -1), [apiKey]);
178180
});
179181

180182
it('should call translateText', function () {
181183
var program = getSample().program;
182184

183185
sinon.stub(program, 'translateText');
184186
program.main(['translate', text, '-k', apiKey, '-t', 'ru']);
185-
assert(program.translateText.calledOnce);
187+
assert.equal(program.translateText.calledOnce, true);
188+
assert.deepEqual(program.translateText.firstCall.args.slice(0, -1), [{
189+
text: text,
190+
to: 'ru',
191+
from: undefined,
192+
apiKey: apiKey
193+
}]);
186194
});
187195
});
188196
});

translate/translate.js

+11-5
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,12 @@ function detectLanguage (text, apiKey, callback) {
4545
return callback(err);
4646
}
4747

48-
console.log('Detected %s with confidence %d', ISO6391.getName(result.language), result.confidence);
48+
console.log(
49+
'Detected %s (%s) with confidence %d',
50+
ISO6391.getName(result.language),
51+
result.language,
52+
result.confidence
53+
);
4954
return callback(null, result);
5055
});
5156
}
@@ -104,7 +109,7 @@ function translateText (options, callback) {
104109
return callback(err);
105110
}
106111

107-
console.log('Translated text to %s', ISO6391.getName(options.to));
112+
console.log('Translated text to %s:', ISO6391.getName(options.to));
108113
return callback(null, translation);
109114
});
110115
}
@@ -113,6 +118,7 @@ function translateText (options, callback) {
113118

114119
// The command-line program
115120
var cli = require('yargs');
121+
var utils = require('../utils');
116122

117123
var program = module.exports = {
118124
detectLanguage: detectLanguage,
@@ -127,10 +133,10 @@ var program = module.exports = {
127133
cli
128134
.demand(1)
129135
.command('detect <text>', 'Detect the language of the provided text', {}, function (options) {
130-
program.detectLanguage(options.text, options.apiKey, console.log);
136+
program.detectLanguage(options.text, options.apiKey, utils.makeHandler(false));
131137
})
132138
.command('list', 'List available translation languages.', {}, function (options) {
133-
program.listLanguages(options.apiKey, console.log);
139+
program.listLanguages(options.apiKey, utils.makeHandler());
134140
})
135141
.command('translate <text>', 'Translate the provided text to the target language.', {
136142
to: {
@@ -147,7 +153,7 @@ cli
147153
description: 'The language of the source text.'
148154
}
149155
}, function (options) {
150-
program.translateText(options, console.log);
156+
program.translateText(utils.pick(options, ['text', 'to', 'from', 'apiKey']), utils.makeHandler());
151157
})
152158
.option('apiKey', {
153159
alias: 'k',

0 commit comments

Comments
 (0)