Skip to content

Commit f87bcaf

Browse files
committed
Added addStressMarks to RussianClient
1 parent d7093f6 commit f87bcaf

File tree

3 files changed

+146
-21
lines changed

3 files changed

+146
-21
lines changed

src/russian/client.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,23 @@ class Client {
123123
});
124124
}
125125

126+
addStressMarks(text = '') {
127+
const params = new Map();
128+
params.set(Communicator.CONTENT_BODY_KEY, text);
129+
130+
const path = this.prefix + '/addstressmarks';
131+
132+
return this.communicator.request(path, params, Communicator.METHOD_POST).
133+
then(response => response.json()).
134+
then(data => {
135+
if (data['message'] && data['code']) {
136+
throw new MorpherError(data['message'], data['code']);
137+
}
138+
139+
return data;
140+
});
141+
}
142+
126143
}
127144

128145
module.exports = Client;

test/e2e/russian/client.test.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,4 +198,25 @@ describe('E2E Russian client', function() {
198198

199199
});
200200

201+
describe('#addStressMarks()', function() {
202+
203+
it('should return valid response', async function() {
204+
const result = await morpher.russian.addStressMarks('Белки питаются белками');
205+
206+
assert.deepEqual(result, 'Бе́лки|Белки́ пита́ются бе́лками|белка́ми');
207+
208+
});
209+
210+
it('should throw MorpherError', async function() {
211+
212+
try {
213+
await morpher.russian.addStressMarks();
214+
} catch (error) {
215+
assert.instanceOf(error, MorpherError);
216+
}
217+
218+
});
219+
220+
});
221+
201222
});

test/unit/russian/client.test.js

Lines changed: 108 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,13 @@ const RussianClient = require('../../../src/russian/client');
66
const Morpher = require('../../../src/morpher');
77
const CommunicatorMock = require('../communicator-mock');
88
const spellResponseMock = require('../../reponse-mock/russian/spell');
9-
const spellOrdinalResponseMock = require('../../reponse-mock/russian/spell-ordinal');
9+
const spellOrdinalResponseMock = require(
10+
'../../reponse-mock/russian/spell-ordinal',
11+
);
1012
const spellDateResponseMock = require('../../reponse-mock/russian/spell-date');
11-
const adjectiveGendersResponseMock = require('../../reponse-mock/russian/adjective-genders');
13+
const adjectiveGendersResponseMock = require(
14+
'../../reponse-mock/russian/adjective-genders',
15+
);
1216
const MorpherError = require('../../../src/morpher-error');
1317

1418
describe('Russian client', function() {
@@ -186,22 +190,28 @@ describe('Russian client', function() {
186190

187191
const numberSpellingResult = await client.spell(235, 'рубль');
188192

189-
assert.equal(numberSpellingResult['n']['родительный'], response['n']['Р']);
193+
assert.equal(numberSpellingResult['n']['родительный'],
194+
response['n']['Р']);
190195
assert.equal(numberSpellingResult['n']['genitive'], response['n']['Р']);
191196

192197
assert.equal(numberSpellingResult['n']['дательный'], response['n']['Д']);
193198
assert.equal(numberSpellingResult['n']['dative'], response['n']['Д']);
194199

195-
assert.equal(numberSpellingResult['n']['винительный'], response['n']['В']);
200+
assert.equal(numberSpellingResult['n']['винительный'],
201+
response['n']['В']);
196202
assert.equal(numberSpellingResult['n']['accusative'], response['n']['В']);
197203

198-
assert.equal(numberSpellingResult['n']['творительный'], response['n']['Т']);
199-
assert.equal(numberSpellingResult['n']['instrumental'], response['n']['Т']);
204+
assert.equal(numberSpellingResult['n']['творительный'],
205+
response['n']['Т']);
206+
assert.equal(numberSpellingResult['n']['instrumental'],
207+
response['n']['Т']);
200208

201209
assert.equal(numberSpellingResult['n']['предложный'], response['n']['П']);
202-
assert.equal(numberSpellingResult['n']['prepositional'], response['n']['П']);
210+
assert.equal(numberSpellingResult['n']['prepositional'],
211+
response['n']['П']);
203212

204-
assert.equal(numberSpellingResult['n']['предложный_О'], response['n']['П_о']);
213+
assert.equal(numberSpellingResult['n']['предложный_О'],
214+
response['n']['П_о']);
205215
assert.equal(numberSpellingResult['n']['prepositional_O'],
206216
response['n']['П_о']);
207217

@@ -294,22 +304,28 @@ describe('Russian client', function() {
294304

295305
const numberSpellingResult = await client.spellOrdinal(5, 'колесо');
296306

297-
assert.equal(numberSpellingResult['n']['родительный'], response['n']['Р']);
307+
assert.equal(numberSpellingResult['n']['родительный'],
308+
response['n']['Р']);
298309
assert.equal(numberSpellingResult['n']['genitive'], response['n']['Р']);
299310

300311
assert.equal(numberSpellingResult['n']['дательный'], response['n']['Д']);
301312
assert.equal(numberSpellingResult['n']['dative'], response['n']['Д']);
302313

303-
assert.equal(numberSpellingResult['n']['винительный'], response['n']['В']);
314+
assert.equal(numberSpellingResult['n']['винительный'],
315+
response['n']['В']);
304316
assert.equal(numberSpellingResult['n']['accusative'], response['n']['В']);
305317

306-
assert.equal(numberSpellingResult['n']['творительный'], response['n']['Т']);
307-
assert.equal(numberSpellingResult['n']['instrumental'], response['n']['Т']);
318+
assert.equal(numberSpellingResult['n']['творительный'],
319+
response['n']['Т']);
320+
assert.equal(numberSpellingResult['n']['instrumental'],
321+
response['n']['Т']);
308322

309323
assert.equal(numberSpellingResult['n']['предложный'], response['n']['П']);
310-
assert.equal(numberSpellingResult['n']['prepositional'], response['n']['П']);
324+
assert.equal(numberSpellingResult['n']['prepositional'],
325+
response['n']['П']);
311326

312-
assert.equal(numberSpellingResult['n']['предложный_О'], response['n']['П_о']);
327+
assert.equal(numberSpellingResult['n']['предложный_О'],
328+
response['n']['П_о']);
313329
assert.equal(numberSpellingResult['n']['prepositional_O'],
314330
response['n']['П_о']);
315331

@@ -541,21 +557,92 @@ describe('Russian client', function() {
541557

542558
assert.equal(client.communicator.lastPath, '/russian/adjectivize');
543559
assert.equal(client.communicator.lastParams.get('s'), 'Ростов');
544-
assert.equal(client.communicator.lastHttpMethod,
545-
CommunicatorMock.METHOD_GET);
560+
assert.equal(
561+
client.communicator.lastHttpMethod,
562+
CommunicatorMock.METHOD_GET,
563+
);
564+
565+
});
566+
567+
it('should return an adjective formed from the name of a city or country',
568+
async function() {
569+
570+
const response = ['ростовский'];
571+
572+
communicatorMock.response = new Response(
573+
JSON.stringify(response),
574+
{status: 200},
575+
);
576+
577+
const result = await client.adjectivize('Ростов');
578+
579+
assert.deepEqual(result, response);
580+
581+
},
582+
);
583+
584+
it('should throw MorpherError', async function() {
585+
586+
const response = {
587+
'code': 6,
588+
'message': 'Не указан обязательный параметр: s.',
589+
};
590+
591+
communicatorMock.response = new Response(
592+
JSON.stringify(response),
593+
{status: 400},
594+
);
595+
596+
try {
597+
await client.adjectivize();
598+
} catch (err) {
599+
assert.instanceOf(err, MorpherError);
600+
}
601+
602+
});
603+
604+
});
605+
606+
describe('#addStressMarks()', async function() {
607+
608+
const communicatorMock = new CommunicatorMock();
609+
610+
const client = new RussianClient();
611+
client.communicator = communicatorMock;
612+
613+
it('should use the correct parameters', async function() {
614+
615+
communicatorMock.response = new Response(
616+
JSON.stringify('Бе́лки|Белки́ пита́ются бе́лками|белка́ми'),
617+
{status: 200},
618+
);
619+
620+
await client.addStressMarks('Белки питаются белками');
621+
622+
assert.equal(client.communicator.lastPath, '/russian/addstressmarks');
623+
624+
assert.equal(
625+
client.communicator.lastParams.get(CommunicatorMock.CONTENT_BODY_KEY),
626+
'Белки питаются белками',
627+
);
628+
629+
assert.equal(
630+
client.communicator.lastHttpMethod,
631+
CommunicatorMock.METHOD_POST,
632+
);
546633

547634
});
548635

549-
it('should return an adjective formed from the name of a city or country', async function() {
636+
it('should return the string with the added stresses', async function() {
550637

551-
const response = ['ростовский'];
638+
const response = 'Бе́лки|Белки́ пита́ются бе́лками|белка́ми';
552639

553640
communicatorMock.response = new Response(
554641
JSON.stringify(response),
555642
{status: 200},
556643
);
557644

558-
const result = await client.adjectivize('Ростов');
645+
const result = await client.addStressMarks('Белки питаются белками');
559646

560647
assert.deepEqual(result, response);
561648

@@ -565,7 +652,7 @@ describe('Russian client', function() {
565652

566653
const response = {
567654
'code': 6,
568-
'message': 'Не указан обязательный параметр: s.',
655+
'message': 'Текст должен передаваться в теле запроса.',
569656
};
570657

571658
communicatorMock.response = new Response(
@@ -574,7 +661,7 @@ describe('Russian client', function() {
574661
);
575662

576663
try {
577-
await client.adjectivize();
664+
await client.addStressMarks();
578665
} catch (err) {
579666
assert.instanceOf(err, MorpherError);
580667
}

0 commit comments

Comments
 (0)