# 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) { {} } describe '#initialize' do 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 '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 '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 '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 '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 end context 'when using `non_splitting_tags` options' do 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 '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 '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 '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 end context 'when using `ignore_tags` options' do 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 '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 '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 '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 end context 'when using `split_sentences` options' do 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 '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 '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 '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 '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 '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 '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 '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 '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 '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 '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 '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 '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 '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 '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 '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 'works with a string' do request = described_class.new(api, nil, nil, nil, formality: 'more') expect(request.options[:formality]).to eq('more') end end context 'when using `model_type` options' do it 'works with a nil value' do request = described_class.new(api, nil, nil, nil, model_type: nil) expect(request.options[:model_type]).to be_nil end it 'works with a string' do request = described_class.new(api, nil, nil, nil, model_type: 'latency_optimized') expect(request.options[:model_type]).to eq('latency_optimized') 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 '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 let(:text) { %w[Sample Word] } 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 let(:text) { '
Sample text
' } let(:options) { { tag_handling: 'xml' } } 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 and passing a variable' do let(:text) { 'Welcome andHello great World
Good Morning!' }
let(:options) { { tag_handling: 'xml', ignore_tags: %w[code span] } }
it 'returns a text object' do
text = translate.request
expect(text).to be_a(DeepL::Resources::Text)
expect(text.text).to eq('Bienvenido y Hello great World
¡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