# frozen_string_literal: true #-- # wwwjdic # © 2014-2021 Marco Bresciani # # This file is part of wwwjdic. # # wwwjdic is free software: you can redistribute it and/or modify it # under the terms of the GNU General Public License as published by the # Free Software Foundation, either version 3 of the License, or (at your # option) any later version. # # wwwjdic is distributed in the hope that it will be useful, but WITHOUT # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or # FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License # for more details. # # You should have received a copy of the GNU General Public License # along with wwwjdic. If not, see . # # SPDX-FileCopyrightText: 2014-2021 Marco Bresciani # # SPDX-License-Identifier: GPL-3.0-or-later #++ require_relative '../test_helper' def test_multilanguage(a_method) describe 'when considering multiple languages' do I18n.load_path = Dir[File.join(File.dirname(__FILE__), 'locales/', '*.yml')] WWWJDic::AVAIL_LANGS.each_pair do |dict, number| I18n.locale = dict.to_s a_word = I18n.t('test.kotoba') it 'shall create a saved file with the proper filename' do filename = "File_#{a_word}" subject.send(a_method, a_word, nil, filename) _(File.exist?(filename)).must_equal true File.delete filename end it "shall return the translated word '#{a_word}'" do translation = subject.send(a_method, a_word, dict: WWWJDic::AVAIL_LANGS[dict]) _(translation).wont_be_nil _(translation).wont_be_empty if a_method == :json_translate _(JSON.parse(translation)).wont_include '' _(JSON.parse(translation)).wont_include '<pre>' _(JSON.parse(translation).keys).must_include a_word an_uri = subject.raw_uri(a_word, dict: WWWJDic::AVAIL_LANGS[dict]) _(an_uri).must_include number _(JSON.parse(translation)[a_word]).must_equal an_uri end end end end end def test_get_translation describe 'when asking to retrieve a translation' do %i[translate json_translate].each do |a_method| describe "when the translation is '#{a_method}'" do ['', nil].each do |a_word| it "shall raise ArgumentError when an invalid word '#{a_word}' is proposed" do assert_raises ArgumentError do subject.send(a_method, a_word) end end end test_multilanguage(a_method) end end end end def test_get_dictionary describe 'when selecting a dictionary' do ['', nil].each do |a_dict| describe "when an invalid dictionary '#{a_dict}' is selected" do it 'shall raise an error' do assert_raises ArgumentError do subject.dictionary = a_dict end end test_return_test_reference end end WWWJDic::DICTIONARY_CODES.each do |dict_number| describe "when dictionary '#{dict_number}' is selected" do it "shall return the proper number '#{dict_number}'" do subject.dictionary = dict_number _(subject.dictionary).must_equal WWWJDic::DICTS_BY_CODES[dict_number] _(subject.to_s).must_equal WWWJDic::URI_DEFAULT + dict_number + WWWJDic::DISPLAY[:regular] end end end it 'shall return the URI with basic dictionary when reset' do subject.dictionary = 'R' subject.reset _(subject.to_s).must_equal WWWJDic::TEST_REFERENCE_URI _(subject.dictionary).must_equal WWWJDic::DICTIONARY_NAMES[0] end WWWJDic::DICTIONARY_NAMES.each do |dict_name| describe "when dictionary '#{dict_name}' is selected" do it "shall return the proper name '#{dict_name}'" do subject.dictionary = dict_name _(subject.dictionary).must_equal dict_name _(subject.to_s).must_equal WWWJDic::URI_DEFAULT + WWWJDic::DICTS_BY_NAMES[dict_name] + WWWJDic::DISPLAY[:regular] end end end it 'shall return the URI with basic dictionary when reset' do subject.dictionary = 'Japanese-Italian' subject.reset _(subject.to_s).must_equal WWWJDic::TEST_REFERENCE_URI _(subject.dictionary).must_equal WWWJDic::DICTIONARY_NAMES[0] end end end def test_return_json(a_word, dict) it "shall return correct JSON data containing '#{a_word}' and the proper URI" do json = subject.json_uri(a_word, dict: WWWJDic::AVAIL_LANGS[dict]) an_uri = subject.uri(a_word, dict: WWWJDic::AVAIL_LANGS[dict]) _(JSON.parse(json).keys).must_include a_word _(JSON.parse(json)[a_word]).must_equal an_uri end end def test_override_custom_params(a_word, dict) it 'shall override user-custom display parameter for raw display' do _(subject.raw_uri(a_word, display: :regular, dict: WWWJDic::AVAIL_LANGS[dict])).must_include WWWJDic::DISPLAY[:raw] end end def test_multiple_languages describe 'when considering multiple languages' do WWWJDic::AVAIL_LANGS.each_pair do |dict, number| I18n.locale = dict.to_s a_word = I18n.t('test.kotoba') %i[uri raw_uri json_uri].each do |a_method| it "shall return URI containing '#{number}' for a '#{dict}' word for '#{a_method}' request" do _(subject.send(a_method, a_word, dict: WWWJDic::AVAIL_LANGS[dict])).must_include number _(subject.send(a_method, a_word, dict: WWWJDic::AVAIL_LANGS[dict])).must_include CGI.escape(a_word).to_s end end test_override_custom_params(a_word, dict) test_return_json(a_word, dict) end end end def test_raise_invalid_server_error(a_server) it 'shall raise an error' do assert_raises ArgumentError do subject.server = a_server end end end def test_return_test_reference it 'shall return the reference URI' do _(subject.to_s).must_equal WWWJDic::TEST_REFERENCE_URI end end def test_invalid_server describe 'when an invalid server is selected' do ['', nil, :invalid].each do |a_server| test_raise_invalid_server_error(a_server) test_return_test_reference end end end def test_server WWWJDic::URIS.each do |name, uri| describe "when server '#{name}' is selected" do it "shall return the proper '#{uri}'" do subject.server = name _(subject.server).must_equal uri _(subject.to_s).must_include uri end end end end def test_select_server describe 'when selecting a server' do test_invalid_server test_server end end def test_invalid_parameter(a_word) describe "when providing an invalid parameter '#{a_word}'" do %i[uri raw_uri json_uri].each do |a_method| it "shall raise an error for #{a_method} request" do assert_raises ArgumentError do subject.send a_method end end end end end def test_ask_uri describe 'when asking for URI' do ['', nil].each(&method(:test_invalid_parameter)) test_multiple_languages end end def test_ask_translation describe 'when asking for a translation' do I18n.load_path = Dir[File.join(File.dirname(__FILE__), './locales/', '*.yml')] # Tests for translation URIs test_ask_uri end end describe WWWJDic::WWWJDic do describe 'when created' do subject { WWWJDic::WWWJDic.new(WWWJDic.parser) } # Basic checks for instance and default results it 'shall return a valid object' do _(subject).wont_be_nil _(subject).must_be_instance_of WWWJDic::WWWJDic end it 'shall return the URI with basic language' do _(subject.to_s).must_equal WWWJDic::TEST_REFERENCE_URI end # Tests for server selection test_select_server # Tests for dictionary selection test_get_dictionary # Tests for translations test_ask_translation test_get_translation end end