From 526f90762923f2977b0c4fa134ba57bd8157e762 Mon Sep 17 00:00:00 2001 From: Mauro George Date: Wed, 31 Dec 2014 18:59:32 -0200 Subject: [PATCH] Improves spec runner harness, switches to better env detection. Does not run rails specs when running mongoid specs WIP Start of remove BRE to check tests Remove old helper of active record Use RSpec skip to skip AR specs Run ActionView only when needed Run ActiveSupport only when needed Run Mongoid only when needed Create ExtVerifier.require_dependencies Run MongoMapper only when needed Always load nokogiri specs Always load ostruct specs Run Ripple only when needed Remove :: when check for defined constants Require spec_helper directly Remove 1.8.6 old monkey patch Add some RSpec configs --- Appraisals | 3 - gemfiles/mongoid_3.0.gemfile | 1 - gemfiles/mongoid_3.1.gemfile | 1 - gemfiles/mongoid_4.0.gemfile | 1 - spec/active_record_helper.rb | 54 ++--- spec/colors_spec.rb | 4 +- spec/core_ext/logger_spec.rb | 4 +- spec/core_ext/string_spec.rb | 4 +- spec/ext/action_view_spec.rb | 33 ++- spec/ext/active_record_spec.rb | 278 ++++++++++++------------- spec/ext/active_support_spec.rb | 54 ++--- spec/ext/mongo_mapper_spec.rb | 184 ++++++++-------- spec/ext/mongoid_spec.rb | 97 ++++----- spec/ext/nokogiri_spec.rb | 46 ++-- spec/ext/ostruct_spec.rb | 38 ++-- spec/ext/ripple_spec.rb | 39 ++-- spec/formats_spec.rb | 4 +- spec/methods_spec.rb | 12 +- spec/misc_spec.rb | 4 +- spec/objects_spec.rb | 4 +- spec/spec_helper.rb | 58 +++--- spec/support/active_record_versions.rb | 26 --- spec/support/ext_verifier.rb | 37 ++++ spec/support/rails_versions.rb | 30 +++ 24 files changed, 490 insertions(+), 526 deletions(-) delete mode 100644 spec/support/active_record_versions.rb create mode 100644 spec/support/ext_verifier.rb create mode 100644 spec/support/rails_versions.rb diff --git a/Appraisals b/Appraisals index ce50e6e..1fa9966 100644 --- a/Appraisals +++ b/Appraisals @@ -15,17 +15,14 @@ appraise 'rails-4.2' do end appraise 'mongoid-3.0' do - gem 'rails', '~> 3.2.0' gem 'mongoid', '~> 3.0.0' end appraise 'mongoid-3.1' do - gem 'rails', '~> 3.2.0' gem 'mongoid', '~> 3.1.0' end appraise 'mongoid-4.0' do - gem 'rails', '~> 4.2.0' gem 'mongoid', '~> 4.0.0' end diff --git a/gemfiles/mongoid_3.0.gemfile b/gemfiles/mongoid_3.0.gemfile index 605ca35..6845eb4 100644 --- a/gemfiles/mongoid_3.0.gemfile +++ b/gemfiles/mongoid_3.0.gemfile @@ -2,7 +2,6 @@ source "https://rubygems.org" -gem "rails", "~> 3.2.0" gem "mongoid", "~> 3.0.0" gemspec :path => "../" diff --git a/gemfiles/mongoid_3.1.gemfile b/gemfiles/mongoid_3.1.gemfile index 819edc5..da1c8e2 100644 --- a/gemfiles/mongoid_3.1.gemfile +++ b/gemfiles/mongoid_3.1.gemfile @@ -2,7 +2,6 @@ source "https://rubygems.org" -gem "rails", "~> 3.2.0" gem "mongoid", "~> 3.1.0" gemspec :path => "../" diff --git a/gemfiles/mongoid_4.0.gemfile b/gemfiles/mongoid_4.0.gemfile index cad90b3..5a038c1 100644 --- a/gemfiles/mongoid_4.0.gemfile +++ b/gemfiles/mongoid_4.0.gemfile @@ -2,7 +2,6 @@ source "https://rubygems.org" -gem "rails", "~> 4.2.0" gem "mongoid", "~> 4.0.0" gemspec :path => "../" diff --git a/spec/active_record_helper.rb b/spec/active_record_helper.rb index 2a5207b..6bb2a62 100644 --- a/spec/active_record_helper.rb +++ b/spec/active_record_helper.rb @@ -1,34 +1,24 @@ -require 'active_record' - -# Required to use the column support -module Rails - def self.env - {} +if ExtVerifier.has_rails? + # Required to use the column support + module Rails + def self.env + {} + end end + + # Establish connection to in-memory SQLite DB + ActiveRecord::Base.establish_connection adapter: "sqlite3", database: ":memory:" + + # Create the users table + ActiveRecord::Migration.verbose = false + ActiveRecord::Migration.create_table :users do |t| + t.string :name + t.integer :rank + t.boolean :admin + t.datetime :created_at + end + + # Create models + class User < ActiveRecord::Base; end + class SubUser < User; end end - -# Establish connection to in-memory SQLite DB -ActiveRecord::Base.establish_connection adapter: "sqlite3", database: ":memory:" - -# Create the users table -ActiveRecord::Migration.verbose = false -ActiveRecord::Migration.create_table :users do |t| - t.string :name - t.integer :rank - t.boolean :admin - t.datetime :created_at -end - -# Create models -class User < ActiveRecord::Base; end -class SubUser < User; end - - -# Helper methods -# ############## - -# we only work with ActiveRecord 2+ -def is_usable_activerecord? - defined?(ActiveRecord::VERSION::MAJOR) && ActiveRecord::VERSION::MAJOR >= 2 -end - diff --git a/spec/colors_spec.rb b/spec/colors_spec.rb index 848d210..16d859b 100644 --- a/spec/colors_spec.rb +++ b/spec/colors_spec.rb @@ -1,6 +1,6 @@ -require File.expand_path(File.dirname(__FILE__) + '/spec_helper') +require 'spec_helper' -describe "AwesomePrint" do +RSpec.describe "AwesomePrint" do def stub_tty!(output = true, stream = STDOUT) if output stream.instance_eval { def tty?; true; end } diff --git a/spec/core_ext/logger_spec.rb b/spec/core_ext/logger_spec.rb index 72720eb..fa10d1f 100644 --- a/spec/core_ext/logger_spec.rb +++ b/spec/core_ext/logger_spec.rb @@ -1,10 +1,10 @@ -require File.expand_path(File.dirname(__FILE__) + '/../spec_helper') +require 'spec_helper' require 'logger' require 'awesome_print/core_ext/logger' -describe "AwesomePrint logging extensions" do +RSpec.describe "AwesomePrint logging extensions" do before(:all) do @logger = Logger.new('/dev/null') rescue Logger.new('nul') end diff --git a/spec/core_ext/string_spec.rb b/spec/core_ext/string_spec.rb index 3adb0e6..17afe9b 100644 --- a/spec/core_ext/string_spec.rb +++ b/spec/core_ext/string_spec.rb @@ -1,6 +1,6 @@ -require File.expand_path(File.dirname(__FILE__) + '/../spec_helper') +require 'spec_helper' -describe "String extensions" do +RSpec.describe "String extensions" do [ :gray, :red, :green, :yellow, :blue, :purple, :cyan, :white ].each_with_index do |color, i| it "should have #{color} color" do expect(color.to_s.send(color)).to eq("\e[1;#{30+i}m#{color}\e[0m") diff --git a/spec/ext/action_view_spec.rb b/spec/ext/action_view_spec.rb index 3637234..4fb915e 100644 --- a/spec/ext/action_view_spec.rb +++ b/spec/ext/action_view_spec.rb @@ -1,25 +1,18 @@ -require File.expand_path(File.dirname(__FILE__) + '/../spec_helper') +require 'spec_helper' -begin - require 'action_view' - require 'awesome_print/ext/action_view' +RSpec.describe "AwesomePrint ActionView extensions", skip: ->{ !ExtVerifier.has_rails? }.call do - describe "AwesomePrint ActionView extensions" do - before do - @view = ActionView::Base.new - end - - it "uses HTML and adds 'debug_dump' class to plain
 tag" do
