Module: Tap::Test::SubsetTest::ClassMethods
- Defined in:
- lib/tap/test/subset_test/class_methods.rb
Overview
Class methods associated with SubsetTest.
Instance Attribute Summary collapse
-
#conditions ⇒ Object
readonly
A hash of [name, [msg, condition_block]] pairs defined by condition.
Class Method Summary collapse
-
.extended(base) ⇒ Object
Initialize conditions.
Instance Method Summary collapse
-
#condition(name, msg = nil, &block) ⇒ Object
Defines a condition block and associated message.
-
#inherited(child) ⇒ Object
Passes conditions to subclass.
-
#match_platform?(*platforms) ⇒ Boolean
Returns true if RUBY_PLATFORM matches one of the specfied platforms.
-
#run_subset?(type) ⇒ Boolean
Returns true if type or ‘ALL’ is specified as ‘true’ in ENV.
-
#satisfied?(*names) ⇒ Boolean
Returns true if the all blocks for the specified conditions return true.
-
#unsatisfied_conditions(*condition_names) ⇒ Object
Returns an array of the unsatified conditions.
Instance Attribute Details
#conditions ⇒ Object (readonly)
A hash of [name, [msg, condition_block]] pairs defined by condition.
22 23 24 |
# File 'lib/tap/test/subset_test/class_methods.rb', line 22 def conditions @conditions end |
Class Method Details
.extended(base) ⇒ Object
Initialize conditions.
17 18 19 |
# File 'lib/tap/test/subset_test/class_methods.rb', line 17 def self.extended(base) # :nodoc: base.instance_variable_set(:@conditions, {}) end |
Instance Method Details
#condition(name, msg = nil, &block) ⇒ Object
Defines a condition block and associated message.
Raises an error if no condition block is given.
26 27 28 29 |
# File 'lib/tap/test/subset_test/class_methods.rb', line 26 def condition(name, msg=nil, &block) raise ArgumentError, "no condition block given" unless block_given? conditions[name] = [msg, block] end |
#inherited(child) ⇒ Object
Passes conditions to subclass
9 10 11 12 13 14 |
# File 'lib/tap/test/subset_test/class_methods.rb', line 9 def inherited(child) # :nodoc: super dup = {} conditions.each_pair {|key, value| dup[key] = value.dup } child.instance_variable_set(:@conditions, dup) end |
#match_platform?(*platforms) ⇒ Boolean
Returns true if RUBY_PLATFORM matches one of the specfied platforms. Use the prefix ‘non_’ to specify any plaform except the specified platform (ex: ‘non_mswin’). Returns true if no platforms are specified.
Some common platforms:
mswin Windows
darwin Mac
78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/tap/test/subset_test/class_methods.rb', line 78 def match_platform?(*platforms) platforms.each do |platform| platform.to_s =~ /^(non_)?(.*)/ non = true if $1 match_platform = !RUBY_PLATFORM.index($2).nil? return false unless (non && !match_platform) || (!non && match_platform) end true end |
#run_subset?(type) ⇒ Boolean
Returns true if type or ‘ALL’ is specified as ‘true’ in ENV.
91 92 93 |
# File 'lib/tap/test/subset_test/class_methods.rb', line 91 def run_subset?(type) ENV[type] == "true" || ENV['ALL'] == "true" ? true : false end |
#satisfied?(*names) ⇒ Boolean
Returns true if the all blocks for the specified conditions return true.
condition(:is_true) { true }
condition(:is_false) { false }
satisfied?(:is_true) # => true
satisfied?(:is_true, :is_false) # => false
Yields the name and message for each unsatisfied condition to the block, if given.
40 41 42 43 44 45 46 47 48 |
# File 'lib/tap/test/subset_test/class_methods.rb', line 40 def satisfied?(*names) # :yields: name-of-unsatisfied-condition, msg unsatisfied = unsatisfied_conditions(*names) unsatisfied.each do |name| yield(name, condition[name][0]) end if block_given? unsatisfied.empty? end |
#unsatisfied_conditions(*condition_names) ⇒ Object
Returns an array of the unsatified conditions. Raises an error if a condition has not been defined.
condition(:is_true) { true }
condition(:is_false) { false }
unsatisfied_conditions(:is_true, :is_false) # => [:is_false]
57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/tap/test/subset_test/class_methods.rb', line 57 def unsatisfied_conditions(*condition_names) condition_names = conditions.keys if condition_names.empty? unsatified = [] condition_names.each do |name| unless condition = conditions[name] raise ArgumentError, "Unknown condition: #{name}" end unsatified << name unless condition.last.call end unsatified end |