~ubuntu-branches/ubuntu/karmic/libcairo-ruby/karmic

« back to all changes in this revision

Viewing changes to test-unit/lib/test/unit/priority.rb

  • Committer: Bazaar Package Importer
  • Author(s): Paul van Tilburg, Gunnar Wolf, Paul van Tilburg
  • Date: 2009-05-05 12:14:31 UTC
  • mfrom: (1.1.6 upstream)
  • Revision ID: james.westby@ubuntu.com-20090505121431-n803uyjz51je38l0
Tags: 1.8.0-1
[ Gunnar Wolf ]
* Changed section to Ruby as per ftp-masters' request

[ Paul van Tilburg ]
* New upstream release.
* debian/patches:
  - Dropped patch 01_fix-st.h-ruby1.9-paths: fixed by upstream. 
* debian/control:
  - Bumped standards version to 3.8.1; no changes required.
  - Added ${misc:Depends} to the depends of libcairo-ruby (binary).

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
require "fileutils"
 
2
require "tmpdir"
 
3
 
 
4
module Test
 
5
  module Unit
 
6
    module Priority
 
7
      class << self
 
8
        def included(base)
 
9
          base.extend(ClassMethods)
 
10
 
 
11
          base.class_eval do
 
12
            setup :priority_setup, :before => :prepend
 
13
            teardown :priority_teardown, :after => :append
 
14
          end
 
15
        end
 
16
      end
 
17
 
 
18
      class Checker
 
19
        class << self
 
20
          def have_priority?(name)
 
21
            singleton_class = (class << self; self; end)
 
22
            singleton_class.method_defined?(priority_check_method_name(name))
 
23
          end
 
24
 
 
25
          def need_to_run?(test)
 
26
            priority = test[:priority] || :normal
 
27
            if have_priority?(priority)
 
28
              send(priority_check_method_name(priority), test)
 
29
            else
 
30
              true
 
31
            end
 
32
          end
 
33
 
 
34
          def run_priority_must?(test)
 
35
            true
 
36
          end
 
37
 
 
38
          def run_priority_important?(test)
 
39
            rand > 0.1
 
40
          end
 
41
 
 
42
          def run_priority_high?(test)
 
43
            rand > 0.3
 
44
          end
 
45
 
 
46
          def run_priority_normal?(test)
 
47
            rand > 0.5
 
48
          end
 
49
 
 
50
          def run_priority_low?(test)
 
51
            rand > 0.75
 
52
          end
 
53
 
 
54
          def run_priority_never?(test)
 
55
            false
 
56
          end
 
57
 
 
58
          private
 
59
          def priority_check_method_name(priority_name)
 
60
            "run_priority_#{priority_name}?"
 
61
          end
 
62
        end
 
63
 
 
64
        attr_reader :test
 
65
        def initialize(test)
 
66
          @test = test
 
67
        end
 
68
 
 
69
        def setup
 
70
          FileUtils.rm_f(passed_file)
 
71
        end
 
72
 
 
73
        def teardown
 
74
          if @test.send(:passed?)
 
75
            FileUtils.touch(passed_file)
 
76
          else
 
77
            FileUtils.rm_f(passed_file)
 
78
          end
 
79
        end
 
80
 
 
81
        def need_to_run?
 
82
          !previous_test_success? or self.class.need_to_run?(@test)
 
83
        end
 
84
 
 
85
        private
 
86
        def previous_test_success?
 
87
          File.exist?(passed_file)
 
88
        end
 
89
 
 
90
        def result_dir
 
91
          components = [".test-result",
 
92
                        @test.class.name || "AnonymousTestCase",
 
93
                        @test.method_name.to_s]
 
94
          parent_directories = [File.dirname($0), Dir.pwd]
 
95
          if Process.respond_to?(:uid)
 
96
            parent_directories << File.join(Dir.tmpdir, Process.uid.to_s)
 
97
          end
 
98
          parent_directories.each do |parent_directory|
 
99
            dir = File.expand_path(File.join(parent_directory, *components))
 
100
            begin
 
101
              FileUtils.mkdir_p(dir)
 
102
              return dir
 
103
            rescue Errno::EACCES
 
104
            end
 
105
          end
 
106
 
 
107
          raise Errno::EACCES, parent_directories.join(", ")
 
108
        end
 
109
 
 
110
        def passed_file
 
111
          File.join(result_dir, "passed")
 
112
        end
 
113
 
 
114
        def escaped_method_name
 
115
          @method_name.to_s.gsub(/[!?=]$/) do |matched|
 
116
            case matched
 
117
            when "!"
 
118
              ".destructive"
 
119
            when "?"
 
120
              ".predicate"
 
121
            when "="
 
122
              ".equal"
 
123
            end
 
124
          end
 
125
        end
 
126
      end
 
127
 
 
128
      module ClassMethods
 
129
        def priority(name, *tests)
 
130
          unless Checker.have_priority?(name)
 
131
            raise ArgumentError, "unknown priority: #{name}"
 
132
          end
 
133
          attribute(:priority, name, {:keep => true}, *tests)
 
134
        end
 
135
      end
 
136
 
 
137
      def priority_setup
 
138
        Checker.new(self).setup
 
139
      end
 
140
 
 
141
      def priority_teardown
 
142
        Checker.new(self).teardown
 
143
      end
 
144
    end
 
145
  end
 
146
end