awesome_print 1.6.1 → 1.9.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (93) hide show
  1. checksums.yaml +5 -5
  2. data/.gitignore +4 -0
  3. data/Appraisals +45 -22
  4. data/{CHANGELOG → CHANGELOG.md} +83 -18
  5. data/CONTRIBUTING.md +45 -4
  6. data/Gemfile.lock +28 -22
  7. data/LICENSE +16 -19
  8. data/README.md +97 -92
  9. data/Rakefile +5 -5
  10. data/awesome_print.gemspec +33 -0
  11. data/init.rb +1 -0
  12. data/lib/ap.rb +2 -2
  13. data/lib/awesome_print/colorize.rb +24 -0
  14. data/lib/awesome_print/core_ext/{array.rb → awesome_method_array.rb} +16 -18
  15. data/lib/awesome_print/core_ext/class.rb +3 -2
  16. data/lib/awesome_print/core_ext/kernel.rb +1 -1
  17. data/lib/awesome_print/core_ext/logger.rb +1 -1
  18. data/lib/awesome_print/core_ext/method.rb +2 -2
  19. data/lib/awesome_print/core_ext/object.rb +3 -2
  20. data/lib/awesome_print/core_ext/string.rb +3 -3
  21. data/lib/awesome_print/custom_defaults.rb +57 -0
  22. data/lib/awesome_print/ext/action_view.rb +8 -4
  23. data/lib/awesome_print/ext/active_record.rb +45 -12
  24. data/lib/awesome_print/ext/active_support.rb +1 -1
  25. data/lib/awesome_print/ext/mongo_mapper.rb +16 -13
  26. data/lib/awesome_print/ext/mongoid.rb +8 -6
  27. data/lib/awesome_print/ext/nobrainer.rb +52 -0
  28. data/lib/awesome_print/ext/nokogiri.rb +4 -4
  29. data/lib/awesome_print/ext/ostruct.rb +1 -1
  30. data/lib/awesome_print/ext/ripple.rb +5 -6
  31. data/lib/awesome_print/ext/sequel.rb +7 -6
  32. data/lib/awesome_print/formatter.rb +54 -327
  33. data/lib/awesome_print/formatters/array_formatter.rb +139 -0
  34. data/lib/awesome_print/formatters/base_formatter.rb +140 -0
  35. data/lib/awesome_print/formatters/class_formatter.rb +25 -0
  36. data/lib/awesome_print/formatters/dir_formatter.rb +22 -0
  37. data/lib/awesome_print/formatters/file_formatter.rb +22 -0
  38. data/lib/awesome_print/formatters/hash_formatter.rb +106 -0
  39. data/lib/awesome_print/formatters/method_formatter.rb +22 -0
  40. data/lib/awesome_print/formatters/object_formatter.rb +78 -0
  41. data/lib/awesome_print/formatters/simple_formatter.rb +21 -0
  42. data/lib/awesome_print/formatters/struct_formatter.rb +71 -0
  43. data/lib/awesome_print/formatters.rb +15 -0
  44. data/lib/awesome_print/indentator.rb +18 -0
  45. data/lib/awesome_print/inspector.rb +92 -94
  46. data/lib/awesome_print/version.rb +2 -2
  47. data/lib/awesome_print.rb +19 -18
  48. data/spec/active_record_helper.rb +24 -28
  49. data/spec/colors_spec.rb +31 -31
  50. data/spec/core_ext/logger_spec.rb +43 -0
  51. data/spec/core_ext/string_spec.rb +20 -0
  52. data/spec/ext/action_view_spec.rb +21 -0
  53. data/spec/ext/active_record_spec.rb +260 -0
  54. data/spec/ext/active_support_spec.rb +30 -0
  55. data/spec/ext/mongo_mapper_spec.rb +261 -0
  56. data/spec/ext/mongoid_spec.rb +67 -0
  57. data/spec/ext/nobrainer_spec.rb +59 -0
  58. data/spec/ext/nokogiri_spec.rb +46 -0
  59. data/spec/ext/ostruct_spec.rb +22 -0
  60. data/spec/ext/ripple_spec.rb +48 -0
  61. data/spec/formats_spec.rb +251 -184
  62. data/spec/methods_spec.rb +126 -130
  63. data/spec/misc_spec.rb +98 -93
  64. data/spec/objects_spec.rb +162 -27
  65. data/spec/spec_helper.rb +72 -38
  66. data/spec/support/active_record_data/3_2_diana.txt +24 -0
  67. data/spec/support/active_record_data/3_2_diana_legacy.txt +24 -0
  68. data/spec/support/active_record_data/3_2_multi.txt +50 -0
  69. data/spec/support/active_record_data/3_2_multi_legacy.txt +50 -0
  70. data/spec/support/active_record_data/4_0_diana.txt +98 -0
  71. data/spec/support/active_record_data/4_0_multi.txt +198 -0
  72. data/spec/support/active_record_data/4_1_diana.txt +97 -0
  73. data/spec/support/active_record_data/4_1_multi.txt +196 -0
  74. data/spec/support/active_record_data/4_2_diana.txt +109 -0
  75. data/spec/support/active_record_data/4_2_diana_legacy.txt +109 -0
  76. data/spec/support/active_record_data/4_2_multi.txt +220 -0
  77. data/spec/support/active_record_data/4_2_multi_legacy.txt +220 -0
  78. data/spec/support/active_record_data/5_0_diana.txt +105 -0
  79. data/spec/support/active_record_data/5_0_multi.txt +212 -0
  80. data/spec/support/active_record_data/5_1_diana.txt +104 -0
  81. data/spec/support/active_record_data/5_1_multi.txt +210 -0
  82. data/spec/support/active_record_data/5_2_diana.txt +104 -0
  83. data/spec/support/active_record_data/5_2_multi.txt +210 -0
  84. data/spec/support/active_record_data/6_0_diana.txt +104 -0
  85. data/spec/support/active_record_data/6_0_multi.txt +210 -0
  86. data/spec/support/active_record_data/6_1_diana.txt +109 -0
  87. data/spec/support/active_record_data/6_1_multi.txt +220 -0
  88. data/spec/support/active_record_data.rb +20 -0
  89. data/spec/support/ext_verifier.rb +42 -0
  90. data/spec/support/mongoid_versions.rb +26 -0
  91. data/spec/support/rails_versions.rb +55 -0
  92. metadata +104 -15
  93. data/lib/awesome_print/ext/no_brainer.rb +0 -58
