awesome_print 1.0.2 → 1.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/.gitignore +1 -0
- data/CHANGELOG +11 -0
- data/Gemfile.lock +1 -1
- data/LICENSE +2 -2
- data/README.md +14 -21
- data/Rakefile +10 -1
- data/lib/ap.rb +1 -1
- data/lib/awesome_print.rb +11 -9
- data/lib/awesome_print/core_ext/array.rb +12 -2
- data/lib/awesome_print/core_ext/class.rb +1 -1
- data/lib/awesome_print/core_ext/kernel.rb +2 -2
- data/lib/awesome_print/core_ext/logger.rb +1 -1
- data/lib/awesome_print/core_ext/method.rb +1 -1
- data/lib/awesome_print/core_ext/object.rb +1 -1
- data/lib/awesome_print/core_ext/string.rb +1 -1
- data/lib/awesome_print/ext/action_view.rb +1 -1
- data/lib/awesome_print/ext/active_record.rb +3 -1
- data/lib/awesome_print/ext/active_support.rb +2 -2
- data/lib/awesome_print/ext/mongo_mapper.rb +86 -3
- data/lib/awesome_print/ext/mongoid.rb +2 -2
- data/lib/awesome_print/ext/nokogiri.rb +1 -1
- data/lib/awesome_print/formatter.rb +26 -11
- data/lib/awesome_print/inspector.rb +36 -5
- data/lib/awesome_print/version.rb +2 -2
- data/spec/formats_spec.rb +18 -83
- data/spec/methods_spec.rb +10 -9
- data/spec/misc_spec.rb +213 -0
- data/spec/objects_spec.rb +11 -5
- data/spec/spec_helper.rb +15 -2
- metadata +26 -3
data/.gitignore
CHANGED
data/CHANGELOG
CHANGED
@@ -1,3 +1,14 @@
|
|
1
|
+
1.1.0
|
2
|
+
- Objects are no longer recursively formatted by default. Reenable by using :raw => true option.
|
3
|
+
- ap(object) now returns nil when running under IRB or Pry
|
4
|
+
- Added support for Mongoid 3 and Moped (Nikolaj Nikolajsen)
|
5
|
+
- Improved formatting of MongoMapper objects (George .)
|
6
|
+
- ActiveRecord::Relation now renders as array (Dan Lynn)
|
7
|
+
- Formatting BigDecimal no longer looses precision (Evan Senter)
|
8
|
+
- Added AwesomePrint.irb! and AwesomePrint.pry! convenience methods
|
9
|
+
- Fixed conflict with the colorize gem
|
10
|
+
- Misc tweaks and bug fixes
|
11
|
+
|
1
12
|
1.0.2
|
2
13
|
- Added formatting of Mongoid documents (Adam Doppelt)
|
3
14
|
- ActiveRecord objects display attributes only. Use :raw => true to display the entire object
|
data/Gemfile.lock
CHANGED
data/LICENSE
CHANGED
data/README.md
CHANGED
@@ -22,6 +22,7 @@ Default options:
|
|
22
22
|
:html => false, # Use ANSI color codes rather than HTML.
|
23
23
|
:multiline => true, # Display in multiple lines.
|
24
24
|
:plain => false, # Use colors.
|
25
|
+
:raw => false, # Do not recursively format object instance variables.
|
25
26
|
:sort_keys => false, # Do not sort hash keys.
|
26
27
|
:limit => false, # Limit large output for arrays and hashes. Set to a boolean or integer.
|
27
28
|
:color => {
|
@@ -37,6 +38,7 @@ Default options:
|
|
37
38
|
:keyword => :cyan,
|
38
39
|
:method => :purpleish,
|
39
40
|
:nilclass => :red,
|
41
|
+
:rational => :blue,
|
40
42
|
:string => :yellowish,
|
41
43
|
:struct => :pale,
|
42
44
|
:symbol => :cyanish,
|
@@ -231,31 +233,15 @@ Supported color names:
|
|
231
233
|
To use awesome_print as default formatter in irb and Rails console add the following
|
232
234
|
code to your ~/.irbrc file:
|
233
235
|
|
234
|
-
require "rubygems"
|
235
236
|
require "awesome_print"
|
236
|
-
|
237
|
-
unless IRB.version.include?('DietRB')
|
238
|
-
IRB::Irb.class_eval do
|
239
|
-
def output_value
|
240
|
-
ap @context.last_value
|
241
|
-
end
|
242
|
-
end
|
243
|
-
else # MacRuby
|
244
|
-
IRB.formatter = Class.new(IRB::Formatter) do
|
245
|
-
def inspect_object(object)
|
246
|
-
object.ai
|
247
|
-
end
|
248
|
-
end.new
|
249
|
-
end
|
237
|
+
AwesomePrint.irb!
|
250
238
|
|
251
239
|
### PRY integration ###
|
252
240
|
If you miss awesome_print's way of formatting output, here's how you can use it in place
|
253
241
|
of the formatting which comes with pry. Add the following code to your ~/.pryrc:
|
254
242
|
|
255
|
-
require "rubygems"
|
256
243
|
require "awesome_print"
|
257
|
-
|
258
|
-
Pry.print = proc { |output, value| output.puts value.ai }
|
244
|
+
AwesomePrint.pry!
|
259
245
|
|
260
246
|
### Logger Convenience Method ###
|
261
247
|
awesome_print adds the 'ap' method to the Logger and ActiveSupport::BufferedLogger classes
|
@@ -308,31 +294,38 @@ For example:
|
|
308
294
|
* Make your feature addition or bug fix.
|
309
295
|
* Add specs for it, making sure $ rake spec is all green.
|
310
296
|
* Commit, do not mess with rakefile, version, or history.
|
311
|
-
* Send me
|
297
|
+
* Send me commit URL (*do not* send me pull requests).
|
312
298
|
|
313
299
|
### Contributors ###
|
314
300
|
|
315
301
|
* Adam Doppelt -- https://github.com/gurgeous
|
316
302
|
* Andrew O'Brien -- https://github.com/AndrewO
|
317
303
|
* Andrew Horsman -- https://github.com/basicxman
|
304
|
+
* Barry Allard -- https://github.com/steakknife
|
318
305
|
* Benoit Daloze -- http://github.com/eregon
|
319
306
|
* Brandon Zylstra -- https://github.com/brandondrew
|
307
|
+
* Dan Lynn -- https://github.com/danlynn
|
320
308
|
* Daniel Johnson -- https://github.com/adhd360
|
321
309
|
* Daniel Bretoi -- http://github.com/danielb2
|
322
310
|
* Eloy Duran -- http://github.com/alloy
|
323
311
|
* Elpizo Choi -- https://github.com/fuJiin
|
312
|
+
* Evan Senter -- https://github.com/evansenter
|
313
|
+
* George . -- https://github.com/gardelea
|
324
314
|
* Greg Weber -- https://github.com/gregwebs
|
325
315
|
* Jeff Felchner -- https://github.com/jfelchner
|
316
|
+
* Nikolaj Nikolajsen -- https://github.com/nikolajsen
|
317
|
+
* Ryan Schlesinger -- https://github.com/ryansch
|
326
318
|
* Sean Gallagher -- http://github.com/torandu
|
327
319
|
* Stephan Hagemann -- https://github.com/shageman
|
328
320
|
* Tim Harper -- http://github.com/timcharper
|
329
321
|
* Tobias Crawley -- http://github.com/tobias
|
322
|
+
* Thibaut Barrère -- https://github.com/thbar
|
330
323
|
* Viktar Basharymau -- https://github.com/DNNX
|
331
324
|
|
332
325
|
### License ###
|
333
|
-
Copyright (c) 2010-
|
326
|
+
Copyright (c) 2010-2012 Michael Dvorkin
|
334
327
|
|
335
|
-
|
328
|
+
http://www.dvorkin.net
|
336
329
|
|
337
330
|
%w(mike dvorkin.net) * "@" || %w(mike fatfreecrm.com) * "@"
|
338
331
|
|
data/Rakefile
CHANGED
@@ -1,2 +1,11 @@
|
|
1
|
-
require
|
1
|
+
require "bundler"
|
2
2
|
Bundler::GemHelper.install_tasks
|
3
|
+
|
4
|
+
task :default => :spec
|
5
|
+
|
6
|
+
desc "Run all awesome_print gem specs"
|
7
|
+
task :spec do
|
8
|
+
# Run plain rspec command without RSpec::Core::RakeTask overrides.
|
9
|
+
exec "rspec -c spec"
|
10
|
+
end
|
11
|
+
|
data/lib/ap.rb
CHANGED
data/lib/awesome_print.rb
CHANGED
@@ -1,13 +1,13 @@
|
|
1
|
-
# Copyright (c) 2010-
|
1
|
+
# Copyright (c) 2010-2012 Michael Dvorkin
|
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
|
#
|
7
|
-
# AwesomePrint might be loaded implicitly through ~/.irbrc
|
8
|
-
# for subsequent requires.
|
7
|
+
# AwesomePrint might be loaded implicitly through ~/.irbrc or ~/.pryrc
|
8
|
+
# so do nothing for subsequent requires.
|
9
9
|
#
|
10
|
-
unless defined?(AwesomePrint)
|
10
|
+
unless defined?(AwesomePrint::Inspector)
|
11
11
|
%w(array string method object class kernel).each do |file|
|
12
12
|
require File.dirname(__FILE__) + "/awesome_print/core_ext/#{file}"
|
13
13
|
end
|
@@ -16,13 +16,15 @@ unless defined?(AwesomePrint)
|
|
16
16
|
require File.dirname(__FILE__) + "/awesome_print/formatter"
|
17
17
|
require File.dirname(__FILE__) + "/awesome_print/version"
|
18
18
|
require File.dirname(__FILE__) + "/awesome_print/core_ext/logger" if defined?(Logger)
|
19
|
-
|
19
|
+
#
|
20
20
|
# Load the following under normal circumstances as well as in Rails
|
21
|
-
# console when required from ~/.irbrc.
|
22
|
-
|
23
|
-
require File.dirname(__FILE__) + "/awesome_print/ext/
|
24
|
-
|
21
|
+
# console when required from ~/.irbrc or ~/.pryrc.
|
22
|
+
#
|
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?
|
25
|
+
#
|
25
26
|
# Load remaining extensions.
|
27
|
+
#
|
26
28
|
require File.dirname(__FILE__) + "/awesome_print/ext/action_view" if defined?(ActionView::Base)
|
27
29
|
require File.dirname(__FILE__) + "/awesome_print/ext/mongo_mapper" if defined?(MongoMapper)
|
28
30
|
require File.dirname(__FILE__) + "/awesome_print/ext/mongoid" if defined?(Mongoid)
|
@@ -1,4 +1,4 @@
|
|
1
|
-
# Copyright (c) 2010-
|
1
|
+
# Copyright (c) 2010-2012 Michael Dvorkin
|
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
|
@@ -58,7 +58,17 @@ class Array #:nodoc:
|
|
58
58
|
original_grep(pattern)
|
59
59
|
else
|
60
60
|
original_grep(pattern) do |match|
|
61
|
-
|
61
|
+
#
|
62
|
+
# The binding can only be used with Ruby-defined methods, therefore
|
63
|
+
# we must rescue potential "ArgumentError: Can't create Binding from
|
64
|
+
# C level Proc" error.
|
65
|
+
#
|
66
|
+
# For example, the following raises ArgumentError since #succ method
|
67
|
+
# is defined in C.
|
68
|
+
#
|
69
|
+
# [ 0, 1, 2, 3, 4 ].grep(1..2, &:succ)
|
70
|
+
#
|
71
|
+
eval("%Q/#{match.to_s.gsub('/', '\/')}/ =~ #{pattern.inspect}", blk.binding) rescue ArgumentError
|
62
72
|
yield match
|
63
73
|
end
|
64
74
|
end
|
@@ -1,4 +1,4 @@
|
|
1
|
-
# Copyright (c) 2010-
|
1
|
+
# Copyright (c) 2010-2012 Michael Dvorkin
|
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
|
@@ -13,7 +13,7 @@ module Kernel
|
|
13
13
|
|
14
14
|
def ap(object, options = {})
|
15
15
|
puts object.ai(options)
|
16
|
-
object
|
16
|
+
object unless AwesomePrint.console?
|
17
17
|
end
|
18
18
|
alias :awesome_print :ap
|
19
19
|
|
@@ -1,4 +1,4 @@
|
|
1
|
-
# Copyright (c) 2010-
|
1
|
+
# Copyright (c) 2010-2012 Michael Dvorkin
|
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
|
@@ -21,6 +21,8 @@ module AwesomePrint
|
|
21
21
|
cast = :active_record_instance
|
22
22
|
elsif object.is_a?(Class) && object.ancestors.include?(::ActiveRecord::Base)
|
23
23
|
cast = :active_record_class
|
24
|
+
elsif type == :activerecord_relation
|
25
|
+
cast = :array
|
24
26
|
end
|
25
27
|
cast
|
26
28
|
end
|
@@ -1,4 +1,4 @@
|
|
1
|
-
# Copyright (c) 2010-
|
1
|
+
# Copyright (c) 2010-2012 Michael Dvorkin
|
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
|
@@ -14,7 +14,7 @@ module AwesomePrint
|
|
14
14
|
def cast_with_active_support(object, type)
|
15
15
|
cast = cast_without_active_support(object, type)
|
16
16
|
if defined?(::ActiveSupport) && defined?(::HashWithIndifferentAccess)
|
17
|
-
if object.is_a?(::ActiveSupport::TimeWithZone) || object.is_a?(::Date)
|
17
|
+
if (defined?(::ActiveSupport::TimeWithZone) && object.is_a?(::ActiveSupport::TimeWithZone)) || object.is_a?(::Date)
|
18
18
|
cast = :active_support_time
|
19
19
|
elsif object.is_a?(::HashWithIndifferentAccess)
|
20
20
|
cast = :hash_with_indifferent_access
|
@@ -1,4 +1,4 @@
|
|
1
|
-
# Copyright (c) 2010-
|
1
|
+
# Copyright (c) 2010-2012 Michael Dvorkin
|
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
|
@@ -14,10 +14,21 @@ module AwesomePrint
|
|
14
14
|
# Add MongoMapper class names to the dispatcher pipeline.
|
15
15
|
#------------------------------------------------------------------------------
|
16
16
|
def cast_with_mongo_mapper(object, type)
|
17
|
+
apply_default_mongo_mapper_options
|
17
18
|
cast = cast_without_mongo_mapper(object, type)
|
18
|
-
|
19
|
-
|
19
|
+
|
20
|
+
if defined?(::MongoMapper::Document)
|
21
|
+
if object.is_a?(Class) && (object.ancestors & [ ::MongoMapper::Document, ::MongoMapper::EmbeddedDocument ]).size > 0
|
22
|
+
cast = :mongo_mapper_class
|
23
|
+
elsif object.is_a?(::MongoMapper::Document) || object.is_a?(::MongoMapper::EmbeddedDocument)
|
24
|
+
cast = :mongo_mapper_instance
|
25
|
+
elsif object.is_a?(::MongoMapper::Plugins::Associations::Base)
|
26
|
+
cast = :mongo_mapper_association
|
27
|
+
elsif object.is_a?(::BSON::ObjectId)
|
28
|
+
cast = :mongo_mapper_bson_id
|
29
|
+
end
|
20
30
|
end
|
31
|
+
|
21
32
|
cast
|
22
33
|
end
|
23
34
|
|
@@ -30,8 +41,80 @@ module AwesomePrint
|
|
30
41
|
hash[c.first] = (c.last.type || "undefined").to_s.underscore.intern
|
31
42
|
hash
|
32
43
|
end
|
44
|
+
|
45
|
+
# Add in associations
|
46
|
+
if @options[:mongo_mapper][:show_associations]
|
47
|
+
object.associations.each do |name, assoc|
|
48
|
+
data[name.to_s] = assoc
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
33
52
|
"class #{object} < #{object.superclass} " << awesome_hash(data)
|
34
53
|
end
|
54
|
+
|
55
|
+
# Format MongoMapper instance object.
|
56
|
+
#
|
57
|
+
# NOTE: by default only instance attributes (i.e. keys) are shown. To format
|
58
|
+
# MongoMapper instance as regular object showing its instance variables and
|
59
|
+
# accessors use :raw => true option:
|
60
|
+
#
|
61
|
+
# ap record, :raw => true
|
62
|
+
#
|
63
|
+
#------------------------------------------------------------------------------
|
64
|
+
def awesome_mongo_mapper_instance(object)
|
65
|
+
return object.inspect if !defined?(::ActiveSupport::OrderedHash)
|
66
|
+
return awesome_object(object) if @options[:raw]
|
67
|
+
|
68
|
+
data = object.keys.keys.sort_by{|k| k}.inject(::ActiveSupport::OrderedHash.new) do |hash, name|
|
69
|
+
hash[name] = object[name]
|
70
|
+
hash
|
71
|
+
end
|
72
|
+
|
73
|
+
# Add in associations
|
74
|
+
if @options[:mongo_mapper][:show_associations]
|
75
|
+
object.associations.each do |name, assoc|
|
76
|
+
if @options[:mongo_mapper][:inline_embedded] and assoc.embeddable?
|
77
|
+
data[name.to_s] = object.send(name)
|
78
|
+
else
|
79
|
+
data[name.to_s] = assoc
|
80
|
+
end
|
81
|
+
end
|
82
|
+
end
|
83
|
+
|
84
|
+
label = object.to_s
|
85
|
+
label = "#{colorize('embedded', :assoc)} #{label}" if object.is_a?(::MongoMapper::EmbeddedDocument)
|
86
|
+
|
87
|
+
"#{label} " << awesome_hash(data)
|
88
|
+
end
|
89
|
+
|
90
|
+
# Format MongoMapper association object.
|
91
|
+
#------------------------------------------------------------------------------
|
92
|
+
def awesome_mongo_mapper_association(object)
|
93
|
+
return object.inspect if !defined?(::ActiveSupport::OrderedHash)
|
94
|
+
return awesome_object(object) if @options[:raw]
|
95
|
+
|
96
|
+
association = object.class.name.split('::').last.titleize.downcase.sub(/ association$/,'')
|
97
|
+
association = "embeds #{association}" if object.embeddable?
|
98
|
+
class_name = object.class_name
|
99
|
+
|
100
|
+
"#{colorize(association, :assoc)} #{colorize(class_name, :class)}"
|
101
|
+
end
|
102
|
+
|
103
|
+
# Format BSON::ObjectId
|
104
|
+
#------------------------------------------------------------------------------
|
105
|
+
def awesome_mongo_mapper_bson_id(object)
|
106
|
+
object.inspect
|
107
|
+
end
|
108
|
+
|
109
|
+
private
|
110
|
+
|
111
|
+
def apply_default_mongo_mapper_options
|
112
|
+
@options[:color][:assoc] ||= :greenish
|
113
|
+
@options[:mongo_mapper] ||= {
|
114
|
+
:show_associations => false, # Display association data for MongoMapper documents and classes.
|
115
|
+
:inline_embedded => false # Display embedded associations inline with MongoMapper documents.
|
116
|
+
}
|
117
|
+
end
|
35
118
|
end
|
36
119
|
end
|
37
120
|
|
@@ -1,4 +1,4 @@
|
|
1
|
-
# Copyright (c) 2010-
|
1
|
+
# Copyright (c) 2010-2012 Michael Dvorkin
|
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
|
@@ -20,7 +20,7 @@ module AwesomePrint
|
|
20
20
|
cast = :mongoid_class
|
21
21
|
elsif object.class.ancestors.include?(::Mongoid::Document)
|
22
22
|
cast = :mongoid_document
|
23
|
-
elsif object.is_a?(::BSON::ObjectId)
|
23
|
+
elsif (defined?(::BSON) && object.is_a?(::BSON::ObjectId)) || (defined?(::Moped) && object.is_a?(::Moped::BSON::ObjectId))
|
24
24
|
cast = :mongoid_bson_id
|
25
25
|
end
|
26
26
|
end
|
@@ -1,9 +1,9 @@
|
|
1
|
-
# Copyright (c) 2010-
|
1
|
+
# Copyright (c) 2010-2012 Michael Dvorkin
|
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
|
+
autoload :CGI, "cgi"
|
7
7
|
require "shellwords"
|
8
8
|
|
9
9
|
module AwesomePrint
|
@@ -39,12 +39,19 @@ module AwesomePrint
|
|
39
39
|
|
40
40
|
# Pick the color and apply it to the given string as necessary.
|
41
41
|
#------------------------------------------------------------------------------
|
42
|
-
def colorize(
|
43
|
-
|
42
|
+
def colorize(str, type)
|
43
|
+
str = CGI.escapeHTML(str) if @options[:html]
|
44
44
|
if @options[:plain] || !@options[:color][type] || !@inspector.colorize?
|
45
|
-
|
45
|
+
str
|
46
|
+
#
|
47
|
+
# Check if the string color method is defined by awesome_print and accepts
|
48
|
+
# html parameter or it has been overriden by some gem such as colorize.
|
49
|
+
#
|
50
|
+
elsif str.method(@options[:color][type]).arity == -1 # Accepts html parameter.
|
51
|
+
str.send(@options[:color][type], @options[:html])
|
46
52
|
else
|
47
|
-
|
53
|
+
str = %Q|<kbd style="color:#{@options[:color][type]}">#{str}</kbd>| if @options[:html]
|
54
|
+
str.send(@options[:color][type])
|
48
55
|
end
|
49
56
|
end
|
50
57
|
|
@@ -54,8 +61,11 @@ module AwesomePrint
|
|
54
61
|
# Catch all method to format an arbitrary object.
|
55
62
|
#------------------------------------------------------------------------------
|
56
63
|
def awesome_self(object, type)
|
57
|
-
|
58
|
-
|
64
|
+
if @options[:raw] && object.instance_variables.any?
|
65
|
+
awesome_object(object)
|
66
|
+
else
|
67
|
+
colorize(object.inspect.to_s, type)
|
68
|
+
end
|
59
69
|
end
|
60
70
|
|
61
71
|
# Format an array.
|
@@ -189,12 +199,17 @@ module AwesomePrint
|
|
189
199
|
colorize(ls.empty? ? d.inspect : "#{d.inspect}\n#{ls.chop}", :dir)
|
190
200
|
end
|
191
201
|
|
192
|
-
# Format BigDecimal
|
202
|
+
# Format BigDecimal object.
|
193
203
|
#------------------------------------------------------------------------------
|
194
204
|
def awesome_bigdecimal(n)
|
195
|
-
colorize(n.
|
205
|
+
colorize(n.to_s("F"), :bigdecimal)
|
206
|
+
end
|
207
|
+
|
208
|
+
# Format Rational object.
|
209
|
+
#------------------------------------------------------------------------------
|
210
|
+
def awesome_rational(n)
|
211
|
+
colorize(n.to_s, :rational)
|
196
212
|
end
|
197
|
-
alias :awesome_rational :awesome_bigdecimal
|
198
213
|
|
199
214
|
# Format a method.
|
200
215
|
#------------------------------------------------------------------------------
|
@@ -1,4 +1,4 @@
|
|
1
|
-
# Copyright (c) 2010-
|
1
|
+
# Copyright (c) 2010-2012 Michael Dvorkin
|
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
|
@@ -14,6 +14,37 @@ module AwesomePrint
|
|
14
14
|
def force_colors!(value = true)
|
15
15
|
@force_colors = value
|
16
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
|
17
48
|
end
|
18
49
|
|
19
50
|
class Inspector
|
@@ -28,6 +59,7 @@ module AwesomePrint
|
|
28
59
|
:html => false, # Use ANSI color codes rather than HTML.
|
29
60
|
:multiline => true, # Display in multiple lines.
|
30
61
|
:plain => false, # Use colors.
|
62
|
+
:raw => false, # Do not recursively format object instance variables.
|
31
63
|
:sort_keys => false, # Do not sort hash keys.
|
32
64
|
:limit => false, # Limit large output for arrays and hashes. Set to a boolean or integer.
|
33
65
|
:color => {
|
@@ -43,6 +75,7 @@ module AwesomePrint
|
|
43
75
|
:keyword => :cyan,
|
44
76
|
:method => :purpleish,
|
45
77
|
:nilclass => :red,
|
78
|
+
:rational => :blue,
|
46
79
|
:string => :yellowish,
|
47
80
|
:struct => :pale,
|
48
81
|
:symbol => :cyanish,
|
@@ -129,10 +162,8 @@ module AwesomePrint
|
|
129
162
|
#------------------------------------------------------------------------------
|
130
163
|
def merge_custom_defaults!
|
131
164
|
dotfile = File.join(ENV["HOME"], ".aprc")
|
132
|
-
if File.readable?(dotfile)
|
133
|
-
|
134
|
-
merge_options!(AwesomePrint.defaults)
|
135
|
-
end
|
165
|
+
load dotfile if File.readable?(dotfile)
|
166
|
+
merge_options!(AwesomePrint.defaults) if AwesomePrint.defaults.is_a?(Hash)
|
136
167
|
rescue => e
|
137
168
|
$stderr.puts "Could not load #{dotfile}: #{e}"
|
138
169
|
end
|
@@ -1,10 +1,10 @@
|
|
1
|
-
# Copyright (c) 2010-
|
1
|
+
# Copyright (c) 2010-2012 Michael Dvorkin
|
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.0
|
8
|
+
'1.1.0'
|
9
9
|
end
|
10
10
|
end
|
data/spec/formats_spec.rb
CHANGED
@@ -479,14 +479,24 @@ EOS
|
|
479
479
|
|
480
480
|
#------------------------------------------------------------------------------
|
481
481
|
describe "BigDecimal and Rational" do
|
482
|
-
it "should present BigDecimal object
|
483
|
-
big = BigDecimal("
|
484
|
-
big.ai(:plain => true).should == "
|
485
|
-
end
|
486
|
-
|
487
|
-
it "should present Rational object
|
488
|
-
rat = Rational(
|
489
|
-
rat.ai(:plain => true)
|
482
|
+
it "should present BigDecimal object with arbitrary precision" do
|
483
|
+
big = BigDecimal("201020102010201020102010201020102010.4")
|
484
|
+
big.ai(:plain => true).should == "201020102010201020102010201020102010.4"
|
485
|
+
end
|
486
|
+
|
487
|
+
it "should present Rational object with arbitrary precision" do
|
488
|
+
rat = Rational(201020102010201020102010201020102010, 2)
|
489
|
+
out = rat.ai(:plain => true)
|
490
|
+
#
|
491
|
+
# Ruby 1.9 slightly changed the format of Rational#to_s, see
|
492
|
+
# http://techtime.getharvest.com/blog/harvest-is-now-on-ruby-1-dot-9-3 and
|
493
|
+
# http://www.ruby-forum.com/topic/189397
|
494
|
+
#
|
495
|
+
if RUBY_VERSION < "1.9"
|
496
|
+
out.should == "100510051005100510051005100510051005"
|
497
|
+
else
|
498
|
+
out.should == "100510051005100510051005100510051005/1"
|
499
|
+
end
|
490
500
|
end
|
491
501
|
end
|
492
502
|
|
@@ -501,7 +511,6 @@ EOS
|
|
501
511
|
end
|
502
512
|
end
|
503
513
|
|
504
|
-
|
505
514
|
#------------------------------------------------------------------------------
|
506
515
|
describe "Struct" do
|
507
516
|
before do
|
@@ -573,80 +582,6 @@ EOS
|
|
573
582
|
end
|
574
583
|
end
|
575
584
|
|
576
|
-
|
577
|
-
#------------------------------------------------------------------------------
|
578
|
-
describe "Misc" do
|
579
|
-
it "handle weird objects that return nil on inspect" do
|
580
|
-
weird = Class.new do
|
581
|
-
def inspect
|
582
|
-
nil
|
583
|
-
end
|
584
|
-
end
|
585
|
-
weird.new.ai(:plain => true).should == ''
|
586
|
-
end
|
587
|
-
|
588
|
-
it "handle frozen object.inspect" do
|
589
|
-
weird = Class.new do
|
590
|
-
def inspect
|
591
|
-
"ice".freeze
|
592
|
-
end
|
593
|
-
end
|
594
|
-
weird.new.ai(:plain => false).should == "ice"
|
595
|
-
end
|
596
|
-
|
597
|
-
# See https://github.com/michaeldv/awesome_print/issues/35
|
598
|
-
it "handle array grep when pattern contains / chapacter" do
|
599
|
-
hash = { "1/x" => 1, "2//x" => :"2" }
|
600
|
-
grepped = hash.keys.grep(/^(\d+)\//) { $1 }
|
601
|
-
grepped.ai(:plain => true, :multiline => false).should == '[ "1", "2" ]'
|
602
|
-
end
|
603
|
-
|
604
|
-
it "returns value passed as a parameter" do
|
605
|
-
object = rand
|
606
|
-
self.stub!(:puts)
|
607
|
-
(ap object).should == object
|
608
|
-
end
|
609
|
-
|
610
|
-
# Require different file name this time (lib/ap.rb vs. lib/awesome_print).
|
611
|
-
it "several require 'awesome_print' should do no harm" do
|
612
|
-
require File.expand_path(File.dirname(__FILE__) + '/../lib/ap')
|
613
|
-
lambda { rand.ai }.should_not raise_error
|
614
|
-
end
|
615
|
-
end
|
616
|
-
|
617
|
-
describe "HTML output" do
|
618
|
-
it "wraps ap output with plain <pre> tag" do
|
619
|
-
markup = rand
|
620
|
-
markup.ai(:html => true, :plain => true).should == "<pre>#{markup}</pre>"
|
621
|
-
end
|
622
|
-
|
623
|
-
it "wraps ap output with <pre> tag with colorized <kbd>" do
|
624
|
-
markup = rand
|
625
|
-
markup.ai(:html => true).should == %Q|<pre><kbd style="color:blue">#{markup}</kbd></pre>|
|
626
|
-
end
|
627
|
-
|
628
|
-
it "wraps multiline ap output with <pre> tag with colorized <kbd>" do
|
629
|
-
markup = [ 1, :two, "three" ]
|
630
|
-
markup.ai(:html => true).should == <<-EOS.strip
|
631
|
-
<pre>[
|
632
|
-
<kbd style="color:white">[0] </kbd><pre><kbd style="color:blue">1</kbd></pre>,
|
633
|
-
<kbd style="color:white">[1] </kbd><pre><kbd style="color:darkcyan">:two</kbd></pre>,
|
634
|
-
<kbd style="color:white">[2] </kbd><pre><kbd style="color:brown">"three"</kbd></pre>
|
635
|
-
]</pre>
|
636
|
-
EOS
|
637
|
-
end
|
638
|
-
|
639
|
-
it "encodes HTML entities (plain)" do
|
640
|
-
markup = ' &<hello>'
|
641
|
-
markup.ai(:html => true, :plain => true).should == '<pre>" &<hello>"</pre>'
|
642
|
-
end
|
643
|
-
|
644
|
-
it "encodes HTML entities (color)" do
|
645
|
-
markup = ' &<hello>'
|
646
|
-
markup.ai(:html => true).should == '<pre><kbd style="color:brown">" &<hello>"</kbd></pre>'
|
647
|
-
end
|
648
|
-
end
|
649
|
-
|
650
585
|
#------------------------------------------------------------------------------
|
651
586
|
describe "Inherited from standard Ruby classes" do
|
652
587
|
after do
|
data/spec/methods_spec.rb
CHANGED
@@ -155,9 +155,10 @@ describe "Object methods" do
|
|
155
155
|
def m1; end
|
156
156
|
def m2; end
|
157
157
|
end
|
158
|
+
|
158
159
|
out = Hello.new.private_methods.ai(:plain => true).split("\n").grep(/m\d/)
|
159
|
-
out.first.should =~ /^\s+\[\d+\]\s+m1\(\)\s+Hello$/
|
160
|
-
out.last.should =~ /^\s+\[\d+\]\s+m2\(\)\s+Hello$/
|
160
|
+
out.first.should =~ /^\s+\[\s*\d+\]\s+m1\(\)\s+Hello$/
|
161
|
+
out.last.should =~ /^\s+\[\s*\d+\]\s+m2\(\)\s+Hello$/
|
161
162
|
end
|
162
163
|
|
163
164
|
it "no index: should handle object.private_methods" do
|
@@ -167,9 +168,9 @@ describe "Object methods" do
|
|
167
168
|
end
|
168
169
|
out = Hello.new.private_methods.ai(:plain => true).split("\n").grep(/m\d/)
|
169
170
|
if RUBY_VERSION < '1.9.2'
|
170
|
-
out.first.should =~ /^\s+\[\d+\]\s+m3\(arg1, arg2\)\s+Hello$/
|
171
|
+
out.first.should =~ /^\s+\[\s*\d+\]\s+m3\(arg1, arg2\)\s+Hello$/
|
171
172
|
else
|
172
|
-
out.first.should =~ /^\s+\[\d+\]\s+m3\(a, b\)\s+Hello$/
|
173
|
+
out.first.should =~ /^\s+\[\s*\d+\]\s+m3\(a, b\)\s+Hello$/
|
173
174
|
end
|
174
175
|
end
|
175
176
|
end
|
@@ -183,8 +184,8 @@ describe "Object methods" do
|
|
183
184
|
end
|
184
185
|
end
|
185
186
|
out = Hello.singleton_methods.ai(:plain => true).split("\n").grep(/m\d/)
|
186
|
-
out.first.should =~ /^\s+\[\d+\]\s+m1\(\)\s+Hello$/
|
187
|
-
out.last.should =~ /^\s+\[\d+\]\s+m2\(\)\s+Hello$/
|
187
|
+
out.first.should =~ /^\s+\[\s*\d+\]\s+m1\(\)\s+Hello$/
|
188
|
+
out.last.should =~ /^\s+\[\s*\d+\]\s+m2\(\)\s+Hello$/
|
188
189
|
end
|
189
190
|
|
190
191
|
it "no index: should handle object.singleton_methods" do
|
@@ -266,8 +267,8 @@ describe "Class methods" do
|
|
266
267
|
def m2; end
|
267
268
|
end
|
268
269
|
out = Hello.protected_instance_methods.ai(:plain => true).split("\n").grep(/m\d/)
|
269
|
-
out.first.should =~ /^\s+\[\d+\]\s+m1\(\)\s+Hello\s\(unbound\)$/
|
270
|
-
out.last.should =~ /^\s+\[\d+\]\s+m2\(\)\s+Hello\s\(unbound\)$/
|
270
|
+
out.first.should =~ /^\s+\[\s*\d+\]\s+m1\(\)\s+Hello\s\(unbound\)$/
|
271
|
+
out.last.should =~ /^\s+\[\s*\d+\]\s+m2\(\)\s+Hello\s\(unbound\)$/
|
271
272
|
end
|
272
273
|
|
273
274
|
it "no index: should handle class.protected_instance_methods" do
|
@@ -410,7 +411,7 @@ describe "Methods arrays" do
|
|
410
411
|
def self.m_two; end
|
411
412
|
end
|
412
413
|
|
413
|
-
out = Hello.methods.grep(/^m_(.+)$/) { $1.to_sym }
|
414
|
+
out = Hello.methods.sort.grep(/^m_(.+)$/) { $1.to_sym }
|
414
415
|
out.should == [:one, :two]
|
415
416
|
end
|
416
417
|
|
data/spec/misc_spec.rb
ADDED
@@ -0,0 +1,213 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
|
2
|
+
|
3
|
+
describe "AwesomePrint" do
|
4
|
+
|
5
|
+
describe "Misc" do
|
6
|
+
before do
|
7
|
+
stub_dotfile!
|
8
|
+
end
|
9
|
+
|
10
|
+
it "handle weird objects that return nil on inspect" do
|
11
|
+
weird = Class.new do
|
12
|
+
def inspect
|
13
|
+
nil
|
14
|
+
end
|
15
|
+
end
|
16
|
+
weird.new.ai(:plain => true).should == ''
|
17
|
+
end
|
18
|
+
|
19
|
+
it "handle frozen object.inspect" do
|
20
|
+
weird = Class.new do
|
21
|
+
def inspect
|
22
|
+
"ice".freeze
|
23
|
+
end
|
24
|
+
end
|
25
|
+
weird.new.ai(:plain => false).should == "ice"
|
26
|
+
end
|
27
|
+
|
28
|
+
# See https://github.com/michaeldv/awesome_print/issues/35
|
29
|
+
it "handle array grep when pattern contains / chapacter" do
|
30
|
+
hash = { "1/x" => 1, "2//x" => :"2" }
|
31
|
+
grepped = hash.keys.sort.grep(/^(\d+)\//) { $1 }
|
32
|
+
grepped.ai(:plain => true, :multiline => false).should == '[ "1", "2" ]'
|
33
|
+
end
|
34
|
+
|
35
|
+
# See https://github.com/michaeldv/awesome_print/issues/85
|
36
|
+
if RUBY_VERSION >= "1.8.7"
|
37
|
+
it "handle array grep when a method is defined in C and thus doesn't have a binding" do
|
38
|
+
arr = (0..6).to_a
|
39
|
+
grepped = arr.grep(1..4, &:succ)
|
40
|
+
grepped.ai(:plain => true, :multiline => false).should == '[ 2, 3, 4, 5 ]'
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
it "returns value passed as a parameter" do
|
45
|
+
object = rand
|
46
|
+
self.stub!(:puts)
|
47
|
+
(ap object).should == object
|
48
|
+
end
|
49
|
+
|
50
|
+
# Require different file name this time (lib/ap.rb vs. lib/awesome_print).
|
51
|
+
it "several require 'awesome_print' should do no harm" do
|
52
|
+
require File.expand_path(File.dirname(__FILE__) + '/../lib/ap')
|
53
|
+
lambda { rand.ai }.should_not raise_error
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
#------------------------------------------------------------------------------
|
58
|
+
describe "HTML output" do
|
59
|
+
before do
|
60
|
+
stub_dotfile!
|
61
|
+
end
|
62
|
+
|
63
|
+
it "wraps ap output with plain <pre> tag" do
|
64
|
+
markup = rand
|
65
|
+
markup.ai(:html => true, :plain => true).should == "<pre>#{markup}</pre>"
|
66
|
+
end
|
67
|
+
|
68
|
+
it "wraps ap output with <pre> tag with colorized <kbd>" do
|
69
|
+
markup = rand
|
70
|
+
markup.ai(:html => true).should == %Q|<pre><kbd style="color:blue">#{markup}</kbd></pre>|
|
71
|
+
end
|
72
|
+
|
73
|
+
it "wraps multiline ap output with <pre> tag with colorized <kbd>" do
|
74
|
+
markup = [ 1, :two, "three" ]
|
75
|
+
markup.ai(:html => true).should == <<-EOS.strip
|
76
|
+
<pre>[
|
77
|
+
<kbd style="color:white">[0] </kbd><pre><kbd style="color:blue">1</kbd></pre>,
|
78
|
+
<kbd style="color:white">[1] </kbd><pre><kbd style="color:darkcyan">:two</kbd></pre>,
|
79
|
+
<kbd style="color:white">[2] </kbd><pre><kbd style="color:brown">"three"</kbd></pre>
|
80
|
+
]</pre>
|
81
|
+
EOS
|
82
|
+
end
|
83
|
+
|
84
|
+
it "encodes HTML entities (plain)" do
|
85
|
+
markup = ' &<hello>'
|
86
|
+
markup.ai(:html => true, :plain => true).should == '<pre>" &<hello>"</pre>'
|
87
|
+
end
|
88
|
+
|
89
|
+
it "encodes HTML entities (color)" do
|
90
|
+
markup = ' &<hello>'
|
91
|
+
markup.ai(:html => true).should == '<pre><kbd style="color:brown">" &<hello>"</kbd></pre>'
|
92
|
+
end
|
93
|
+
end
|
94
|
+
|
95
|
+
#------------------------------------------------------------------------------
|
96
|
+
describe "AwesomePrint.defaults" do
|
97
|
+
before do
|
98
|
+
stub_dotfile!
|
99
|
+
end
|
100
|
+
|
101
|
+
after do
|
102
|
+
AwesomePrint.defaults = nil
|
103
|
+
end
|
104
|
+
|
105
|
+
# See https://github.com/michaeldv/awesome_print/issues/98
|
106
|
+
it "should properly merge the defaults" do
|
107
|
+
AwesomePrint.defaults = { :indent => -2, :sort_keys => true }
|
108
|
+
hash = { [0, 0, 255] => :yellow, :red => "rgb(255, 0, 0)", "magenta" => "rgb(255, 0, 255)" }
|
109
|
+
out = hash.ai(:plain => true)
|
110
|
+
out.should == <<-EOS.strip
|
111
|
+
{
|
112
|
+
[ 0, 0, 255 ] => :yellow,
|
113
|
+
"magenta" => "rgb(255, 0, 255)",
|
114
|
+
:red => "rgb(255, 0, 0)"
|
115
|
+
}
|
116
|
+
EOS
|
117
|
+
end
|
118
|
+
end
|
119
|
+
|
120
|
+
#------------------------------------------------------------------------------
|
121
|
+
describe "Coexistence with the colorize gem" do
|
122
|
+
before do
|
123
|
+
stub_dotfile!
|
124
|
+
end
|
125
|
+
|
126
|
+
before do # Redefine String#red just like colorize gem does it.
|
127
|
+
@awesome_method = "".method(:red)
|
128
|
+
|
129
|
+
String.instance_eval do
|
130
|
+
define_method :red do # Method arity is now 0 in Ruby 1.9+.
|
131
|
+
"[red]#{self}[/red]"
|
132
|
+
end
|
133
|
+
end
|
134
|
+
end
|
135
|
+
|
136
|
+
after do # Restore String#red method.
|
137
|
+
awesome_method = @awesome_method
|
138
|
+
String.instance_eval do
|
139
|
+
define_method :red, awesome_method
|
140
|
+
end
|
141
|
+
end
|
142
|
+
|
143
|
+
it "shoud not raise ArgumentError when formatting HTML" do
|
144
|
+
out = "hello".ai(:color => { :string => :red }, :html => true)
|
145
|
+
if RUBY_VERSION >= "1.9"
|
146
|
+
out.should == %Q|<pre>[red]<kbd style="color:red">"hello"</kbd>[/red]</pre>|
|
147
|
+
else
|
148
|
+
out.should == %Q|<pre>[red]"hello"[/red]</pre>|
|
149
|
+
end
|
150
|
+
end
|
151
|
+
|
152
|
+
it "shoud not raise ArgumentError when formatting HTML (shade color)" do
|
153
|
+
out = "hello".ai(:color => { :string => :redish }, :html => true)
|
154
|
+
out.should == %Q|<pre><kbd style="color:darkred">"hello"</kbd></pre>|
|
155
|
+
end
|
156
|
+
|
157
|
+
it "shoud not raise ArgumentError when formatting non-HTML" do
|
158
|
+
out = "hello".ai(:color => { :string => :red }, :html => false)
|
159
|
+
out.should == %Q|[red]"hello"[/red]|
|
160
|
+
end
|
161
|
+
|
162
|
+
it "shoud not raise ArgumentError when formatting non-HTML (shade color)" do
|
163
|
+
out = "hello".ai(:color => { :string => :redish }, :html => false)
|
164
|
+
out.should == %Q|\e[0;31m"hello"\e[0m|
|
165
|
+
end
|
166
|
+
end
|
167
|
+
|
168
|
+
#------------------------------------------------------------------------------
|
169
|
+
describe "Console" do
|
170
|
+
it "should detect IRB" do
|
171
|
+
class IRB; end
|
172
|
+
AwesomePrint.console?.should == true
|
173
|
+
AwesomePrint.rails_console?.should == false
|
174
|
+
Object.instance_eval{ remove_const :IRB }
|
175
|
+
end
|
176
|
+
|
177
|
+
it "should detect Pry" do
|
178
|
+
class Pry; end
|
179
|
+
AwesomePrint.console?.should == true
|
180
|
+
AwesomePrint.rails_console?.should == false
|
181
|
+
Object.instance_eval{ remove_const :Pry }
|
182
|
+
end
|
183
|
+
|
184
|
+
it "should detect Rails::Console" do
|
185
|
+
class IRB; end
|
186
|
+
class Rails; class Console; end; end
|
187
|
+
AwesomePrint.console?.should == true
|
188
|
+
AwesomePrint.rails_console?.should == true
|
189
|
+
Object.instance_eval{ remove_const :IRB }
|
190
|
+
Object.instance_eval{ remove_const :Rails }
|
191
|
+
end
|
192
|
+
|
193
|
+
it "should detect ENV['RAILS_ENV']" do
|
194
|
+
class Pry; end
|
195
|
+
ENV["RAILS_ENV"] = "development"
|
196
|
+
AwesomePrint.console?.should == true
|
197
|
+
AwesomePrint.rails_console?.should == true
|
198
|
+
Object.instance_eval{ remove_const :Pry }
|
199
|
+
end
|
200
|
+
|
201
|
+
it "should return the actual object when *not* running under console" do
|
202
|
+
capture! { ap([ 1, 2, 3 ]) }.should == [ 1, 2, 3 ]
|
203
|
+
capture! { ap({ :a => 1 }) }.should == { :a => 1 }
|
204
|
+
end
|
205
|
+
|
206
|
+
it "should return nil when running under console" do
|
207
|
+
class IRB; end
|
208
|
+
capture! { ap([ 1, 2, 3 ]) }.should == nil
|
209
|
+
capture! { ap({ :a => 1 }) }.should == nil
|
210
|
+
Object.instance_eval{ remove_const :IRB }
|
211
|
+
end
|
212
|
+
end
|
213
|
+
end
|
data/spec/objects_spec.rb
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
|
2
2
|
|
3
|
-
describe "
|
3
|
+
describe "Objects" do
|
4
4
|
before do
|
5
5
|
stub_dotfile!
|
6
6
|
end
|
@@ -9,7 +9,7 @@ describe "Single method" do
|
|
9
9
|
Object.instance_eval{ remove_const :Hello } if defined?(Hello)
|
10
10
|
end
|
11
11
|
|
12
|
-
describe "object" do
|
12
|
+
describe "Formatting an object" do
|
13
13
|
it "attributes" do
|
14
14
|
class Hello
|
15
15
|
attr_reader :abra
|
@@ -21,7 +21,8 @@ describe "Single method" do
|
|
21
21
|
end
|
22
22
|
end
|
23
23
|
|
24
|
-
|
24
|
+
hello = Hello.new
|
25
|
+
out = hello.ai(:plain => true, :raw => true)
|
25
26
|
str = <<-EOS.strip
|
26
27
|
#<Hello:0x01234567
|
27
28
|
attr_accessor :dabra = 3,
|
@@ -30,6 +31,7 @@ describe "Single method" do
|
|
30
31
|
>
|
31
32
|
EOS
|
32
33
|
out.gsub(/0x([a-f\d]+)/, "0x01234567").should == str
|
34
|
+
hello.ai(:plain => true, :raw => false).should == hello.inspect
|
33
35
|
end
|
34
36
|
|
35
37
|
it "instance variables" do
|
@@ -39,7 +41,8 @@ EOS
|
|
39
41
|
end
|
40
42
|
end
|
41
43
|
|
42
|
-
|
44
|
+
hello = Hello.new
|
45
|
+
out = hello.ai(:plain => true, :raw => true)
|
43
46
|
str = <<-EOS.strip
|
44
47
|
#<Hello:0x01234567
|
45
48
|
@abra = 1,
|
@@ -48,6 +51,7 @@ EOS
|
|
48
51
|
>
|
49
52
|
EOS
|
50
53
|
out.gsub(/0x([a-f\d]+)/, "0x01234567").should == str
|
54
|
+
hello.ai(:plain => true, :raw => false).should == hello.inspect
|
51
55
|
end
|
52
56
|
|
53
57
|
it "attributes and instance variables" do
|
@@ -62,7 +66,8 @@ EOS
|
|
62
66
|
end
|
63
67
|
end
|
64
68
|
|
65
|
-
|
69
|
+
hello = Hello.new
|
70
|
+
out = hello.ai(:plain => true, :raw => true)
|
66
71
|
str = <<-EOS.strip
|
67
72
|
#<Hello:0x01234567
|
68
73
|
@doo = 1,
|
@@ -74,6 +79,7 @@ EOS
|
|
74
79
|
>
|
75
80
|
EOS
|
76
81
|
out.gsub(/0x([a-f\d]+)/, "0x01234567").should == str
|
82
|
+
hello.ai(:plain => true, :raw => false).should == hello.inspect
|
77
83
|
end
|
78
84
|
end
|
79
85
|
end
|
data/spec/spec_helper.rb
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
# Copyright (c) 2010-
|
1
|
+
# Copyright (c) 2010-2012 Michael Dvorkin
|
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
|
@@ -6,7 +6,13 @@
|
|
6
6
|
#
|
7
7
|
# Running specs from the command line:
|
8
8
|
# $ rake spec # Entire spec suite.
|
9
|
-
# $ rspec spec/
|
9
|
+
# $ rspec spec/objects_spec.rb # Individual spec file.
|
10
|
+
#
|
11
|
+
# NOTE: To successfully run specs with Ruby 1.8.6 the older versions of
|
12
|
+
# Bundler and RSpec gems are required:
|
13
|
+
#
|
14
|
+
# $ gem install bundler -v=1.0.2
|
15
|
+
# $ gem install rspec -v=2.6.0
|
10
16
|
#
|
11
17
|
$LOAD_PATH.unshift(File.dirname(__FILE__))
|
12
18
|
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
|
@@ -17,6 +23,13 @@ def stub_dotfile!
|
|
17
23
|
File.should_receive(:readable?).at_least(:once).with(dotfile).and_return(false)
|
18
24
|
end
|
19
25
|
|
26
|
+
def capture!
|
27
|
+
standard, $stdout = $stdout, StringIO.new
|
28
|
+
yield
|
29
|
+
ensure
|
30
|
+
$stdout = standard
|
31
|
+
end
|
32
|
+
|
20
33
|
# The following is needed for the Infinity Test. It runs tests as subprocesses,
|
21
34
|
# which sets STDOUT.tty? to false and would otherwise prematurely disallow colors.
|
22
35
|
### AwesomePrint.force_colors!
|
metadata
CHANGED
@@ -1,8 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: awesome_print
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
+
hash: 19
|
4
5
|
prerelease:
|
5
|
-
|
6
|
+
segments:
|
7
|
+
- 1
|
8
|
+
- 1
|
9
|
+
- 0
|
10
|
+
version: 1.1.0
|
6
11
|
platform: ruby
|
7
12
|
authors:
|
8
13
|
- Michael Dvorkin
|
@@ -10,7 +15,7 @@ autorequire:
|
|
10
15
|
bindir: bin
|
11
16
|
cert_chain: []
|
12
17
|
|
13
|
-
date:
|
18
|
+
date: 2012-09-11 00:00:00 Z
|
14
19
|
dependencies:
|
15
20
|
- !ruby/object:Gem::Dependency
|
16
21
|
name: rspec
|
@@ -20,6 +25,11 @@ dependencies:
|
|
20
25
|
requirements:
|
21
26
|
- - ">="
|
22
27
|
- !ruby/object:Gem::Version
|
28
|
+
hash: 23
|
29
|
+
segments:
|
30
|
+
- 2
|
31
|
+
- 6
|
32
|
+
- 0
|
23
33
|
version: 2.6.0
|
24
34
|
type: :development
|
25
35
|
version_requirements: *id001
|
@@ -31,6 +41,11 @@ dependencies:
|
|
31
41
|
requirements:
|
32
42
|
- - ">="
|
33
43
|
- !ruby/object:Gem::Version
|
44
|
+
hash: 21
|
45
|
+
segments:
|
46
|
+
- 0
|
47
|
+
- 2
|
48
|
+
- 1
|
34
49
|
version: 0.2.1
|
35
50
|
type: :development
|
36
51
|
version_requirements: *id002
|
@@ -70,6 +85,7 @@ files:
|
|
70
85
|
- spec/colors_spec.rb
|
71
86
|
- spec/formats_spec.rb
|
72
87
|
- spec/methods_spec.rb
|
88
|
+
- spec/misc_spec.rb
|
73
89
|
- spec/objects_spec.rb
|
74
90
|
- spec/spec_helper.rb
|
75
91
|
- .gitignore
|
@@ -86,17 +102,23 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
86
102
|
requirements:
|
87
103
|
- - ">="
|
88
104
|
- !ruby/object:Gem::Version
|
105
|
+
hash: 3
|
106
|
+
segments:
|
107
|
+
- 0
|
89
108
|
version: "0"
|
90
109
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
91
110
|
none: false
|
92
111
|
requirements:
|
93
112
|
- - ">="
|
94
113
|
- !ruby/object:Gem::Version
|
114
|
+
hash: 3
|
115
|
+
segments:
|
116
|
+
- 0
|
95
117
|
version: "0"
|
96
118
|
requirements: []
|
97
119
|
|
98
120
|
rubyforge_project: awesome_print
|
99
|
-
rubygems_version: 1.8.
|
121
|
+
rubygems_version: 1.8.24
|
100
122
|
signing_key:
|
101
123
|
specification_version: 3
|
102
124
|
summary: Pretty print Ruby objects with proper indentation and colors
|
@@ -104,5 +126,6 @@ test_files:
|
|
104
126
|
- spec/colors_spec.rb
|
105
127
|
- spec/formats_spec.rb
|
106
128
|
- spec/methods_spec.rb
|
129
|
+
- spec/misc_spec.rb
|
107
130
|
- spec/objects_spec.rb
|
108
131
|
- spec/spec_helper.rb
|