1
0
Fork 0
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:
James Cox 2019-01-22 12:48:03 -05:00
parent 3738676ed6
commit bb5e75125b
16 changed files with 75 additions and 118 deletions

View file

@ -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'

View file

@ -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?

View file

@ -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)

View file

@ -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

View file

@ -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)

View file

@ -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)

View file

@ -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)

View file

@ -6,6 +6,10 @@ module AwesomePrint
formatter_for :method
def self.core?
true
end
def self.formattable?(object)
puts "formattable? for METHOD..."
true

View file

@ -6,6 +6,10 @@ module AwesomePrint
formatter_for :object
def self.core?
true
end
def self.formattable?(object)
object.is_a?(Object)
end

View file

@ -6,6 +6,10 @@ module AwesomePrint
formatter_for :rational
def self.formattable?(object)
object.is_a?(Rational)
end
end
end
end

View 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

View file

@ -6,6 +6,10 @@ module AwesomePrint
formatter_for :simple
def self.core?
true
end
def self.formattable?(object)
object.respond_to?(:to_s)
end

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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