spec/requests/translate_spec.rb in deepl-rb-2.5.3 vs spec/requests/translate_spec.rb in deepl-rb-3.0.0
- old
+ new
@@ -1,262 +1,256 @@
+# Copyright 2018 Daniel Herzog
+# Use of this source code is governed by an MIT
+# license that can be found in the LICENSE.md file.
# frozen_string_literal: true
require 'spec_helper'
describe DeepL::Requests::Translate do
+ subject(:translate) { described_class.new(api, text, source_lang, target_lang, options) }
+
+ around do |tests|
+ tmp_env = replace_env_preserving_deepl_vars_except_mock_server
+ tests.call
+ ENV.replace(tmp_env)
+ end
+
let(:tags_str) { 'p,strong,span' }
let(:tags_array) { %w[p strong span] }
let(:api) { build_deepl_api }
let(:text) { 'Sample text' }
let(:source_lang) { 'EN' }
let(:target_lang) { 'ES' }
let(:options) { {} }
- subject { DeepL::Requests::Translate.new(api, text, source_lang, target_lang, options) }
-
describe '#initialize' do
- context 'When building a request' do
- it 'should create a request object' do
- expect(subject).to be_a(DeepL::Requests::Translate)
+ context 'when building a request' do
+ it 'creates a request object' do
+ expect(translate).to be_a(described_class)
end
end
context 'when using `splitting_tags` options' do
- it 'should work with a nil values' do
- request = DeepL::Requests::Translate.new(api, nil, nil, nil, splitting_tags: nil)
- expect(request.options[:splitting_tags]).to eq(nil)
+ it 'works with a nil values' do
+ request = described_class.new(api, nil, nil, nil, splitting_tags: nil)
+ expect(request.options[:splitting_tags]).to be_nil
end
- it 'should work with a blank list' do
- request = DeepL::Requests::Translate.new(api, nil, nil, nil, splitting_tags: '')
- expect(request.options[:splitting_tags]).to eq('')
+ it 'works with a blank list' do
+ request = described_class.new(api, nil, nil, nil, splitting_tags: '')
+ expect(request.options[:splitting_tags]).to eq([])
end
- it 'should work with a comma-separated list' do
- request = DeepL::Requests::Translate.new(api, nil, nil, nil, splitting_tags: tags_str)
- expect(request.options[:splitting_tags]).to eq(tags_str)
+ it 'works with a comma-separated list and converts strings to an array' do
+ request = described_class.new(api, nil, nil, nil, splitting_tags: tags_str)
+ expect(request.options[:splitting_tags]).to eq(tags_array)
end
- it 'should convert arrays to strings' do
- request = DeepL::Requests::Translate.new(api, nil, nil, nil, splitting_tags: tags_array)
- expect(request.options[:splitting_tags]).to eq(tags_str)
+ it 'works with an array of tags and leaves it as is' do
+ request = described_class.new(api, nil, nil, nil, splitting_tags: tags_array)
+ expect(request.options[:splitting_tags]).to eq(tags_array)
end
-
- it 'should leave strings as they are' do
- request = DeepL::Requests::Translate.new(api, nil, nil, nil, splitting_tags: tags_str)
- expect(request.options[:splitting_tags]).to eq(tags_str)
- end
end
context 'when using `non_splitting_tags` options' do
- it 'should work with a nil values' do
- request = DeepL::Requests::Translate.new(api, nil, nil, nil, non_splitting_tags: nil)
- expect(request.options[:non_splitting_tags]).to eq(nil)
+ it 'works with a nil values' do
+ request = described_class.new(api, nil, nil, nil, non_splitting_tags: nil)
+ expect(request.options[:non_splitting_tags]).to be_nil
end
- it 'should work with a blank list' do
- request = DeepL::Requests::Translate.new(api, nil, nil, nil, non_splitting_tags: '')
- expect(request.options[:non_splitting_tags]).to eq('')
+ it 'works with a blank list' do
+ request = described_class.new(api, nil, nil, nil, non_splitting_tags: '')
+ expect(request.options[:non_splitting_tags]).to eq([])
end
- it 'should work with a comma-separated list' do
- request = DeepL::Requests::Translate.new(api, nil, nil, nil, non_splitting_tags: tags_str)
- expect(request.options[:non_splitting_tags]).to eq(tags_str)
+ it 'works with a comma-separated list and converts strings to an array' do
+ request = described_class.new(api, nil, nil, nil, non_splitting_tags: tags_str)
+ expect(request.options[:non_splitting_tags]).to eq(tags_array)
end
- it 'should convert arrays to strings' do
- request = DeepL::Requests::Translate.new(api, nil, nil, nil, non_splitting_tags: tags_array)
- expect(request.options[:non_splitting_tags]).to eq(tags_str)
+ it 'works with an array and leaves it as it is' do
+ request = described_class.new(api, nil, nil, nil, non_splitting_tags: tags_array)
+ expect(request.options[:non_splitting_tags]).to eq(tags_array)
end
-
- it 'should leave strings as they are' do
- request = DeepL::Requests::Translate.new(api, nil, nil, nil, non_splitting_tags: tags_str)
- expect(request.options[:non_splitting_tags]).to eq(tags_str)
- end
end
context 'when using `ignore_tags` options' do
- it 'should work with a nil values' do
- request = DeepL::Requests::Translate.new(api, nil, nil, nil, ignore_tags: nil)
- expect(request.options[:ignore_tags]).to eq(nil)
+ it 'works with a nil values' do
+ request = described_class.new(api, nil, nil, nil, ignore_tags: nil)
+ expect(request.options[:ignore_tags]).to be_nil
end
- it 'should work with a blank list' do
- request = DeepL::Requests::Translate.new(api, nil, nil, nil, ignore_tags: '')
- expect(request.options[:ignore_tags]).to eq('')
+ it 'works with a blank list' do
+ request = described_class.new(api, nil, nil, nil, ignore_tags: '')
+ expect(request.options[:ignore_tags]).to eq([])
end
- it 'should work with a comma-separated list' do
- request = DeepL::Requests::Translate.new(api, nil, nil, nil, ignore_tags: tags_str)
- expect(request.options[:ignore_tags]).to eq(tags_str)
+ it 'works with a comma-separated list and converts a string to an array' do
+ request = described_class.new(api, nil, nil, nil, ignore_tags: tags_str)
+ expect(request.options[:ignore_tags]).to eq(tags_array)
end
- it 'should convert arrays to strings' do
- request = DeepL::Requests::Translate.new(api, nil, nil, nil, ignore_tags: tags_array)
- expect(request.options[:ignore_tags]).to eq(tags_str)
+ it 'works with an array and leaves it as it is' do
+ request = described_class.new(api, nil, nil, nil, ignore_tags: tags_array)
+ expect(request.options[:ignore_tags]).to eq(tags_array)
end
-
- it 'should leave strings as they are' do
- request = DeepL::Requests::Translate.new(api, nil, nil, nil, ignore_tags: tags_str)
- expect(request.options[:ignore_tags]).to eq(tags_str)
- end
end
context 'when using `split_sentences` options' do
- it 'should convert `true` to `1`' do
- request = DeepL::Requests::Translate.new(api, nil, nil, nil, split_sentences: true)
+ it 'converts `true` to `1`' do
+ request = described_class.new(api, nil, nil, nil, split_sentences: true)
expect(request.options[:split_sentences]).to eq('1')
end
- it 'should convert `false` to `0`' do
- request = DeepL::Requests::Translate.new(api, nil, nil, nil, split_sentences: false)
+ it 'converts `false` to `0`' do
+ request = described_class.new(api, nil, nil, nil, split_sentences: false)
expect(request.options[:split_sentences]).to eq('0')
end
- it 'should leave `0` as is' do
- request = DeepL::Requests::Translate.new(api, nil, nil, nil, split_sentences: '0')
+ it 'leaves `0` as is' do
+ request = described_class.new(api, nil, nil, nil, split_sentences: '0')
expect(request.options[:split_sentences]).to eq('0')
end
- it 'should leave `nonewlines` as is' do
- request = DeepL::Requests::Translate.new(api, nil, nil, nil, split_sentences: 'nonewlines')
+ it 'leaves `nonewlines` as is' do
+ request = described_class.new(api, nil, nil, nil, split_sentences: 'nonewlines')
expect(request.options[:split_sentences]).to eq('nonewlines')
end
- it 'should leave `1` as is' do
- request = DeepL::Requests::Translate.new(api, nil, nil, nil, split_sentences: '1')
+ it 'leaves `1` as is' do
+ request = described_class.new(api, nil, nil, nil, split_sentences: '1')
expect(request.options[:split_sentences]).to eq('1')
end
end
context 'when using `preserve_formatting` options' do
- it 'should convert `true` to `1`' do
- request = DeepL::Requests::Translate.new(api, nil, nil, nil, preserve_formatting: true)
- expect(request.options[:preserve_formatting]).to eq('1')
+ it 'leaves `true` as is' do
+ request = described_class.new(api, nil, nil, nil, preserve_formatting: true)
+ expect(request.options[:preserve_formatting]).to be(true)
end
- it 'should convert `false` to `0`' do
- request = DeepL::Requests::Translate.new(api, nil, nil, nil, preserve_formatting: false)
- expect(request.options[:preserve_formatting]).to eq('0')
+ it 'leaves `false` as is' do
+ request = described_class.new(api, nil, nil, nil, preserve_formatting: false)
+ expect(request.options[:preserve_formatting]).to be(false)
end
- it 'should leave `0` as is' do
- request = DeepL::Requests::Translate.new(api, nil, nil, nil, preserve_formatting: '0')
- expect(request.options[:preserve_formatting]).to eq('0')
+ it 'converts `0` to `false`' do
+ request = described_class.new(api, nil, nil, nil, preserve_formatting: '0')
+ expect(request.options[:preserve_formatting]).to be(false)
end
- it 'should leave `1` as is' do
- request = DeepL::Requests::Translate.new(api, nil, nil, nil, preserve_formatting: '1')
- expect(request.options[:preserve_formatting]).to eq('1')
+ it 'converts `1` to `true`' do
+ request = described_class.new(api, nil, nil, nil, preserve_formatting: '1')
+ expect(request.options[:preserve_formatting]).to be(true)
end
end
context 'when using `outline_detection` options' do
- it 'should convert `true` to `1`' do
- request = DeepL::Requests::Translate.new(api, nil, nil, nil, outline_detection: true)
- expect(request.options[:outline_detection]).to eq('1')
+ it 'leaves `true` as is' do
+ request = described_class.new(api, nil, nil, nil, outline_detection: true)
+ expect(request.options[:outline_detection]).to be(true)
end
- it 'should convert `false` to `0`' do
- request = DeepL::Requests::Translate.new(api, nil, nil, nil, outline_detection: false)
- expect(request.options[:outline_detection]).to eq('0')
+ it 'leaves `false` as is' do
+ request = described_class.new(api, nil, nil, nil, outline_detection: false)
+ expect(request.options[:outline_detection]).to be(false)
end
- it 'should leave `0` as is' do
- request = DeepL::Requests::Translate.new(api, nil, nil, nil, outline_detection: '0')
- expect(request.options[:outline_detection]).to eq('0')
+ it 'converts `0` to `false`' do
+ request = described_class.new(api, nil, nil, nil, outline_detection: '0')
+ expect(request.options[:outline_detection]).to be(false)
end
- it 'should leave `1` as is' do
- request = DeepL::Requests::Translate.new(api, nil, nil, nil, outline_detection: '1')
- expect(request.options[:outline_detection]).to eq('1')
+ it 'converts `1` to `true`' do
+ request = described_class.new(api, nil, nil, nil, outline_detection: '1')
+ expect(request.options[:outline_detection]).to be(true)
end
end
context 'when using `glossary_id` options' do
- it 'should work with a nil values' do
- request = DeepL::Requests::Translate.new(api, nil, nil, nil, glossary_id: nil)
- expect(request.options[:glossary_id]).to eq(nil)
+ it 'works with a nil values' do
+ request = described_class.new(api, nil, nil, nil, glossary_id: nil)
+ expect(request.options[:glossary_id]).to be_nil
end
- it 'should work with a string' do
- request = DeepL::Requests::Translate.new(api, nil, nil, nil, glossary_id: 'sample_id')
+ it 'works with a string' do
+ request = described_class.new(api, nil, nil, nil, glossary_id: 'sample_id')
expect(request.options[:glossary_id]).to eq('sample_id')
end
end
context 'when using `formality` options' do
- it 'should work with a nil values' do
- request = DeepL::Requests::Translate.new(api, nil, nil, nil, formality: nil)
- expect(request.options[:formality]).to eq(nil)
+ it 'works with a nil values' do
+ request = described_class.new(api, nil, nil, nil, formality: nil)
+ expect(request.options[:formality]).to be_nil
end
- it 'should work with a string' do
- request = DeepL::Requests::Translate.new(api, nil, nil, nil, formality: 'more')
+ it 'works with a string' do
+ request = described_class.new(api, nil, nil, nil, formality: 'more')
expect(request.options[:formality]).to eq('more')
end
end
end
describe '#request' do
around do |example|
VCR.use_cassette('translate_texts') { example.call }
end
- context 'When performing a valid request with one text' do
- it 'should return a text object' do
- text = subject.request
+ context 'when performing a valid request with one text' do
+ it 'returns a text object' do
+ text = translate.request
expect(text).to be_a(DeepL::Resources::Text)
expect(text.text).to eq('Texto de muestra')
expect(text.detected_source_language).to eq('EN')
end
end
- context 'When performing a valid request with multiple texts' do
+ context 'when performing a valid request with multiple texts' do
let(:text) { %w[Sample Word] }
- it 'should return a text object' do
- texts = subject.request
+ it 'returns a text object' do
+ texts = translate.request
expect(texts).to be_a(Array)
expect(texts.first.text).to eq('Muestra')
expect(texts.first.detected_source_language).to eq('EN')
expect(texts.last.text).to eq('Palabra')
expect(texts.last.detected_source_language).to eq('EN')
end
end
- context 'When performing a valid request with tag handling' do
+ context 'when performing a valid request with tag handling' do
let(:text) { '<p>Sample text</p>' }
let(:options) { { tag_handling: 'xml' } }
- it 'should return a text object' do
- text = subject.request
+ it 'returns a text object' do
+ text = translate.request
expect(text).to be_a(DeepL::Resources::Text)
expect(text.text).to eq('<p>Texto de muestra</p>')
expect(text.detected_source_language).to eq('EN')
end
end
- context 'When performing a valid request and passing a variable' do
+ context 'when performing a valid request and passing a variable' do
let(:text) { 'Welcome and <code>Hello great World</code> Good Morning!' }
let(:options) { { tag_handling: 'xml', ignore_tags: %w[code span] } }
- it 'should return a text object' do
- text = subject.request
+ it 'returns a text object' do
+ text = translate.request
expect(text).to be_a(DeepL::Resources::Text)
expect(text.text).to eq('Bienvenido y <code>Hello great World</code> ¡Buenos días!')
expect(text.detected_source_language).to eq('EN')
end
end
- context 'When performing a valid request with an HTML document' do
+ context 'when performing a valid request with an HTML document' do
let(:text) do
<<~XML
<document>
<meta>
<title>A document's title</title>
@@ -275,19 +269,19 @@
outline_detection: false,
splitting_tags: %w[title par]
}
end
- it 'should return a text object' do
- text = subject.request
+ it 'returns a text object' do
+ text = translate.request
expect(text).to be_a(DeepL::Resources::Text)
expect(text.text).to eq(
<<~XML
<document>
<meta>
- <title>El título de un documento</title>
+ <title>Título de un documento</title>
</meta>
<content>
<par>Es la primera frase. Seguido de una segunda.</par>
<par>Esta es la tercera frase.</par>
</content>
@@ -296,47 +290,47 @@
)
expect(text.detected_source_language).to eq('EN')
end
end
- context 'When performing a bad request' do
- context 'When using an invalid token' do
+ context 'when performing a bad request' do
+ context 'when using an invalid token' do
let(:api) do
api = build_deepl_api
api.configuration.auth_key = 'invalid'
api
end
- it 'should raise an unauthorized error' do
- expect { subject.request }.to raise_error(DeepL::Exceptions::AuthorizationFailed)
+ it 'raises an unauthorized error' do
+ expect { translate.request }.to raise_error(DeepL::Exceptions::AuthorizationFailed)
end
end
- context 'When using an invalid text' do
+ context 'when using an invalid text' do
let(:text) { nil }
- it 'should raise a bad request error' do
- message = "Parameter 'text' not specified."
- expect { subject.request }.to raise_error(DeepL::Exceptions::BadRequest, message)
+ it 'raises a bad request error' do
+ message = "Invalid request: Expected 'text' parameter to be an array of strings"
+ expect { translate.request }.to raise_error(DeepL::Exceptions::BadRequest, message)
end
end
- context 'When using an invalid target language' do
+ context 'when using an invalid target language' do
let(:target_lang) { nil }
- it 'should raise a bad request error' do
+ it 'raises a bad request error' do
message = "Value for 'target_lang' not supported."
- expect { subject.request }.to raise_error(DeepL::Exceptions::BadRequest, message)
+ expect { translate.request }.to raise_error(DeepL::Exceptions::BadRequest, message)
end
end
end
- context 'When performing a request with too many texts' do
+ context 'when performing a request with too many texts' do
let(:text) { Array.new(10_000) { |i| "This is the sentence number #{i}" } }
- it 'should raise a request entity too large error' do
- expect { subject.request }.to raise_error(DeepL::Exceptions::RequestEntityTooLarge,
- /request size has reached the supported limit/)
+ it 'raises a request entity too large error' do
+ expect { translate.request }.to raise_error(DeepL::Exceptions::RequestEntityTooLarge,
+ /request size has reached the supported limit/)
end
end
end
end