From 41b0aa3675e049acae3f6d222bb529378a03d7ab Mon Sep 17 00:00:00 2001 From: Brett Stewart Date: Wed, 7 Jul 2021 11:01:34 +0900 Subject: [PATCH] Update rubocop and fix errors (#182) --- .circleci/config.yml | 2 +- .rubocop_todo.yml | 53 ++------ Gemfile | 5 - lib/wovnrb.rb | 4 +- lib/wovnrb/api_translator.rb | 2 +- lib/wovnrb/headers.rb | 69 +++++------ lib/wovnrb/helpers/nokogumbo_helper.rb | 16 ++- lib/wovnrb/lang.rb | 29 ++--- lib/wovnrb/services/html_converter.rb | 14 +-- lib/wovnrb/store.rb | 13 +- lib/wovnrb/text_caches/cache_base.rb | 53 -------- lib/wovnrb/text_caches/memory_cache.rb | 51 -------- lib/wovnrb/version.rb | 2 +- test/lib/api_translator_test.rb | 7 +- test/lib/lang_test.rb | 141 +--------------------- test/lib/services/html_converter_test.rb | 4 +- test/lib/text_caches/cache_base_test.rb | 31 ----- test/lib/text_caches/memory_cache_test.rb | 91 -------------- test/lib/wovnrb_test.rb | 24 ++-- wovnrb.gemspec | 1 + 20 files changed, 100 insertions(+), 512 deletions(-) delete mode 100644 lib/wovnrb/text_caches/cache_base.rb delete mode 100644 lib/wovnrb/text_caches/memory_cache.rb delete mode 100644 test/lib/text_caches/cache_base_test.rb delete mode 100644 test/lib/text_caches/memory_cache_test.rb diff --git a/.circleci/config.yml b/.circleci/config.yml index 2cb382c1..3b80f1b9 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -3,7 +3,7 @@ version: 2 jobs: build: docker: - - image: circleci/ruby:2.3-jessie + - image: circleci/ruby:2.5.4 steps: - checkout - restore_cache: diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml index 0cac1773..d6d90acf 100644 --- a/.rubocop_todo.yml +++ b/.rubocop_todo.yml @@ -6,17 +6,8 @@ # Note that changes in the inspected code, or installation of new # versions of RuboCop, may require this file to be generated again. -# Offense count: 1 -# Cop supports --auto-correct. -Layout/EmptyLinesAroundAccessModifier: - Exclude: - - 'lib/wovnrb/text_caches/memory_cache.rb' - -# Offense count: 1 -# Cop supports --auto-correct. -Layout/EmptyLinesAroundMethodBody: - Exclude: - - 'test/lib/text_caches/memory_cache_test.rb' +Gemspec/RequiredRubyVersion: + Enabled: false # Offense count: 11 # Cop supports --auto-correct. @@ -25,13 +16,9 @@ Layout/ExtraSpacing: Exclude: - 'lib/wovnrb/lang.rb' -# Offense count: 1 -# Cop supports --auto-correct. -# Configuration parameters: EnforcedStyle, IndentationWidth. -# SupportedStyles: special_inside_parentheses, consistent, align_braces -Layout/FirstHashElementIndentation: +Style/HashEachMethods: Exclude: - - 'lib/wovnrb/text_caches/cache_base.rb' + - 'lib/wovnrb/store.rb' # Offense count: 4 # Cop supports --auto-correct. @@ -42,13 +29,6 @@ Layout/HeredocIndentation: - 'test/lib/lang_test.rb' - 'test/lib/wovnrb_test.rb' -# Offense count: 1 -# Cop supports --auto-correct. -# Configuration parameters: AllowForAlignment. -Layout/SpaceAroundOperators: - Exclude: - - 'lib/wovnrb/text_caches/memory_cache.rb' - # Offense count: 3 # Configuration parameters: AllowSafeAssignment. Lint/AssignmentInCondition: @@ -70,13 +50,6 @@ Lint/ImplicitStringConcatenation: Exclude: - 'test/lib/lang_test.rb' -# Offense count: 3 -# Cop supports --auto-correct. -# Configuration parameters: AllowUnusedKeywordArguments, IgnoreEmptyMethods. -Lint/UnusedMethodArgument: - Exclude: - - 'lib/wovnrb/text_caches/cache_base.rb' - # Offense count: 5 Lint/UselessAssignment: Exclude: @@ -116,12 +89,13 @@ Metrics/MethodLength: # Offense count: 12 Metrics/PerceivedComplexity: Max: 22 + Exclude: + - 'lib/wovnrb/headers.rb' # Offense count: 3 Naming/AccessorMethodName: Exclude: - 'lib/wovnrb/services/wovn_logger.rb' - - 'lib/wovnrb/text_caches/cache_base.rb' # Offense count: 120 # Configuration parameters: . @@ -153,12 +127,6 @@ Style/ClassCheck: Exclude: - 'lib/wovnrb/store.rb' -# Offense count: 6 -Style/ClassVars: - Exclude: - - 'lib/wovnrb/text_caches/cache_base.rb' - - 'lib/wovnrb/text_caches/memory_cache.rb' - # Offense count: 1 # Cop supports --auto-correct. # Configuration parameters: Keywords. @@ -183,8 +151,6 @@ Style/Documentation: - 'lib/wovnrb/services/wovn_logger.rb' - 'lib/wovnrb/settings.rb' - 'lib/wovnrb/store.rb' - - 'lib/wovnrb/text_caches/cache_base.rb' - - 'lib/wovnrb/text_caches/memory_cache.rb' # Offense count: 2 Style/DoubleNegation: @@ -224,7 +190,6 @@ Style/IfUnlessModifier: - 'Rakefile' - 'lib/wovnrb/helpers/nokogumbo_helper.rb' - 'lib/wovnrb/store.rb' - - 'lib/wovnrb/text_caches/memory_cache.rb' # Offense count: 1 # Cop supports --auto-correct. @@ -241,7 +206,6 @@ Style/NegatedIf: Style/PreferredHashMethods: Exclude: - 'lib/wovnrb/store.rb' - - 'lib/wovnrb/text_caches/memory_cache.rb' # Offense count: 2 # Cop supports --auto-correct. @@ -297,7 +261,7 @@ Style/ZeroLengthPredicate: # Offense count: 1436 # Configuration parameters: AllowHeredoc, AllowURI, URISchemes, IgnoreCopDirectives, IgnoredPatterns. # URISchemes: http, https -Metrics/LineLength: +Layout/LineLength: Max: 1424 Style/FrozenStringLiteralComment: @@ -310,6 +274,7 @@ Style/SafeNavigation: Enabled: false AllCops: + NewCops: enable Exclude: - 'docker/**/*' - - 'vendor/**/*' \ No newline at end of file + - 'vendor/**/*' diff --git a/Gemfile b/Gemfile index d6f19137..851fabc2 100644 --- a/Gemfile +++ b/Gemfile @@ -1,7 +1,2 @@ source 'https://rubygems.org' - -# Specify your gem's dependencies in wovn.gemspec -if RUBY_VERSION < '2.2.2' - gem 'activesupport', '< 5.0.0' -end gemspec diff --git a/lib/wovnrb.rb b/lib/wovnrb.rb index 7ccd0931..d98f92c9 100644 --- a/lib/wovnrb.rb +++ b/lib/wovnrb.rb @@ -9,7 +9,6 @@ require 'active_support' require 'json' require 'wovnrb/helpers/nokogumbo_helper' -require 'wovnrb/text_caches/cache_base' require 'wovnrb/railtie' if defined?(Rails) require 'wovnrb/version' @@ -18,9 +17,8 @@ class Interceptor def initialize(app, opts = {}) @app = app @store = Store.instance - opts = opts.each_with_object({}) { |(k, v), memo| memo[k.to_s] = v } + opts = opts.transform_keys(&:to_s) @store.update_settings(opts) - CacheBase.set_single(@store.settings) end def call(env) diff --git a/lib/wovnrb/api_translator.rb b/lib/wovnrb/api_translator.rb index 3a26fd34..960b71f0 100644 --- a/lib/wovnrb/api_translator.rb +++ b/lib/wovnrb/api_translator.rb @@ -109,7 +109,7 @@ def compress_request_data(data_hash) end def api_uri - Addressable::URI.parse(@store.settings['api_url'] + '/v0/') + Addressable::URI.parse("#{@store.settings['api_url']}/v0/") end def api_timeout diff --git a/lib/wovnrb/headers.rb b/lib/wovnrb/headers.rb index 5fe1eb0f..38344fb9 100644 --- a/lib/wovnrb/headers.rb +++ b/lib/wovnrb/headers.rb @@ -1,17 +1,10 @@ module Wovnrb class Headers - attr_reader :unmasked_url - attr_reader :url - attr_reader :protocol - attr_reader :unmasked_host - attr_reader :host - attr_reader :unmasked_pathname - attr_reader :pathname - attr_reader :pathname_with_trailing_slash_if_present + attr_reader :unmasked_url, :url, :protocol, :unmasked_host, :host, :unmasked_pathname, :pathname, :pathname_with_trailing_slash_if_present # Generates new instance of Wovnrb::Headers. # Its parameters are set by parsing env variable. - # + def initialize(env, settings) request = Rack::Request.new(env) @@ -42,21 +35,21 @@ def initialize(env, settings) @pathname, @query = @env['REQUEST_URI'].split('?') @pathname = settings['url_pattern'] == 'path' ? remove_lang(@pathname, lang_code) : @pathname @query ||= '' - @url = "#{@host}#{@pathname}#{(!@query.empty? ? '?' : '') + remove_lang(@query, lang_code)}" - if !settings['query'].empty? + @url = "#{@host}#{@pathname}#{(@query.empty? ? '' : '?') + remove_lang(@query, lang_code)}" + if settings['query'].empty? + @query = '' + else query_vals = [] settings['query'].each do |qv| rx = Regexp.new("(^|&)(?#{qv}[^&]+)(&|$)") m = @query.match(rx) query_vals.push(m[:query_val]) if m && m[:query_val] end - @query = if !query_vals.empty? - "?#{query_vals.sort.join('&')}" - else + @query = if query_vals.empty? '' + else + "?#{query_vals.sort.join('&')}" end - else - @query = '' end @query = remove_lang(@query, lang_code) @pathname_with_trailing_slash_if_present = @pathname @@ -115,11 +108,11 @@ def redirect_location(lang) case @settings['url_pattern'] when 'query' lang_param_name = @settings['lang_param_name'] - if location !~ /\?/ - location = "#{location}?#{lang_param_name}=#{lang_code}" - else @env['REQUEST_URI'] !~ /(\?|&)#{lang_param_name}=/ - location = "#{location}&#{lang_param_name}=#{lang_code}" - end + location = if location =~ /\?/ + "#{location}&#{lang_param_name}=#{lang_code}" + else + "#{location}?#{lang_param_name}=#{lang_code}" + end when 'subdomain' location = "#{lang_code.downcase}.#{location}" # when 'path' @@ -173,7 +166,7 @@ def remove_lang(uri, lang = path_lang) lang_param_name = @settings['lang_param_name'] uri.sub(/(^|\?|&)#{lang_param_name}=#{lang_code}(&|$)/, '\1').gsub(/(\?|&)$/, '') when 'subdomain' - rp = Regexp.new('(^|(//))' + lang_code + '\.', 'i') + rp = Regexp.new("(^|(//))#{lang_code}\\.", 'i') uri.sub(rp, '\1') # when 'path' else @@ -182,24 +175,22 @@ def remove_lang(uri, lang = path_lang) end def out(headers) - r = Regexp.new('//' + @host) + r = Regexp.new("//#{@host}") lang_code = Store.instance.settings['custom_lang_aliases'][self.lang_code] || self.lang_code - if lang_code != @settings['default_lang'] && headers.key?('Location') && headers['Location'] =~ r - unless @settings['ignore_globs'].ignore?(headers['Location']) - case @settings['url_pattern'] - when 'query' - headers['Location'] += if headers['Location'] =~ /\?/ - '&' - else - '?' - end - headers['Location'] += "#{@settings['lang_param_name']}=#{lang_code}" - when 'subdomain' - headers['Location'] = headers['Location'].sub(/\/\/([^.]+)/, '//' + lang_code + '.\1') - # when 'path' - else - headers['Location'] = headers['Location'].sub(/(\/\/[^\/]+)/, '\1/' + lang_code) - end + if lang_code != @settings['default_lang'] && headers.key?('Location') && headers['Location'] =~ r && !@settings['ignore_globs'].ignore?(headers['Location']) + case @settings['url_pattern'] + when 'query' + headers['Location'] += if headers['Location'] =~ /\?/ + '&' + else + '?' + end + headers['Location'] += "#{@settings['lang_param_name']}=#{lang_code}" + when 'subdomain' + headers['Location'] = headers['Location'].sub(/\/\/([^.]+)/, "//#{lang_code}.\\1") + # when 'path' + else + headers['Location'] = headers['Location'].sub(/(\/\/[^\/]+)/, "\\1/#{lang_code}") end end headers diff --git a/lib/wovnrb/helpers/nokogumbo_helper.rb b/lib/wovnrb/helpers/nokogumbo_helper.rb index de040ca5..c9fc6854 100644 --- a/lib/wovnrb/helpers/nokogumbo_helper.rb +++ b/lib/wovnrb/helpers/nokogumbo_helper.rb @@ -2,15 +2,13 @@ module Wovnrb module Helpers module NokogumboHelper def parse_html(html_string, encoding = 'UTF-8') - dom = if html_string.strip[0..999] =~ / e - raise "Could not find #{strategy_sym} (#{e})" - end - - strategy_class = Wovnrb.const_get(ActiveSupport::Inflector.camelize(strategy_sym)) - strategy_class.new(config) - end - - def put(key, value) - raise NotImplementedError.new('put is not defined') - end - - def get(key) - raise NotImplementedError.new('put is not defined') - end - end -end diff --git a/lib/wovnrb/text_caches/memory_cache.rb b/lib/wovnrb/text_caches/memory_cache.rb deleted file mode 100644 index 012d04b7..00000000 --- a/lib/wovnrb/text_caches/memory_cache.rb +++ /dev/null @@ -1,51 +0,0 @@ -require 'active_support/cache' -require 'lz4-ruby' - -module Wovnrb - class MemoryCache < CacheBase - @@default_memory_cache_config = { - cache_megabytes: 200, - ttl_seconds: 300 - } - - def initialize(config) - @config = merge_setting(@@default_memory_cache_config, config) - cache_size = @config[:cache_megabytes].to_f - ttl = @config[:ttl_seconds].to_i - @cache_store = ActiveSupport::Cache::MemoryStore.new(expires_in: ttl.seconds, size: cache_size.megabytes) - end - - def put(key, value) - @cache_store.write(key, compress(value)) - end - - def get(key) - stored_value =@cache_store.fetch(key) - decompress(stored_value) if stored_value - end - - def options - @cache_store.options.clone - end - - private - def merge_setting(original_config, merging_config) - config = original_config.clone - config.keys.each do |key| - key_string = key.to_s - if merging_config.has_key?(key_string) && merging_config[key_string].present? - config[key] = merging_config[key_string] - end - end - config - end - - def compress(value) - LZ4.compress(value) - end - - def decompress(value) - LZ4.decompress(value, value.bytesize, 'UTF-8') - end - end -end diff --git a/lib/wovnrb/version.rb b/lib/wovnrb/version.rb index 80d932bd..6cccb7c9 100644 --- a/lib/wovnrb/version.rb +++ b/lib/wovnrb/version.rb @@ -1,3 +1,3 @@ module Wovnrb - VERSION = '3.0.1'.freeze + VERSION = '3.0.2'.freeze end diff --git a/test/lib/api_translator_test.rb b/test/lib/api_translator_test.rb index 7df73525..a3dbf9f0 100644 --- a/test/lib/api_translator_test.rb +++ b/test/lib/api_translator_test.rb @@ -85,11 +85,10 @@ def stub_translation_api_request(store, headers, original_html, translated_html, compress(stub_response_json) end response_headers = { 'Content-Encoding' => response[:encoding] || 'gzip' } - stub = stub_request(:post, api_url) - .with(body: compressed_data, headers: headers) - .to_return(status: response[:status_code] || 200, body: stub_response, headers: response_headers) + stub_request(:post, api_url) + .with(body: compressed_data, headers: headers) + .to_return(status: response[:status_code] || 200, body: stub_response, headers: response_headers) - stub end end diff --git a/test/lib/lang_test.rb b/test/lib/lang_test.rb index 41d06134..2c3eb018 100644 --- a/test/lib/lang_test.rb +++ b/test/lib/lang_test.rb @@ -21,9 +21,10 @@ def test_keys_exist def test_iso_639_1_normalization Wovnrb::Lang::LANG.each do |_, l| - if l[:code] == 'zh-CHS' + case l[:code] + when 'zh-CHS' assert_equal('zh-Hans', Lang.iso_639_1_normalization('zh-CHS')) - elsif l[:code] == 'zh-CHT' + when 'zh-CHT' assert_equal('zh-Hant', Lang.iso_639_1_normalization('zh-CHT')) else assert_equal(l[:code], Lang.iso_639_1_normalization(l[:code])) @@ -89,7 +90,7 @@ def test_add_lang_code_trad_chinese assert_equal('http://zh-cht.wovn.io/topics/31', lang.add_lang_code('http://wovn.io/topics/31', 'subdomain', h)) end - def test_add_lang_code_trad_chinese_2 + def test_add_lang_code_trad_chinese2 lang = Lang.new('zh-cht') h = Wovnrb::Headers.new(Wovnrb.get_env('url' => 'http://zh-cht.wovn.io'), Wovnrb.get_settings('url_pattern' => 'subdomain', 'url_pattern_reg' => '^(?[^.]+).')) assert_equal('http://zh-cht.wovn.io/topics/31', lang.add_lang_code('/topics/31', 'subdomain', h)) @@ -107,7 +108,7 @@ def test_add_lang_code_no_protocol assert_equal('//zh-cht.google.com', lang.add_lang_code('//google.com', 'subdomain', h)) end - def test_add_lang_code_no_protocol_2 + def test_add_lang_code_no_protocol2 lang = Lang.new('zh-cht') h = Wovnrb::Headers.new(Wovnrb.get_env('url' => 'https://zh-cht.wovn.io'), Wovnrb.get_settings('url_pattern' => 'subdomain', 'url_pattern_reg' => '^(?[^.]+).')) assert_equal('//google.com', lang.add_lang_code('//google.com', 'subdomain', h)) @@ -311,138 +312,6 @@ def test_add_lang_code_relative_path_at_root assert_equal('/fr/index.html', lang.add_lang_code('index.html', 'path', headers)) end - def generate_body(param = '') - body = case param - when 'ignore_parent' - "

Mr. Belvedere Fan Club

-

Hello

- " - when 'ignore_everything' - "

Mr. Belvedere Fan Club

-

Hello

- " - when 'ignore_parent_translated_in_japanese' - "

ベルベデアさんファンクラブ

-

Hello

- - " - when 'translated_in_japanese' - "

ベルベデアさんファンクラブ

-

こんにちは

- - " - when 'ignore_everything_translated' - "

Mr. Belvedere Fan Club

-

Hello

- - " - when 'empty' - '

Mr.BelvedereFanClub

Hello

' - when 'empty_single_quote' - "

Mr.BelvedereFanClub

Hello

" - when 'empty_double_quote' - '

Mr.BelvedereFanClub

Hello

' - when 'value_single_quote' - "

Mr.BelvedereFanClub

Hello

" - when 'value_double_quote' - '

Mr.BelvedereFanClub

Hello

' - when 'empty_translated' - "

Mr.BelvedereFanClub

Hello

\n" - when 'empty_single_quote_translated' - "

Mr.BelvedereFanClub

Hello

\n" - when 'empty_double_quote_translated' - "

Mr.BelvedereFanClub

Hello

\n" - when 'value_single_quote_translated' - "

Mr.BelvedereFanClub

Hello

\n" - when 'value_double_quote_translated' - "

Mr.BelvedereFanClub

Hello

\n" - when 'meta_img_alt_tags_translated' - "\n\n\n\n\n\n

ベルベデアさんファンクラブ

\n

こんにちは

\n\"こんにちは\"\n\n" - when 'meta_img_alt_tags' - "\n\n\n\n\n -

Mr. Belvedere Fan Club

-

Hello

- \"Hello\" - " - when 'a_href_javascript' - "

Mr. Belvedere Fan Club

-

Hello

- " - when 'a_href_javascript_translated' - "

ベルベデアさんファンクラブ

-

こんにちは

- - " - when 'unified_values' - <<-HTML - -
- a b c -
-
- ab -
-
- b c -
- - HTML - - when 'unified_values_ja' - <<-HTML - -
\u3042\u3044\u3046
-
\u200B\u3044\u3046 -
-
- \u3042\u3044\u200B
- - - HTML - - else # "" case - "

Mr. Belvedere Fan Club

-

Hello

- " - end - - body - end - - def generate_dom(param = '') - Wovnrb.to_dom(generate_body(param)) - end - - def generate_values - values = {} - values['text_vals'] = { - 'Hello' => { 'ja' => [{ 'data' => 'こんにちは' }] }, - 'Mr. Belvedere Fan Club' => { 'ja' => [{ 'data' => 'ベルベデアさんファンクラブ' }] } - } - values - end - - def generate_unified_values - { - 'html_text_vals' => { - 'abc' => - { 'ja' => - [{ 'data' => 'あう' }] }, - 'ab' => - { 'ja' => - [{ 'data' => 'う' }] }, - 'bc' => - { 'ja' => - [{ 'data' => 'あ' }] } - - } - } - end - def test_get_code_from_custom_lang store = Store.instance store.settings['custom_lang_aliases'] = { 'ja' => 'staging-ja' } diff --git a/test/lib/services/html_converter_test.rb b/test/lib/services/html_converter_test.rb index 132325bb..73822bc5 100644 --- a/test/lib/services/html_converter_test.rb +++ b/test/lib/services/html_converter_test.rb @@ -25,10 +25,10 @@ def test_build_api_compatible_html_with_custom_lang_param_name def test_build_api_compatible_html_not_fail_for_big_content long_string = 'a' * 60_000 - converter = prepare_html_converter('

' + long_string + '

', supported_langs: %w[en vi]) + converter = prepare_html_converter("

#{long_string}

", supported_langs: %w[en vi]) converted_html, = converter.build_api_compatible_html - expected_html = "

" + long_string + '

' + expected_html = "

#{long_string}

" assert_equal(expected_html, converted_html) end diff --git a/test/lib/text_caches/cache_base_test.rb b/test/lib/text_caches/cache_base_test.rb deleted file mode 100644 index fbbe5525..00000000 --- a/test/lib/text_caches/cache_base_test.rb +++ /dev/null @@ -1,31 +0,0 @@ -require 'wovnrb/text_caches/cache_base' -require 'minitest/autorun' - -class CacheBaseTest < Minitest::Test - def setup - Wovnrb::CacheBase.reset_cache - end - - def test_build - cache = Wovnrb::CacheBase.build({}) - assert_equal('Wovnrb::MemoryCache', cache.class.name) - end - - def test_build_with_invalid_strategy - assert_raises RuntimeError do - Wovnrb::CacheBase.build(strategy: :invalid) - end - end - - def test_set_and_get_single - Wovnrb::CacheBase.set_single({}) - cache = Wovnrb::CacheBase.get_single - assert_equal('Wovnrb::MemoryCache', cache.class.name) - end - - def test_get_single_without_set - assert_raises RuntimeError do - Wovnrb::CacheBase.get_single - end - end -end diff --git a/test/lib/text_caches/memory_cache_test.rb b/test/lib/text_caches/memory_cache_test.rb deleted file mode 100644 index 0646f40f..00000000 --- a/test/lib/text_caches/memory_cache_test.rb +++ /dev/null @@ -1,91 +0,0 @@ -require 'wovnrb/text_caches/cache_base' -require 'wovnrb/text_caches/memory_cache' -require 'minitest/autorun' -require 'timecop' - -class MemoryCacheTest < Minitest::Test - def test_initialize - memory = Wovnrb::MemoryCache.new( - 'cache_megabytes' => 1, - 'ttl_seconds' => 1 - ) - option = memory.options - assert_equal(1.megabytes, option[:size]) - assert_equal(1.megabytes, option[:size]) - assert_equal(1.seconds, option[:expires_in]) - end - - def test_initialize_without_cache_megabytes - memory = Wovnrb::MemoryCache.new( - 'ttl_seconds' => 1 - ) - option = memory.options - assert_equal(200.megabytes, option[:size]) - assert_equal(1.seconds, option[:expires_in]) - end - - def test_initialize_without_ttl - memory = Wovnrb::MemoryCache.new( - 'cache_megabytes' => 1 - ) - option = memory.options - assert_equal(1.megabytes, option[:size]) - assert_equal(300.seconds, option[:expires_in]) - end - - def test_initialize_with_no_config - memory = Wovnrb::MemoryCache.new({}) - option = memory.options - assert_equal(200.megabytes, option[:size]) - assert_equal(300.seconds, option[:expires_in]) - end - - def test_put_without_cache - memory = Wovnrb::MemoryCache.new({}) - memory.put('a', 'b') - assert_equal('b', memory.get('a')) - end - - def test_put_with_cache - memory = Wovnrb::MemoryCache.new({}) - memory.put('a', 'b') - memory.put('a', 'b2') - assert_equal('b2', memory.get('a')) - end - - def test_get_with_cache - memory = Wovnrb::MemoryCache.new({}) - memory.put('a', 'b') - assert_equal('b', memory.get('a')) - end - - def test_get_without_cache - memory = Wovnrb::MemoryCache.new({}) - assert_nil(memory.get('a')) - - end - - def test_get_with_timeout - memory = Wovnrb::MemoryCache.new({}) - memory.put('a', 'b') - Timecop.travel(1.day.since) - assert_nil(memory.get('a')) - end - - def test_get_with_over_memory - # ActiveSupport::Cache::MemoryStore has 240 bytes overhead per instance - memory = Wovnrb::MemoryCache.new( - 'cache_megabytes' => 400.0 / 1000 / 1000 - ) - memory.put('a', 'c') - memory.put('b', 'd') - assert_nil(memory.get('a')) - assert_equal('d', memory.get('b')) - end - - def test_get_with_utf8 - memory = Wovnrb::MemoryCache.new({}) - memory.put('http://www.example.com', 'あいうえお') - assert_equal('あいうえお', memory.get('http://www.example.com')) - end -end diff --git a/test/lib/wovnrb_test.rb b/test/lib/wovnrb_test.rb index 9966f258..7a258da9 100644 --- a/test/lib/wovnrb_test.rb +++ b/test/lib/wovnrb_test.rb @@ -24,7 +24,7 @@ def test_switch_lang '' ].join - assert_switch_lang('en', 'ja', body, expected_body, true) + assert_switch_lang('en', 'ja', body, expected_body, api_expected: true) end def test_switch_lang_with_input_tags @@ -52,14 +52,14 @@ def test_switch_lang_with_input_tags '' ].join - assert_switch_lang('en', 'ja', body, expected_body, true) + assert_switch_lang('en', 'ja', body, expected_body, api_expected: true) end def test_switch_lang_of_html_fragment_with_japanese_translations bodies = ['Hello'].join expected_bodies = ['こんにちは'].join - assert_switch_lang('en', 'ja', bodies, expected_bodies, true) + assert_switch_lang('en', 'ja', bodies, expected_bodies, api_expected: true) end def test_switch_lang_splitted_body @@ -68,7 +68,7 @@ def test_switch_lang_splitted_body ''].join expected_bodies = ["

Mr. Belvedere Fan Club

Hello

"].join - assert_switch_lang('en', 'ja', bodies, expected_bodies, true) + assert_switch_lang('en', 'ja', bodies, expected_bodies, api_expected: true) end def test_switch_lang_of_html_fragment_in_splitted_body @@ -76,7 +76,7 @@ def test_switch_lang_of_html_fragment_in_splitted_body ''].join expected_body = [''].join - assert_switch_lang('en', 'ja', body, expected_body, true) + assert_switch_lang('en', 'ja', body, expected_body, api_expected: true) end def test_switch_lang_missing_values @@ -88,7 +88,7 @@ def test_switch_lang_missing_values " - assert_switch_lang('en', 'ja', body, expected_body, true) + assert_switch_lang('en', 'ja', body, expected_body, api_expected: true) end def test_switch_lang_on_fragment_with_translate_fragment_false @@ -96,7 +96,7 @@ def test_switch_lang_on_fragment_with_translate_fragment_false

Hello

" Wovnrb::Store.instance.settings['translate_fragment'] = false - assert_switch_lang('en', 'ja', body, body, false) + assert_switch_lang('en', 'ja', body, body, api_expected: false) end def test_switch_lang_on_fragment_with_translate_fragment_true @@ -106,7 +106,7 @@ def test_switch_lang_on_fragment_with_translate_fragment_true

こんにちは

" Wovnrb::Store.instance.settings['translate_fragment'] = true - assert_switch_lang('en', 'ja', body, expected_body, true) + assert_switch_lang('en', 'ja', body, expected_body, api_expected: true) end def test_switch_lang_ignores_amp @@ -120,7 +120,7 @@ def test_switch_lang_ignores_amp HTML - assert_switch_lang('en', 'ja', body, body, false) + assert_switch_lang('en', 'ja', body, body, api_expected: false) end def test_switch_lang_ignores_amp_defined_with_symbol_attribute @@ -133,7 +133,7 @@ def test_switch_lang_ignores_amp_defined_with_symbol_attribute HTML - assert_switch_lang('en', 'ja', body, body, false) + assert_switch_lang('en', 'ja', body, body, api_expected: false) end def test_call_without_path_ignored_should_change_environment @@ -249,7 +249,7 @@ def assert_call_affects_env(settings, env, mock_api:, affected:) assert_equal(unaffected_env != app_mock.env, affected) end - def assert_switch_lang(original_lang, target_lang, body, expected_body, api_expected = true) + def assert_switch_lang(original_lang, target_lang, body, expected_body, api_expected: true) subdomain = target_lang == original_lang ? '' : "#{target_lang}." interceptor = Wovnrb::Interceptor.new(get_app) @@ -303,7 +303,7 @@ class RackMock def initialize(opts = {}) @params = {} - if opts.key?(:params) && opts[:params].class == Hash + if opts.key?(:params) && opts[:params].instance_of?(Hash) opts[:params].each do |key, val| @params[key] = val end diff --git a/wovnrb.gemspec b/wovnrb.gemspec index 8ef03fca..c18bb2dd 100644 --- a/wovnrb.gemspec +++ b/wovnrb.gemspec @@ -11,6 +11,7 @@ Gem::Specification.new do |spec| spec.description = 'Ruby gem for WOVN backend on Rack.' spec.homepage = 'https://wovn.io' spec.license = 'MIT' + spec.required_ruby_version = '>= 2.5' files = `git ls-files -z`.split("\x0") files.delete('BEFORE_PUSHING')