Module: Matchy

Defined in:
lib/matchy.rb,
lib/matchy/modals.rb,
lib/matchy/version.rb,
lib/matchy/assertions.rb,
lib/matchy/custom_matcher.rb,
lib/matchy/matcher_builder.rb,
lib/matchy/expectation_builder.rb,
lib/matchy/built_in/error_expectations.rb,
lib/matchy/built_in/truth_expectations.rb,
lib/matchy/built_in/change_expectations.rb,
lib/matchy/built_in/operator_expectations.rb,
lib/matchy/built_in/enumerable_expectations.rb

Overview

Matchy works with either Test::Unit or MiniTest, on both Ruby 1.8 and >= 1.9.1. Note that Ruby 1.9.0 is NOT supported… but you should really be using 1.9.1 ;)

Defined Under Namespace

Modules: Assertions, CustomMatcher, ExpectationBuilder, Expectations, MatcherBuilder, Modals, VERSION Classes: Adapter

Constant Summary collapse

@@adapters =
{}
@@current_adapter =
nil

Class Method Summary collapse

Class Method Details

.adapter(adapter_name, full_name, &block) ⇒ Object



16
17
18
# File 'lib/matchy.rb', line 16

def adapter(adapter_name, full_name, &block)
  @@adapters[adapter_name.to_s] = Adapter.new(adapter_name, full_name).extend(Module.new(&block))
end

.adapter_not_found_msgObject



32
33
34
# File 'lib/matchy.rb', line 32

def adapter_not_found_msg
  "Matchy detected you have a testing library loaded, but it doesn't have an adapter for it. Try these adapters: #{@@adapters.keys.sort.join(", ")}"
end

.classic?Boolean

Returns:

  • (Boolean)


57
58
59
# File 'lib/matchy.rb', line 57

def classic?
  defined?(Test::Unit) && defined?(Test::Unit::TestCase) && !minitest_tu_shim?
end

.initObject



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/matchy.rb', line 74

def init
  require 'matchy/assertions'
  require 'matchy/expectation_builder'
  require 'matchy/modals'
  require 'matchy/matcher_builder'
  require 'matchy/custom_matcher'
  require 'matchy/version'

  require 'matchy/built_in/enumerable_expectations'
  require 'matchy/built_in/error_expectations'
  require 'matchy/built_in/truth_expectations'
  require 'matchy/built_in/operator_expectations'
  require 'matchy/built_in/change_expectations'

  Matchy.test_case_class.class_eval do
    include Matchy::Expectations::TestCaseExtensions
    include Matchy::Assertions
    
    # Track the current testcase and 
    # provide it to the operator matchers.
    alias_method :old_run_method_aliased_by_matchy, :run
    def run(whatever, *args, &block)
      $current_test_case = self
      old_run_method_aliased_by_matchy(whatever, *args, &block)
    end
  end

  Object.class_eval { include Matchy::CustomMatcher }
end

.minitest?Boolean

Returns:

  • (Boolean)


61
62
63
64
65
66
# File 'lib/matchy.rb', line 61

def minitest?
  # We have to decide if we really have a suite of MiniTest tests.
  # Rails for example defines MiniTest, so to only check for
  # defined?(MiniTest) would be malicious
  defined?(MiniTest) && !classic?
end

.minitest_tu_shim?Boolean

Returns:

  • (Boolean)


52
53
54
55
# File 'lib/matchy.rb', line 52

def minitest_tu_shim?
  defined?(MiniTest) && defined?(MiniTest::Assertions) &&
  defined?(Test::Unit::TestCase) && Test::Unit::TestCase < MiniTest::Assertions
end

.use(adapter_name) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/matchy.rb', line 19

def use(adapter_name)
  adapter_name = adapter_name.to_s
  @@current_adapter = @@adapters[adapter_name] or raise "Couldn't find adapter by name '#{adapter_name}'"
  if ENV["MATCHY_DEBUG"] || $MATCHY_DEBUG
    msg = "\n"
    msg << " -- This is Ruby version #{RUBY_VERSION}, using #{@@current_adapter.full_name}"
    msg << " (MiniTest-Test::Unit shim)" if @@current_adapter.name == :minitest && Matchy.minitest_tu_shim?
    msg << "\n\n"
    print msg
  end
  @@current_adapter.on_use
  Matchy.init
end