awesome_print 1.6.1 → 1.7.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.
@@ -5,6 +5,6 @@
5
5
  #------------------------------------------------------------------------------
6
6
  module AwesomePrint
7
7
  def self.version
8
- "1.6.1"
8
+ '1.7.0'
9
9
  end
10
10
  end
@@ -1,34 +1,24 @@
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
-
22
- # Create models
23
- class User < ActiveRecord::Base; end
24
- class SubUser < User; end
25
8
 
9
+ # Establish connection to in-memory SQLite DB
10
+ ActiveRecord::Base.establish_connection adapter: "sqlite3", database: ":memory:"
26
11
 
27
- # Helper methods
28
- # ##############
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
29
20
 
30
- # we only work with ActiveRecord 2+
31
- def is_usable_activerecord?
32
- defined?(ActiveRecord::VERSION::MAJOR) && ActiveRecord::VERSION::MAJOR >= 2
21
+ # Create models
22
+ class User < ActiveRecord::Base; end
23
+ class SubUser < User; end
33
24
  end
34
-
@@ -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 }
@@ -1,9 +1,9 @@
1
- require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
1
+ require 'spec_helper'
2
2
  require "bigdecimal"
3
3
  require "rational"
4
4
  require "set"
5
5
 
6
- describe "AwesomePrint" do
6
+ RSpec.describe "AwesomePrint" do
7
7
  before do
8
8
  stub_dotfile!
9
9
  end
@@ -451,11 +451,11 @@ EOS
451
451
 
452
452
  #------------------------------------------------------------------------------
453
453
  describe "Class" do
454
- it "shoud show superclass (plain)" do
454
+ it "should show superclass (plain)" do
455
455
  expect(self.class.ai(:plain => true)).to eq("#{self.class} < #{self.class.superclass}")
456
456
  end
457
457
 
458
- it "shoud show superclass (color)" do
458
+ it "should show superclass (color)" do
459
459
  expect(self.class.ai).to eq("#{self.class} < #{self.class.superclass}".yellow)
460
460
  end
461
461
  end
@@ -576,56 +576,44 @@ EOS
576
576
 
577
577
  it "plain multiline" do
578
578
  s1 = <<-EOS.strip
579
- {
580
- :address => "1313 Mockingbird Lane",
581
- :name => "Herman Munster"
582
- }
579
+ address = \"1313 Mockingbird Lane\",
580
+ name = \"Herman Munster\"
583
581
  EOS
584
582
  s2 = <<-EOS.strip
585
- {
586
- :name => "Herman Munster",
587
- :address => "1313 Mockingbird Lane"
588
- }
583
+ name = \"Herman Munster\",
584
+ address = \"1313 Mockingbird Lane\"
589
585
  EOS
590
- expect(@struct.ai(:plain => true)).to satisfy { |match| match == s1 || match == s2 }
586
+ expect(@struct.ai(:plain => true)).to satisfy { |out| out.match(s1) || out.match(s2) }
591
587
  end
592
588
 
593
589
  it "plain multiline indented" do
594
590
  s1 = <<-EOS.strip
595
- {
596
- :address => "1313 Mockingbird Lane",
597
- :name => "Herman Munster"
598
- }
591
+ address = "1313 Mockingbird Lane",
592
+ name = "Herman Munster"
599
593
  EOS
600
594
  s2 = <<-EOS.strip
601
- {
602
- :name => "Herman Munster",
603
- :address => "1313 Mockingbird Lane"
604
- }
595
+ name = "Herman Munster",
596
+ address = "1313 Mockingbird Lane"
605
597
  EOS
606
- expect(@struct.ai(:plain => true, :indent => 1)).to satisfy { |match| match == s1 || match == s2 }
598
+ expect(@struct.ai(:plain => true, :indent => 1)).to satisfy { |out| out.match(s1) || out.match(s2) }
607
599
  end
608
600
 
609
601
  it "plain single line" do
