File: test_fixture.rb

package info (click to toggle)
libcairo-ruby 1.8.1-1
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 2,476 kB
  • ctags: 5,116
  • sloc: ruby: 9,621; ansic: 6,413; sh: 19; makefile: 3
file content (252 lines) | stat: -rw-r--r-- 8,605 bytes parent folder | download
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
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
class TestUnitFixture < Test::Unit::TestCase
  module EmptyModule
  end

  def test_setup_without_option
    test_case = assert_setup([:setup,
                              :custom_setup_method0,
                              :custom_setup_method1,
                              :custom_setup_method3],
                             [])
    assert_inherited_setup([:setup,
                            :custom_setup_method0,
                            :custom_setup_method1,
                            :custom_setup_method3],
                           test_case)
    assert_inherited_setup([:setup], nil)
  end

  def test_setup_with_before_option
    test_case = assert_setup([:custom_setup_method3,
                              :custom_setup_method0,
                              :custom_setup_method1,
                              :setup],
                             [[{:before => :append}],
                              [{:before => :append}],
                              [{:before => :prepend}],
                              [{:before => :prepend}]])
    assert_inherited_setup([:custom_setup_method3,
                            :custom_setup_method0,
                            :custom_setup_method1,
                            :setup],
                           test_case)
    assert_inherited_setup([:setup], nil)
  end

  def test_setup_with_after_option
    test_case = assert_setup([:setup,
                              :custom_setup_method3,
                              :custom_setup_method0,
                              :custom_setup_method1],
                             [[{:after => :append}],
                              [{:after => :append}],
                              [{:after => :prepend}],
                              [{:after => :prepend}]])
    assert_inherited_setup([:setup,
                            :custom_setup_method3,
                            :custom_setup_method0,
                            :custom_setup_method1],
                           test_case)
    assert_inherited_setup([:setup], nil)
  end

  def test_setup_with_invalid_option
    assert_invalid_setup_option(:unknown => true)
    assert_invalid_setup_option(:before => :unknown)
    assert_invalid_setup_option(:after => :unknown)
  end

  def test_teardown_without_option
    test_case = assert_teardown([:custom_teardown_method3,
                                 :custom_teardown_method1,
                                 :custom_teardown_method0,
                                 :teardown],
                                [])
    assert_inherited_teardown([:custom_teardown_method3,
                               :custom_teardown_method1,
                               :custom_teardown_method0,
                               :teardown],
                              test_case)
    assert_inherited_teardown([:teardown], nil)
  end

  def test_teardown_with_before_option
    test_case = assert_teardown([:custom_teardown_method3,
                                 :custom_teardown_method0,
                                 :custom_teardown_method1,
                                 :teardown],
                                [[{:before => :append}],
                                 [{:before => :append}],
                                 [{:before => :prepend}],
                                 [{:before => :prepend}]])
    assert_inherited_teardown([:custom_teardown_method3,
                               :custom_teardown_method0,
                               :custom_teardown_method1,
                               :teardown],
                              test_case)
    assert_inherited_teardown([:teardown], nil)
  end

  def test_teardown_with_after_option
    test_case = assert_teardown([:teardown,
                                 :custom_teardown_method3,
                                 :custom_teardown_method0,
                                 :custom_teardown_method1],
                                [[{:after => :append}],
                                 [{:after => :append}],
                                 [{:after => :prepend}],
                                 [{:after => :prepend}]])
    assert_inherited_teardown([:teardown,
                               :custom_teardown_method3,
                               :custom_teardown_method0,
                               :custom_teardown_method1],
                              test_case)
    assert_inherited_teardown([:teardown], nil)
  end

  def test_teardown_with_invalid_option
    assert_invalid_teardown_option(:unknown => true)
    assert_invalid_teardown_option(:before => :unknown)
    assert_invalid_teardown_option(:after => :unknown)
  end

  private
  def assert_setup_customizable(expected, parent, options)
    called = []
    test_case = Class.new(parent || Test::Unit::TestCase) do
      yield(self, :before) if block_given?

      @@called = called
      def setup
        @@called << :setup
      end

      setup(*(options[0] || [])) if options
      def custom_setup_method0
        @@called << :custom_setup_method0
      end

      def custom_setup_method1
        @@called << :custom_setup_method1
      end
      setup(*[:custom_setup_method1, *(options[1] || [])]) if options

      setup(*(options[2] || [])) if options
      def custom_setup_method2
        @@called << :custom_setup_method2
      end
      unregister_setup(:custom_setup_method2) if options

      setup(*(options[3] || [])) if options
      def custom_setup_method3
        @@called << :custom_setup_method3
      end

      def test_nothing
      end

      yield(self, :after) if block_given?
    end

    test_case.new("test_nothing").run(Test::Unit::TestResult.new) {}
    assert_equal(expected, called)
    test_case
  end

  def assert_setup(expected, options)
    _test_case = assert_setup_customizable(expected, nil, options)
    assert_setup_customizable(expected, nil, options) do |test_case, tag|
      test_case.send(:include, EmptyModule) if tag == :before
    end
    _test_case
  end

  def assert_inherited_setup(expected, parent)
    _test_case = assert_setup_customizable(expected, parent, nil)
    assert_setup_customizable(expected, parent, nil) do |test_case, tag|
      test_case.send(:include, EmptyModule) if tag == :before
    end
    _test_case
  end

  def assert_teardown_customizable(expected, parent, options)
    called = []
    test_case = Class.new(parent || Test::Unit::TestCase) do
      yield(self, :before) if block_given?

      @@called = called
      def teardown
        @@called << :teardown
      end

      teardown(*(options[0] || [])) if options
      def custom_teardown_method0
        @@called << :custom_teardown_method0
      end

      def custom_teardown_method1
        @@called << :custom_teardown_method1
      end
      teardown(*[:custom_teardown_method1, *(options[1] || [])]) if options

      teardown(*(options[2] || [])) if options
      def custom_teardown_method2
        @@called << :custom_teardown_method2
      end
      unregister_teardown(:custom_teardown_method2) if options

      teardown(*(options[3] || [])) if options
      def custom_teardown_method3
        @@called << :custom_teardown_method3
      end

      def test_nothing
      end

      yield(self, :after) if block_given?
    end

    test_case.new("test_nothing").run(Test::Unit::TestResult.new) {}
    assert_equal(expected, called)
    test_case
  end

  def assert_teardown(expected, options)
    assert_teardown_customizable(expected, nil, options)
    assert_teardown_customizable(expected, nil, options) do |test_case, tag|
      test_case.send(:include, EmptyModule) if tag == :before
    end
  end

  def assert_inherited_teardown(expected, parent)
    assert_teardown_customizable(expected, parent, nil)
    assert_teardown_customizable(expected, parent, nil) do |test_case, tag|
      test_case.send(:include, EmptyModule) if tag == :before
    end
  end

  def assert_invalid_option(fixture_type, option)
    exception = assert_raise(ArgumentError) do
      Class.new(Test::Unit::TestCase) do
        def test_nothing
        end

        send(fixture_type, option)
        def fixture
        end
      end
    end
    assert_equal("must be {:before => :prepend}, {:before => :append}, " +
                 "{:after => :prepend} or {:after => :append}" +
                 ": #{option.inspect}",
                 exception.message)
  end

  def assert_invalid_setup_option(option)
    assert_invalid_option(:setup, option)
  end

  def assert_invalid_teardown_option(option)
    assert_invalid_option(:teardown, option)
  end
end