@@ -1,87 +1,51 @@
1
- # Copyright (c) 2010-2013 Michael Dvorkin
1
+ # Copyright (c) 2010-2016 Michael Dvorkin and contributors
2
2
  #
3
3
  # Awesome Print is freely distributable under the terms of MIT license.
4
4
  # See LICENSE file or http://www.opensource.org/licenses/mit-license.php
5
5
  #------------------------------------------------------------------------------
6
- module AwesomePrint
7
-
8
- class << self # Class accessors for custom defaults.
9
- attr_accessor :defaults, :force_colors
10
-
11
- # Class accessor to force colorized output (ex. forked subprocess where TERM
12
- # might be dumb).
13
- #------------------------------------------------------------------------------
14
- def force_colors!(value = true)
15
- @force_colors = value
16
- end
17
-
18
- def console?
19
- !!(defined?(IRB) || defined?(Pry))
20
- end
21
-
22
- def rails_console?
23
- console? && !!(defined?(Rails::Console) || ENV["RAILS_ENV"])
24
- end
25
-
26
- def irb!
27
- return unless defined?(IRB)
28
- unless IRB.version.include?("DietRB")
29
- IRB::Irb.class_eval do
30
- def output_value
31
- ap @context.last_value
32
- end
33
- end
34
- else # MacRuby
35
- IRB.formatter = Class.new(IRB::Formatter) do
36
- def inspect_object(object)
37
- object.ai
38
- end
39
- end.new
40
- end
41
- end
42
-
43
- def pry!
44
- if defined?(Pry)
45
- Pry.print = proc { |output, value| output.puts value.ai }
46
- end
47
- end
48
- end
6
+ require_relative 'indentator'
49
7
 
8
+ module AwesomePrint
50
9
  class Inspector
51
- attr_accessor :options
10
+ attr_accessor :options, :indentator
52
11
 
53
12
  AP = :__awesome_print__
54
13
 
55
14
  def initialize(options = {})
56
- @options = {
57
- :indent => 4, # Indent using 4 spaces.
58
- :index => true, # Display array indices.
59
- :html => false, # Use ANSI color codes rather than HTML.
60
- :multiline => true, # Display in multiple lines.
61
- :plain => false, # Use colors.
62
- :raw => false, # Do not recursively format object instance variables.
63
- :sort_keys => false, # Do not sort hash keys.
64
- :limit => false, # Limit large output for arrays and hashes. Set to a boolean or integer.
65
- :color => {
66
- :args => :pale,
67
- :array => :white,
68
- :bigdecimal => :blue,
69
- :class => :yellow,
70
- :date => :greenish,
71
- :falseclass => :red,
72
- :fixnum => :blue,
73
- :float => :blue,
74
- :hash => :pale,
75
- :keyword => :cyan,
76
- :method => :purpleish,
77
- :nilclass => :red,
78
- :rational => :blue,
79
- :string => :yellowish,
80
- :struct => :pale,
81
- :symbol => :cyanish,
82
- :time => :greenish,
83
- :trueclass => :green,
84
- :variable => :cyanish
15
+ @options = {
16
+ indent: 4, # Number of spaces for indenting.
17
+ index: true, # Display array indices.
18
+ html: false, # Use ANSI color codes rather than HTML.
19
+ multiline: true, # Display in multiple lines.
20
+ plain: false, # Use colors.
21
+ raw: false, # Do not recursively format instance variables.
22
+ sort_keys: false, # Do not sort hash keys.
23
+ sort_vars: true, # Sort instance variables.
24
+ limit: false, # Limit arrays & hashes. Accepts bool or int.
25
+ ruby19_syntax: false, # Use Ruby 1.9 hash syntax in output.
26
+ class_name: :class, # Method used to get Instance class name.
27
+ object_id: true, # Show object_id.
28
+ color: {
29
+ args: :pale,
30
+ array: :white,
31
+ bigdecimal: :blue,
32
+ class: :yellow,
33
+ date: :greenish,
34
+ falseclass: :red,
35
+ fixnum: :blue,
36
+ integer: :blue,
37
+ float: :blue,
38
+ hash: :pale,
39
+ keyword: :cyan,
40
+ method: :purpleish,
41
+ nilclass: :red,
42
+ rational: :blue,
43
+ string: :yellowish,
44
+ struct: :pale,
45
+ symbol: :cyanish,
46
+ time: :greenish,
47
+ trueclass: :green,
48
+ variable: :cyanish
85
49
  }
86
50
  }
87
51
 
@@ -90,11 +54,20 @@ module AwesomePrint
90
54
  merge_options!(options)
91
55
 
92
56
  @formatter = AwesomePrint::Formatter.new(self)
57
+ @indentator = AwesomePrint::Indentator.new(@options[:indent].abs)
93
58
  Thread.current[AP] ||= []
94
59
  end
95
-
60
+
61
+ def current_indentation
62
+ indentator.indentation
63
+ end
64
+
65
+ def increase_indentation(&block)
66
+ indentator.indent(&block)
67
+ end
68
+
96
69
  # Dispatcher that detects data nesting and invokes object-aware formatter.
97
- #------------------------------------------------------------------------------
70
+ #---------------------------------------------------------------------------
98
71
  def awesome(object)
99
72
  if Thread.current[AP].include?(object.object_id)
100
73
  nested(object)
@@ -109,10 +82,18 @@ module AwesomePrint
109
82
  end
110
83
 
111
84
  # Return true if we are to colorize the output.
112
- #------------------------------------------------------------------------------
85
+ #---------------------------------------------------------------------------
113
86
  def colorize?
114
87
  AwesomePrint.force_colors ||= false
115
- AwesomePrint.force_colors || (STDOUT.tty? && ((ENV['TERM'] && ENV['TERM'] != 'dumb') || ENV['ANSICON']))
88
+ AwesomePrint.force_colors || (
89
+ STDOUT.tty? && (
90
+ (
91
+ ENV['TERM'] &&
92
+ ENV['TERM'] != 'dumb'
93
+ ) ||
94
+ ENV['ANSICON']
95
+ )
96
+ )
116
97
  end
117
98
 
118
99
  private
@@ -122,24 +103,25 @@ module AwesomePrint
122
103
  # => [1,2, [...]]
123
104
  # hash = { :a => 1 }; hash[:b] = hash
124
105
  # => { :a => 1, :b => {...} }
125
- #------------------------------------------------------------------------------
106
+ #---------------------------------------------------------------------------
126
107
  def nested(object)
127
108
  case printable(object)
128
- when :array then @formatter.colorize("[...]", :array)
129
- when :hash then @formatter.colorize("{...}", :hash)
130
- when :struct then @formatter.colorize("{...}", :struct)
131
- else @formatter.colorize("...#{object.class}...", :class)
109
+ when :array then @formatter.colorize('[...]', :array)
110
+ when :hash then @formatter.colorize('{...}', :hash)
111
+ when :struct then @formatter.colorize('{...}', :struct)
112
+ else @formatter.colorize("...#{object.class}...", :class)
132
113
  end
133
114
  end
134
115
 
135
- #------------------------------------------------------------------------------
116
+ #---------------------------------------------------------------------------
136
117
  def unnested(object)
137
118
  @formatter.format(object, printable(object))
138
119
  end
139
120
 
140
- # Turn class name into symbol, ex: Hello::World => :hello_world. Classes that
141
- # inherit from Array, Hash, File, Dir, and Struct are treated as the base class.
142
- #------------------------------------------------------------------------------
121
+ # Turn class name into symbol, ex: Hello::World => :hello_world. Classes
122
+ # that inherit from Array, Hash, File, Dir, and Struct are treated as the
123
+ # base class.
124
+ #---------------------------------------------------------------------------
143
125
  def printable(object)
144
126
  case object
145
127
  when Array then :array
@@ -147,25 +129,41 @@ module AwesomePrint
147
129
  when File then :file
148
130
  when Dir then :dir
149
131
  when Struct then :struct
150
- else object.class.to_s.gsub(/:+/, "_").downcase.to_sym
132
+ else object.class.to_s.gsub(/:+/, '_').downcase.to_sym
151
133
  end
152
134
  end
153
135
 
154
- # Update @options by first merging the :color hash and then the remaining keys.
155
- #------------------------------------------------------------------------------
136
+ # Update @options by first merging the :color hash and then the remaining
137
+ # keys.
138
+ #---------------------------------------------------------------------------
156
139
  def merge_options!(options = {})
157
140
  @options[:color].merge!(options.delete(:color) || {})
158
141
  @options.merge!(options)
159
142
  end
160
143
 
144
+ # This method needs to be mocked during testing so that it always loads
145
+ # predictable values
146
+ #---------------------------------------------------------------------------
147
+ def load_dotfile
148
+ dotfile = File.join(ENV['HOME'], '.aprc')
149
+ load dotfile if dotfile_readable?(dotfile)
150
+ end
151
+
152
+ def dotfile_readable? dotfile
153
+ if @@dotfile_readable.nil? || @@dotfile != dotfile
154
+ @@dotfile_readable = File.readable?(@@dotfile = dotfile)
155
+ end
156
+ @@dotfile_readable
157
+ end
158
+ @@dotfile_readable = @@dotfile = nil
159
+
161
160
  # Load ~/.aprc file with custom defaults that override default options.
162
- #------------------------------------------------------------------------------
161
+ #---------------------------------------------------------------------------
163
162
  def merge_custom_defaults!
164
- dotfile = File.join(ENV["HOME"], ".aprc")
165
- load dotfile if File.readable?(dotfile)
163
+ load_dotfile
166
164
  merge_options!(AwesomePrint.defaults) if AwesomePrint.defaults.is_a?(Hash)
167
165
  rescue => e
168
- $stderr.puts "Could not load #{dotfile}: #{e}"
166
+ $stderr.puts "Could not load '.aprc' from ENV['HOME']: #{e}"
169
167
  end
170
168
  end
171
169
  end
@@ -1,10 +1,10 @@
1
- # Copyright (c) 2010-2013 Michael Dvorkin
1
+ # Copyright (c) 2010-2016 Michael Dvorkin and contributors
2
2
  #
3
3
  # Awesome Print is freely distributable under the terms of MIT license.
4
4
  # See LICENSE file or http://www.opensource.org/licenses/mit-license.php
5
5
  #------------------------------------------------------------------------------
6
6
  module AwesomePrint
7
7
  def self.version
8
- "1.6.1"
8
+ '1.9.2'
9
9
  end
10
10
  end
data/lib/awesome_print.rb CHANGED
@@ -1,4 +1,4 @@
1
- # Copyright (c) 2010-2013 Michael Dvorkin
1
+ # Copyright (c) 2010-2016 Michael Dvorkin and contributors
2
2
  #
3
3
  # Awesome Print is freely distributable under the terms of MIT license.
4
4
  # See LICENSE file or http://www.opensource.org/licenses/mit-license.php
@@ -8,33 +8,34 @@
8
8
  # so do nothing for subsequent requires.
9
9
  #
10
10
  unless defined?(AwesomePrint::Inspector)
11
- %w(array string method object class kernel).each do |file|
12
- require File.dirname(__FILE__) + "/awesome_print/core_ext/#{file}"
11
+ %w(awesome_method_array string method object class kernel).each do |file|
12
+ require "awesome_print/core_ext/#{file}"
13
13
  end
14
14
 
15
- require File.dirname(__FILE__) + "/awesome_print/inspector"
16
- require File.dirname(__FILE__) + "/awesome_print/formatter"
17
- require File.dirname(__FILE__) + "/awesome_print/version"
18
- require File.dirname(__FILE__) + "/awesome_print/core_ext/logger" if defined?(Logger)
15
+ require 'awesome_print/custom_defaults'
16
+ require 'awesome_print/inspector'
17
+ require 'awesome_print/formatter'
18
+ require 'awesome_print/version'
19
+ require 'awesome_print/core_ext/logger' if defined?(Logger)
19
20
  #
20
21
  # Load the following under normal circumstances as well as in Rails
21
22
  # console when required from ~/.irbrc or ~/.pryrc.
22
23
  #
23
- require File.dirname(__FILE__) + "/awesome_print/ext/active_record" if defined?(ActiveRecord) || AwesomePrint.rails_console?
24
- require File.dirname(__FILE__) + "/awesome_print/ext/active_support" if defined?(ActiveSupport) || AwesomePrint.rails_console?
24
+ require 'awesome_print/ext/active_record' if defined?(ActiveRecord) || AwesomePrint.rails_console?
25
+ require 'awesome_print/ext/active_support' if defined?(ActiveSupport) || AwesomePrint.rails_console?
25
26
  #
26
27
  # Load remaining extensions.
27
28
  #
28
- if defined?(ActiveSupport)
29
+ if defined?(ActiveSupport.on_load)
29
30
  ActiveSupport.on_load(:action_view) do
30
- require File.dirname(__FILE__) + "/awesome_print/ext/action_view"
31
+ require 'awesome_print/ext/action_view'
31
32
  end
32
33
  end
33
- require File.dirname(__FILE__) + "/awesome_print/ext/mongo_mapper" if defined?(MongoMapper)
34
- require File.dirname(__FILE__) + "/awesome_print/ext/mongoid" if defined?(Mongoid)
35
- require File.dirname(__FILE__) + "/awesome_print/ext/nokogiri" if defined?(Nokogiri)
36
- require File.dirname(__FILE__) + "/awesome_print/ext/no_brainer" if defined?(NoBrainer)
37
- require File.dirname(__FILE__) + "/awesome_print/ext/ripple" if defined?(Ripple)
38
- require File.dirname(__FILE__) + "/awesome_print/ext/sequel" if defined?(Sequel)
39
- require File.dirname(__FILE__) + "/awesome_print/ext/ostruct" if defined?(OpenStruct)
34
+ require 'awesome_print/ext/mongo_mapper' if defined?(MongoMapper)
35
+ require 'awesome_print/ext/mongoid' if defined?(Mongoid)
36
+ require 'awesome_print/ext/nokogiri' if defined?(Nokogiri)
37
+ require 'awesome_print/ext/nobrainer' if defined?(NoBrainer)
38
+ require 'awesome_print/ext/ripple' if defined?(Ripple)
39
+ require 'awesome_print/ext/sequel' if defined?(Sequel)
40
+ require 'awesome_print/ext/ostruct' if defined?(OpenStruct)
40
41
  end
@@ -1,34 +1,30 @@
1
- require 'active_record'
2
-
3
- # Required to use the column support
4
- module Rails
5
- def self.env
6
- {}
1
+ if ExtVerifier.has_rails?
2
+ # Required to use the column support
3
+ module Rails
4
+ def self.env
5
+ {}
6
+ end
7
7
  end
8
- end
9
-
10
- # Establish connection to in-memory SQLite DB
11
- ActiveRecord::Base.establish_connection adapter: "sqlite3", database: ":memory:"
12
-
13
- # Create the users table
14
- ActiveRecord::Migration.verbose = false
15
- ActiveRecord::Migration.create_table :users do |t|
16
- t.string :name
17
- t.integer :rank
18
- t.boolean :admin
19
- t.datetime :created_at
20
- end
21
8
 
22
- # Create models
23
- class User < ActiveRecord::Base; end
24
- class SubUser < User; end
9
+ # Establish connection to in-memory SQLite DB
10
+ ActiveRecord::Base.establish_connection adapter: 'sqlite3', database: ':memory:'
25
11
 
12
+ # Create the users table
13
+ ActiveRecord::Migration.verbose = false
14
+ ActiveRecord::Migration.create_table :users do |t|
15
+ t.string :name
16
+ t.integer :rank
17
+ t.boolean :admin
18
+ t.datetime :created_at
19
+ end
26
20
 
27
- # Helper methods
28
- # ##############
21
+ ActiveRecord::Migration.create_table :emails do |t|
22
+ t.references :user
23
+ t.string :email_address
24
+ end
29
25
 
30
- # we only work with ActiveRecord 2+
31
- def is_usable_activerecord?
32
- defined?(ActiveRecord::VERSION::MAJOR) && ActiveRecord::VERSION::MAJOR >= 2
26
+ # Create models
27
+ class User < ActiveRecord::Base; has_many :emails; end
28
+ class SubUser < User; end
29
+ class Email < ActiveRecord::Base; belongs_to :user; end
33
30
  end
34
-
data/spec/colors_spec.rb CHANGED
@@ -1,6 +1,6 @@
1
- require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
1
+ require 'spec_helper'
2
2
 
3
- describe "AwesomePrint" do
3
+ RSpec.describe 'AwesomePrint' do
4
4
  def stub_tty!(output = true, stream = STDOUT)
5
5
  if output
6
6
  stream.instance_eval { def tty?; true; end }
@@ -9,94 +9,94 @@ describe "AwesomePrint" do
9
9
  end
10
10
  end
11
11
 
12
- before do
13
- stub_dotfile!
14
- end
15
-
16
- describe "colorization" do
12
+ describe 'colorization' do
17
13
  PLAIN = '[ 1, :two, "three", [ nil, [ true, false ] ] ]'
18
14
  COLORIZED = "[ \e[1;34m1\e[0m, \e[0;36m:two\e[0m, \e[0;33m\"three\"\e[0m, [ \e[1;31mnil\e[0m, [ \e[1;32mtrue\e[0m, \e[1;31mfalse\e[0m ] ] ]"
19
15
 
20
16
  before do
21
- ENV['TERM'] = "xterm-colors"
17
+ ENV['TERM'] = 'xterm-colors'
22
18
  ENV.delete('ANSICON')
23
- @arr = [ 1, :two, "three", [ nil, [ true, false] ] ]
19
+ @arr = [1, :two, 'three', [nil, [true, false]]]
24
20
  end
25
21
 
26
- describe "default settings (no forced colors)" do
22
+ describe 'default settings (no forced colors)' do
27
23
  before do
28
24
  AwesomePrint.force_colors! false
29
25
  end
30
26
 
31
- it "colorizes tty processes by default" do
27
+ it 'colorizes tty processes by default' do
32
28
  stub_tty!
33
- expect(@arr.ai(:multiline => false)).to eq(COLORIZED)
29
+ expect(@arr.ai(multiline: false)).to eq(COLORIZED)
34
30
  end
35
31
 
36
32
  it "colorizes processes with ENV['ANSICON'] by default" do
37
33
  begin
38
34
  stub_tty!
39
- term, ENV['ANSICON'] = ENV['ANSICON'], "1"
40
- expect(@arr.ai(:multiline => false)).to eq(COLORIZED)
35
+ term = ENV['ANSICON']
36
+ ENV['ANSICON'] = '1'
37
+ expect(@arr.ai(multiline: false)).to eq(COLORIZED)
41
38
  ensure
42
39
  ENV['ANSICON'] = term
43
40
  end
44
41
  end
45
42
 
46
- it "does not colorize tty processes running in dumb terminals by default" do
43
+ it 'does not colorize tty processes running in dumb terminals by default' do
47
44
  begin
48
45
  stub_tty!
49
- term, ENV['TERM'] = ENV['TERM'], "dumb"
50
- expect(@arr.ai(:multiline => false)).to eq(PLAIN)
46
+ term = ENV['TERM']
47
+ ENV['TERM'] = 'dumb'
48
+ expect(@arr.ai(multiline: false)).to eq(PLAIN)
51
49
  ensure
52
50
  ENV['TERM'] = term
53
51
  end
54
52
  end
55
53
 
56
- it "does not colorize subprocesses by default" do
54
+ it 'does not colorize subprocesses by default' do
57
55
  begin
58
56
  stub_tty! false
59
- expect(@arr.ai(:multiline => false)).to eq(PLAIN)
57
+ expect(@arr.ai(multiline: false)).to eq(PLAIN)
60
58
  ensure
61
59
  stub_tty!
62
60
  end
63
61
  end
64
62
  end
65
63
 
66
- describe "forced colors override" do
64
+ describe 'forced colors override' do
67
65
  before do
68
66
  AwesomePrint.force_colors!
69
67
  end
70
-
71
- it "still colorizes tty processes" do
68
+
69
+ it 'still colorizes tty processes' do
72
70
  stub_tty!
73
- expect(@arr.ai(:multiline => false)).to eq(COLORIZED)
71
+ expect(@arr.ai(multiline: false)).to eq(COLORIZED)
74
72
  end
75
73
 
76
74
  it "colorizes processes with ENV['ANSICON'] set to 0" do
77
75
  begin
78
76
  stub_tty!
79
- term, ENV['ANSICON'] = ENV['ANSICON'], "1"
80
- expect(@arr.ai(:multiline => false)).to eq(COLORIZED)
77
+ term = ENV['ANSICON']
78
+ ENV['ANSICON'] = '1'
79
+ expect(@arr.ai(multiline: false)).to eq(COLORIZED)
81
80
  ensure
82
81
  ENV['ANSICON'] = term
83
82
  end
84
83
  end
85
-
86
- it "colorizes dumb terminals" do
84
+
85
+ it 'colorizes dumb terminals' do
87
86
  begin
88
87
  stub_tty!
89
- term, ENV['TERM'] = ENV['TERM'], "dumb"
90
- expect(@arr.ai(:multiline => false)).to eq(COLORIZED)
88
+ term = ENV['TERM']
89
+ ENV['TERM'] = 'dumb'
90
+ expect(@arr.ai(multiline: false)).to eq(COLORIZED)
91
91
  ensure
92
92
  ENV['TERM'] = term
93
93
  end
94
94
  end
95
95
 
96
- it "colorizes subprocess" do
96
+ it 'colorizes subprocess' do
97
97
  begin
98
98
  stub_tty! false
99
- expect(@arr.ai(:multiline => false)).to eq(COLORIZED)
99
+ expect(@arr.ai(multiline: false)).to eq(COLORIZED)
100
100
  ensure
101
101
  stub_tty!
102
102
  end
@@ -0,0 +1,43 @@
1
+ require 'spec_helper'
2
+
3
+
4
+ require 'logger'
5
+ require 'awesome_print/core_ext/logger'
6
+
7
+ RSpec.describe 'AwesomePrint logging extensions' do
8
+ before(:all) do
9
+ @logger = Logger.new('/dev/null') rescue Logger.new('nul')
10
+ end
11
+
12
+ describe 'ap method' do
13
+ it 'should awesome_inspect the given object' do
14
+ object = double
15
+ expect(object).to receive(:ai)
16
+ @logger.ap object
17
+ end
18
+
19
+ describe 'the log level' do
20
+ before do
21
+ AwesomePrint.defaults = {}
22
+ end
23
+
24
+ it 'should fallback to the default :debug log level' do
25
+ expect(@logger).to receive(:debug)
26
+ @logger.ap(nil)
27
+ end
28
+
29
+ it 'should use the global user default if no level passed' do
30
+ AwesomePrint.defaults = { log_level: :info }
31
+ expect(@logger).to receive(:info)
32
+ @logger.ap(nil)
33
+ end
34
+
35
+ it 'should use the passed in level' do
36
+ expect(@logger).to receive(:warn)
37
+ @logger.ap(nil, :warn)
38
+ end
39
+ end
40
+ end
41
+ end
42
+
43
+
@@ -0,0 +1,20 @@
1
+ require 'spec_helper'
2
+
3
+ RSpec.describe 'String extensions' do
4
+ [:gray, :red, :green, :yellow, :blue, :purple, :cyan, :white].each_with_index do |color, i|
5
+ it "should have #{color} color" do
6
+ expect(color.to_s.send(color)).to eq("\e[1;#{30 + i}m#{color}\e[0m")
7
+ end
8
+
9
+ it "should have #{color}ish color" do
10
+ expect(color.to_s.send(:"#{color}ish")).to eq("\e[0;#{30 + i}m#{color}\e[0m")
11
+ end
12
+ end
13
+
14
+ it 'should have black and pale colors' do
15
+ expect('black'.send(:black)).to eq('black'.send(:grayish))
16
+ expect('pale'.send(:pale)).to eq('pale'.send(:whiteish))
17
+ expect('pale'.send(:pale)).to eq("\e[0;37mpale\e[0m")
18
+ expect('whiteish'.send(:whiteish)).to eq("\e[0;37mwhiteish\e[0m")
19
+ end
20
+ end
@@ -0,0 +1,21 @@
1
+ require 'spec_helper'
2
+
3
+ RSpec.describe 'AwesomePrint ActionView extensions', skip: -> { !ExtVerifier.has_rails? }.call do
4
+ before do
5
+ @view = if rails_6_1?
6
+ ActionView::Base.new(ActionView::LookupContext.new([]), {}, {})
7
+ else
8
+ ActionView::Base.new
9
+ end
10
+ end
11
+
12
+ it "uses HTML and adds 'debug_dump' class to plain <pre> tag" do
13
+ markup = rand
14
+ expect(@view.ap(markup, plain: true)).to eq(%Q|<pre class="debug_dump">#{markup}</pre>|)
15
+ end
16
+
17
+ it "uses HTML and adds 'debug_dump' class to colorized <pre> tag" do
18
+ markup = ' &<hello>'
19
+ expect(@view.ap(markup)).to eq('<pre class="debug_dump"><kbd style="color:brown">&quot; &amp;&lt;hello&gt;&quot;</kbd></pre>')
20
+ end
21
+ end