diff options
author | Austin Ziegler <austin@zieglers.ca> | 2019-01-26 00:19:23 -0500 |
---|---|---|
committer | Austin Ziegler <austin@zieglers.ca> | 2019-01-26 00:19:23 -0500 |
commit | 35403e85906d4a8c338cb994491726a7dda5cb16 (patch) | |
tree | 146373d31332df271149299b477901b088d08ae0 | |
parent | 677acff04b23868e19b84e0f6a860dbc30a0f79a (diff) | |
download | mime-types-35403e85906d4a8c338cb994491726a7dda5cb16.tar.gz |
Code formatting:
- Clean up the code to be a bit more like what I write.
- Create a .rubocop.yml that disables bone-headed defaults and
sets things I agree with.
-rw-r--r-- | .autotest | 2 | ||||
-rw-r--r-- | .fasterer.yml | 3 | ||||
-rw-r--r-- | .gitignore | 1 | ||||
-rw-r--r-- | .rubocop.yml | 94 | ||||
-rw-r--r-- | Gemfile | 2 | ||||
-rw-r--r-- | Rakefile | 20 | ||||
-rw-r--r-- | lib/mime/type.rb | 42 | ||||
-rw-r--r-- | lib/mime/type/columnar.rb | 2 | ||||
-rw-r--r-- | lib/mime/types.rb | 7 | ||||
-rw-r--r-- | lib/mime/types/_columnar.rb | 3 | ||||
-rw-r--r-- | lib/mime/types/cache.rb | 10 | ||||
-rw-r--r-- | lib/mime/types/container.rb | 16 | ||||
-rw-r--r-- | lib/mime/types/deprecations.rb | 2 | ||||
-rw-r--r-- | lib/mime/types/registry.rb | 14 | ||||
-rw-r--r-- | support/benchmarks/load.rb | 21 | ||||
-rw-r--r-- | support/benchmarks/load_allocations.rb | 45 | ||||
-rw-r--r-- | support/benchmarks/object_counts.rb | 9 | ||||
-rw-r--r-- | test/minitest_helper.rb | 2 | ||||
-rw-r--r-- | test/test_mime_type.rb | 30 | ||||
-rw-r--r-- | test/test_mime_types.rb | 22 | ||||
-rw-r--r-- | test/test_mime_types_cache.rb | 4 | ||||
-rw-r--r-- | test/test_mime_types_class.rb | 20 | ||||
-rw-r--r-- | test/test_mime_types_lazy.rb | 2 | ||||
-rw-r--r-- | test/test_mime_types_loader.rb | 4 |
24 files changed, 227 insertions, 150 deletions
@@ -1,4 +1,4 @@ -# -*- ruby -*- +# frozen_string_literal: true require 'autotest/bundler' require 'autotest/restart' diff --git a/.fasterer.yml b/.fasterer.yml new file mode 100644 index 0000000..34bd766 --- /dev/null +++ b/.fasterer.yml @@ -0,0 +1,3 @@ +exclude_paths: + - lib/mime/types.rb # sort_vs_sort_by on priority_compare + - Rakefile # each_with_index vs while @@ -13,5 +13,4 @@ pkg publish test/cache.tst tmp/ -.rubocop.yml .byebug_history diff --git a/.rubocop.yml b/.rubocop.yml new file mode 100644 index 0000000..8332e59 --- /dev/null +++ b/.rubocop.yml @@ -0,0 +1,94 @@ +--- +AllCops: + DisplayCopNames: true + DisplayStyleGuide: true + ExtraDetails: true + Exclude: + - mime-types.gemspec + +Layout/AlignParameters: + EnforcedStyle: with_fixed_indentation + +Layout/DotPosition: + EnforcedStyle: trailing + +Layout/MultilineMethodCallIndentation: + EnforcedStyle: indented + +Metrics/LineLength: + Max: 100 + +Naming/FileName: + Exclude: + - lib/mime-types.rb + +Naming/MemoizedInstanceVariableName: + Exclude: + - lib/mime/types/registry.rb + +Naming/UncommunicativeMethodParamName: + Exclude: + - lib/mime/types/logger.rb + +Naming/VariableNumber: + Exclude: + - lib/mime/types/logger.rb + +Performance/Caller: + Exclude: + - lib/mime/types/deprecations.rb + +Security/MarshalLoad: + Exclude: + - lib/mime/types/cache.rb + - test/test_mime_types_cache.rb + +Security/YAMLLoad: + Exclude: + - lib/mime/types/loader.rb + +Style/BlockDelimiters: + EnforcedStyle: semantic + ProceduralMethods: + - trace + - assert_raises + - spec + FunctionalMethods: + - let + +Style/PercentLiteralDelimiters: + PreferredDelimiters: + default: () + '%i': '()' + '%I': '()' + '%r': '{}' + '%w': '()' + '%W': '()' + +Style/RescueStandardError: + EnforcedStyle: implicit + +Style/SignalException: + EnforcedStyle: semantic + +Layout/IndentHeredoc: { Enabled: false } +Metrics/AbcSize: { Enabled: false } +Metrics/BlockLength: { Enabled: false } +Metrics/ClassLength: { Enabled: false } +Metrics/CyclomaticComplexity: { Enabled: false } +Metrics/MethodLength: { Enabled: false} +Metrics/PerceivedComplexity: { Enabled: false } +Style/AndOr: { Enabled: false } +Style/AsciiComments: { Enabled: false } +Style/ClassAndModuleChildren: { Enabled: false } +Style/ClassCheck: { Enabled: false } +Style/CommentedKeyword: { Enabled: false } +Style/DoubleNegation: { Enabled: false } +Style/EmptyMethod: { Enabled: false } +Style/ExpandPathArguments: { Enabled: false } +Style/FormatString: { Enabled: false } +Style/FormatStringToken: { Enabled: false } +Style/MultilineBlockChain: { Enabled: false } +Style/SafeNavigation: { Enabled: false } +Style/WordArray: { Enabled: false } +Style/SymbolArray: { Enabled: false } @@ -1,4 +1,4 @@ -# -*- ruby -*- +# frozen_string_literal: true # NOTE: This file is present to keep Travis CI happy. Edits to it will not # be accepted. @@ -1,7 +1,5 @@ # frozen_string_literal: true -# -*- ruby encoding: utf-8 -*- - require 'rubygems' require 'hoe' require 'rake/clean' @@ -37,7 +35,7 @@ spec = Hoe.spec 'mime-types' do extra_dev_deps << ['minitest-bonus-assertions', '~> 3.0'] extra_dev_deps << ['minitest-hooks', '~> 1.4'] extra_dev_deps << ['rake', '>= 10.0', '< 13.0'] - extra_dev_deps << ['fivemat', '~> 1.3' ] + extra_dev_deps << ['fivemat', '~> 1.3'] extra_dev_deps << ['minitest-rg', '~> 5.2'] if Gem::Version.new(RUBY_VERSION) >= Gem::Version.new('2.0') @@ -56,7 +54,7 @@ namespace :benchmark do end desc 'Benchmark Load Times' - task :load, [ :repeats ] => 'benchmark:support' do |_, args| + task :load, [:repeats] => 'benchmark:support' do |_, args| require 'benchmarks/load' Benchmarks::Load.report( File.join(Rake.application.original_dir, 'lib'), @@ -65,7 +63,7 @@ namespace :benchmark do end desc 'Allocation counts' - task :allocations, [ :top_x, :mime_types_only ] => 'benchmark:support' do |_, args| + task :allocations, [:top_x, :mime_types_only] => 'benchmark:support' do |_, args| require 'benchmarks/load_allocations' Benchmarks::LoadAllocations.report( top_x: args.top_x, @@ -74,7 +72,7 @@ namespace :benchmark do end desc 'Columnar allocation counts' - task 'allocations:columnar', [ :top_x, :mime_types_only ] => 'benchmark:support' do |_, args| + task 'allocations:columnar', [:top_x, :mime_types_only] => 'benchmark:support' do |_, args| require 'benchmarks/load_allocations' Benchmarks::LoadAllocations.report( columnar: true, @@ -84,7 +82,7 @@ namespace :benchmark do end desc 'Columnar allocation counts (full load)' - task 'allocations:columnar:full', [ :top_x, :mime_types_only ] => 'benchmark:support' do |_, args| + task 'allocations:columnar:full', [:top_x, :mime_types_only] => 'benchmark:support' do |_, args| require 'benchmarks/load_allocations' Benchmarks::LoadAllocations.report( columnar: true, @@ -131,7 +129,7 @@ namespace :profile do '-R', 'mime/types', '-s', 'self', '-p', 'multi', - '-f', "#{output}", + '-f', output.to_s, script.to_s ] ruby args.join(' ') @@ -194,7 +192,7 @@ namespace :convert do rdoc = name mark = "#{File.basename(name, '.rdoc')}.md" - file mark => [ rdoc, :setup ] do |t| + file mark => [rdoc, :setup] do |t| puts "#{rdoc} => #{mark}" File.open(t.name, 'wb') { |target| target.write @doc_converter.convert(IO.read(t.prerequisites.first)) @@ -203,7 +201,7 @@ namespace :convert do CLEAN.add mark - task run: [ mark ] + task run: [mark] end end @@ -211,7 +209,7 @@ namespace :convert do task docs: 'convert:docs:run' end -task 'deps:top', [ :number ] do |_, args| +task 'deps:top', [:number] do |_, args| require 'net/http' require 'json' diff --git a/lib/mime/type.rb b/lib/mime/type.rb index 11b8530..5a05a91 100644 --- a/lib/mime/type.rb +++ b/lib/mime/type.rb @@ -64,10 +64,10 @@ class MIME::Type # :stopdoc: # TODO verify mime-type character restrictions; I am pretty sure that this is # too wide open. - MEDIA_TYPE_RE = %r{([-\w.+]+)/([-\w.+]*)} - I18N_RE = %r{[^[:alnum:]]} - BINARY_ENCODINGS = %w(base64 8bit) - ASCII_ENCODINGS = %w(7bit quoted-printable) + MEDIA_TYPE_RE = %r{([-\w.+]+)/([-\w.+]*)}.freeze + I18N_RE = /[^[:alnum:]]/.freeze + BINARY_ENCODINGS = %w(base64 8bit).freeze + ASCII_ENCODINGS = %w(7bit quoted-printable).freeze # :startdoc: private_constant :MEDIA_TYPE_RE, :I18N_RE, :BINARY_ENCODINGS, @@ -301,7 +301,7 @@ class MIME::Type # Returns the default encoding for the MIME::Type based on the media type. def default_encoding - (@media_type == 'text') ? 'quoted-printable' : 'base64' + @media_type == 'text' ? 'quoted-printable' : 'base64' end ## @@ -321,7 +321,7 @@ class MIME::Type # Returns +true+ if the media type is obsolete. attr_accessor :obsolete - alias_method :obsolete?, :obsolete + alias obsolete? obsolete # The documentation for this MIME::Type. attr_accessor :docs @@ -367,8 +367,8 @@ class MIME::Type attr_reader :xrefs ## - def xrefs=(x) # :nodoc: - @xrefs = MIME::Types::Container.new(x) + def xrefs=(xrefs) # :nodoc: + @xrefs = MIME::Types::Container.new(xrefs) end # The decoded cross-reference URL list for this MIME::Type. @@ -381,7 +381,7 @@ class MIME::Type # Indicates whether the MIME type has been registered with IANA. attr_accessor :registered - alias_method :registered?, :registered + alias registered? registered # MIME types can be specified to be sent across a network in particular # formats. This method returns +true+ when the MIME::Type encoding is set @@ -399,7 +399,7 @@ class MIME::Type # Indicateswhether the MIME type is declared as a signature type. attr_accessor :signature - alias_method :signature?, :signature + alias signature? signature # Returns +true+ if the MIME::Type specifies an extension list, # indicating that it is a complete MIME::Type. @@ -438,17 +438,15 @@ class MIME::Type # # This method should be considered a private implementation detail. def encode_with(coder) - coder['content-type'] = @content_type - coder['docs'] = @docs unless @docs.nil? or @docs.empty? - unless @friendly.nil? or @friendly.empty? - coder['friendly'] = @friendly - end - coder['encoding'] = @encoding - coder['extensions'] = @extensions.to_a unless @extensions.empty? + coder['content-type'] = @content_type + coder['docs'] = @docs unless @docs.nil? or @docs.empty? + coder['friendly'] = @friendly unless @friendly.nil? or @friendly.empty? + coder['encoding'] = @encoding + coder['extensions'] = @extensions.to_a unless @extensions.empty? coder['preferred-extension'] = @preferred_extension if @preferred_extension if obsolete? - coder['obsolete'] = obsolete? - coder['use-instead'] = use_instead if use_instead + coder['obsolete'] = obsolete? + coder['use-instead'] = use_instead if use_instead end unless xrefs.empty? {}.tap do |hash| @@ -458,8 +456,8 @@ class MIME::Type coder['xrefs'] = hash end end - coder['registered'] = registered? - coder['signature'] = signature? if signature? + coder['registered'] = registered? + coder['signature'] = signature? if signature? coder end @@ -526,7 +524,7 @@ class MIME::Type matchdata.captures.map { |e| e.downcase! - e.sub!(%r{^x-}, '') if remove_x + e.sub!(/^x-/, '') if remove_x yield e if block_given? e }.join(joiner) diff --git a/lib/mime/type/columnar.rb b/lib/mime/type/columnar.rb index 357470f..b5bb263 100644 --- a/lib/mime/type/columnar.rb +++ b/lib/mime/type/columnar.rb @@ -20,7 +20,7 @@ class MIME::Type::Columnar < MIME::Type end def self.column(*methods, file: nil) # :nodoc: - file = methods.first unless file + file ||= methods.first file_method = :"load_#{file}" methods.each do |m| diff --git a/lib/mime/types.rb b/lib/mime/types.rb index 9cfd52e..26ff937 100644 --- a/lib/mime/types.rb +++ b/lib/mime/types.rb @@ -155,7 +155,7 @@ class MIME::Types a.priority_compare(b) } end - alias_method :of, :type_for + alias of type_for # Add one or more MIME::Type objects to the set of known types. If the # type is already known, a warning will be displayed. @@ -185,9 +185,9 @@ class MIME::Types # truthy value to suppress that warning. def add_type(type, quiet = false) if !quiet and @type_variants[type.simplified].include?(type) - MIME::Types.logger.warn <<-warning + MIME::Types.logger.warn <<-WARNING Type #{type} is already registered as a variant of #{type.simplified}. - warning + WARNING end add_type_variant!(type) @@ -202,6 +202,7 @@ Type #{type} is already registered as a variant of #{type.simplified}. def reindex_extensions!(mime_type) return unless @type_variants[mime_type.simplified].include?(mime_type) + index_extensions!(mime_type) end diff --git a/lib/mime/types/_columnar.rb b/lib/mime/types/_columnar.rb index 8938f19..f68bf61 100644 --- a/lib/mime/types/_columnar.rb +++ b/lib/mime/types/_columnar.rb @@ -63,7 +63,6 @@ module MIME::Types::Columnar def load_encoding each_file_line('encoding') do |type, line| pool ||= {} - line type.instance_variable_set(:@encoding, (pool[line] ||= line)) end end @@ -132,6 +131,6 @@ module MIME::Types::Columnar end def flag(line) - line == '1' ? true : false + line == '1' end end diff --git a/lib/mime/types/cache.rb b/lib/mime/types/cache.rb index cea282f..e3d69c8 100644 --- a/lib/mime/types/cache.rb +++ b/lib/mime/types/cache.rb @@ -22,16 +22,16 @@ class << MIME::Types::Cache if cache.version == MIME::Types::Data::VERSION Marshal.load(cache.data) else - MIME::Types.logger.warn <<-warning.chomp + MIME::Types.logger.warn <<-WARNING.chomp Could not load MIME::Types cache: invalid version - warning + WARNING nil end rescue => e - MIME::Types.logger.warn <<-warning.chomp + MIME::Types.logger.warn <<-WARNING.chomp Could not load MIME::Types cache: #{e} - warning - return nil + WARNING + nil end # Attempts to save the types provided to the cache file provided. diff --git a/lib/mime/types/container.rb b/lib/mime/types/container.rb index 5dc1da3..53c107b 100644 --- a/lib/mime/types/container.rb +++ b/lib/mime/types/container.rb @@ -22,12 +22,13 @@ class MIME::Types::Container #:nodoc: end def []=(key, value) - case value - when Set - container[key] = value - else - container[key] = Set[*value] - end + container[key] = + case value + when Set + value + else + Set[*value] + end end def merge(other) @@ -37,7 +38,7 @@ class MIME::Types::Container #:nodoc: def merge!(other) tap { other = other.kind_of?(MIME::Types::Container) ? other.container : other - self.container.merge!(other) + container.merge!(other) normalize } end @@ -85,6 +86,7 @@ class MIME::Types::Container #:nodoc: def normalize container.each do |k, v| next if v.kind_of?(Set) + container[k] = Set[*v] end end diff --git a/lib/mime/types/deprecations.rb b/lib/mime/types/deprecations.rb index e0e6573..9de2946 100644 --- a/lib/mime/types/deprecations.rb +++ b/lib/mime/types/deprecations.rb @@ -1,7 +1,5 @@ # frozen_string_literal: true -# -*- ruby encoding: utf-8 -*- - require 'mime/types/logger' # The namespace for MIME applications, tools, and libraries. diff --git a/lib/mime/types/registry.rb b/lib/mime/types/registry.rb index fa58618..5c8e5c7 100644 --- a/lib/mime/types/registry.rb +++ b/lib/mime/types/registry.rb @@ -33,7 +33,7 @@ class << MIME::Types def type_for(filename) __types__.type_for(filename) end - alias_method :of, :type_for + alias of type_for # MIME::Types#add against the default MIME::Types registry. def add(*types) @@ -43,13 +43,13 @@ class << MIME::Types private def lazy_load? - if ENV.key?('RUBY_MIME_TYPES_LAZY_LOAD') - MIME::Types.logger.warn <<-WARNING.chomp + return unless ENV.key?('RUBY_MIME_TYPES_LAZY_LOAD') + + MIME::Types.logger.warn <<-WARNING.chomp Lazy loading ($RUBY_MIME_TYPES_LAZY_LOAD) is deprecated and will be removed. - WARNING + WARNING - (lazy = ENV['RUBY_MIME_TYPES_LAZY_LOAD']) && (lazy != 'false') - end + (lazy = ENV['RUBY_MIME_TYPES_LAZY_LOAD']) && (lazy != 'false') end def __types__ @@ -63,7 +63,7 @@ Lazy loading ($RUBY_MIME_TYPES_LAZY_LOAD) is deprecated and will be removed. end def load_default_mime_types(mode = load_mode) - if @__types__ = MIME::Types::Cache.load + if (@__types__ = MIME::Types::Cache.load) __instances__.add(@__types__) else @__types__ = MIME::Types::Loader.load(mode) diff --git a/support/benchmarks/load.rb b/support/benchmarks/load.rb index b222987..0712676 100644 --- a/support/benchmarks/load.rb +++ b/support/benchmarks/load.rb @@ -1,11 +1,10 @@ # frozen_string_literal: true -# -*- ruby encoding: utf-8 -*- - require 'benchmark' require 'mime/types' module Benchmarks + # Benchmark loading speed class Load def self.report(load_path, repeats) new(load_path, repeats.to_i).report @@ -35,23 +34,23 @@ module Benchmarks remove_cache Benchmark.bm(30) do |mark| - mark.report('Normal') { reload_mime_types(@repeats) } - mark.report('Columnar') { + mark.report('Normal') do reload_mime_types(@repeats) end + mark.report('Columnar') do reload_mime_types(@repeats, columnar: true) - } - mark.report('Columnar Full') { + end + mark.report('Columnar Full') do reload_mime_types(@repeats, columnar: true, force: true) - } + end ENV['RUBY_MIME_TYPES_CACHE'] = @cache_file - mark.report('Cache Initialize') { reload_mime_types(cache: true) } - mark.report('Cached') { reload_mime_types(@repeats, cache: true) } + mark.report('Cache Initialize') do reload_mime_types(cache: true) end + mark.report('Cached') do reload_mime_types(@repeats, cache: true) end remove_cache ENV['RUBY_MIME_TYPES_CACHE'] = @cache_file - mark.report('Columnar Cache Initialize') { + mark.report('Columnar Cache Initialize') do reload_mime_types(columnar: true, cache: true) - } + end mark.report('Columnar Cached') { reload_mime_types(@repeats, columnar: true, cache: true) } diff --git a/support/benchmarks/load_allocations.rb b/support/benchmarks/load_allocations.rb index 3c93aaf..f97aa70 100644 --- a/support/benchmarks/load_allocations.rb +++ b/support/benchmarks/load_allocations.rb @@ -1,20 +1,19 @@ # frozen_string_literal: true -# -*- ruby encoding: utf-8 -*- - if RUBY_VERSION < '2.1' - $stderr.puts "Cannot count allocations on #{RUBY_VERSION}." + warn "Cannot count allocations on #{RUBY_VERSION}." exit 1 end begin require 'allocation_tracer' rescue LoadError - $stderr.puts "Allocation tracking requires the gem 'allocation_tracer'." + warn "Allocation tracking requires the gem 'allocation_tracer'." exit 1 end module Benchmarks + # Calculate the number of allocations during loading. class LoadAllocations def self.report(columnar: false, full: false, top_x: nil, mime_types_only: false) new( @@ -33,6 +32,7 @@ module Benchmarks @top_x = top_x return unless @top_x + @top_x = top_x.to_i @top_x = 10 if @top_x <= 0 end @@ -49,41 +49,44 @@ module Benchmarks table = @allocations.sort_by { |_, v| v.first }.reverse.first(@top_x) table.map! { |(location, allocs)| next if @mime_types_only and location.first !~ %r{mime-types/lib} - [ location.join(':').gsub(%r{^#{Dir.pwd}/}, ''), *allocs ] + + [location.join(':').gsub(%r{^#{Dir.pwd}/}, ''), *allocs] }.compact! - head = (ObjectSpace::AllocationTracer.header - [ :line ]).map {|h| + head = (ObjectSpace::AllocationTracer.header - [:line]).map { |h| h.to_s.split(/_/).map(&:capitalize).join(' ') } table.unshift head - max_widths = [].tap do |mw| + max_widths = [].tap { |mw| table.map { |row| row.lazy.map(&:to_s).map(&:length).to_a }.tap do |w| w.first.each_index do |i| mw << w.lazy.map { |r| r[i] }.max end end - end + } - pattern = [ '%%-%ds' ] - pattern << ([ '%% %ds' ] * (max_widths.length - 1)) + pattern = ['%%-%ds'] + pattern << (['%% %ds'] * (max_widths.length - 1)) pattern = pattern.join("\t") % max_widths - table.each { |row| puts pattern % row } + table.each do |row| + puts pattern % row + end puts end def collect - if @columnar - @allocations = ObjectSpace::AllocationTracer.trace do - require 'mime/types' - - MIME::Types.first.to_h if @full - end - else - @allocations = ObjectSpace::AllocationTracer.trace do - require 'mime/types/full' + @allocations = + if @columnar + ObjectSpace::AllocationTracer.trace do + require 'mime/types' + MIME::Types.first.to_h if @full + end + else + ObjectSpace::AllocationTracer.trace do + require 'mime/types/full' + end end - end @count = ObjectSpace::AllocationTracer.allocated_count_table.values. inject(:+) diff --git a/support/benchmarks/object_counts.rb b/support/benchmarks/object_counts.rb index 2f7569b..d548bd1 100644 --- a/support/benchmarks/object_counts.rb +++ b/support/benchmarks/object_counts.rb @@ -1,11 +1,10 @@ # frozen_string_literal: true -# -*- ruby encoding: utf-8 -*- - module Benchmarks + # Benchmark object counts class ObjectCounts def self.report(columnar: false, full: false) - new(columnar: columnar).report + new(columnar: columnar, full: full).report end def initialize(columnar: false, full: false) @@ -16,9 +15,9 @@ module Benchmarks def report collect @before.keys.grep(/T_/).map { |key| - [ key, @after[key] - @before[key] ] + [key, @after[key] - @before[key]] }.sort_by { |_, delta| -delta }.each { |key, delta| - puts '%10s +%6d' % [ key, delta ] + puts '%10s +%6d' % [key, delta] } end diff --git a/test/minitest_helper.rb b/test/minitest_helper.rb index 40b1ef8..27c3913 100644 --- a/test/minitest_helper.rb +++ b/test/minitest_helper.rb @@ -1,7 +1,5 @@ # frozen_string_literal: true -# -*- ruby encoding: utf-8 -*- - require 'mime/type' require 'fileutils' diff --git a/test/test_mime_type.rb b/test/test_mime_type.rb index 70690c7..feae3f9 100644 --- a/test/test_mime_type.rb +++ b/test/test_mime_type.rb @@ -1,13 +1,9 @@ # frozen_string_literal: true -# -*- ruby encoding: utf-8 -*- - require 'mime/types' require 'minitest_helper' describe MIME::Type do - # it { fail } - def mime_type(content_type) MIME::Type.new(content_type) { |mt| yield mt if block_given? } end @@ -333,8 +329,8 @@ describe MIME::Type do end it 'sorts (2) based on extensions' do - text_1.extensions = ["foo", "bar"] - text_2.extensions = ["foo"] + text_1.extensions = ['foo', 'bar'] + text_2.extensions = ['foo'] assert_priority_same text_1, text_2 @@ -476,7 +472,7 @@ describe MIME::Type do describe '#to_s, #to_str' do it 'represents itself as a string of the canonical content_type' do - assert_equal 'text/plain', "#{text_plain}" + assert_equal 'text/plain', "#{text_plain}" # rubocop:disable Style/UnneededInterpolation end it 'acts like a string of the canonical content_type for comparison' do @@ -490,7 +486,7 @@ describe MIME::Type do describe '#xrefs, #xrefs=' do let(:expected) { - MIME::Types::Container.new({ 'rfc' => Set[*%w(rfc1234 rfc5678)] }) + MIME::Types::Container.new('rfc' => Set['rfc1234', 'rfc5678']) } it 'returns the expected results' do @@ -518,13 +514,13 @@ describe MIME::Type do let(:type) { mime_type('a/b').tap do |t| t.xrefs = { - 'draft' => [ 'RFC1' ], - 'template' => [ 'a/b' ], - 'person' => [ 'p-1' ], - 'rfc' => [ 'rfc-1' ], - 'rfc-errata' => [ 'err-1' ], - 'uri' => [ 'http://example.org' ], - 'text' => [ 'text' ] + 'draft' => ['RFC1'], + 'template' => ['a/b'], + 'person' => ['p-1'], + 'rfc' => ['rfc-1'], + 'rfc-errata' => ['err-1'], + 'uri' => ['http://example.org'], + 'text' => ['text'] } end } @@ -588,10 +584,10 @@ describe MIME::Type do it 'merges new values from an array parameter' do expected = { 'en' => 'Text files' } - assert_equal expected, text_plain.friendly([ 'en', 'Text files' ]) + assert_equal expected, text_plain.friendly(['en', 'Text files']) expected.update('fr' => 'des fichiers texte') assert_equal expected, - text_plain.friendly([ 'fr', 'des fichiers texte' ]) + text_plain.friendly(['fr', 'des fichiers texte']) end it 'merges new values from a hash parameter' do diff --git a/test/test_mime_types.rb b/test/test_mime_types.rb index f4528a3..f8b1a0b 100644 --- a/test/test_mime_types.rb +++ b/test/test_mime_types.rb @@ -1,13 +1,11 @@ # frozen_string_literal: true -# -*- ruby encoding: utf-8 -*- - require 'mime/types' require 'minitest_helper' describe MIME::Types do def mime_types - @mime_types ||= MIME::Types.new.tap do |mt| + @mime_types ||= MIME::Types.new.tap { |mt| mt.add MIME::Type.new(['text/plain', %w(txt)]), MIME::Type.new(['image/jpeg', %w(jpg jpeg)]), MIME::Type.new('application/x-wordperfect6.1'), @@ -21,12 +19,12 @@ describe MIME::Types do 'extensions' => 'gz', 'registered' => true ) - end + } end describe 'is enumerable' do it 'correctly uses an Enumerable method like #any?' do - assert mime_types.any? { |type| type.content_type == 'text/plain' } + assert(mime_types.any? { |type| type.content_type == 'text/plain' }) end it 'implements each with no parameters to return an Enumerator' do @@ -95,11 +93,11 @@ describe MIME::Types do describe '#add' do let(:eruby) { MIME::Type.new('application/x-eruby') } - let(:jinja) { MIME::Type.new('application/jinja2' )} + let(:jinja) { MIME::Type.new('application/jinja2') } it 'successfully adds a new type' do mime_types.add(eruby) - assert_equal mime_types['application/x-eruby'], [ eruby ] + assert_equal mime_types['application/x-eruby'], [eruby] end it 'complains about adding a duplicate type' do @@ -115,13 +113,13 @@ describe MIME::Types do assert_output '', '' do mime_types.add(eruby, :silent) end - assert_equal mime_types['application/x-eruby'], [ eruby ] + assert_equal mime_types['application/x-eruby'], [eruby] end it 'successfully adds from an array' do - mime_types.add([ eruby, jinja ]) - assert_equal mime_types['application/x-eruby'], [ eruby ] - assert_equal mime_types['application/jinja2'], [ jinja ] + mime_types.add([eruby, jinja]) + assert_equal mime_types['application/x-eruby'], [eruby] + assert_equal mime_types['application/jinja2'], [jinja] end it 'successfully adds from another MIME::Types' do @@ -130,7 +128,7 @@ describe MIME::Types do assert_equal mime_types.count, mt.count mime_types.each do |type| - assert_equal mt[type.content_type], [ type ] + assert_equal mt[type.content_type], [type] end end end diff --git a/test/test_mime_types_cache.rb b/test/test_mime_types_cache.rb index 2209cab..b56526f 100644 --- a/test/test_mime_types_cache.rb +++ b/test/test_mime_types_cache.rb @@ -1,7 +1,5 @@ # frozen_string_literal: true -# -*- ruby encoding: utf-8 -*- - require 'mime/types' require 'minitest_helper' @@ -69,7 +67,7 @@ describe MIME::Types::Cache do it 'outputs an error when there is a marshal file incompatibility' do MIME::Types::Cache.save data = File.binread(@cache_file).reverse - File.open(@cache_file, 'wb') { |f| f.write(data) } + File.open(@cache_file, 'wb') do |f| f.write(data) end MIME::Types.instance_variable_set(:@__types__, nil) assert_output '', /incompatible marshal file format/ do MIME::Types['text/html'] diff --git a/test/test_mime_types_class.rb b/test/test_mime_types_class.rb index 578341e..418678a 100644 --- a/test/test_mime_types_class.rb +++ b/test/test_mime_types_class.rb @@ -1,7 +1,5 @@ # frozen_string_literal: true -# -*- ruby encoding: utf-8 -*- - require 'mime/types' require 'minitest_helper' @@ -12,7 +10,7 @@ describe MIME::Types, 'registry' do describe 'is enumerable' do it 'correctly uses an Enumerable method like #any?' do - assert MIME::Types.any? { |type| type.content_type == 'text/plain' } + assert(MIME::Types.any? { |type| type.content_type == 'text/plain' }) end it 'implements each with no parameters to return an Enumerator' do @@ -45,7 +43,7 @@ describe MIME::Types, 'registry' do it 'sorts by priority with multiple matches' do types = MIME::Types[/gzip$/].select { |t| - t == 'application/gzip' || t == 'application/x-gzip' || t == 'multipart/x-gzip' + %w(application/gzip application/x-gzip multipart/x-gzip).include?(t) } # This is this way because of a new type ending with gzip that only # appears in some data files. @@ -117,11 +115,11 @@ describe MIME::Types, 'registry' do end let(:eruby) { MIME::Type.new('application/x-eruby') } - let(:jinja) { MIME::Type.new('application/jinja2' )} + let(:jinja) { MIME::Type.new('application/jinja2') } it 'successfully adds a new type' do MIME::Types.add(eruby) - assert_equal MIME::Types['application/x-eruby'], [ eruby ] + assert_equal MIME::Types['application/x-eruby'], [eruby] end it 'complains about adding a duplicate type' do @@ -137,13 +135,13 @@ describe MIME::Types, 'registry' do assert_silent do MIME::Types.add(eruby, :silent) end - assert_equal MIME::Types['application/x-eruby'], [ eruby ] + assert_equal MIME::Types['application/x-eruby'], [eruby] end it 'successfully adds from an array' do - MIME::Types.add([ eruby, jinja ]) - assert_equal MIME::Types['application/x-eruby'], [ eruby ] - assert_equal MIME::Types['application/jinja2'], [ jinja ] + MIME::Types.add([eruby, jinja]) + assert_equal MIME::Types['application/x-eruby'], [eruby] + assert_equal MIME::Types['application/jinja2'], [jinja] end it 'successfully adds from another MIME::Types' do @@ -155,7 +153,7 @@ describe MIME::Types, 'registry' do MIME::Types.add(mt) assert_equal old_count + 1, MIME::Types.count - assert_equal MIME::Types[eruby.content_type], [ eruby ] + assert_equal MIME::Types[eruby.content_type], [eruby] end end end diff --git a/test/test_mime_types_lazy.rb b/test/test_mime_types_lazy.rb index e730ba1..12233c0 100644 --- a/test/test_mime_types_lazy.rb +++ b/test/test_mime_types_lazy.rb @@ -1,7 +1,5 @@ # frozen_string_literal: true -# -*- ruby encoding: utf-8 -*- - require 'mime/types' require 'minitest_helper' diff --git a/test/test_mime_types_loader.rb b/test/test_mime_types_loader.rb index 2515f81..3f3206a 100644 --- a/test/test_mime_types_loader.rb +++ b/test/test_mime_types_loader.rb @@ -1,7 +1,5 @@ # frozen_string_literal: true -# -*- ruby encoding: utf-8 -*- - require 'mime/types' require 'minitest_helper' @@ -18,7 +16,7 @@ describe MIME::Types::Loader do refute(types['audio/webm'].first.registered?) assert_equal('Fixes a bug with IE6 and progressive JPEGs', - types['image/pjpeg'].first.docs) + types['image/pjpeg'].first.docs) assert(types['audio/vnd.qcelp'].first.obsolete?) assert_equal('audio/QCELP', types['audio/vnd.qcelp'].first.use_instead) |