module Cordon

Public Class Methods

blacklist(subject, methods) click to toggle source

Declare specific methods as off-limits so that invocations raise an exception

# File lib/cordon.rb, line 42
def self.blacklist(subject, methods)
  Blacklist.wrap_methods(subject, methods)
end
dont_filter_violation_backtrace!() click to toggle source

Reset custom filtering of backtraces. (This is mostly here for ease of testing.)

# File lib/cordon.rb, line 75
def self.dont_filter_violation_backtrace!
  Violation.clear_custom_backtrace_filters
end
embargo(framework) click to toggle source

Shorthand for blacklisting the undesirable methods in specific frameworks

# File lib/cordon.rb, line 30
def self.embargo(framework)
  wrap_framework(framework, :blacklist)
end
filter_violation_backtrace(&proc) click to toggle source

Allow custom filtering of backtraces on Cordon::Violation errors. Pass this a block that takes a backtrace and returns a backtrace. Multiple filters can be defined; all will be applied.

# File lib/cordon.rb, line 69
def self.filter_violation_backtrace(&proc)
  Violation.add_custom_backtrace_filter(&proc)
end
incursion_report() click to toggle source

Plain-text report of watchlist incursions

# File lib/cordon.rb, line 62
def self.incursion_report
  Watchlist.incursion_report
end
incursions() click to toggle source

Convenience accessor for reporting on watchlist incursions

# File lib/cordon.rb, line 57
def self.incursions
  Watchlist.incursions
end
monitor(framework) click to toggle source

Shorthand for watchlisting the undesirable methods in specific frameworks NOTE: while this will record incursions, you’ll have to call Cordon.incursion_report yourself until I figure out how to make it print out at exit.

# File lib/cordon.rb, line 37
def self.monitor(framework)
  wrap_framework(framework, :watchlist)
end
watchlist(subject, methods) click to toggle source

Declare specific methods as off-limits so that invocations are logged

# File lib/cordon.rb, line 47
def self.watchlist(subject, methods)
  Watchlist.wrap_methods(subject, methods)
end
wrap_assertions_with(custom_method_name) click to toggle source

Allow user-defined aliases of assert_that

# File lib/cordon.rb, line 52
def self.wrap_assertions_with(custom_method_name)
  Sanitaire.wrap_assertions_with(custom_method_name)
end

Protected Class Methods

wrap_framework(framework, technique) click to toggle source
# File lib/cordon.rb, line 81
def self.wrap_framework(framework, technique)
  raise "Don't know how to wrap framework with #{technique.inspect}!" unless [:blacklist, :watchlist].include?(technique)

  # Figure out which methods to wrap
  list = []
  case framework
  when :rspec
    list << [Kernel, [:should, :should_not]]
  when :minitest_spec
    must_and_wont = MiniTest::Expectations.instance_methods.map(&:to_s).select {|e| e =~ /^(must|wont)_/}
    list << [MiniTest::Expectations, must_and_wont]
  else
    raise "I don't know how to embargo #{framework}!"
  end

  # Wrap them using the appropriate technique
  # (which should be either :blacklist or :watchlist)
  list.each do |subject, methods|
    send technique, subject, methods
  end
end