awesome_print_lite 0.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.
@@ -0,0 +1,238 @@
1
+ require 'spec_helper'
2
+
3
+ RSpec.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
+ expect(weird.new.ai(:plain => true)).to eq('')
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
+ expect(weird.new.ai(:plain => false)).to eq("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
+ expect(grepped.ai(:plain => true, :multiline => false)).to eq('[ "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
+ expect(grepped.ai(:plain => true, :multiline => false)).to eq('[ 2, 3, 4, 5 ]')
41
+ end
42
+ end
43
+
44
+ it "returns value passed as a parameter" do
45
+ object = rand
46
+ allow(self).to receive(:puts)
47
+ expect(ap object).to eq(object)
48
+ end
49
+
50
+
51
+ it "format ENV as hash" do
52
+ expect(ENV.ai(:plain => true)).to eq(ENV.to_hash.ai(:plain => true))
53
+ expect(ENV.ai).to eq(ENV.to_hash.ai)
54
+ end
55
+
56
+ # See https://github.com/michaeldv/awesome_print/issues/139
57
+ it "Object that overrides == and expects the :id method" do
58
+ weird = Class.new do
59
+ # Raises NoMethodError: undefined method `id' when "other" is nil or ENV.
60
+ def ==(other)
61
+ self.id == other.id
62
+ end
63
+ alias :eql? :==
64
+ end
65
+ expect { weird.new.ai }.not_to raise_error
66
+ end
67
+ end
68
+
69
+ #------------------------------------------------------------------------------
70
+ describe "HTML output" do
71
+ before do
72
+ stub_dotfile!
73
+ end
74
+
75
+ it "wraps ap output with plain <pre> tag" do
76
+ markup = rand
77
+ expect(markup.ai(:html => true, :plain => true)).to eq("<pre>#{markup}</pre>")
78
+ end
79
+
80
+ it "wraps ap output with <pre> tag with colorized <kbd>" do
81
+ markup = rand
82
+ expect(markup.ai(:html => true)).to eq(%Q|<pre><kbd style="color:blue">#{markup}</kbd></pre>|)
83
+ end
84
+
85
+ it "wraps multiline ap output with <pre> tag with colorized <kbd>" do
86
+ markup = [ 1, :two, "three" ]
87
+ expect(markup.ai(:html => true)).to eq <<-EOS.strip
88
+ <pre>[
89
+ <kbd style="color:white">[0] </kbd><kbd style="color:blue">1</kbd>,
90
+ <kbd style="color:white">[1] </kbd><kbd style="color:darkcyan">:two</kbd>,
91
+ <kbd style="color:white">[2] </kbd><kbd style="color:brown">&quot;three&quot;</kbd>
92
+ ]</pre>
93
+ EOS
94
+ end
95
+
96
+ it "wraps hash ap output with only an outer <pre> tag" do
97
+ markup = [ { "hello" => "world" } ]
98
+ expect(markup.ai(:html => true)).to eq <<-EOS.strip
99
+ <pre>[
100
+ <kbd style="color:white">[0] </kbd>{
101
+ &quot;hello&quot;<kbd style="color:slategray"> =&gt; </kbd><kbd style="color:brown">&quot;world&quot;</kbd>
102
+ }
103
+ ]</pre>
104
+ EOS
105
+ end
106
+
107
+ it "encodes HTML entities (plain)" do
108
+ markup = ' &<hello>'
109
+ expect(markup.ai(:html => true, :plain => true)).to eq('<pre>&quot; &amp;&lt;hello&gt;&quot;</pre>')
110
+ end
111
+
112
+ it "encodes HTML entities (color)" do
113
+ markup = ' &<hello>'
114
+ expect(markup.ai(:html => true)).to eq('<pre><kbd style="color:brown">&quot; &amp;&lt;hello&gt;&quot;</kbd></pre>')
115
+ end
116
+ end
117
+
118
+ #------------------------------------------------------------------------------
119
+ describe "AwesomePrintLite.defaults" do
120
+ before do
121
+ stub_dotfile!
122
+ end
123
+
124
+ after do
125
+ AwesomePrintLite.defaults = nil
126
+ end
127
+
128
+ # See https://github.com/michaeldv/awesome_print/issues/98
129
+ it "should properly merge the defaults" do
130
+ AwesomePrintLite.defaults = { :indent => -2, :sort_keys => true }
131
+ hash = { [0, 0, 255] => :yellow, :red => "rgb(255, 0, 0)", "magenta" => "rgb(255, 0, 255)" }
132
+ out = hash.ai(:plain => true)
133
+ expect(out).to eq <<-EOS.strip
134
+ {
135
+ [ 0, 0, 255 ] => :yellow,
136
+ "magenta" => "rgb(255, 0, 255)",
137
+ :red => "rgb(255, 0, 0)"
138
+ }
139
+ EOS
140
+ end
141
+ end
142
+
143
+ #------------------------------------------------------------------------------
144
+ describe "Coexistence with the colorize gem" do
145
+ before do
146
+ stub_dotfile!
147
+ end
148
+
149
+ before do # Redefine String#red just like colorize gem does it.
150
+ @awesome_method = "".method(:red)
151
+
152
+ String.instance_eval do
153
+ define_method :red do # Method arity is now 0 in Ruby 1.9+.
154
+ "[red]#{self}[/red]"
155
+ end
156
+ end
157
+ end
158
+
159
+ after do # Restore String#red method.
160
+ awesome_method = @awesome_method
161
+ String.instance_eval do
162
+ define_method :red, awesome_method
163
+ end
164
+ end
165
+
166
+ it "shoud not raise ArgumentError when formatting HTML" do
167
+ out = "hello".ai(:color => { :string => :red }, :html => true)
168
+ if RUBY_VERSION >= "1.9"
169
+ expect(out).to eq(%Q|<pre>[red]<kbd style="color:red">&quot;hello&quot;</kbd>[/red]</pre>|)
170
+ else
171
+ expect(out).to eq(%Q|<pre>[red]&quot;hello&quot;[/red]</pre>|)
172
+ end
173
+ end
174
+
175
+ it "shoud not raise ArgumentError when formatting HTML (shade color)" do
176
+ out = "hello".ai(:color => { :string => :redish }, :html => true)
177
+ expect(out).to eq(%Q|<pre><kbd style="color:darkred">&quot;hello&quot;</kbd></pre>|)
178
+ end
179
+
180
+ it "shoud not raise ArgumentError when formatting non-HTML" do
181
+ out = "hello".ai(:color => { :string => :red }, :html => false)
182
+ expect(out).to eq(%Q|[red]"hello"[/red]|)
183
+ end
184
+
185
+ it "shoud not raise ArgumentError when formatting non-HTML (shade color)" do
186
+ out = "hello".ai(:color => { :string => :redish }, :html => false)
187
+ expect(out).to eq(%Q|\e[0;31m"hello"\e[0m|)
188
+ end
189
+ end
190
+
191
+ #------------------------------------------------------------------------------
192
+ describe "Console" do
193
+ it "should detect IRB" do
194
+ class IRB; end
195
+ ENV.delete('RAILS_ENV')
196
+ expect(AwesomePrintLite.console?).to eq(true)
197
+ expect(AwesomePrintLite.rails_console?).to eq(false)
198
+ Object.instance_eval{ remove_const :IRB }
199
+ end
200
+
201
+ it "should detect Pry" do
202
+ class Pry; end
203
+ ENV.delete('RAILS_ENV')
204
+ expect(AwesomePrintLite.console?).to eq(true)
205
+ expect(AwesomePrintLite.rails_console?).to eq(false)
206
+ Object.instance_eval{ remove_const :Pry }
207
+ end
208
+
209
+ it "should detect Rails::Console" do
210
+ class IRB; end
211
+ module Rails; class Console; end; end
212
+ expect(AwesomePrintLite.console?).to eq(true)
213
+ expect(AwesomePrintLite.rails_console?).to eq(true)
214
+ Object.instance_eval{ remove_const :IRB }
215
+ Object.instance_eval{ remove_const :Rails }
216
+ end
217
+
218
+ it "should detect ENV['RAILS_ENV']" do
219
+ class Pry; end
220
+ ENV["RAILS_ENV"] = "development"
221
+ expect(AwesomePrintLite.console?).to eq(true)
222
+ expect(AwesomePrintLite.rails_console?).to eq(true)
223
+ Object.instance_eval{ remove_const :Pry }
224
+ end
225
+
226
+ it "should return the actual object when *not* running under console" do
227
+ expect(capture! { ap([ 1, 2, 3 ]) }).to eq([ 1, 2, 3 ])
228
+ expect(capture! { ap({ :a => 1 }) }).to eq({ :a => 1 })
229
+ end
230
+
231
+ it "should return nil when running under console" do
232
+ class IRB; end
233
+ expect(capture! { ap([ 1, 2, 3 ]) }).to eq(nil)
234
+ expect(capture! { ap({ :a => 1 }) }).to eq(nil)
235
+ Object.instance_eval{ remove_const :IRB }
236
+ end
237
+ end
238
+ end
@@ -0,0 +1,129 @@
1
+ require 'spec_helper'
2
+
3
+ RSpec.describe "Objects" do
4
+ before do
5
+ stub_dotfile!
6
+ end
7
+
8
+ after do
9
+ Object.instance_eval{ remove_const :Hello } if defined?(Hello)
10
+ end
11
+
12
+ describe "Formatting an object" do
13
+ it "attributes" do
14
+ class Hello
15
+ attr_reader :abra
16
+ attr_writer :ca
17
+ attr_accessor :dabra
18
+
19
+ def initialize
20
+ @abra, @ca, @dabra = 1, 2, 3
21
+ end
22
+ end
23
+
24
+ hello = Hello.new
25
+ out = hello.ai(:plain => true, :raw => true)
26
+ str = <<-EOS.strip
27
+ #<Hello:0x01234567
28
+ attr_accessor :dabra = 3,
29
+ attr_reader :abra = 1,
30
+ attr_writer :ca = 2
31
+ >
32
+ EOS
33
+ expect(out.gsub(/0x([a-f\d]+)/, "0x01234567")).to eq(str)
34
+ expect(hello.ai(:plain => true, :raw => false)).to eq(hello.inspect)
35
+ end
36
+
37
+ it "instance variables" do
38
+ class Hello
39
+ def initialize
40
+ @abra, @ca, @dabra = 1, 2, 3
41
+ end
42
+ end
43
+
44
+ hello = Hello.new
45
+ out = hello.ai(:plain => true, :raw => true)
46
+ str = <<-EOS.strip
47
+ #<Hello:0x01234567
48
+ @abra = 1,
49
+ @ca = 2,
50
+ @dabra = 3
51
+ >
52
+ EOS
53
+ expect(out.gsub(/0x([a-f\d]+)/, "0x01234567")).to eq(str)
54
+ expect(hello.ai(:plain => true, :raw => false)).to eq(hello.inspect)
55
+ end
56
+
57
+ it "attributes and instance variables" do
58
+ class Hello
59
+ attr_reader :abra
60
+ attr_writer :ca
61
+ attr_accessor :dabra
62
+
63
+ def initialize
64
+ @abra, @ca, @dabra = 1, 2, 3
65
+ @scooby, @dooby, @doo = 3, 2, 1
66
+ end
67
+ end
68
+
69
+ hello = Hello.new
70
+ out = hello.ai(:plain => true, :raw => true)
71
+ str = <<-EOS.strip
72
+ #<Hello:0x01234567
73
+ @doo = 1,
74
+ @dooby = 2,
75
+ @scooby = 3,
76
+ attr_accessor :dabra = 3,
77
+ attr_reader :abra = 1,
78
+ attr_writer :ca = 2
79
+ >
80
+ EOS
81
+ expect(out.gsub(/0x([a-f\d]+)/, "0x01234567")).to eq(str)
82
+ expect(hello.ai(:plain => true, :raw => false)).to eq(hello.inspect)
83
+ end
84
+
85
+ it "without the plain options print the colorized values" do
86
+ class Hello
87
+ attr_reader :abra
88
+ attr_writer :ca
89
+
90
+ def initialize
91
+ @abra, @ca = 1, 2
92
+ @dabra = 3
93
+ end
94
+ end
95
+
96
+ hello = Hello.new
97
+ out = hello.ai(:raw => true)
98
+ str = <<-EOS.strip
99
+ #<Hello:0x01234567
100
+ \e[0;36m@dabra\e[0m\e[0;37m = \e[0m\e[1;34m3\e[0m,
101
+ \e[1;36mattr_reader\e[0m \e[0;35m:abra\e[0m\e[0;37m = \e[0m\e[1;34m1\e[0m,
102
+ \e[1;36mattr_writer\e[0m \e[0;35m:ca\e[0m\e[0;37m = \e[0m\e[1;34m2\e[0m
103
+ >
104
+ EOS
105
+ expect(out.gsub(/0x([a-f\d]+)/, "0x01234567")).to eq(str)
106
+ expect(hello.ai(:plain => true, :raw => false)).to eq(hello.inspect)
107
+ end
108
+
109
+ it "with multine as false show inline values" do
110
+ class Hello
111
+ attr_reader :abra
112
+ attr_writer :ca
113
+
114
+ def initialize
115
+ @abra, @ca = 1, 2
116
+ @dabra = 3
117
+ end
118
+ end
119
+
120
+ hello = Hello.new
121
+ out = hello.ai(:multiline => false, :plain => true, :raw => true)
122
+ str = <<-EOS.strip
123
+ #<Hello:0x01234567 @dabra = 3, attr_reader :abra = 1, attr_writer :ca = 2>
124
+ EOS
125
+ expect(out.gsub(/0x([a-f\d]+)/, "0x01234567")).to eq(str)
126
+ expect(hello.ai(:plain => true, :raw => false)).to eq(hello.inspect)
127
+ end
128
+ end
129
+ end
@@ -0,0 +1,55 @@
1
+ # Copyright (c) 2010-2013 Michael Dvorkin
2
+ #
3
+ # Awesome Print is freely distributable under the terms of MIT license.
4
+ # See LICENSE file or http://www.opensource.org/licenses/mit-license.php
5
+ #------------------------------------------------------------------------------
6
+ #
7
+ # Running specs from the command line:
8
+ # $ rake spec # Entire spec suite.
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
16
+ #
17
+ # require 'codeclimate-test-reporter'
18
+ # CodeClimate::TestReporter.start
19
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
20
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
21
+
22
+ # require 'nokogiri'
23
+ require 'awesome_print_lite'
24
+
25
+ RSpec.configure do |config|
26
+ # config.disable_monkey_patching!
27
+ # TODO: Make specs not order dependent
28
+ # config.order = :random
29
+ Kernel.srand config.seed
30
+ config.filter_run focus: true
31
+ config.run_all_when_everything_filtered = true
32
+ config.expect_with :rspec do |expectations|
33
+ expectations.syntax = :expect
34
+ end
35
+ config.mock_with :rspec do |mocks|
36
+ mocks.syntax = :expect
37
+ mocks.verify_partial_doubles = true
38
+ end
39
+ if config.files_to_run.one?
40
+ config.default_formatter = 'doc'
41
+ end
42
+ end
43
+
44
+ def stub_dotfile!
45
+ dotfile = File.join(ENV["HOME"], ".aprc")
46
+ expect(File).to receive(:readable?).at_least(:once).with(dotfile).and_return(false)
47
+ end
48
+
49
+ def capture!
50
+ standard, $stdout = $stdout, StringIO.new
51
+ yield
52
+ ensure
53
+ $stdout = standard
54
+ end
55
+
metadata ADDED
@@ -0,0 +1,145 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: awesome_print_lite
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Forrest Chang
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2016-04-14 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.7'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.7'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: opal
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: opal-rspec
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ description: Subset of Awesome print functionality for Opal.
84
+ email:
85
+ - fkc_email-ruby@hedgeye.com
86
+ executables: []
87
+ extensions: []
88
+ extra_rdoc_files: []
89
+ files:
90
+ - ".gitignore"
91
+ - Gemfile
92
+ - LICENSE.txt
93
+ - README.md
94
+ - Rakefile
95
+ - awesome_print_lite.gemspec
96
+ - lib/awesome_print_lite.rb
97
+ - lib/awesome_print_lite/core_ext/array.rb
98
+ - lib/awesome_print_lite/core_ext/class.rb
99
+ - lib/awesome_print_lite/core_ext/kernel.rb
100
+ - lib/awesome_print_lite/core_ext/logger.rb
101
+ - lib/awesome_print_lite/core_ext/method.rb
102
+ - lib/awesome_print_lite/core_ext/object.rb
103
+ - lib/awesome_print_lite/core_ext/string.rb
104
+ - lib/awesome_print_lite/formatter.rb
105
+ - lib/awesome_print_lite/inspector.rb
106
+ - lib/awesome_print_lite/version.rb
107
+ - spec/colors_spec.rb
108
+ - spec/core_ext/string_spec.rb
109
+ - spec/formats_spec.rb
110
+ - spec/methods_spec.rb
111
+ - spec/misc_spec.rb
112
+ - spec/objects_spec.rb
113
+ - spec/spec_helper.rb
114
+ homepage: https://github.com/fkchang/awesome_print_lite
115
+ licenses:
116
+ - MIT
117
+ metadata: {}
118
+ post_install_message:
119
+ rdoc_options: []
120
+ require_paths:
121
+ - lib
122
+ required_ruby_version: !ruby/object:Gem::Requirement
123
+ requirements:
124
+ - - ">="
125
+ - !ruby/object:Gem::Version
126
+ version: '0'
127
+ required_rubygems_version: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - ">="
130
+ - !ruby/object:Gem::Version
131
+ version: '0'
132
+ requirements: []
133
+ rubyforge_project:
134
+ rubygems_version: 2.4.6
135
+ signing_key:
136
+ specification_version: 4
137
+ summary: Subset of Awesome print functionality for Opal.
138
+ test_files:
139
+ - spec/colors_spec.rb
140
+ - spec/core_ext/string_spec.rb
141
+ - spec/formats_spec.rb
142
+ - spec/methods_spec.rb
143
+ - spec/misc_spec.rb
144
+ - spec/objects_spec.rb
145
+ - spec/spec_helper.rb