File: assertion_library.rb

package info (click to toggle)
ruby-multi-test 0.1.2-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 300 kB
  • sloc: ruby: 136; sh: 38; makefile: 7
file content (73 lines) | stat: -rw-r--r-- 1,816 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
require 'multi_test/minitest_world'

module MultiTest
  class AssertionLibrary
    def self.detect_best
      available.detect(&:require?)
    end

    def initialize(requirer, extender)
      @requirer = requirer
      @extender = extender
    end

    def require?
      begin
        @requirer.call
        true
      rescue LoadError
        false
      end
    end

    def extend_world(world)
      @extender.call(world)
    end

    private

    def self.null
      AssertionLibrary.new(
        proc { },
        proc { }
      )
    end

    def self.available
      @available ||= [
        AssertionLibrary.new(
          proc { require 'rspec/expectations' },
          proc { |object| object.extend(::RSpec::Matchers) }
        ),
        AssertionLibrary.new(
          proc {
            require 'spec/expectations'
            require 'spec/runner/differs/default'
            require 'ostruct'
          },
          proc { |object|
            options = OpenStruct.new(:diff_format => :unified, :context_lines => 3)
            Spec::Expectations.differ = Spec::Expectations::Differs::Default.new(options)
            object.extend(Spec::Matchers)
          }
        ),
        AssertionLibrary.new(
          proc { require 'minitest/assertions' },
          proc { |object| object.extend(MinitestWorld) }
        ),
        AssertionLibrary.new(
          proc { require 'minitest/unit' },
          proc { |object| object.extend(MiniTest::Assertions) }
        ),
        AssertionLibrary.new(
          proc { require 'test/unit/assertions' },
          proc { |object| object.extend(Test::Unit::Assertions) }
        ),
        # Null assertion library must come last to prevent exceptions if
        # unable to load a test framework
        AssertionLibrary.null
      ]
    end
  end
end