spec/requests/translate_spec.rb in deepl-rb-2.5.0 vs spec/requests/translate_spec.rb in deepl-rb-2.5.1
- old
+ new
@@ -1,27 +1,55 @@
# frozen_string_literal: true
require 'spec_helper'
describe DeepL::Requests::Translate do
- let(:tags_str) { 'p, strong, span' }
+ 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)
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)
+ 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('')
+ 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)
+ 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)
+ 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)
end
@@ -213,16 +241,60 @@
end
end
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: 'code,span' } }
+ let(:options) { { tag_handling: 'xml', ignore_tags: %w[code span] } }
it 'should return a text object' do
text = subject.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
+ let(:text) do
+ <<~XML
+ <document>
+ <meta>
+ <title>A document's title</title>
+ </meta>
+ <content>
+ <par>This is the first sentence. Followed by a second one.</par>
+ <par>This is the third sentence.</par>
+ </content>
+ </document>
+ XML
+ end
+ let(:options) do
+ {
+ tag_handling: 'xml',
+ split_sentences: 'nonewlines',
+ outline_detection: false,
+ splitting_tags: %w[title par]
+ }
+ end
+
+ it 'should return a text object' do
+ text = subject.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>
+ </meta>
+ <content>
+ <par>Es la primera frase. Seguido de una segunda.</par>
+ <par>Esta es la tercera frase.</par>
+ </content>
+ </document>
+ XML
+ )
expect(text.detected_source_language).to eq('EN')
end
end
context 'When performing a bad request' do