Project

General

Profile

Bug #771 ยป testcase_compatibility.patch

bitsweat (Jeremy Daer), 11/21/2008 09:33 AM

View differences:

unit/assertions.rb (working copy)
1
# test/unit compatibility layer using minitest.
2

  
3 1
require 'minitest/unit'
4 2
require 'pp'
5 3

  
6 4
module Test
7 5
  module Unit
8
    TEST_UNIT_IMPLEMENTATION = 'test/unit compatibility layer using minitest'
9

  
10
    def self.setup_argv(original_argv=ARGV)
11
      minitest_argv = []
12
      files = []
13
      reject = []
14
      original_argv = original_argv.dup
15
      while arg = original_argv.shift
16
        case arg
17
        when '-v'
18
          minitest_argv << arg
19
        when /\A(-n)(.+)?/, /\A(--name)=?\b(.+)?/
20
          minitest_argv << $1
21
          minitest_argv << ($2 || original_argv.shift)
22
        when /\A-x(.+)?/
23
          reject << ($1 || original_argv.shift)
24
        else
25
          files << arg
26
        end
27
      end
28

  
29
      if block_given?
30
        files = yield files
31
      end
32

  
33
      files.map! {|f|
34
        if File.directory? f
35
          Dir["#{f}/**/test_*.rb"]
36
        elsif File.file? f
37
          f
38
        else
39
          raise ArgumentError, "file not found: #{f}"
40
        end
41
      }
42
      files.flatten!
