From bb5e75125b9b027b5dcf0d955531ff85b64578da Mon Sep 17 00:00:00 2001 From: James Cox Date: Tue, 22 Jan 2019 12:48:03 -0500 Subject: [PATCH] Further fix tests, set some formatters as core (too generic) and clean up more specs --- lib/awesome_print.rb | 1 + lib/awesome_print/colorize.rb | 2 +- lib/awesome_print/formatter.rb | 25 +++++--- .../formatters/base_formatter.rb | 8 +++ .../formatters/bigdecimal_formatter.rb | 3 +- lib/awesome_print/formatters/dir_formatter.rb | 2 +- .../formatters/fallback_formatter.rb | 4 ++ .../formatters/method_formatter.rb | 4 ++ .../formatters/object_formatter.rb | 4 ++ .../formatters/rational_formatter.rb | 4 ++ lib/awesome_print/formatters/set_formatter.rb | 19 ++++++ .../formatters/simple_formatter.rb | 4 ++ lib/awesome_print/version.rb | 5 ++ spec/ext/nobrainer_spec.rb | 59 ------------------- spec/ext/ripple_spec.rb | 48 --------------- spec/formatters/dir_file_spec.rb | 1 + 16 files changed, 75 insertions(+), 118 deletions(-) create mode 100644 lib/awesome_print/formatters/set_formatter.rb delete mode 100644 spec/ext/nobrainer_spec.rb delete mode 100644 spec/ext/ripple_spec.rb diff --git a/lib/awesome_print.rb b/lib/awesome_print.rb index eeaa7a4..efc1e25 100644 --- a/lib/awesome_print.rb +++ b/lib/awesome_print.rb @@ -15,6 +15,7 @@ unless defined?(AwesomePrint::Inspector) require 'awesome_print/custom_defaults' require 'awesome_print/inspector' require 'awesome_print/formatter' + Dir["./lib/awesome_print/formatters/**/*.rb"].each { |f| require f } require 'awesome_print/version' diff --git a/lib/awesome_print/colorize.rb b/lib/awesome_print/colorize.rb index 70e3335..0d0742d 100644 --- a/lib/awesome_print/colorize.rb +++ b/lib/awesome_print/colorize.rb @@ -6,7 +6,7 @@ module AwesomePrint # Pick the color and apply it to the given string as necessary. #------------------------------------------------------------------------------ def colorize(str, type) - puts "[COLORIZING] - using #{options[:color][type]} for #{type}" + puts "[COLORIZING] - using #{options[:color][type]} for #{type}" if AwesomePrint.debug str = CGI.escapeHTML(str) if options[:html] if options[:plain] || !options[:color][type] || !inspector.colorize? diff --git a/lib/awesome_print/formatter.rb b/lib/awesome_print/formatter.rb index 49cae34..c5c996b 100644 --- a/lib/awesome_print/formatter.rb +++ b/lib/awesome_print/formatter.rb @@ -31,18 +31,27 @@ module AwesomePrint # type is determined by Inspector#printable #------------------------------------------------------------------------------ def format(object, type = nil) - puts "[FORMAT] #{type.to_s.red} >>> #{object}" + puts "[FORMAT] #{type.to_s.red} >>> #{object}" if AwesomePrint.debug format_with = active_formatter(type) - puts "[ACTIVE] using > #{format_with.to_s.blueish} < to format" + puts "[ACTIVE] using > #{format_with.to_s.blueish} < to format" if AwesomePrint.debug - if format_with && format_with.send(:formattable?, object) - format_with.new(@inspector).format(object) - else - puts "[FALLBACK] well darn, we're just gonna have to fb" - # in this case, formatter is missing or fails format test - AwesomePrint::Formatters::FallbackFormatter.new(@inspector).format(object) + # if we have an active formatter, and it's good to go, lets return that + + return format_with.new(@inspector).format(object) if format_with&.formattable?(object) + + # if that's not working, lets try discover the format via formattable? + self.class.registered_formatters.each do |fmt| + puts "[FORMATTABLE] trying to use #{fmt} - #{fmt.last.core?}" if AwesomePrint.debug + next if fmt.last.core? # if it's a core level formatter, move on + + return fmt.last.new(@inspector).format(object) if fmt.last.formattable?(object) end + + # we've run out of options. lets try and coerce into something we can work + # with + puts "[FALLBACK] well darn, we're just gonna have to fb" if AwesomePrint.debug + AwesomePrint::Formatters::FallbackFormatter.new(@inspector).format(object) end def active_formatter(type) diff --git a/lib/awesome_print/formatters/base_formatter.rb b/lib/awesome_print/formatters/base_formatter.rb index 61897a1..4f67bc5 100644 --- a/lib/awesome_print/formatters/base_formatter.rb +++ b/lib/awesome_print/formatters/base_formatter.rb @@ -12,6 +12,14 @@ module AwesomePrint attr_reader :object, :inspector, :options + def self.formattable?(obj) + false + end + + def self.core? + false + end + def initialize(inspector) @inspector = inspector @options = inspector.options diff --git a/lib/awesome_print/formatters/bigdecimal_formatter.rb b/lib/awesome_print/formatters/bigdecimal_formatter.rb index a626b93..46cf2cc 100644 --- a/lib/awesome_print/formatters/bigdecimal_formatter.rb +++ b/lib/awesome_print/formatters/bigdecimal_formatter.rb @@ -1,4 +1,5 @@ require_relative 'base_formatter' +require 'bigdecimal' module AwesomePrint module Formatters @@ -7,7 +8,7 @@ module AwesomePrint formatter_for :bigdecimal def self.formattable?(object) - true + object.is_a?(BigDecimal) end def format(object) diff --git a/lib/awesome_print/formatters/dir_formatter.rb b/lib/awesome_print/formatters/dir_formatter.rb index 69bff1d..521a641 100644 --- a/lib/awesome_print/formatters/dir_formatter.rb +++ b/lib/awesome_print/formatters/dir_formatter.rb @@ -8,7 +8,7 @@ module AwesomePrint formatter_for :dir def self.formattable?(object) - object.is_a?(Dir) + object.kind_of?(Dir) end def format(dir) diff --git a/lib/awesome_print/formatters/fallback_formatter.rb b/lib/awesome_print/formatters/fallback_formatter.rb index 083e298..f6fd24e 100644 --- a/lib/awesome_print/formatters/fallback_formatter.rb +++ b/lib/awesome_print/formatters/fallback_formatter.rb @@ -12,6 +12,10 @@ module AwesomePrint true end + def self.core? + true + end + def format(object) if @options[:raw] && object.instance_variables.any? Formatters::ObjectFormatter.new(@inspector).format(object) diff --git a/lib/awesome_print/formatters/method_formatter.rb b/lib/awesome_print/formatters/method_formatter.rb index a61312f..af0db55 100644 --- a/lib/awesome_print/formatters/method_formatter.rb +++ b/lib/awesome_print/formatters/method_formatter.rb @@ -6,6 +6,10 @@ module AwesomePrint formatter_for :method + def self.core? + true + end + def self.formattable?(object) puts "formattable? for METHOD..." true diff --git a/lib/awesome_print/formatters/object_formatter.rb b/lib/awesome_print/formatters/object_formatter.rb index 0b0b344..67c2bef 100644 --- a/lib/awesome_print/formatters/object_formatter.rb +++ b/lib/awesome_print/formatters/object_formatter.rb @@ -6,6 +6,10 @@ module AwesomePrint formatter_for :object + def self.core? + true + end + def self.formattable?(object) object.is_a?(Object) end diff --git a/lib/awesome_print/formatters/rational_formatter.rb b/lib/awesome_print/formatters/rational_formatter.rb index c91e632..e75c25d 100644 --- a/lib/awesome_print/formatters/rational_formatter.rb +++ b/lib/awesome_print/formatters/rational_formatter.rb @@ -6,6 +6,10 @@ module AwesomePrint formatter_for :rational + def self.formattable?(object) + object.is_a?(Rational) + end + end end end diff --git a/lib/awesome_print/formatters/set_formatter.rb b/lib/awesome_print/formatters/set_formatter.rb new file mode 100644 index 0000000..35adfba --- /dev/null +++ b/lib/awesome_print/formatters/set_formatter.rb @@ -0,0 +1,19 @@ +require_relative 'base_formatter' + +module AwesomePrint + module Formatters + class SetFormatter < BaseFormatter + + formatter_for :set + + def self.formattable?(object) + object.kind_of?(Set) + end + + def format(object) + Formatters::ArrayFormatter.new(@inspector).format(object.to_a) + end + + end + end +end diff --git a/lib/awesome_print/formatters/simple_formatter.rb b/lib/awesome_print/formatters/simple_formatter.rb index 04d403e..2aa7138 100644 --- a/lib/awesome_print/formatters/simple_formatter.rb +++ b/lib/awesome_print/formatters/simple_formatter.rb @@ -6,6 +6,10 @@ module AwesomePrint formatter_for :simple + def self.core? + true + end + def self.formattable?(object) object.respond_to?(:to_s) end diff --git a/lib/awesome_print/version.rb b/lib/awesome_print/version.rb index bc7cf95..f937384 100644 --- a/lib/awesome_print/version.rb +++ b/lib/awesome_print/version.rb @@ -4,6 +4,11 @@ # See LICENSE file or http://www.opensource.org/licenses/mit-license.php #------------------------------------------------------------------------------ module AwesomePrint + + def self.debug + false + end + def self.version '1.8.0' end diff --git a/spec/ext/nobrainer_spec.rb b/spec/ext/nobrainer_spec.rb deleted file mode 100644 index e08c83b..0000000 --- a/spec/ext/nobrainer_spec.rb +++ /dev/null @@ -1,59 +0,0 @@ -require 'spec_helper' - -RSpec.describe 'AwesomePrint/NoBrainer', skip: -> { !ExtVerifier.has_nobrainer? }.call do - - if ExtVerifier.has_nobrainer? - before :all do - NoBrainer.configure do |config| - config.app_name = 'ap_test' - config.environment = :test - end - end - - before :all do - class SomeModel - include NoBrainer::Document - - field :first_name, type: String - field :last_name, type: String - field :some_field - end - end - - after :all do - Object.instance_eval { remove_const :SomeModel } - end - end - - before do - @ap = AwesomePrint::Inspector.new plain: true - end - - it 'should print class instance' do - user = SomeModel.new first_name: 'Al', last_name: 'Capone' - out = @ap.send :awesome, user - - object_id = user.id.inspect - str = <<-EOS.strip -# { - :id => #{object_id}, - :first_name => "Al", - :last_name => "Capone" -} - EOS - expect(out).to eq(str) - end - - it 'should print the class' do - class_spec = <<-EOS.strip -class SomeModel < Object { - :id => :string, - :first_name => :string, - :last_name => :string, - :some_field => :object -} - EOS - - expect(@ap.send(:awesome, SomeModel)).to eq class_spec - end -end diff --git a/spec/ext/ripple_spec.rb b/spec/ext/ripple_spec.rb deleted file mode 100644 index 01eef65..0000000 --- a/spec/ext/ripple_spec.rb +++ /dev/null @@ -1,48 +0,0 @@ -require 'spec_helper' - -RSpec.describe 'AwesomePrint/Ripple', skip: -> { !ExtVerifier.has_ripple? }.call do - - if ExtVerifier.has_ripple? - before :all do - class RippleUser - include Ripple::Document - - key_on :_id - property :_id, String - property :first_name, String - property :last_name, String - end - end - - after :all do - Object.instance_eval { remove_const :RippleUser } - end - end - - before do - @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 - - expect(out).to be_similar_to <<-EOS.strip -# { - :_id => "12345", - :first_name => "Al", - :last_name => "Capone" -} - EOS - end - - 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 -end diff --git a/spec/formatters/dir_file_spec.rb b/spec/formatters/dir_file_spec.rb index e87d915..c542d8a 100644 --- a/spec/formatters/dir_file_spec.rb +++ b/spec/formatters/dir_file_spec.rb @@ -35,6 +35,7 @@ RSpec.describe 'AwesomePrint' do require 'tmpdir' my = My.new(Dir.tmpdir) + expect(my.ai(plain: true)).to eq("#{my.inspect}\n" << `ls -alF #{my.path}`.chop) end