610
- s1 = "{ :address => \"1313 Mockingbird Lane\", :name => \"Herman Munster\" }"
611
- s2 = "{ :name => \"Herman Munster\", :address => \"1313 Mockingbird Lane\" }"
612
- expect(@struct.ai(:plain => true, :multiline => false)).to satisfy { |match| match == s1 || match == s2 }
602
+ s1 = "address = \"1313 Mockingbird Lane\", name = \"Herman Munster\""
603
+ s2 = "name = \"Herman Munster\", address = \"1313 Mockingbird Lane\""
604
+ expect(@struct.ai(:plain => true, :multiline => false)).to satisfy { |out| out.match(s1) || out.match(s2) }
613
605
  end
614
606
 
615
607
  it "colored multiline (default)" do
616
608
  s1 = <<-EOS.strip
617
- {
618
- :address\e[0;37m => \e[0m\e[0;33m\"1313 Mockingbird Lane\"\e[0m,
619
- :name\e[0;37m => \e[0m\e[0;33m\"Herman Munster\"\e[0m
620
- }
609
+ address\e[0;37m = \e[0m\e[0;33m\"1313 Mockingbird Lane\"\e[0m,
610
+ name\e[0;37m = \e[0m\e[0;33m\"Herman Munster\"\e[0m
621
611
  EOS
622
612
  s2 = <<-EOS.strip
623
- {
624
- :name\e[0;37m => \e[0m\e[0;33m\"Herman Munster\"\e[0m,
625
- :address\e[0;37m => \e[0m\e[0;33m\"1313 Mockingbird Lane\"\e[0m
626
- }
613
+ name\e[0;37m = \e[0m\e[0;33m\"Herman Munster\"\e[0m,
614
+ address\e[0;37m = \e[0m\e[0;33m\"1313 Mockingbird Lane\"\e[0m
627
615
  EOS
628
- expect(@struct.ai).to satisfy { |match| match == s1 || match == s2 }
616
+ expect(@struct.ai).to satisfy { |out| out.include?(s1) || out.include?(s2) }
629
617
  end
630
618
  end
631
619
 
@@ -708,5 +696,56 @@ EOS
708
696
  my = My.new
709
697
  expect { my.methods.ai(:plain => true) }.not_to raise_error
710
698
  end
699
+
700
+ describe "should handle a class that defines its own #to_hash method" do
701
+ it "that takes arguments" do
702
+ class My
703
+ def to_hash(a, b)
704
+ end
705
+ end
706
+
707
+ my = My.new
708
+ expect { my.ai(:plain => true) }.not_to raise_error
709
+ end
710
+
711
+ it "that returns nil" do
712
+ class My
713
+ def to_hash()
714
+ return nil
715
+ end
716
+ end
717
+
718
+ my = My.new
719
+ expect { my.ai(:plain => true) }.not_to raise_error
720
+ end
721
+
722
+ it "that returns an object that doesn't support #keys" do
723
+ class My
724
+ def to_hash()
725
+ object = Object.new
726
+ object.define_singleton_method('[]') { return nil }
727
+
728
+ return object
729
+ end
730
+ end
731
+
732
+ my = My.new
733
+ expect { my.ai(:plain => true) }.not_to raise_error
734
+ end
735
+
736
+ it "that returns an object that doesn't support subscripting" do
737
+ class My
738
+ def to_hash()
739
+ object = Object.new
740
+ object.define_singleton_method(:keys) { return [:foo] }
741
+
742
+ return object
743
+ end
744
+ end
745
+
746
+ my = My.new
747
+ expect { my.ai(:plain => true) }.not_to raise_error
748
+ end
749
+ end
711
750
  end
712
751
  end
@@ -1,6 +1,6 @@
1
- require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
1
+ require 'spec_helper'
2
2
 
3
- describe "Single method" do
3
+ RSpec.describe "Single method" do
4
4
  before do
5
5
  stub_dotfile!
6
6
  end
@@ -80,7 +80,7 @@ describe "Single method" do
80
80
  end
81
81
  end
82
82
 
83
- describe "Object methods" do
83
+ RSpec.describe "Object methods" do
84
84
  before do
85
85
  stub_dotfile!
86
86
  end
@@ -202,7 +202,7 @@ describe "Object methods" do
202
202
  end
203
203
  end
204
204
 
205
- describe "Class methods" do
205
+ RSpec.describe "Class methods" do
206
206
  before do
207
207
  stub_dotfile!
208
208
  end
@@ -313,7 +313,7 @@ describe "Class methods" do
313
313
  end
314
314
 
315
315
  if RUBY_VERSION >= '1.9.2'
316
- describe "Ruby 1.9.2+ Method#parameters" do
316
+ RSpec.describe "Ruby 1.9.2+ Method#parameters" do
317
317
  before do
318
318
  stub_dotfile!
319
319
  end
@@ -364,7 +364,7 @@ if RUBY_VERSION >= '1.9.2'
364
364
  end
365
365
  end
366
366
 
367
- describe "Methods arrays" do
367
+ RSpec.describe "Methods arrays" do
368
368
  after do
369
369
  Object.instance_eval{ remove_const :Hello } if defined?(Hello)
370
370
  Object.instance_eval{ remove_const :World } if defined?(World)
@@ -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
 
5
5
  describe "Misc" do
6
6
  before do
@@ -204,6 +204,7 @@ EOS
204
204
  describe "Console" do
205
205
  it "should detect IRB" do
206
206
  class IRB; end
207
+ ENV.delete('RAILS_ENV')
207
208
  expect(AwesomePrint.console?).to eq(true)
208
209
  expect(AwesomePrint.rails_console?).to eq(false)
209
210
  Object.instance_eval{ remove_const :IRB }
@@ -211,6 +212,7 @@ EOS
211
212
 
212
213
  it "should detect Pry" do
213
214
  class Pry; end
215
+ ENV.delete('RAILS_ENV')
214
216
  expect(AwesomePrint.console?).to eq(true)
215
217
  expect(AwesomePrint.rails_console?).to eq(false)
216
218
  Object.instance_eval{ remove_const :Pry }
@@ -1,6 +1,6 @@
1
- require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
1
+ require 'spec_helper'
2
2
 
3
- describe "Objects" do
3
+ RSpec.describe "Objects" do
4
4
  before do
5
5
  stub_dotfile!
6
6
  end
@@ -24,13 +24,13 @@ describe "Objects" do
24
24
  hello = Hello.new
25
25
  out = hello.ai(:plain => true, :raw => true)
26
26
  str = <<-EOS.strip
27
- #<Hello:0x01234567
27
+ #<Hello:placeholder_id
28
28
  attr_accessor :dabra = 3,
29
29
  attr_reader :abra = 1,
30
30
  attr_writer :ca = 2
31
31
  >
32
32
  EOS
33
- expect(out.gsub(/0x([a-f\d]+)/, "0x01234567")).to eq(str)
33
+ expect(out).to be_similar_to(str)
34
34
  expect(hello.ai(:plain => true, :raw => false)).to eq(hello.inspect)
35
35
  end
36
36
 
@@ -44,13 +44,13 @@ EOS
44
44
  hello = Hello.new
45
45
  out = hello.ai(:plain => true, :raw => true)
46
46
  str = <<-EOS.strip
47
- #<Hello:0x01234567
47
+ #<Hello:placeholder_id
48
48
  @abra = 1,
49
49
  @ca = 2,
50
50
  @dabra = 3
51
51
  >
52
52
  EOS
53
- expect(out.gsub(/0x([a-f\d]+)/, "0x01234567")).to eq(str)
53
+ expect(out).to be_similar_to(str)
54
54
  expect(hello.ai(:plain => true, :raw => false)).to eq(hello.inspect)
55
55
  end
56
56
 
@@ -69,7 +69,7 @@ EOS
69
69
  hello = Hello.new
70
70
  out = hello.ai(:plain => true, :raw => true)
71
71
  str = <<-EOS.strip
72
- #<Hello:0x01234567
72
+ #<Hello:placeholder_id
73
73
  @doo = 1,
74
74
  @dooby = 2,
75
75
  @scooby = 3,
@@ -78,7 +78,51 @@ EOS
78
78
  attr_writer :ca = 2
79
79
  >
80
80
  EOS
81
- expect(out.gsub(/0x([a-f\d]+)/, "0x01234567")).to eq(str)
81
+ expect(out).to be_similar_to(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:placeholder_id
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).to be_similar_to(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:placeholder_id @dabra = 3, attr_reader :abra = 1, attr_writer :ca = 2>
124
+ EOS
125
+ expect(out).to be_similar_to(str)
82
126
  expect(hello.ai(:plain => true, :raw => false)).to eq(hello.inspect)
83
127
  end
84
128
  end
@@ -14,14 +14,62 @@
14
14
  # $ gem install bundler -v=1.0.2
15
15
  # $ gem install rspec -v=2.6.0
16
16
  #
17
+ require 'codeclimate-test-reporter'
18
+ CodeClimate::TestReporter.start
17
19
  $LOAD_PATH.unshift(File.dirname(__FILE__))
18
20
  $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
19
- require 'awesome_print'
20
21
 
21
22
  Dir[File.dirname(__FILE__) + '/support/**/*.rb'].each do |file|
22
23
  require file
23
24
  end
24
25
 
26
+ ExtVerifier.require_dependencies!(%w{rails active_record action_view
27
+ active_support/all mongoid mongo_mapper ripple nobrainer})
28
+ require 'nokogiri'
29
+ require 'awesome_print'
30
+
31
+ RSpec.configure do |config|
32
+ config.disable_monkey_patching!
33
+ # TODO: Make specs not order dependent
34
+ # config.order = :random
35
+ Kernel.srand config.seed
36
+ config.filter_run focus: true
37
+ config.run_all_when_everything_filtered = true
38
+ config.expect_with :rspec do |expectations|
39
+ expectations.syntax = :expect
40
+ end
41
+ config.mock_with :rspec do |mocks|
42
+ mocks.syntax = :expect
43
+ mocks.verify_partial_doubles = true
44
+ end
45
+ if config.files_to_run.one?
46
+ config.default_formatter = 'doc'
47
+ end
48
+ end
49
+
50
+ # This matcher handles the normalization of objects to replace non deterministic
51
+ # parts (such as object IDs) with simple placeholder strings before doing a
52
+ # comparison with a given string. It's important that this method only matches
53
+ # a string which strictly conforms to the expected object ID format.
54
+ RSpec::Matchers.define :be_similar_to do |expected, options|
55
+ match do |actual|
56
+ options ||= {}
57
+ @actual = normalize_object_id_strings(actual, options)
58
+ values_match? expected, @actual
59
+ end
60
+
61
+ diffable
62
+ end
63
+
64
+ # Override the Object IDs with a placeholder so that we are only checking
65
+ # that an ID is present and not that it matches a certain value. This is
66
+ # necessary as the Object IDs are not deterministic.
67
+ def normalize_object_id_strings(str, options)
68
+ str = str.gsub(/#<(.*?):0x[a-f\d]+/, '#<\1:placeholder_id') unless options[:skip_standard]
69
+ str = str.gsub(/BSON::ObjectId\('[a-f\d]{24}'\)/, 'placeholder_bson_id') unless options[:skip_bson]
70
+ str
71
+ end
72
+
25
73
  def stub_dotfile!
26
74
  dotfile = File.join(ENV["HOME"], ".aprc")
27
75
  expect(File).to receive(:readable?).at_least(:once).with(dotfile).and_return(false)
@@ -33,36 +81,3 @@ def capture!
33
81
  ensure
34
82
  $stdout = standard
35
83
  end
36
-
37
- # The following is needed for the Infinity Test. It runs tests as subprocesses,
38
- # which sets STDOUT.tty? to false and would otherwise prematurely disallow colors.
39
- ### AwesomePrint.force_colors!
40
-
41
- # Ruby 1.8.6 only: define missing String methods that are needed for the specs to pass.
42
- if RUBY_VERSION < '1.8.7'
43
- class String
44
- def shellescape # Taken from Ruby 1.9.2 standard library, see lib/shellwords.rb.
45
- return "''" if self.empty?
46
- str = self.dup
47
- str.gsub!(/([^A-Za-z0-9_\-.,:\/@\n])/n, "\\\\\\1")
48
- str.gsub!(/\n/, "'\n'")
49
- str
50
- end
51
-
52
- def start_with?(*prefixes)
53
- prefixes.each do |prefix|
54
- prefix = prefix.to_s
55
- return true if prefix == self[0, prefix.size]
56
- end
57
- false
58
- end
59
-
60
- def end_with?(*suffixes)
61
- suffixes.each do |suffix|
62
- suffix = suffix.to_s
63
- return true if suffix == self[-suffix.size, suffix.size]
64
- end
65
- false
66
- end
67
- end
68
- end