1
0
Fork 0
mirror of https://github.com/awesome-print/awesome_print synced 2023-03-27 23:22:34 -04:00
awesome_print/spec/spec_helper.rb
Gerard Caulfield c5e8669e9c
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.
2016-05-10 04:32:36 +10:00

80 lines
2.6 KiB
Ruby

# Copyright (c) 2010-2013 Michael Dvorkin
#
# Awesome Print is freely distributable under the terms of MIT license.
# See LICENSE file or http://www.opensource.org/licenses/mit-license.php
#------------------------------------------------------------------------------
#
# Running specs from the command line:
# $ rake spec # Entire spec suite.
# $ rspec spec/objects_spec.rb # Individual spec file.
#
# NOTE: To successfully run specs with Ruby 1.8.6 the older versions of
# Bundler and RSpec gems are required:
#
# $ gem install bundler -v=1.0.2
# $ gem install rspec -v=2.6.0
#
require 'codeclimate-test-reporter'
CodeClimate::TestReporter.start
$LOAD_PATH.unshift(File.dirname(__FILE__))
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
Dir[File.dirname(__FILE__) + '/support/**/*.rb'].each do |file|
require file
end
ExtVerifier.require_dependencies!(%w{rails active_record action_view
active_support/all mongoid mongo_mapper ripple nobrainer})
require 'nokogiri'
require 'awesome_print'
RSpec.configure do |config|
config.disable_monkey_patching!
# TODO: Make specs not order dependent
# config.order = :random
Kernel.srand config.seed
config.filter_run focus: true
config.run_all_when_everything_filtered = true
config.expect_with :rspec do |expectations|
expectations.syntax = :expect
end
config.mock_with :rspec do |mocks|
mocks.syntax = :expect
mocks.verify_partial_doubles = true
end
if config.files_to_run.one?
config.default_formatter = 'doc'
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)
end
def capture!
standard, $stdout = $stdout, StringIO.new
yield
ensure
$stdout = standard
end