-      markup = rand
-      expect(@view.ap(markup, :plain => true)).to eq(%Q|
#{markup}
|) - end - - it "uses HTML and adds 'debug_dump' class to colorized
 tag" do
-      markup = ' &'
-      expect(@view.ap(markup)).to eq('
" &<hello>"
') - end + before do + @view = ActionView::Base.new end -rescue LoadError => error - puts "Skipping ActionView specs: #{error}" + it "uses HTML and adds 'debug_dump' class to plain
 tag" do
+    markup = rand
+    expect(@view.ap(markup, :plain => true)).to eq(%Q|
#{markup}
|) + end + + it "uses HTML and adds 'debug_dump' class to colorized
 tag" do
+    markup = ' &'
+    expect(@view.ap(markup)).to eq('
" &<hello>"
') + end end diff --git a/spec/ext/active_record_spec.rb b/spec/ext/active_record_spec.rb index a9630f9..bdb48e2 100644 --- a/spec/ext/active_record_spec.rb +++ b/spec/ext/active_record_spec.rb @@ -1,28 +1,24 @@ -require File.expand_path(File.dirname(__FILE__) + '/../spec_helper') +require 'spec_helper' +require 'active_record_helper' -begin - require 'awesome_print/ext/active_record' - require File.expand_path(File.dirname(__FILE__) + '/../active_record_helper') +RSpec.describe "AwesomePrint/ActiveRecord", skip: ->{ !ExtVerifier.has_rails? }.call do - if is_usable_activerecord? + before do + stub_dotfile! + end - describe "AwesomePrint/ActiveRecord" do - before do - stub_dotfile! - end + #------------------------------------------------------------------------------ + describe "ActiveRecord instance, attributes only (default)" do + before do + ActiveRecord::Base.default_timezone = :utc + @diana = User.new(:name => "Diana", :rank => 1, :admin => false, :created_at => "1992-10-10 12:30:00") + @laura = User.new(:name => "Laura", :rank => 2, :admin => true, :created_at => "2003-05-26 14:15:00") + @ap = AwesomePrint::Inspector.new(:plain => true, :sort_keys => true) + end - #------------------------------------------------------------------------------ - describe "ActiveRecord instance, attributes only (default)" do - before do - ActiveRecord::Base.default_timezone = :utc - @diana = User.new(:name => "Diana", :rank => 1, :admin => false, :created_at => "1992-10-10 12:30:00") - @laura = User.new(:name => "Laura", :rank => 2, :admin => true, :created_at => "2003-05-26 14:15:00") - @ap = AwesomePrint::Inspector.new(:plain => true, :sort_keys => true) - end - - it "display single record" do - out = @ap.send(:awesome, @diana) - str = <<-EOS.strip + it "display single record" do + out = @ap.send(:awesome, @diana) + str = <<-EOS.strip # { :admin => false, :created_at => ?, @@ -30,18 +26,18 @@ begin :name => "Diana", :rank => 1 } -EOS - if RUBY_VERSION < '1.9' - str.sub!('?', 'Sat Oct 10 12:30:00 UTC 1992') - else - str.sub!('?', '1992-10-10 12:30:00 UTC') - end - expect(out.gsub(/0x([a-f\d]+)/, "0x01234567")).to eq(str) - end + EOS + if RUBY_VERSION < '1.9' + str.sub!('?', 'Sat Oct 10 12:30:00 UTC 1992') + else + str.sub!('?', '1992-10-10 12:30:00 UTC') + end + expect(out.gsub(/0x([a-f\d]+)/, "0x01234567")).to eq(str) + end - it "display multiple records" do - out = @ap.send(:awesome, [ @diana, @laura ]) - str = <<-EOS.strip + it "display multiple records" do + out = @ap.send(:awesome, [ @diana, @laura ]) + str = <<-EOS.strip [ [0] # { :admin => false, @@ -58,32 +54,32 @@ EOS :rank => 2 } ] -EOS - if RUBY_VERSION < '1.9' - str.sub!('??', 'Sat Oct 10 12:30:00 UTC 1992') - str.sub!('?!', 'Mon May 26 14:15:00 UTC 2003') - else - str.sub!('??', '1992-10-10 12:30:00 UTC') - str.sub!('?!', '2003-05-26 14:15:00 UTC') - end - expect(out.gsub(/0x([a-f\d]+)/, "0x01234567")).to eq(str) - end + EOS + if RUBY_VERSION < '1.9' + str.sub!('??', 'Sat Oct 10 12:30:00 UTC 1992') + str.sub!('?!', 'Mon May 26 14:15:00 UTC 2003') + else + str.sub!('??', '1992-10-10 12:30:00 UTC') + str.sub!('?!', '2003-05-26 14:15:00 UTC') end + expect(out.gsub(/0x([a-f\d]+)/, "0x01234567")).to eq(str) + end + end - #------------------------------------------------------------------------------ - describe "ActiveRecord instance (raw)" do - before do - ActiveRecord::Base.default_timezone = :utc - @diana = User.new(:name => "Diana", :rank => 1, :admin => false, :created_at => "1992-10-10 12:30:00") - @laura = User.new(:name => "Laura", :rank => 2, :admin => true, :created_at => "2003-05-26 14:15:00") - @ap = AwesomePrint::Inspector.new(:plain => true, :sort_keys => true, :raw => true) - end + #------------------------------------------------------------------------------ + describe "ActiveRecord instance (raw)" do + before do + ActiveRecord::Base.default_timezone = :utc + @diana = User.new(:name => "Diana", :rank => 1, :admin => false, :created_at => "1992-10-10 12:30:00") + @laura = User.new(:name => "Laura", :rank => 2, :admin => true, :created_at => "2003-05-26 14:15:00") + @ap = AwesomePrint::Inspector.new(:plain => true, :sort_keys => true, :raw => true) + end - it "display single record" do - out = @ap.send(:awesome, @diana) + it "display single record" do + out = @ap.send(:awesome, @diana) - if activerecord_4_2? - str = <<-EOS.strip + if activerecord_4_2? + str = <<-EOS.strip # nil } > -EOS - elsif activerecord_4_1? - str = <<-EOS.strip + EOS + elsif activerecord_4_1? + str = <<-EOS.strip # nil } > -EOS - elsif activerecord_4_0? - str = <<-EOS.strip + EOS + elsif activerecord_4_0? + str = <<-EOS.strip # nil } > -EOS - elsif activerecord_3_2? - str = <<-EOS.strip + EOS + elsif activerecord_3_2? + str = <<-EOS.strip # nil } > -EOS - end - str.sub!('?', '1992-10-10 12:30:00') - expect(out.gsub(/0x([a-f\d]+)/, "0x01234567")).to eq(str) - end + EOS + end + str.sub!('?', '1992-10-10 12:30:00') + expect(out.gsub(/0x([a-f\d]+)/, "0x01234567")).to eq(str) + end - it "display multiple records" do - out = @ap.send(:awesome, [ @diana, @laura ]) + it "display multiple records" do + out = @ap.send(:awesome, [ @diana, @laura ]) - if activerecord_4_2? - str = <<-EOS.strip + if activerecord_4_2? + str = <<-EOS.strip [ [0] # ] -EOS - elsif activerecord_4_1? - str = <<-EOS.strip + EOS + elsif activerecord_4_1? + str = <<-EOS.strip [ [0] # ] -EOS - elsif activerecord_4_0? - str = <<-EOS.strip + EOS + elsif activerecord_4_0? + str = <<-EOS.strip [ [0] # ] -EOS - elsif activerecord_3_2? - str = <<-EOS.strip + EOS + elsif activerecord_3_2? + str = <<-EOS.strip [ [0] # ] -EOS - end - str.sub!('?', '1992-10-10 12:30:00') - str.sub!('?', '2003-05-26 14:15:00') - expect(out.gsub(/0x([a-f\d]+)/, "0x01234567")).to eq(str) - end + EOS end + str.sub!('?', '1992-10-10 12:30:00') + str.sub!('?', '2003-05-26 14:15:00') + expect(out.gsub(/0x([a-f\d]+)/, "0x01234567")).to eq(str) + end + end - #------------------------------------------------------------------------------ - describe "ActiveRecord class" do - before do - @ap = AwesomePrint::Inspector.new(:plain => true) - end + #------------------------------------------------------------------------------ + describe "ActiveRecord class" do + before do + @ap = AwesomePrint::Inspector.new(:plain => true) + end - it "should print the class" do - expect(@ap.send(:awesome, User)).to eq <<-EOS.strip + it "should print the class" do + expect(@ap.send(:awesome, User)).to eq <<-EOS.strip class User < ActiveRecord::Base { :id => :integer, :name => :string, @@ -1170,12 +1166,12 @@ class User < ActiveRecord::Base { :admin => :boolean, :created_at => :datetime } -EOS - end + EOS + end - it "should print the class for non-direct subclasses of ActiveRecord::Base" do - out = @ap.send(:awesome, SubUser) - expect(out).to eq <<-EOS.strip + it "should print the class for non-direct subclasses of ActiveRecord::Base" do + out = @ap.send(:awesome, SubUser) + expect(out).to eq <<-EOS.strip class SubUser < User { :id => :integer, :name => :string, @@ -1183,51 +1179,47 @@ class SubUser < User { :admin => :boolean, :created_at => :datetime } -EOS - end + EOS + end + + it "should print ActiveRecord::Base objects (ex. ancestors)" do + expect { @ap.send(:awesome, User.ancestors) }.not_to raise_error + end + end + + #------------------------------------------------------------------------------ + describe "ActiveRecord methods formatting" do + before do + @ap = AwesomePrint::Inspector.new(:plain => true) + end + + it "should format class methods properly" do + # spec 1 + out = @ap.send(:awesome, User.methods.grep(/first/)) + + if ActiveRecord::VERSION::STRING >= "3.2" + if RUBY_VERSION >= "1.9" + expect(out).to match(/\sfirst\(\*args,\s&block\)\s+Class \(ActiveRecord::Querying\)/) + else + expect(out).to match(/\sfirst\(\*arg1\)\s+Class \(ActiveRecord::Querying\)/) + end + else + expect(out).to match(/\sfirst\(\*arg.*?\)\s+User \(ActiveRecord::Base\)/) + end + + # spec 2 + out = @ap.send(:awesome, User.methods.grep(/primary_key/)) + expect(out).to match(/\sprimary_key\(.*?\)\s+Class \(ActiveRecord::AttributeMethods::PrimaryKey::ClassMethods\)/) + + # spec 3 + out = @ap.send(:awesome, User.methods.grep(/validate/)) + + if ActiveRecord::VERSION::MAJOR < 3 + expect(out).to match(/\svalidate\(\*arg.*?\)\s+User \(ActiveRecord::Base\)/) + else + expect(out).to match(/\svalidate\(\*arg.*?\)\s+Class \(ActiveModel::Validations::ClassMethods\)/) + end - it "should print ActiveRecord::Base objects (ex. ancestors)" do - expect { @ap.send(:awesome, User.ancestors) }.not_to raise_error - end - end - - #------------------------------------------------------------------------------ - describe "ActiveRecord methods formatting" do - before do - @ap = AwesomePrint::Inspector.new(:plain => true) - end - - it "should format class methods properly" do - # spec 1 - out = @ap.send(:awesome, User.methods.grep(/first/)) - - if ActiveRecord::VERSION::STRING >= "3.2" - if RUBY_VERSION >= "1.9" - expect(out).to match(/\sfirst\(\*args,\s&block\)\s+Class \(ActiveRecord::Querying\)/) - else - expect(out).to match(/\sfirst\(\*arg1\)\s+Class \(ActiveRecord::Querying\)/) - end - else - expect(out).to match(/\sfirst\(\*arg.*?\)\s+User \(ActiveRecord::Base\)/) - end - - # spec 2 - out = @ap.send(:awesome, User.methods.grep(/primary_key/)) - expect(out).to match(/\sprimary_key\(.*?\)\s+Class \(ActiveRecord::AttributeMethods::PrimaryKey::ClassMethods\)/) - - # spec 3 - out = @ap.send(:awesome, User.methods.grep(/validate/)) - - if ActiveRecord::VERSION::MAJOR < 3 - expect(out).to match(/\svalidate\(\*arg.*?\)\s+User \(ActiveRecord::Base\)/) - else - expect(out).to match(/\svalidate\(\*arg.*?\)\s+Class \(ActiveModel::Validations::ClassMethods\)/) - end - - end - end end end -rescue LoadError => error - puts "Skipping ActiveRecord specs: #{error}" end diff --git a/spec/ext/active_support_spec.rb b/spec/ext/active_support_spec.rb index 9202763..f6f3ad7 100644 --- a/spec/ext/active_support_spec.rb +++ b/spec/ext/active_support_spec.rb @@ -1,35 +1,27 @@ -require File.expand_path(File.dirname(__FILE__) + '/../spec_helper') +require 'spec_helper' -begin - require 'active_support/all' - require 'awesome_print/ext/active_support' - - describe "AwesomePrint::ActiveSupport" do - before do - stub_dotfile! - @ap = AwesomePrint::Inspector.new - end - - it "should format ActiveSupport::TimeWithZone as regular Time" do - Time.zone = 'Eastern Time (US & Canada)' - time = Time.utc(2007, 2, 10, 20, 30, 45).in_time_zone - expect(@ap.send(:awesome, time)).to eq("\e[0;32mSat, 10 Feb 2007 15:30:45 EST -05:00\e[0m") - end - - it "should format HashWithIndifferentAccess as regular Hash" do - hash = HashWithIndifferentAccess.new({ :hello => "world" }) - expect(@ap.send(:awesome, hash)).to eq("{\n \"hello\"\e[0;37m => \e[0m\e[0;33m\"world\"\e[0m\n}") - end - - # ActiveSupport sticks in instance variables to the date object. Make sure - # we ignore that and format Date instance as regular date. - it "should formate Date object as date" do - date = Date.new(2003, 5, 26) - expect(date.ai(:plain => true)).to eq("Mon, 26 May 2003") - expect(date.ai).to eq("\e[0;32mMon, 26 May 2003\e[0m") - end +RSpec.describe "AwesomePrint::ActiveSupport", skip: ->{ !ExtVerifier.has_rails? }.call do + before do + stub_dotfile! + @ap = AwesomePrint::Inspector.new end -rescue LoadError => error - puts "Skipping ActiveSupport specs: #{error}" + it "should format ActiveSupport::TimeWithZone as regular Time" do + Time.zone = 'Eastern Time (US & Canada)' + time = Time.utc(2007, 2, 10, 20, 30, 45).in_time_zone + expect(@ap.send(:awesome, time)).to eq("\e[0;32mSat, 10 Feb 2007 15:30:45 EST -05:00\e[0m") + end + + it "should format HashWithIndifferentAccess as regular Hash" do + hash = HashWithIndifferentAccess.new({ :hello => "world" }) + expect(@ap.send(:awesome, hash)).to eq("{\n \"hello\"\e[0;37m => \e[0m\e[0;33m\"world\"\e[0m\n}") + end + + # ActiveSupport sticks in instance variables to the date object. Make sure + # we ignore that and format Date instance as regular date. + it "should formate Date object as date" do + date = Date.new(2003, 5, 26) + expect(date.ai(:plain => true)).to eq("Mon, 26 May 2003") + expect(date.ai).to eq("\e[0;32mMon, 26 May 2003\e[0m") + end end diff --git a/spec/ext/mongo_mapper_spec.rb b/spec/ext/mongo_mapper_spec.rb index f76faa6..9c0872a 100644 --- a/spec/ext/mongo_mapper_spec.rb +++ b/spec/ext/mongo_mapper_spec.rb @@ -1,10 +1,8 @@ -require File.expand_path(File.dirname(__FILE__) + '/../spec_helper') +require 'spec_helper' -begin - require "mongo_mapper" - require "awesome_print/ext/mongo_mapper" +RSpec.describe "AwesomePrint/MongoMapper", skip: ->{ !ExtVerifier.has_mongo_mapper? }.call do - describe "AwesomePrint/MongoMapper" do + if ExtVerifier.has_mongo_mapper? before :all do class MongoUser include MongoMapper::Document @@ -18,25 +16,26 @@ begin Object.instance_eval{ remove_const :MongoUser } Object.instance_eval{ remove_const :Chamelion } end + end - before do - stub_dotfile! - @ap = AwesomePrint::Inspector.new(:plain => true, :sort_keys => true) - end + before do + stub_dotfile! + @ap = AwesomePrint::Inspector.new(:plain => true, :sort_keys => true) + end - describe "with the raw option set to true" do - # before { @ap.options[:raw] = true } - before { @ap = AwesomePrint::Inspector.new(:plain => true, :sort_keys => true, :raw => true) } + describe "with the raw option set to true" do + # before { @ap.options[:raw] = true } + before { @ap = AwesomePrint::Inspector.new(:plain => true, :sort_keys => true, :raw => true) } - it "should print class instance" do - user = MongoUser.new(:first_name => "Al", :last_name => "Capone") + it "should print class instance" do + user = MongoUser.new(:first_name => "Al", :last_name => "Capone") - out = @ap.send(:awesome, user) - out.gsub!(/#\/, 'AWESOME_PRINT_PROC_STUB') - out.gsub!(/BSON::ObjectId\('[\da-f]+?'\)/, "BSON::ObjectId('123456789')") + out = @ap.send(:awesome, user) + out.gsub!(/#\/, 'AWESOME_PRINT_PROC_STUB') + out.gsub!(/BSON::ObjectId\('[\da-f]+?'\)/, "BSON::ObjectId('123456789')") - if MongoMapper::Version >= "0.13" - str = <<-EOS.strip + if MongoMapper::Version >= "0.13" + str = <<-EOS.strip # nil } > -EOS - else - str = <<-EOS.strip + EOS + else + str = <<-EOS.strip # -EOS - end - out.gsub!(/0x([a-f\d]+)/, "0x01234567") - expect(out).to eq(str) + EOS end + out.gsub!(/0x([a-f\d]+)/, "0x01234567") + expect(out).to eq(str) + end - it "should print the class" do - expect(@ap.send(:awesome, MongoUser)).to eq <<-EOS.strip + it "should print the class" do + expect(@ap.send(:awesome, MongoUser)).to eq <<-EOS.strip class MongoUser < Object { "_id" => :object_id, "first_name" => :string, "last_name" => :string } -EOS + EOS + end + + it "should print the class when type is undefined" do + class Chamelion + include MongoMapper::Document + key :last_attribute end - it "should print the class when type is undefined" do - class Chamelion - include MongoMapper::Document - key :last_attribute - end - - expect(@ap.send(:awesome, Chamelion)).to eq <<-EOS.strip + expect(@ap.send(:awesome, Chamelion)).to eq <<-EOS.strip class Chamelion < Object { "_id" => :object_id, "last_attribute" => :undefined } -EOS - end + EOS end + end - describe "with associations" do + describe "with associations" do + + if ExtVerifier.has_mongo_mapper? before :all do class Child include MongoMapper::EmbeddedDocument @@ -176,79 +177,80 @@ EOS one :sibling end end + end - describe "with show associations turned off (default)" do - it "should render the class as normal" do - expect(@ap.send(:awesome, Parent)).to eq <<-EOS.strip + describe "with show associations turned off (default)" do + it "should render the class as normal" do + expect(@ap.send(:awesome, Parent)).to eq <<-EOS.strip class Parent < Object { "_id" => :object_id, "name" => :undefined } -EOS - end + EOS + end - it "should render an instance as normal" do - parent = Parent.new(:name => 'test') - out = @ap.send(:awesome, parent) - str = <<-EOS.strip + it "should render an instance as normal" do + parent = Parent.new(:name => 'test') + out = @ap.send(:awesome, parent) + str = <<-EOS.strip # { "_id" => BSON::ObjectId('4d9183739a546f6806000001'), "name" => "test" } -EOS - out.gsub!(/'([\w]+){23}'/, "'4d9183739a546f6806000001'") - out.gsub!(/0x([a-f\d]+)/, "0x01234567") - expect(out).to eq(str) - end + EOS + out.gsub!(/'([\w]+){23}'/, "'4d9183739a546f6806000001'") + out.gsub!(/0x([a-f\d]+)/, "0x01234567") + expect(out).to eq(str) + end + end + + describe "with show associations turned on and inline embedded turned off" do + before :each do + @ap = AwesomePrint::Inspector.new(:plain => true, :mongo_mapper => { :show_associations => true }) end - describe "with show associations turned on and inline embedded turned off" do - before :each do - @ap = AwesomePrint::Inspector.new(:plain => true, :mongo_mapper => { :show_associations => true }) - end - - it "should render the class with associations shown" do - expect(@ap.send(:awesome, Parent)).to eq <<-EOS.strip + it "should render the class with associations shown" do + expect(@ap.send(:awesome, Parent)).to eq <<-EOS.strip class Parent < Object { "_id" => :object_id, "name" => :undefined, "child" => embeds one Child, "sibling" => one Sibling } -EOS - end + EOS + end - it "should render an instance with associations shown" do - parent = Parent.new(:name => 'test') - out = @ap.send(:awesome, parent) - str = <<-EOS.strip + it "should render an instance with associations shown" do + parent = Parent.new(:name => 'test') + out = @ap.send(:awesome, parent) + str = <<-EOS.strip # { "_id" => BSON::ObjectId('4d9183739a546f6806000001'), "name" => "test", "child" => embeds one Child, "sibling" => one Sibling } -EOS - out.gsub!(/'([\w]+){23}'/, "'4d9183739a546f6806000001'") - out.gsub!(/0x([a-f\d]+)/, "0x01234567") - expect(out).to eq(str) - end + EOS + out.gsub!(/'([\w]+){23}'/, "'4d9183739a546f6806000001'") + out.gsub!(/0x([a-f\d]+)/, "0x01234567") + expect(out).to eq(str) + end + end + + describe "with show associations turned on and inline embedded turned on" do + before :each do + @ap = AwesomePrint::Inspector.new(:plain => true, + :mongo_mapper => { + :show_associations => true, + :inline_embedded => true + } + ) end - describe "with show associations turned on and inline embedded turned on" do - before :each do - @ap = AwesomePrint::Inspector.new(:plain => true, - :mongo_mapper => { - :show_associations => true, - :inline_embedded => true - } - ) - end - - it "should render an instance with associations shown and embeds there" do - parent = Parent.new(:name => 'test', :child => Child.new(:data => 5)) - out = @ap.send(:awesome, parent) - str = <<-EOS.strip + it "should render an instance with associations shown and embeds there" do + parent = Parent.new(:name => 'test', :child => Child.new(:data => 5)) + out = @ap.send(:awesome, parent) + str = <<-EOS.strip # { "_id" => BSON::ObjectId('4d9183739a546f6806000001'), "name" => "test", @@ -258,15 +260,11 @@ EOS }, "sibling" => one Sibling } -EOS - out.gsub!(/'([\w]+){23}'/, "'4d9183739a546f6806000001'") - out.gsub!(/0x([a-f\d]+)/, "0x01234567") - expect(out).to eq(str) - end + EOS + out.gsub!(/'([\w]+){23}'/, "'4d9183739a546f6806000001'") + out.gsub!(/0x([a-f\d]+)/, "0x01234567") + expect(out).to eq(str) end end end - -rescue LoadError => error - puts "Skipping MongoMapper specs: #{error}" end diff --git a/spec/ext/mongoid_spec.rb b/spec/ext/mongoid_spec.rb index 38bf685..b241b1b 100644 --- a/spec/ext/mongoid_spec.rb +++ b/spec/ext/mongoid_spec.rb @@ -1,10 +1,8 @@ -require File.expand_path(File.dirname(__FILE__) + '/../spec_helper') +require 'spec_helper' -begin - require "mongoid" - require "awesome_print/ext/mongoid" +RSpec.describe "AwesomePrint/Mongoid", skip: ->{ !ExtVerifier.has_mongoid? }.call do - describe "AwesomePrint/Mongoid" do + if ExtVerifier.has_mongoid? before :all do class MongoUser include Mongoid::Document @@ -18,94 +16,91 @@ begin Object.instance_eval{ remove_const :MongoUser } Object.instance_eval{ remove_const :Chamelion } end + end - before do - stub_dotfile! - @ap = AwesomePrint::Inspector.new :plain => true, :sort_keys => true - end + before do + stub_dotfile! + @ap = AwesomePrint::Inspector.new :plain => true, :sort_keys => true + end - it "should print class instance" do - user = MongoUser.new :first_name => "Al", :last_name => "Capone" - out = @ap.send :awesome, user + it "should print class instance" do + user = MongoUser.new :first_name => "Al", :last_name => "Capone" + out = @ap.send :awesome, user - object_id = user.id.inspect - str = <<-EOS.strip + object_id = user.id.inspect + str = <<-EOS.strip # { :_id => #{object_id}, :first_name => "Al", :last_name => "Capone" } -EOS - out.gsub!(/0x([a-f\d]+)/, "0x01234567") - expect(out).to eq(str) - end + EOS + out.gsub!(/0x([a-f\d]+)/, "0x01234567") + expect(out).to eq(str) + end - it "should print the class" do - class_spec = if mongoid_3_0? - <<-EOS.strip + it "should print the class" do + class_spec = if mongoid_3_0? + <<-EOS.strip class MongoUser < Object { :_id => :"moped/bson/object_id", :_type => :string, :first_name => :string, :last_name => :string } - EOS - elsif mongoid_3_1? - <<-EOS.strip + EOS + elsif mongoid_3_1? + <<-EOS.strip class MongoUser < Object { :_id => :"moped/bson/object_id", :first_name => :string, :last_name => :string } - EOS - elsif mongoid_4_0? - <<-EOS.strip + EOS + elsif mongoid_4_0? + <<-EOS.strip class MongoUser < Object { :_id => :"bson/object_id", :first_name => :string, :last_name => :string } - EOS - end + EOS + end - expect(@ap.send(:awesome, MongoUser)).to eq class_spec + expect(@ap.send(:awesome, MongoUser)).to eq class_spec + end + + it "should print the class when type is undefined" do + class Chamelion + include Mongoid::Document + field :last_attribute end - it "should print the class when type is undefined" do - class Chamelion - include Mongoid::Document - field :last_attribute - end - - class_spec = if mongoid_3_0? - <<-EOS.strip + class_spec = if mongoid_3_0? + <<-EOS.strip class Chamelion < Object { :_id => :"moped/bson/object_id", :_type => :string, :last_attribute => :object } -EOS - elsif mongoid_3_1? - <<-EOS.strip + EOS + elsif mongoid_3_1? + <<-EOS.strip class Chamelion < Object { :_id => :"moped/bson/object_id", :last_attribute => :object } -EOS - elsif mongoid_4_0? - <<-EOS.strip + EOS + elsif mongoid_4_0? + <<-EOS.strip class Chamelion < Object { :_id => :"bson/object_id", :last_attribute => :object } -EOS - end + EOS + end - expect(@ap.send(:awesome, Chamelion)).to eq class_spec - end + expect(@ap.send(:awesome, Chamelion)).to eq class_spec end - -rescue LoadError => error - puts "Skipping Mongoid specs: #{error}" end diff --git a/spec/ext/nokogiri_spec.rb b/spec/ext/nokogiri_spec.rb index a0ecc23..19623e0 100644 --- a/spec/ext/nokogiri_spec.rb +++ b/spec/ext/nokogiri_spec.rb @@ -1,41 +1,37 @@ -require File.expand_path(File.dirname(__FILE__) + '/../spec_helper') +require 'spec_helper' -begin - require "nokogiri" - require "awesome_print/ext/nokogiri" +RSpec.describe "AwesomePrint/Nokogiri" do + before do + stub_dotfile! + end - describe "AwesomePrint/Nokogiri" do - before do - stub_dotfile! - end - - it "should colorize tags" do - xml = Nokogiri::XML('

') - expect(xml.ai).to eq <<-EOS + it "should colorize tags" do + xml = Nokogiri::XML('

') + expect(xml.ai).to eq <<-EOS \e[1;32m \e[0m<\e[1;36mhtml\e[0m>\e[1;32m \e[0m<\e[1;36mbody\e[0m>\e[1;32m \e[0m<\e[1;36mh1\e[0m/>\e[1;32m \e[0m<\e[1;36m/body\e[0m>\e[1;32m \e[0m<\e[1;36m/html\e[0m> -EOS - end + EOS + end - it "should colorize contents" do - xml = Nokogiri::XML('

Hello

') - expect(xml.ai).to eq <<-EOS + it "should colorize contents" do + xml = Nokogiri::XML('

Hello

') + expect(xml.ai).to eq <<-EOS \e[1;32m \e[0m<\e[1;36mhtml\e[0m>\e[1;32m \e[0m<\e[1;36mbody\e[0m>\e[1;32m \e[0m<\e[1;36mh1\e[0m>\e[1;32mHello\e[0m<\e[1;36m/h1\e[0m>\e[1;32m \e[0m<\e[1;36m/body\e[0m>\e[1;32m \e[0m<\e[1;36m/html\e[0m> -EOS - end + EOS + end - it "should colorize class and id" do - xml = Nokogiri::XML('

') - expect(xml.ai).to eq <<-EOS + it "should colorize class and id" do + xml = Nokogiri::XML('

') + expect(xml.ai).to eq <<-EOS \e[1;32m \e[0m<\e[1;36mhtml\e[0m>\e[1;32m \e[0m<\e[1;36mbody\e[0m>\e[1;32m @@ -44,10 +40,6 @@ EOS \e[0m<\e[1;36m/h1\e[0m>\e[1;32m \e[0m<\e[1;36m/body\e[0m>\e[1;32m \e[0m<\e[1;36m/html\e[0m> -EOS - end + EOS end - -rescue LoadError => error - puts "Skipping Nokogiri specs: #{error}" end diff --git a/spec/ext/ostruct_spec.rb b/spec/ext/ostruct_spec.rb index 85887dd..d58c2fe 100644 --- a/spec/ext/ostruct_spec.rb +++ b/spec/ext/ostruct_spec.rb @@ -1,31 +1,23 @@ -require File.expand_path(File.dirname(__FILE__) + '/../spec_helper') +require 'spec_helper' -begin - require 'ostruct' - require 'awesome_print/ext/ostruct' +RSpec.describe 'AwesomePrint Ostruct extension' do + before do + stub_dotfile! + @ap = AwesomePrint::Inspector.new(:plain => true, :sort_keys => true) + end - describe 'AwesomePrint Ostruct extension' do - before do - stub_dotfile! - @ap = AwesomePrint::Inspector.new(:plain => true, :sort_keys => true) - end + it "empty hash" do + struct = OpenStruct.new + expect(@ap.send(:awesome, struct)).to eq("OpenStruct {}") + end - it "empty hash" do - struct = OpenStruct.new - expect(@ap.send(:awesome, struct)).to eq("OpenStruct {}") - end - - it "plain multiline" do - struct = OpenStruct.new :name => "Foo", :address => "Bar" - expect(@ap.send(:awesome, struct)).to eq <<-EOS.strip + it "plain multiline" do + struct = OpenStruct.new :name => "Foo", :address => "Bar" + expect(@ap.send(:awesome, struct)).to eq <<-EOS.strip OpenStruct { :address => "Bar", :name => "Foo" } -EOS - end + EOS end - -rescue LoadError => error - puts "Skipping OpenStruct specs: #{error}" -end \ No newline at end of file +end diff --git a/spec/ext/ripple_spec.rb b/spec/ext/ripple_spec.rb index d91a878..3f94aec 100644 --- a/spec/ext/ripple_spec.rb +++ b/spec/ext/ripple_spec.rb @@ -1,10 +1,8 @@ -require File.expand_path(File.dirname(__FILE__) + '/../spec_helper') +require 'spec_helper' -begin - require 'ripple' - require 'awesome_print/ext/ripple' +RSpec.describe 'AwesomePrint/Ripple', skip: ->{ !ExtVerifier.has_ripple? }.call do - describe 'AwesomePrint/Ripple' do + if ExtVerifier.has_ripple? before :all do class RippleUser include Ripple::Document @@ -19,36 +17,33 @@ begin after :all do Object.instance_eval { remove_const :RippleUser } end + end - before do - stub_dotfile! - @ap = AwesomePrint::Inspector.new :plain => true, :sort_keys => true - end + before do + stub_dotfile! + @ap = AwesomePrint::Inspector.new :plain => true, :sort_keys => true + end - it "should print class instance" do - user = RippleUser.new :_id => "12345", :first_name => "Al", :last_name => "Capone" - out = @ap.send :awesome, user + it "should print class instance" do + user = RippleUser.new :_id => "12345", :first_name => "Al", :last_name => "Capone" + out = @ap.send :awesome, user - expect(out.gsub(/0x([a-f\d]+)/, "0x01234567")).to eq <<-EOS.strip + expect(out.gsub(/0x([a-f\d]+)/, "0x01234567")).to eq <<-EOS.strip # { :_id => "12345", :first_name => "Al", :last_name => "Capone" } -EOS - end + EOS + end - it "should print the class" do - expect(@ap.send(:awesome, RippleUser)).to eq <<-EOS.strip + it "should print the class" do + expect(@ap.send(:awesome, RippleUser)).to eq <<-EOS.strip class RippleUser < Object { :_id => :string, :first_name => :string, :last_name => :string } -EOS - end + EOS end - -rescue LoadError => error - puts "Skipping Ripple specs: #{error}" end diff --git a/spec/formats_spec.rb b/spec/formats_spec.rb index d882e65..f769663 100644 --- a/spec/formats_spec.rb +++ b/spec/formats_spec.rb @@ -1,9 +1,9 @@ -require File.expand_path(File.dirname(__FILE__) + '/spec_helper') +require 'spec_helper' require "bigdecimal" require "rational" require "set" -describe "AwesomePrint" do +RSpec.describe "AwesomePrint" do before do stub_dotfile! end diff --git a/spec/methods_spec.rb b/spec/methods_spec.rb index db7dc7d..0ad25ab 100644 --- a/spec/methods_spec.rb +++ b/spec/methods_spec.rb @@ -1,6 +1,6 @@ -require File.expand_path(File.dirname(__FILE__) + '/spec_helper') +require 'spec_helper' -describe "Single method" do +RSpec.describe "Single method" do before do stub_dotfile! end @@ -80,7 +80,7 @@ describe "Single method" do end end -describe "Object methods" do +RSpec.describe "Object methods" do before do stub_dotfile! end @@ -202,7 +202,7 @@ describe "Object methods" do end end -describe "Class methods" do +RSpec.describe "Class methods" do before do stub_dotfile! end @@ -313,7 +313,7 @@ describe "Class methods" do end if RUBY_VERSION >= '1.9.2' - describe "Ruby 1.9.2+ Method#parameters" do + RSpec.describe "Ruby 1.9.2+ Method#parameters" do before do stub_dotfile! end @@ -364,7 +364,7 @@ if RUBY_VERSION >= '1.9.2' end end -describe "Methods arrays" do +RSpec.describe "Methods arrays" do after do Object.instance_eval{ remove_const :Hello } if defined?(Hello) Object.instance_eval{ remove_const :World } if defined?(World) diff --git a/spec/misc_spec.rb b/spec/misc_spec.rb index d4cf43d..7e33d27 100644 --- a/spec/misc_spec.rb +++ b/spec/misc_spec.rb @@ -1,6 +1,6 @@ -require File.expand_path(File.dirname(__FILE__) + '/spec_helper') +require 'spec_helper' -describe "AwesomePrint" do +RSpec.describe "AwesomePrint" do describe "Misc" do before do diff --git a/spec/objects_spec.rb b/spec/objects_spec.rb index 4d92a9e..ee7d241 100644 --- a/spec/objects_spec.rb +++ b/spec/objects_spec.rb @@ -1,6 +1,6 @@ -require File.expand_path(File.dirname(__FILE__) + '/spec_helper') +require 'spec_helper' -describe "Objects" do +RSpec.describe "Objects" do before do stub_dotfile! end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 1d4c779..9107e9a 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -16,12 +16,35 @@ # $LOAD_PATH.unshift(File.dirname(__FILE__)) $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib')) -require 'awesome_print' Dir[File.dirname(__FILE__) + '/support/**/*.rb'].each do |file| require file end +ExtVerifier.require_dependencies!(%w{rails active_record action_view + active_support/all mongoid mongo_mapper ripple}) +require 'nokogiri' +require 'awesome_print' + +RSpec.configure do |config| + config.disable_monkey_patching! + # TODO: Make specs not order dependent + # config.order = :random + Kernel.srand config.seed + config.filter_run focus: true + config.run_all_when_everything_filtered = true + config.expect_with :rspec do |expectations| + expectations.syntax = :expect + end + config.mock_with :rspec do |mocks| + mocks.syntax = :expect + mocks.verify_partial_doubles = true + end + if config.files_to_run.one? + config.default_formatter = 'doc' + end +end + def stub_dotfile! dotfile = File.join(ENV["HOME"], ".aprc") expect(File).to receive(:readable?).at_least(:once).with(dotfile).and_return(false) @@ -33,36 +56,3 @@ def capture! ensure $stdout = standard end - -# The following is needed for the Infinity Test. It runs tests as subprocesses, -# which sets STDOUT.tty? to false and would otherwise prematurely disallow colors. -### AwesomePrint.force_colors! - -# Ruby 1.8.6 only: define missing String methods that are needed for the specs to pass. -if RUBY_VERSION < '1.8.7' - class String - def shellescape # Taken from Ruby 1.9.2 standard library, see lib/shellwords.rb. - return "''" if self.empty? - str = self.dup - str.gsub!(/([^A-Za-z0-9_\-.,:\/@\n])/n, "\\\\\\1") - str.gsub!(/\n/, "'\n'") - str - end - - def start_with?(*prefixes) - prefixes.each do |prefix| - prefix = prefix.to_s - return true if prefix == self[0, prefix.size] - end - false - end - - def end_with?(*suffixes) - suffixes.each do |suffix| - suffix = suffix.to_s - return true if suffix == self[-suffix.size, suffix.size] - end - false - end - end -end diff --git a/spec/support/active_record_versions.rb b/spec/support/active_record_versions.rb deleted file mode 100644 index 4381361..0000000 --- a/spec/support/active_record_versions.rb +++ /dev/null @@ -1,26 +0,0 @@ -module ActiveRecordVersions - def activerecord_version - Gem::Version.new(ActiveRecord::VERSION::STRING) - end - - def activerecord_4_2? - Gem::Requirement.new('~> 4.2.0').satisfied_by?(activerecord_version) - end - - def activerecord_4_1? - Gem::Requirement.new('~> 4.1.0').satisfied_by?(activerecord_version) - end - - def activerecord_4_0? - Gem::Requirement.new('~> 4.0.0').satisfied_by?(activerecord_version) - end - - def activerecord_3_2? - Gem::Requirement.new('~> 3.2.0').satisfied_by?(activerecord_version) - end -end - -RSpec.configure do |config| - config.include(ActiveRecordVersions) - config.extend(ActiveRecordVersions) -end diff --git a/spec/support/ext_verifier.rb b/spec/support/ext_verifier.rb new file mode 100644 index 0000000..a41b1ac --- /dev/null +++ b/spec/support/ext_verifier.rb @@ -0,0 +1,37 @@ +module ExtVerifier + + def require_dependencies!(dependencies) + dependencies.each do |dependency| + begin + require dependency + rescue LoadError + end + end + end + module_function :require_dependencies! + + def has_rails? + defined?(Rails) + end + module_function :has_rails? + + def has_mongoid? + defined?(Mongoid) + end + module_function :has_mongoid? + + def has_mongo_mapper? + defined?(MongoMapper) + end + module_function :has_mongo_mapper? + + def has_ripple? + defined?(Ripple) + end + module_function :has_ripple? +end + +RSpec.configure do |config| + config.include(ExtVerifier) + config.extend(ExtVerifier) +end diff --git a/spec/support/rails_versions.rb b/spec/support/rails_versions.rb new file mode 100644 index 0000000..027e948 --- /dev/null +++ b/spec/support/rails_versions.rb @@ -0,0 +1,30 @@ +module RailsVersions + def rails_version + Gem::Version.new(Rails::VERSION::STRING) + end + + def rails_4_2? + Gem::Requirement.new('~> 4.2.0').satisfied_by?(rails_version) + end + alias_method :activerecord_4_2?, :rails_4_2? + + def rails_4_1? + Gem::Requirement.new('~> 4.1.0').satisfied_by?(rails_version) + end + alias_method :activerecord_4_1?, :rails_4_1? + + def rails_4_0? + Gem::Requirement.new('~> 4.0.0').satisfied_by?(rails_version) + end + alias_method :activerecord_4_0?, :rails_4_0? + + def rails_3_2? + Gem::Requirement.new('~> 3.2.0').satisfied_by?(rails_version) + end + alias_method :activerecord_3_2?, :rails_3_2? +end + +RSpec.configure do |config| + config.include(RailsVersions) + config.extend(RailsVersions) +end