Class: ActiveGuide::SimpleAnalyzer

Inherits:
Object
  • Object
show all
Defined in:
lib/active_guide/simple_analyzer.rb

Defined Under Namespace

Classes: Env

Instance Method Summary collapse

Instance Method Details

#analyze_group(group, env, depth = 0) ⇒ Object


56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/active_guide/simple_analyzer.rb', line 56

def analyze_group(group, env, depth = 0)
  report = { failed: 0, passed: 0, points: [] }
  log_group(env, group.name, depth)
  call_callbacks(group, env) do
    group.items.each do |item|
      r = analyze_item(item, env, depth + 1)
      report[:failed] += r[:failed]
      report[:passed] += r[:passed]
      report[:points] += r[:points]
    end
  end
  report
end

#analyze_item(item, env, depth = 0) ⇒ Object


42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/active_guide/simple_analyzer.rb', line 42

def analyze_item(item, env, depth = 0)
  if item.is_a? ActiveGuide::Group
    analyze_group(item, env, depth)
  elsif item.is_a? ActiveGuide::Test
    analyze_test(item, env, depth)
  elsif item.is_a? ActiveGuide::Question
    analyze_question(item, env, depth)
  elsif item.is_a? ActiveGuide::Result
    analyze_result(item, env)
  else
    raise "Unknown item type: #{item.class.name}"
  end
end

#analyze_question(question, env, depth) ⇒ Object


102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/active_guide/simple_analyzer.rb', line 102

def analyze_question(question, env, depth)
  report = { failed: 0, passed: 0, points: [] }
  call_callbacks(question, env) do
    if (r = rand > 0.5)
      report[:passed] += 1
    else
      report[:failed] += 1
    end
    report[:points] << { item: question.name, success: r }
    log_result(env, "#{question.name.to_s.humanize} ?", r, depth)
  end
  report
end

#analyze_result(result, env) ⇒ Object


116
117
118
119
# File 'lib/active_guide/simple_analyzer.rb', line 116

def analyze_result(result, env)
  env.results << result
  { failed: 0, passed: 0, points: [] }
end

#analyze_test(test, env, depth = 0) ⇒ Object


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
# File 'lib/active_guide/simple_analyzer.rb', line 70

def analyze_test(test, env, depth = 0)
  report = { failed: 0, passed: 0, points: [] }
  call_callbacks(test, env) do
    if test.validate?
      if r = !!env.instance_exec(&test.validate_block)
        report[:passed] += 1
      else
        report[:failed] += 1
      end
      report[:points] << { item: test.name, success: r }
      log_result(env, test.name, r, depth)
    else
      failed = 0
      subtests = []
      test.subtests.each do |subtest|
        r = analyze_test(subtest, env, depth + 1)
        rr = r[:failed]
        failed += 1 if rr > 0
        subtests << { item: test.name, success: rr }
      end
      if r = failed.zero?
        report[:passed] += 1
      else
        report[:failed] += 1
      end
      report[:points] << { item: test.name, success: r, subtests: subtests }
      log_result(env, test.name, r, depth)
    end
  end
  report
end

#run(guide, options = {}) ⇒ Object


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
# File 'lib/active_guide/simple_analyzer.rb', line 14

def run(guide, options = {})
  started_at = Time.zone.now
  env = Env.new
  env.verbose = !options[:verbose].is_a?(FalseClass)
  puts 'Running...'.yellow if env.verbose
  report = analyze_item(guide.root, env, -1)
  # Adds results if any
  unless env.results.empty?
    report[:results] = []
    env.results.each do |result|
      report[:results] << { item: result.name, value: env.variables.send(result.name) }
    end
  end
  # Adds end time
  report[:started_at] = started_at
  report[:stopped_at] = Time.zone.now
  # Display report if wanted
  if env.verbose
    puts "#{report[:failed].to_s.red} tests failed, #{report[:passed].to_s.green} tests passed"
    if report[:results]
      report[:results].each do |r|
        puts " > #{r[:item].name.to_s.humanize}: #{r[:value].to_s.yellow}"
      end
    end
  end
  report
end