Module: Tap::Test::SubsetTestClass

Includes:
EnvVars
Defined in:
lib/tap/test/subset_test_class.rb

Overview

Class methods extending tests which include SubsetTest.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from EnvVars

#env, #env_true?

Instance Attribute Details

#conditionsObject (readonly)

A hash of [name, [msg, condition_block]] pairs defined by condition.



24
25
26
# File 'lib/tap/test/subset_test_class.rb', line 24

def conditions
  @conditions
end

Class Method Details

.extended(base) ⇒ Object

Initialize conditions.



19
20
21
# File 'lib/tap/test/subset_test_class.rb', line 19

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.

Raises:

  • (ArgumentError)


28
29
30
31
# File 'lib/tap/test/subset_test_class.rb', line 28

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



11
12
13
14
15
16
# File 'lib/tap/test/subset_test_class.rb', line 11

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

Returns:

  • (Boolean)


80
81
82
83
84
85
86
87
88
89
90
# File 'lib/tap/test/subset_test_class.rb', line 80

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 the subset type or ‘ALL’ is specified in ENV

Returns:

  • (Boolean)


93
94
95
# File 'lib/tap/test/subset_test_class.rb', line 93

def run_subset?(type)
  env_true?(type) || env_true?("ALL") ? 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.

Returns:

  • (Boolean)


42
43
44
45
46
47
48
49
50
# File 'lib/tap/test/subset_test_class.rb', line 42

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]


59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/tap/test/subset_test_class.rb', line 59

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