mirror of
https://github.com/awesome-print/awesome_print
synced 2023-03-27 23:22:34 -04:00
Further fix tests, set some formatters as core (too generic) and clean up more specs
This commit is contained in:
parent
3738676ed6
commit
bb5e75125b
16 changed files with 75 additions and 118 deletions
|
@ -15,6 +15,7 @@ unless defined?(AwesomePrint::Inspector)
|
||||||
require 'awesome_print/custom_defaults'
|
require 'awesome_print/custom_defaults'
|
||||||
require 'awesome_print/inspector'
|
require 'awesome_print/inspector'
|
||||||
require 'awesome_print/formatter'
|
require 'awesome_print/formatter'
|
||||||
|
|
||||||
Dir["./lib/awesome_print/formatters/**/*.rb"].each { |f| require f }
|
Dir["./lib/awesome_print/formatters/**/*.rb"].each { |f| require f }
|
||||||
|
|
||||||
require 'awesome_print/version'
|
require 'awesome_print/version'
|
||||||
|
|
|
@ -6,7 +6,7 @@ module AwesomePrint
|
||||||
# Pick the color and apply it to the given string as necessary.
|
# Pick the color and apply it to the given string as necessary.
|
||||||
#------------------------------------------------------------------------------
|
#------------------------------------------------------------------------------
|
||||||
def colorize(str, type)
|
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]
|
str = CGI.escapeHTML(str) if options[:html]
|
||||||
if options[:plain] || !options[:color][type] || !inspector.colorize?
|
if options[:plain] || !options[:color][type] || !inspector.colorize?
|
||||||
|
|
|
@ -31,18 +31,27 @@ module AwesomePrint
|
||||||
# type is determined by Inspector#printable
|
# type is determined by Inspector#printable
|
||||||
#------------------------------------------------------------------------------
|
#------------------------------------------------------------------------------
|
||||||
def format(object, type = nil)
|
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)
|
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)
|
# if we have an active formatter, and it's good to go, lets return that
|
||||||
format_with.new(@inspector).format(object)
|
|
||||||
else
|
return format_with.new(@inspector).format(object) if format_with&.formattable?(object)
|
||||||
puts "[FALLBACK] well darn, we're just gonna have to fb"
|
|
||||||
# in this case, formatter is missing or fails format test
|
# if that's not working, lets try discover the format via formattable?
|
||||||
AwesomePrint::Formatters::FallbackFormatter.new(@inspector).format(object)
|
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
|
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
|
end
|
||||||
|
|
||||||
def active_formatter(type)
|
def active_formatter(type)
|
||||||
|
|
|
@ -12,6 +12,14 @@ module AwesomePrint
|
||||||
|
|
||||||
attr_reader :object, :inspector, :options
|
attr_reader :object, :inspector, :options
|
||||||
|
|
||||||
|
def self.formattable?(obj)
|
||||||
|
false
|
||||||
|
end
|
||||||
|
|
||||||
|
def self.core?
|
||||||
|
false
|
||||||
|
end
|
||||||
|
|
||||||
def initialize(inspector)
|
def initialize(inspector)
|
||||||
@inspector = inspector
|
@inspector = inspector
|
||||||
@options = inspector.options
|
@options = inspector.options
|
||||||
|
|
|
@ -1,4 +1,5 @@
|
||||||
require_relative 'base_formatter'
|
require_relative 'base_formatter'
|
||||||
|
require 'bigdecimal'
|
||||||
|
|
||||||
module AwesomePrint
|
module AwesomePrint
|
||||||
module Formatters
|
module Formatters
|
||||||
|
@ -7,7 +8,7 @@ module AwesomePrint
|
||||||
formatter_for :bigdecimal
|
formatter_for :bigdecimal
|
||||||
|
|
||||||
def self.formattable?(object)
|
def self.formattable?(object)
|
||||||
true
|
object.is_a?(BigDecimal)
|
||||||
end
|
end
|
||||||
|
|
||||||
def format(object)
|
def format(object)
|
||||||
|
|
|
@ -8,7 +8,7 @@ module AwesomePrint
|
||||||
formatter_for :dir
|
formatter_for :dir
|
||||||
|
|
||||||
def self.formattable?(object)
|
def self.formattable?(object)
|
||||||
object.is_a?(Dir)
|
object.kind_of?(Dir)
|
||||||
end
|
end
|
||||||
|
|
||||||
def format(dir)
|
def format(dir)
|
||||||
|
|
|
@ -12,6 +12,10 @@ module AwesomePrint
|
||||||
true
|
true
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def self.core?
|
||||||
|
true
|
||||||
|
end
|
||||||
|
|
||||||
def format(object)
|
def format(object)
|
||||||
if @options[:raw] && object.instance_variables.any?
|
if @options[:raw] && object.instance_variables.any?
|
||||||
Formatters::ObjectFormatter.new(@inspector).format(object)
|
Formatters::ObjectFormatter.new(@inspector).format(object)
|
||||||
|
|
|
@ -6,6 +6,10 @@ module AwesomePrint
|
||||||
|
|
||||||
formatter_for :method
|
formatter_for :method
|
||||||
|
|
||||||
|
def self.core?
|
||||||
|
true
|
||||||
|
end
|
||||||
|
|
||||||
def self.formattable?(object)
|
def self.formattable?(object)
|
||||||
puts "formattable? for METHOD..."
|
puts "formattable? for METHOD..."
|
||||||
true
|
true
|
||||||
|
|
|
@ -6,6 +6,10 @@ module AwesomePrint
|
||||||
|
|
||||||
formatter_for :object
|
formatter_for :object
|
||||||
|
|
||||||
|
def self.core?
|
||||||
|
true
|
||||||
|
end
|
||||||
|
|
||||||
def self.formattable?(object)
|
def self.formattable?(object)
|
||||||
object.is_a?(Object)
|
object.is_a?(Object)
|
||||||
end
|
end
|
||||||
|
|
|
@ -6,6 +6,10 @@ module AwesomePrint
|
||||||
|
|
||||||
formatter_for :rational
|
formatter_for :rational
|
||||||
|
|
||||||
|
def self.formattable?(object)
|
||||||
|
object.is_a?(Rational)
|
||||||
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
19
lib/awesome_print/formatters/set_formatter.rb
Normal file
19
lib/awesome_print/formatters/set_formatter.rb
Normal file
|
@ -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
|
|
@ -6,6 +6,10 @@ module AwesomePrint
|
||||||
|
|
||||||
formatter_for :simple
|
formatter_for :simple
|
||||||
|
|
||||||
|
def self.core?
|
||||||
|
true
|
||||||
|
end
|
||||||
|
|
||||||
def self.formattable?(object)
|
def self.formattable?(object)
|
||||||
object.respond_to?(:to_s)
|
object.respond_to?(:to_s)
|
||||||
end
|
end
|
||||||
|
|
|
@ -4,6 +4,11 @@
|
||||||
# See LICENSE file or http://www.opensource.org/licenses/mit-license.php
|
# See LICENSE file or http://www.opensource.org/licenses/mit-license.php
|
||||||
#------------------------------------------------------------------------------
|
#------------------------------------------------------------------------------
|
||||||
module AwesomePrint
|
module AwesomePrint
|
||||||
|
|
||||||
|
def self.debug
|
||||||
|
false
|
||||||
|
end
|
||||||
|
|
||||||
def self.version
|
def self.version
|
||||||
'1.8.0'
|
'1.8.0'
|
||||||
end
|
end
|
||||||
|
|
|
@ -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
|
|
||||||
#<SomeModel id: #{object_id}> {
|
|
||||||
: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
|
|
|
@ -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
|
|
||||||
#<RippleUser:placeholder_id> {
|
|
||||||
:_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
|
|
|
@ -35,6 +35,7 @@ RSpec.describe 'AwesomePrint' do
|
||||||
|
|
||||||
require 'tmpdir'
|
require 'tmpdir'
|
||||||
my = My.new(Dir.tmpdir)
|
my = My.new(Dir.tmpdir)
|
||||||
|
|
||||||
expect(my.ai(plain: true)).to eq("#{my.inspect}\n" << `ls -alF #{my.path}`.chop)
|
expect(my.ai(plain: true)).to eq("#{my.inspect}\n" << `ls -alF #{my.path}`.chop)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue