1
0
Fork 0
mirror of https://github.com/awesome-print/awesome_print synced 2023-03-27 23:22:34 -04:00

Simplify matching logic in tests

Simplify matching logic by adding a custom matcher which
handles the task of normalisation of object IDs instead of
calling gsub many times throughout the test code.

This allows our tests to focus more on what needs to be tested
instead obfuscating the tests with logic unrelated to what
the test is trying to cover.

It also allows making the normalisation code more robust so
there is less chance of an accidental match or typo slipping
in.

The change also replaces the arbitary default object IDs with a
string that makes it very obvious that it is just a placeholder.
This commit is contained in:
Gerard Caulfield 2016-05-10 00:04:42 +10:00
parent 28901dcca2
commit c5e8669e9c
No known key found for this signature in database
GPG key ID: 623B327128A9BEC3
6 changed files with 223 additions and 211 deletions

File diff suppressed because it is too large Load diff

View file

@ -36,9 +36,9 @@ RSpec.describe "AwesomePrint/MongoMapper", skip: ->{ !ExtVerifier.has_mongo_mapp
if MongoMapper::Version >= "0.13"
str = <<-EOS.strip
#<MongoUser:0x01234567
#<MongoUser:placeholder_id
@__mm_default_keys = [
[0] #<MongoMapper::Plugins::Keys::Key:0x01234567
[0] #<MongoMapper::Plugins::Keys::Key:placeholder_id
@dynamic = false,
@embeddable = false,
@has_default = true,
@ -55,7 +55,7 @@ RSpec.describe "AwesomePrint/MongoMapper", skip: ->{ !ExtVerifier.has_mongo_mapp
>
],
@__mm_keys = {
"_id" => #<MongoMapper::Plugins::Keys::Key:0x01234567
"_id" => #<MongoMapper::Plugins::Keys::Key:placeholder_id
@dynamic = false,
@embeddable = false,
@has_default = true,
@ -70,7 +70,7 @@ RSpec.describe "AwesomePrint/MongoMapper", skip: ->{ !ExtVerifier.has_mongo_mapp
},
attr_accessor :type = ObjectId < Object
>,
"first_name" => #<MongoMapper::Plugins::Keys::Key:0x01234567
"first_name" => #<MongoMapper::Plugins::Keys::Key:placeholder_id
@dynamic = false,
@embeddable = false,
@has_default = false,
@ -82,7 +82,7 @@ RSpec.describe "AwesomePrint/MongoMapper", skip: ->{ !ExtVerifier.has_mongo_mapp
attr_accessor :options = {},
attr_accessor :type = String < Object
>,
"last_name" => #<MongoMapper::Plugins::Keys::Key:0x01234567
"last_name" => #<MongoMapper::Plugins::Keys::Key:placeholder_id
@dynamic = false,
@embeddable = false,
@has_default = false,
@ -113,7 +113,7 @@ RSpec.describe "AwesomePrint/MongoMapper", skip: ->{ !ExtVerifier.has_mongo_mapp
EOS
else
str = <<-EOS.strip
#<MongoUser:0x01234567
#<MongoUser:placeholder_id
@_new = true,
attr_accessor :first_name = "Al",
attr_accessor :last_name = "Capone",
@ -126,8 +126,7 @@ RSpec.describe "AwesomePrint/MongoMapper", skip: ->{ !ExtVerifier.has_mongo_mapp
>
EOS
end
out.gsub!(/0x([a-f\d]+)/, "0x01234567")
expect(out).to eq(str)
expect(out).to be_similar_to(str)
end
it "should print the class" do
@ -193,14 +192,12 @@ class Parent < Object {
parent = Parent.new(:name => 'test')
out = @ap.send(:awesome, parent)
str = <<-EOS.strip
#<Parent:0x01234567> {
"_id" => BSON::ObjectId('4d9183739a546f6806000001'),
#<Parent:placeholder_id> {
"_id" => placeholder_bison_id,
"name" => "test"
}
EOS
out.gsub!(/'([\w]+){23}'/, "'4d9183739a546f6806000001'")
out.gsub!(/0x([a-f\d]+)/, "0x01234567")
expect(out).to eq(str)
expect(out).to be_similar_to(str)
end
end
@ -224,16 +221,14 @@ class Parent < Object {
parent = Parent.new(:name => 'test')
out = @ap.send(:awesome, parent)
str = <<-EOS.strip
#<Parent:0x01234567> {
"_id" => BSON::ObjectId('4d9183739a546f6806000001'),
#<Parent:placeholder_id> {
"_id" => placeholder_bison_id,
"name" => "test",
"child" => embeds one Child,
"sibling" => one Sibling
}
EOS
out.gsub!(/'([\w]+){23}'/, "'4d9183739a546f6806000001'")
out.gsub!(/0x([a-f\d]+)/, "0x01234567")
expect(out).to eq(str)
expect(out).to be_similar_to(str)
end
end
@ -251,19 +246,17 @@ class Parent < Object {
parent = Parent.new(:name => 'test', :child => Child.new(:data => 5))
out = @ap.send(:awesome, parent)
str = <<-EOS.strip
#<Parent:0x01234567> {
"_id" => BSON::ObjectId('4d9183739a546f6806000001'),
#<Parent:placeholder_id> {
"_id" => placeholder_bison_id,
"name" => "test",
"child" => embedded #<Child:0x01234567> {
"_id" => BSON::ObjectId('4d9183739a546f6806000001'),
"child" => embedded #<Child:placeholder_id> {
"_id" => placeholder_bison_id,
"data" => 5
},
"sibling" => one Sibling
}
EOS
out.gsub!(/'([\w]+){23}'/, "'4d9183739a546f6806000001'")
out.gsub!(/0x([a-f\d]+)/, "0x01234567")
expect(out).to eq(str)
expect(out).to be_similar_to(str)
end
end
end

View file

@ -29,14 +29,13 @@ RSpec.describe "AwesomePrint/Mongoid", skip: ->{ !ExtVerifier.has_mongoid? }.cal
object_id = user.id.inspect
str = <<-EOS.strip
#<MongoUser:0x01234567> {
#<MongoUser:placeholder_id> {
:_id => #{object_id},
:first_name => "Al",
:last_name => "Capone"
}
EOS
out.gsub!(/0x([a-f\d]+)/, "0x01234567")
expect(out).to eq(str)
expect(out).to be_similar_to(str)
end
it "should print the class" do

View file

@ -28,8 +28,8 @@ RSpec.describe 'AwesomePrint/Ripple', skip: ->{ !ExtVerifier.has_ripple? }.call
user = RippleUser.new :_id => "12345", :first_name => "Al", :last_name => "Capone"
out = @ap.send :awesome, user
expect(out.gsub(/0x([a-f\d]+)/, "0x01234567")).to eq <<-EOS.strip
#<RippleUser:0x01234567> {
expect(out).to be_similar_to <<-EOS.strip
#<RippleUser:placeholder_id> {
:_id => "12345",
:first_name => "Al",
:last_name => "Capone"

View file

@ -24,13 +24,13 @@ RSpec.describe "Objects" do
hello = Hello.new
out = hello.ai(:plain => true, :raw => true)
str = <<-EOS.strip
#<Hello:0x01234567
#<Hello:placeholder_id
attr_accessor :dabra = 3,
attr_reader :abra = 1,
attr_writer :ca = 2
>
EOS
expect(out.gsub(/0x([a-f\d]+)/, "0x01234567")).to eq(str)
expect(out).to be_similar_to(str)
expect(hello.ai(:plain => true, :raw => false)).to eq(hello.inspect)
end
@ -44,13 +44,13 @@ EOS
hello = Hello.new
out = hello.ai(:plain => true, :raw => true)
str = <<-EOS.strip
#<Hello:0x01234567
#<Hello:placeholder_id
@abra = 1,
@ca = 2,
@dabra = 3
>
EOS
expect(out.gsub(/0x([a-f\d]+)/, "0x01234567")).to eq(str)
expect(out).to be_similar_to(str)
expect(hello.ai(:plain => true, :raw => false)).to eq(hello.inspect)
end
@ -69,7 +69,7 @@ EOS
hello = Hello.new
out = hello.ai(:plain => true, :raw => true)
str = <<-EOS.strip
#<Hello:0x01234567
#<Hello:placeholder_id
@doo = 1,
@dooby = 2,
@scooby = 3,
@ -78,7 +78,7 @@ EOS
attr_writer :ca = 2
>
EOS
expect(out.gsub(/0x([a-f\d]+)/, "0x01234567")).to eq(str)
expect(out).to be_similar_to(str)
expect(hello.ai(:plain => true, :raw => false)).to eq(hello.inspect)
end
@ -96,13 +96,13 @@ EOS
hello = Hello.new
out = hello.ai(:raw => true)
str = <<-EOS.strip
#<Hello:0x01234567
#<Hello:placeholder_id
\e[0;36m@dabra\e[0m\e[0;37m = \e[0m\e[1;34m3\e[0m,
\e[1;36mattr_reader\e[0m \e[0;35m:abra\e[0m\e[0;37m = \e[0m\e[1;34m1\e[0m,
\e[1;36mattr_writer\e[0m \e[0;35m:ca\e[0m\e[0;37m = \e[0m\e[1;34m2\e[0m
>
EOS
expect(out.gsub(/0x([a-f\d]+)/, "0x01234567")).to eq(str)
expect(out).to be_similar_to(str)
expect(hello.ai(:plain => true, :raw => false)).to eq(hello.inspect)
end
@ -120,9 +120,9 @@ EOS
hello = Hello.new
out = hello.ai(:multiline => false, :plain => true, :raw => true)
str = <<-EOS.strip
#<Hello:0x01234567 @dabra = 3, attr_reader :abra = 1, attr_writer :ca = 2>
#<Hello:placeholder_id @dabra = 3, attr_reader :abra = 1, attr_writer :ca = 2>
EOS
expect(out.gsub(/0x([a-f\d]+)/, "0x01234567")).to eq(str)
expect(out).to be_similar_to(str)
expect(hello.ai(:plain => true, :raw => false)).to eq(hello.inspect)
end
end

View file

@ -47,6 +47,26 @@ RSpec.configure do |config|
end
end
# This matcher handles the normalization of objects to replace non deterministic
# parts (such as object IDs) with simple placeholder strings before doing a
# comparison with a given string. It's important that this method only matches
# a string which strictly conforms to the expected object ID format.
RSpec::Matchers.define :be_similar_to do |expected|
match do |actual|
@actual = normalize_object_strings(actual)
values_match? expected, @actual
end
diffable
end
# Override the Object IDs with a placeholder so that we are only checking
# that an ID is present and not that it matches a certain value. This is
# necessary as the Object IDs are not deterministic.
def normalize_object_strings(str)
str.gsub(/#<(.*?):0x[a-f\d]+/, '#<\1:placeholder_id')
end
def stub_dotfile!
dotfile = File.join(ENV["HOME"], ".aprc")
expect(File).to receive(:readable?).at_least(:once).with(dotfile).and_return(false)