Class: Stendhal::ExampleGroup

Inherits:
Object
  • Object
show all
Defined in:
lib/stendhal/example_group.rb

Constant Summary collapse

@@example_groups =
[]

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(docstring, options = {}, &block) ⇒ ExampleGroup

Returns a new instance of ExampleGroup.



10
11
12
13
14
15
16
17
18
# File 'lib/stendhal/example_group.rb', line 10

def initialize(docstring, options = {}, &block)
  @description = docstring
  @block = block
  @parent = options[:parent]
  @example_groups = []
  @examples = []
  instance_exec(&@block) if block_given?
  @@example_groups << self
end

Class Attribute Details

.example_groupsObject (readonly)

Returns the value of attribute example_groups.



73
74
75
# File 'lib/stendhal/example_group.rb', line 73

def example_groups
  @example_groups
end

Instance Attribute Details

#blockObject (readonly)

Returns the value of attribute block.



7
8
9
# File 'lib/stendhal/example_group.rb', line 7

def block
  @block
end

#descriptionObject (readonly)

Returns the value of attribute description.



5
6
7
# File 'lib/stendhal/example_group.rb', line 5

def description
  @description
end

#example_groupsObject (readonly)

Returns the value of attribute example_groups.



6
7
8
# File 'lib/stendhal/example_group.rb', line 6

def example_groups
  @example_groups
end

#examplesObject (readonly)

Returns the value of attribute examples.



8
9
10
# File 'lib/stendhal/example_group.rb', line 8

def examples
  @examples
end

Class Method Details

.countObject



94
95
96
# File 'lib/stendhal/example_group.rb', line 94

def count
  @@example_groups.count
end

.destroy_allObject



75
76
77
# File 'lib/stendhal/example_group.rb', line 75

def destroy_all
  @@example_groups = []
end

.primary_example_groupsObject



90
91
92
# File 'lib/stendhal/example_group.rb', line 90

def primary_example_groups
  @@example_groups.reject { |g| g.has_parent? }
end

.run_allObject



79
80
81
82
83
84
85
86
87
88
# File 'lib/stendhal/example_group.rb', line 79

def run_all
  Reporter.whitespace
  result = [0,0,0]
  primary_example_groups.each do |g|
    group_result = g.run
    Reporter.whitespace
    result = result.zip(group_result).map{ |pair| pair[0] + pair[1] }
  end
  result
end

Instance Method Details

#add_example(example) ⇒ Object



68
69
70
# File 'lib/stendhal/example_group.rb', line 68

def add_example(example)
  examples << example 
end

#add_example_group(*args, &block) ⇒ Object



20
21
22
23
24
25
26
27
# File 'lib/stendhal/example_group.rb', line 20

def add_example_group(*args, &block)
  if args.last.is_a?(Hash)
    args.last.update(:parent => true)
  else
    args << {:parent => true}
  end
  @example_groups << ExampleGroup.new(*args, &block)
end

#has_parent?Boolean

Returns:

  • (Boolean)


29
30
31
# File 'lib/stendhal/example_group.rb', line 29

def has_parent?
  !@parent.nil?
end

#runObject



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
# File 'lib/stendhal/example_group.rb', line 33

def run
  Reporter.line @description, :indent => Reporter.current_indentation, :color => :white
  original_indentation = Reporter.current_indentation

  failures = pending = 0
  examples.reject do |example|
    if example.pending? 
      pending += 1
      Reporter.line "* #{example.description} [PENDING]", :indent => Reporter.current_indentation + 1, :color => :yellow
      true
    else
      false
    end
  end.each do |example|
    failures += example.run
    color = :red
    color = :green unless example.failed? || example.aborted?
    status = " [FAILED]" if example.failed?
    status = " [ABORTED]" if example.aborted?
    Reporter.line "* #{example.description}#{status || ''}", :indent => Reporter.current_indentation + 1, :color => color
    Reporter.line "#{example.failed_message}", :indent => Reporter.current_indentation + 2, :color => :red if example.failed?
    Reporter.line "#{example.aborted_message}", :indent => Reporter.current_indentation + 2, :color => :red if example.aborted?
  end
  group_result = [examples.count, failures, pending]

  @example_groups.each do |group|
    Reporter.current_indentation += 1
    sub_result = group.run
    group_result = group_result.zip(sub_result).map{ |pair| pair[0] + pair[1] } if sub_result
  end
  Reporter.current_indentation = original_indentation

  group_result
end