43

  
44
      reject_pat = Regexp.union(reject.map {|r| /#{r}/ })
45
      files.reject! {|f| reject_pat =~ f }
46
        
47
      files.each {|f|
48
        d = File.dirname(File.expand_path(f))
49
        unless $:.include? d
50
          $: << d
51
        end
52
        begin
53
          require f
54
        rescue LoadError
55
          puts "#{f}: #{$!}"
56
        end
57
      }
58

  
59
      ARGV.replace minitest_argv
60
    end
61

  
62 6
    module Assertions
63 7
      include MiniTest::Assertions
64 8

  
......
162 106
        template.gsub(/\?/) { mu_pp(arguments.shift) }
163 107
      end
164 108
    end
165

  
166
    class TestCase < MiniTest::Unit::TestCase
167
      include Assertions
168
      def self.test_order
169
        :sorted
170
      end
171
    end
172 109
  end
173 110
end
174

  
175
MiniTest::Unit.autorun
unit/testcase.rb (working copy)
1 1
# test/unit compatibility layer using minitest.
2 2

  
3
require 'minitest/unit'
4
require 'pp'
3
require 'test/unit/assertions'
5 4

  
6 5
module Test
7 6
  module Unit
......
59 58
      ARGV.replace minitest_argv
60 59
    end
61 60

  
62
    module Assertions
63
      include MiniTest::Assertions
64

  
65
      def mu_pp(obj)
66
        obj.pretty_inspect.chomp
67
      end
68

  
69
      def assert_raise(*args, &b)
70
        assert_raises(*args, &b)
71
      end
72

  
73
      def assert_nothing_raised(*args)
74
        if Module === args.last
75
          msg = nil
76
        else
77
          msg = args.pop
78
        end
79
        begin
80
          yield
81
        rescue Exception => e
82
          if ((args.empty? && !e.instance_of?(MiniTest::Assertion)) ||
83
              args.any? {|a| a.instance_of?(Module) ? e.is_a?(a) : e.class == a })
84
            msg = message(msg) { "Exception raised:\n<#{mu_pp(e)}>" }
85
            raise MiniTest::Assertion, msg.call, e.backtrace
86
          else
87
            raise
88
          end
89
        end
90
        nil
91
      end
92

  
93
      def assert_nothing_thrown(msg=nil)
94
        begin
95
          yield
96
        rescue ArgumentError => error
97
          raise error if /\Auncaught throw (.+)\z/m !~ error.message
98
          msg = message(msg) { "<#{$1}> was thrown when nothing was expected" }
99
          flunk(msg)
100
        end
101
        assert(true, "Expected nothing to be thrown")
102
      end
103

  
104
      def assert_equal(exp, act, msg = nil)
105
        msg = message(msg) {
106
          exp_str = mu_pp(exp)
107
          act_str = mu_pp(act)
108
          exp_comment = ''
109
          act_comment = ''
110
          if exp_str == act_str
111
            if exp.is_a?(String) && act.is_a?(String)
112
              exp_comment = " (#{exp.encoding})"
113
              act_comment = " (#{act.encoding})"
114
            elsif exp.is_a?(Time) && act.is_a?(Time)
115
              exp_comment = " (nsec=#{exp.nsec})"
116
              act_comment = " (nsec=#{act.nsec})"
117
            end
118
          elsif !Encoding.compatible?(exp_str, act_str)
119
            if exp.is_a?(String) && act.is_a?(String)
120
              exp_str = exp.dump
121
              act_str = act.dump
122
              exp_comment = " (#{exp.encoding})"
123
              act_comment = " (#{act.encoding})"
124
            else
125
              exp_str = exp_str.dump
126
              act_str = act_str.dump
127
            end
128
          end
129
          "<#{exp_str}>#{exp_comment} expected but was\n<#{act_str}>#{act_comment}"
130
        }
131
        assert(exp == act, msg)
132
      end
133

  
134
      def assert_not_nil(exp, msg=nil)
135
        msg = message(msg) { "<#{mu_pp(exp)}> expected to not be nil" }
136
        assert(!exp.nil?, msg)
137
      end
138

  
139
      def assert_not_equal(exp, act, msg=nil)
140
        msg = message(msg) { "<#{mu_pp(exp)}> expected to be != to\n<#{mu_pp(act)}>" }
141
        assert(exp != act, msg)
142
      end
143

  
144
      def assert_no_match(regexp, string, msg=nil)
145
        assert_instance_of(Regexp, regexp, "The first argument to assert_no_match should be a Regexp.")
146
        msg = message(msg) { "<#{mu_pp(regexp)}> expected to not match\n<#{mu_pp(string)}>" }
147
        assert(regexp !~ string, msg)
148
      end
149

  
150
      def assert_not_same(expected, actual, message="")
151
        msg = message(msg) { build_message(message, <<EOT, expected, expected.__id__, actual, actual.__id__) }
152
<?>
153
with id <?> expected to not be equal\\? to
154
<?>
155
with id <?>.
156
EOT
157
        assert(!actual.equal?(expected), msg)
158
      end
159

  
160
      def build_message(head, template=nil, *arguments)
161
        template &&= template.chomp
162
        template.gsub(/\?/) { mu_pp(arguments.shift) }
163
      end
164
    end
165

  
166 61
    class TestCase < MiniTest::Unit::TestCase
167 62
      include Assertions
168 63
      def self.test_order
......
171 66
    end
172 67
  end
173 68
end
174

  
175
MiniTest::Unit.autorun
unit.rb (working copy)
1 1
# test/unit compatibility layer using minitest.
2 2

  
3 3
require 'minitest/unit'
4
require 'pp'
4
require 'test/unit/testcase'
5 5

  
6
module Test
7
  module Unit
8
    TEST_UNIT_IMPLEMENTATION = 'test/unit compatibility layer using minitest'
9

  
10
    def self.setup_argv(original_argv=ARGV)
11
      minitest_argv = []
12
      files = []
13
      reject = []
14
      original_argv = original_argv.dup
15
      while arg = original_argv.shift
16
        case arg
17
        when '-v'
18
          minitest_argv << arg
19
        when /\A(-n)(.+)?/, /\A(--name)=?\b(.+)?/
20
          minitest_argv << $1
21
          minitest_argv << ($2 || original_argv.shift)
22
        when /\A-x(.+)?/
23
          reject << ($1 || original_argv.shift)
24
        else
25
          files << arg
26
        end
27
      end
28

  
29
      if block_given?
30
        files = yield files
31
      end
32

  
33
      files.map! {|f|
34
        if File.directory? f
35
          Dir["#{f}/**/test_*.rb"]
36
        elsif File.file? f
37
          f
38
        else
39
          raise ArgumentError, "file not found: #{f}"
40
        end
41
      }
42
      files.flatten!
43

  
44
      reject_pat = Regexp.union(reject.map {|r| /#{r}/ })
45
      files.reject! {|f| reject_pat =~ f }
46
        
47
      files.each {|f|
48
        d = File.dirname(File.expand_path(f))
49
        unless $:.include? d
50
          $: << d
51
        end
52
        begin
53
          require f
54
        rescue LoadError
55
          puts "#{f}: #{$!}"
56
        end
57
      }
58

  
59
      ARGV.replace minitest_argv
60
    end
61

  
62
    module Assertions
63
      include MiniTest::Assertions
64

  
65
      def mu_pp(obj)
66
        obj.pretty_inspect.chomp
67
      end
68

  
69
      def assert_raise(*args, &b)
70
        assert_raises(*args, &b)
71
      end
72

  
73
      def assert_nothing_raised(*args)
74
        if Module === args.last
75
          msg = nil
76
        else
77
          msg = args.pop
78
        end
79
        begin
80
          yield
81
        rescue Exception => e
82
          if ((args.empty? && !e.instance_of?(MiniTest::Assertion)) ||
83
              args.any? {|a| a.instance_of?(Module) ? e.is_a?(a) : e.class == a })
84
            msg = message(msg) { "Exception raised:\n<#{mu_pp(e)}>" }
85
            raise MiniTest::Assertion, msg.call, e.backtrace
86
          else
87
            raise
88
          end
89
        end
90
        nil
91
      end
92

  
93
      def assert_nothing_thrown(msg=nil)
94
        begin
95
          yield
96
        rescue ArgumentError => error
97
          raise error if /\Auncaught throw (.+)\z/m !~ error.message
98
          msg = message(msg) { "<#{$1}> was thrown when nothing was expected" }
99
          flunk(msg)
100
        end
101
        assert(true, "Expected nothing to be thrown")
102
      end
103

  
104
      def assert_equal(exp, act, msg = nil)
105
        msg = message(msg) {
106
          exp_str = mu_pp(exp)
107
          act_str = mu_pp(act)
108
          exp_comment = ''
109
          act_comment = ''
110
          if exp_str == act_str
111
            if exp.is_a?(String) && act.is_a?(String)
112
              exp_comment = " (#{exp.encoding})"
113
              act_comment = " (#{act.encoding})"
114
            elsif exp.is_a?(Time) && act.is_a?(Time)
115
              exp_comment = " (nsec=#{exp.nsec})"
116
              act_comment = " (nsec=#{act.nsec})"
117
            end
118
          elsif !Encoding.compatible?(exp_str, act_str)
119
            if exp.is_a?(String) && act.is_a?(String)
120
              exp_str = exp.dump
121
              act_str = act.dump
122
              exp_comment = " (#{exp.encoding})"
123
              act_comment = " (#{act.encoding})"
124
            else
125
              exp_str = exp_str.dump
126
              act_str = act_str.dump
127
            end
128
          end
129
          "<#{exp_str}>#{exp_comment} expected but was\n<#{act_str}>#{act_comment}"
130
        }
131
        assert(exp == act, msg)
132
      end
133

  
134
      def assert_not_nil(exp, msg=nil)
135
        msg = message(msg) { "<#{mu_pp(exp)}> expected to not be nil" }
136
        assert(!exp.nil?, msg)
137
      end
138

  
139
      def assert_not_equal(exp, act, msg=nil)
140
        msg = message(msg) { "<#{mu_pp(exp)}> expected to be != to\n<#{mu_pp(act)}>" }
141
        assert(exp != act, msg)
142
      end
143

  
144
      def assert_no_match(regexp, string, msg=nil)
145
        assert_instance_of(Regexp, regexp, "The first argument to assert_no_match should be a Regexp.")
146
        msg = message(msg) { "<#{mu_pp(regexp)}> expected to not match\n<#{mu_pp(string)}>" }
147
        assert(regexp !~ string, msg)
148
      end
149

  
150
      def assert_not_same(expected, actual, message="")
151
        msg = message(msg) { build_message(message, <<EOT, expected, expected.__id__, actual, actual.__id__) }
152
<?>
153
with id <?> expected to not be equal\\? to
154
<?>
155
with id <?>.
156
EOT
157
        assert(!actual.equal?(expected), msg)
158
      end
159

  
160
      def build_message(head, template=nil, *arguments)
161
        template &&= template.chomp
162
        template.gsub(/\?/) { mu_pp(arguments.shift) }
163
      end
164
    end
165

  
166
    class TestCase < MiniTest::Unit::TestCase
167
      include Assertions
168
      def self.test_order
169
        :sorted
170
      end
171
    end
172
  end
173
end
174

  
175 6
MiniTest::Unit.autorun