From aa2afb68a78fed7a71a6d574586202a0377eba25 Mon Sep 17 00:00:00 2001 From: Lamont Granquist Date: Thu, 9 Jul 2015 13:51:28 -0700 Subject: yet more cops --- .rubocop.yml | 4 +- Gemfile | 4 +- Rakefile | 12 +++--- lib/ffi_yajl/benchmark/encode_profile.rb | 24 ++++++------ lib/ffi_yajl/benchmark/http.rb | 2 +- lib/ffi_yajl/benchmark/parse.rb | 2 +- lib/ffi_yajl/benchmark/parse_json_and_marshal.rb | 2 +- lib/ffi_yajl/benchmark/parse_json_and_yaml.rb | 2 +- lib/ffi_yajl/benchmark/parse_stream.rb | 2 +- lib/ffi_yajl/encoder.rb | 4 +- lib/ffi_yajl/ffi/encoder.rb | 4 +- spec/ffi_yajl/encoder_spec.rb | 8 ++-- spec/ffi_yajl/parser_spec.rb | 48 ++++++++++++------------ spec/spec_helper.rb | 4 +- 14 files changed, 62 insertions(+), 60 deletions(-) diff --git a/.rubocop.yml b/.rubocop.yml index ad55721..557947e 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -2,6 +2,8 @@ AbcSize: Enabled: false AndOr: Enabled: false +Lint/AssignmentInCondition: + Enabled: false ClassAndModuleCamelCase: Enabled: false ClassLength: @@ -23,7 +25,7 @@ Style/FileName: - bin/ffi-yajl-bench FormatString: Enabled: false -HashSyntax: +Lint/HandleExceptions: Enabled: false LineLength: Enabled: false diff --git a/Gemfile b/Gemfile index 3298262..b6963ac 100644 --- a/Gemfile +++ b/Gemfile @@ -1,6 +1,6 @@ source "https://rubygems.org" -gemspec :name => "ffi-yajl" +gemspec name: "ffi-yajl" platforms :rbx do gem 'rubysl', '~> 2.0' @@ -8,7 +8,7 @@ end group :development do # for testing loading concurrently with yajl-ruby, not on jruby - gem 'yajl-ruby', :platforms => [ :ruby, :mswin, :mingw ] + gem 'yajl-ruby', platforms: [ :ruby, :mswin, :mingw ] end group :development_extras do diff --git a/Rakefile b/Rakefile index e70a915..983f049 100644 --- a/Rakefile +++ b/Rakefile @@ -11,7 +11,7 @@ Dir[File.expand_path("../*gemspec", __FILE__)].reverse_each do |gemspec_path| end desc "Build it and ship it" -task :ship => [:clean, :gem] do +task ship: [:clean, :gem] do sh("git tag #{FFI_Yajl::VERSION}") sh("git push --tags") Dir[File.expand_path("../pkg/*.gem", __FILE__)].reverse_each do |built_gem| @@ -27,7 +27,7 @@ task :clean do end desc "install the gem locally" -task :install => [:package] do +task install: [:package] do if defined?(RUBY_ENGINE) && RUBY_ENGINE == "jruby" sh %{gem install pkg/#{unix_gemspec.name}-#{unix_gemspec.version}-universal-java.gem} else @@ -162,12 +162,12 @@ else end desc 'Run all style checks' -task :style => ['style:rubocop', 'style:reek'] +task style: ['style:rubocop', 'style:reek'] desc 'Run style + spec tests by default on travis' -task :travis => %w{style spec} +task travis: %w{style spec} desc 'Run style, spec and test kichen on travis' -task :travis_all => ['style', 'spec', 'integration:cloud'] +task travis_all: ['style', 'spec', 'integration:cloud'] -task :default => ['style', 'spec', 'integration:vagrant'] +task default: ['style', 'spec', 'integration:vagrant'] diff --git a/lib/ffi_yajl/benchmark/encode_profile.rb b/lib/ffi_yajl/benchmark/encode_profile.rb index 6158508..2cf19f1 100644 --- a/lib/ffi_yajl/benchmark/encode_profile.rb +++ b/lib/ffi_yajl/benchmark/encode_profile.rb @@ -15,21 +15,21 @@ module FFI_Yajl class Benchmark class EncodeProfile def run - if defined?(PerfTools) - filename = File.expand_path(File.join(File.dirname(__FILE__), "subjects", "ohai.json")) - hash = File.open(filename, 'rb') { |f| FFI_Yajl::Parser.parse(f.read) } + return unless defined?(PerfTools) - times = 1000 - puts "Starting profiling encoding #{filename} #{times} times\n\n" + filename = File.expand_path(File.join(File.dirname(__FILE__), "subjects", "ohai.json")) + hash = File.open(filename, 'rb') { |f| FFI_Yajl::Parser.parse(f.read) } - ffi_string_encoder = FFI_Yajl::Encoder.new - PerfTools::CpuProfiler.start("/tmp/ffi_yajl_encode_profile.out") do - times.times { - ffi_string_encoder.encode(hash) - } - end - system("pprof.rb --text /tmp/ffi_yajl_encode_profile.out") + times = 1000 + puts "Starting profiling encoding #{filename} #{times} times\n\n" + + ffi_string_encoder = FFI_Yajl::Encoder.new + PerfTools::CpuProfiler.start("/tmp/ffi_yajl_encode_profile.out") do + times.times { + ffi_string_encoder.encode(hash) + } end + system("pprof.rb --text /tmp/ffi_yajl_encode_profile.out") end end end diff --git a/lib/ffi_yajl/benchmark/http.rb b/lib/ffi_yajl/benchmark/http.rb index 4c61df5..771e452 100644 --- a/lib/ffi_yajl/benchmark/http.rb +++ b/lib/ffi_yajl/benchmark/http.rb @@ -26,7 +26,7 @@ Benchmark.bmbm { |x| x.report { puts "JSON.parser" times.times { - JSON.parse(Net::HTTP.get_response(uri).body, :max_nesting => false) + JSON.parse(Net::HTTP.get_response(uri).body, max_nesting: false) } } } diff --git a/lib/ffi_yajl/benchmark/parse.rb b/lib/ffi_yajl/benchmark/parse.rb index 0625e33..7e602fa 100644 --- a/lib/ffi_yajl/benchmark/parse.rb +++ b/lib/ffi_yajl/benchmark/parse.rb @@ -92,7 +92,7 @@ class FFI_Yajl::Benchmark::Parse puts "JSON.parse" times.times { json.rewind - JSON.parse(json.read, :max_nesting => false) + JSON.parse(json.read, max_nesting: false) } } end diff --git a/lib/ffi_yajl/benchmark/parse_json_and_marshal.rb b/lib/ffi_yajl/benchmark/parse_json_and_marshal.rb index 163f559..204ab06 100644 --- a/lib/ffi_yajl/benchmark/parse_json_and_marshal.rb +++ b/lib/ffi_yajl/benchmark/parse_json_and_marshal.rb @@ -34,7 +34,7 @@ Benchmark.bmbm { |x| puts "JSON.parse" times.times { json.rewind - JSON.parse(json.read, :max_nesting => false) + JSON.parse(json.read, max_nesting: false) } } end diff --git a/lib/ffi_yajl/benchmark/parse_json_and_yaml.rb b/lib/ffi_yajl/benchmark/parse_json_and_yaml.rb index 4e96e02..5076f67 100644 --- a/lib/ffi_yajl/benchmark/parse_json_and_yaml.rb +++ b/lib/ffi_yajl/benchmark/parse_json_and_yaml.rb @@ -31,7 +31,7 @@ Benchmark.bmbm { |x| puts "JSON.parse" times.times { json.rewind - JSON.parse(json.read, :max_nesting => false) + JSON.parse(json.read, max_nesting: false) } } end diff --git a/lib/ffi_yajl/benchmark/parse_stream.rb b/lib/ffi_yajl/benchmark/parse_stream.rb index 955f22f..acf776a 100644 --- a/lib/ffi_yajl/benchmark/parse_stream.rb +++ b/lib/ffi_yajl/benchmark/parse_stream.rb @@ -34,7 +34,7 @@ Benchmark.bmbm { |x| times.times { json.rewind while chunk = json.gets - JSON.parse(chunk, :max_nesting => false) + JSON.parse(chunk, max_nesting: false) end } } diff --git a/lib/ffi_yajl/encoder.rb b/lib/ffi_yajl/encoder.rb index 9f2e564..f4dd8c8 100644 --- a/lib/ffi_yajl/encoder.rb +++ b/lib/ffi_yajl/encoder.rb @@ -41,7 +41,7 @@ module FFI_Yajl # call either the ext or ffi hook str = do_yajl_encode(obj, yajl_gen_opts, opts) # we can skip cleaning the whole string for utf-8 issues if we have yajl validate as we go - str.encode!("utf-8", "binary", :undef => :replace) unless yajl_gen_opts[:yajl_gen_validate_utf8] + str.encode!("utf-8", "binary", undef: :replace) unless yajl_gen_opts[:yajl_gen_validate_utf8] str end @@ -56,7 +56,7 @@ module FFI_Yajl def self.raise_error_for_status(status, token = nil) # scrub token to valid utf-8 since we may be issuing an exception on an invalid utf-8 token - token = token.to_s.encode("utf-8", "binary", :undef => :replace) + token = token.to_s.encode("utf-8", "binary", undef: :replace) case status when 1 # yajl_gen_keys_must_be_strings raise FFI_Yajl::EncodeError, "YAJL internal error: attempted use of non-string object as key" diff --git a/lib/ffi_yajl/ffi/encoder.rb b/lib/ffi_yajl/ffi/encoder.rb index 250ed68..51a3c7a 100644 --- a/lib/ffi_yajl/ffi/encoder.rb +++ b/lib/ffi_yajl/ffi/encoder.rb @@ -41,8 +41,8 @@ module FFI_Yajl # setup our own state state = { - :json_opts => opts, - :processing_key => false, + json_opts: opts, + processing_key: false, } # do the encoding diff --git a/spec/ffi_yajl/encoder_spec.rb b/spec/ffi_yajl/encoder_spec.rb index d8ee707..91009f4 100644 --- a/spec/ffi_yajl/encoder_spec.rb +++ b/spec/ffi_yajl/encoder_spec.rb @@ -29,12 +29,12 @@ describe "FFI_Yajl::Encoder" do let(:encoder) { FFI_Yajl::Encoder.new(options) } - it "encodes hashes in keys as strings", :ruby_gte_193 => true do + it "encodes hashes in keys as strings", ruby_gte_193: true do ruby = { { 'a' => 'b' } => 2 } expect(encoder.encode(ruby)).to eq('{"{\"a\"=>\"b\"}":2}') end - it "encodes arrays in keys as strings", :ruby_gte_193 => true do + it "encodes arrays in keys as strings", ruby_gte_193: true do ruby = { [0, 1] => 2 } expect(encoder.encode(ruby)).to eq('{"[0, 1]":2}') end @@ -102,7 +102,7 @@ describe "FFI_Yajl::Encoder" do end it "encodes symbols in keys as strings" do - ruby = { :thing => 1 } + ruby = { thing: 1 } expect(encoder.encode(ruby)).to eq('{"thing":1}') end @@ -190,7 +190,7 @@ describe "FFI_Yajl::Encoder" do end context "when validate_utf8 is off" do - let(:options) { { :validate_utf8 => false } } + let(:options) { { validate_utf8: false } } it "does not raise an error" do expect { encoder.encode(ruby) }.not_to raise_error diff --git a/spec/ffi_yajl/parser_spec.rb b/spec/ffi_yajl/parser_spec.rb index 968e0ee..dde8646 100644 --- a/spec/ffi_yajl/parser_spec.rb +++ b/spec/ffi_yajl/parser_spec.rb @@ -87,7 +87,7 @@ describe "FFI_Yajl::Parser" do let(:json) { '{"key": /* this is a comment */ "value"}' } context "when allow_comments is false" do - let(:options) { { :allow_comments => false } } + let(:options) { { allow_comments: false } } it "should not parse" do expect { parser }.to raise_error(FFI_Yajl::ParseError) @@ -95,7 +95,7 @@ describe "FFI_Yajl::Parser" do end context "when allow_comments is true" do - let(:options) { { :allow_comments => true } } + let(:options) { { allow_comments: true } } it "should parse" do expect(parser).to eq("key" => "value") @@ -115,7 +115,7 @@ describe "FFI_Yajl::Parser" do let(:json) { %{{"key": \n/*\n this is a multiline comment \n*/\n "value"}} } context "when allow_comments is false" do - let(:options) { { :allow_comments => false } } + let(:options) { { allow_comments: false } } it "should not parse" do expect { parser }.to raise_error(FFI_Yajl::ParseError) @@ -123,7 +123,7 @@ describe "FFI_Yajl::Parser" do end context "when allow_comments is true" do - let(:options) { { :allow_comments => true } } + let(:options) { { allow_comments: true } } it "should parse" do expect(parser).to eq("key" => "value") @@ -135,7 +135,7 @@ describe "FFI_Yajl::Parser" do let(:json) { %{{"key": \n// this is an inline comment\n "value"}} } context "when allow_comments is false" do - let(:options) { { :allow_comments => false } } + let(:options) { { allow_comments: false } } it "should not parse" do expect { parser }.to raise_error(FFI_Yajl::ParseError) @@ -143,7 +143,7 @@ describe "FFI_Yajl::Parser" do end context "when allow_comments is true" do - let(:options) { { :allow_comments => true } } + let(:options) { { allow_comments: true } } it "should parse" do expect(parser).to eq("key" => "value") @@ -152,14 +152,14 @@ describe "FFI_Yajl::Parser" do end context "when json is invalid UTF8" do - let(:json) { "[\"#{"\201\203"}\"]" } + let(:json) { "[\"\201\203\"]" } it "should not parse by default" do expect { parser }.to raise_error(FFI_Yajl::ParseError) end context "when :dont_validate_strings is set to true" do - let(:options) { { :dont_validate_strings => true } } + let(:options) { { dont_validate_strings: true } } it "should parse" do expect(parser).to eq(["\x81\x83"]) @@ -167,7 +167,7 @@ describe "FFI_Yajl::Parser" do end context "when :dont_validate_strings is set to false" do - let(:options) { { :dont_validate_strings => false } } + let(:options) { { dont_validate_strings: false } } it "should not parse" do expect { parser }.to raise_error(FFI_Yajl::ParseError) @@ -175,14 +175,14 @@ describe "FFI_Yajl::Parser" do end context "when :check_utf8 is set to true" do - let(:options) { { :check_utf8 => true } } + let(:options) { { check_utf8: true } } it "should not parse" do expect { parser }.to raise_error(FFI_Yajl::ParseError) end context "when :dont_validate_strings is set to true" do - let(:options) { { :check_utf8 => true, :dont_validate_strings => true } } + let(:options) { { check_utf8: true, dont_validate_strings: true } } it "should raise an ArgumentError" do expect { parser }.to raise_error(ArgumentError) @@ -190,7 +190,7 @@ describe "FFI_Yajl::Parser" do end context "when :dont_validate_strings is set to false" do - let(:options) { { :check_utf8 => true, :dont_validate_strings => false } } + let(:options) { { check_utf8: true, dont_validate_strings: false } } it "should not parse" do expect { parser }.to raise_error(FFI_Yajl::ParseError) @@ -199,14 +199,14 @@ describe "FFI_Yajl::Parser" do end context "when :check_utf8 is set to false" do - let(:options) { { :check_utf8 => false } } + let(:options) { { check_utf8: false } } it "should parse" do expect(parser).to eq(["\x81\x83"]) end context "when :dont_validate_strings is set to true" do - let(:options) { { :check_utf8 => false, :dont_validate_strings => true } } + let(:options) { { check_utf8: false, dont_validate_strings: true } } it "should parse" do expect(parser).to eq(["\x81\x83"]) @@ -214,7 +214,7 @@ describe "FFI_Yajl::Parser" do end context "when :dont_validate_strings is set to false" do - let(:options) { { :check_utf8 => false, :dont_validate_strings => false } } + let(:options) { { check_utf8: false, dont_validate_strings: false } } it "should raise an ArgumentError" do expect { parser }.to raise_error(ArgumentError) @@ -239,10 +239,10 @@ describe "FFI_Yajl::Parser" do end context "when symbolize_keys is true" do - let(:options) { { :symbolize_keys => true } } + let(:options) { { symbolize_keys: true } } it "should symbolize keys correctly" do - expect(parser).to eq(:key => 1234) + expect(parser).to eq(key: 1234) end end @@ -308,10 +308,10 @@ describe "FFI_Yajl::Parser" do end context "when symbolize_keys is true" do - let(:options) { { :symbolize_keys => true } } + let(:options) { { symbolize_keys: true } } it "should symbolize keys correctly" do - expect(parser).to eq(:"日本語" => 1234) + expect(parser).to eq("日本語": 1234) end if RUBY_VERSION.to_f >= 1.9 @@ -382,7 +382,7 @@ describe "FFI_Yajl::Parser" do end context "with allow_trailing_garbage" do - let(:options) { { :allow_trailing_garbage => true } } + let(:options) { { allow_trailing_garbage: true } } it "parses" do expect(parser).to eq("foo" => { "foo" => 1234 }) end @@ -474,7 +474,7 @@ describe "FFI_Yajl::Parser" do # NOTE: parsing floats with 8 million digits on windows has some kind of huge # perf issues likely in ruby and/or the underlying windows libs - context "when parsing big floats", :ruby_gte_193 => true, :unix_only => true do + context "when parsing big floats", ruby_gte_193: true, unix_only: true do let(:json) { '[0.' + '1' * 2**23 + ']' } it "parses" do @@ -482,9 +482,9 @@ describe "FFI_Yajl::Parser" do end end - context "when parsing long hash keys with symbolize_keys option", :ruby_gte_193 => true do + context "when parsing long hash keys with symbolize_keys option", ruby_gte_193: true do let(:json) { '{"' + 'a' * 2**23 + '": 0}' } - let(:options) { { :symbolize_keys => true } } + let(:options) { { symbolize_keys: true } } it "parses" do expect { parser }.not_to raise_error @@ -500,7 +500,7 @@ describe "FFI_Yajl::Parser" do context "should raise an exception for repeated keys" do let(:json) { '{"foo":"bar","foo":"baz"}' } - let(:options) { { :unique_key_checking => true } } + let(:options) { { unique_key_checking: true } } it "should raise" do expect { parser }.to raise_error(FFI_Yajl::ParseError) end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 17dabcd..9ec853d 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -34,8 +34,8 @@ end require 'ffi_yajl' RSpec.configure do |conf| - conf.filter_run_excluding :unix_only => true unless RUBY_PLATFORM !~ /mswin|mingw|windows/ - conf.filter_run_excluding :ruby_gte_193 => true unless RUBY_VERSION.to_f >= 2.0 || RUBY_VERSION =~ /^1\.9\.3/ + conf.filter_run_excluding unix_only: true unless RUBY_PLATFORM !~ /mswin|mingw|windows/ + conf.filter_run_excluding ruby_gte_193: true unless RUBY_VERSION.to_f >= 2.0 || RUBY_VERSION =~ /^1\.9\.3/ conf.order = 'random' -- cgit v1.2.1