Class: Exemplor::ExampleEnv

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

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeExampleEnv

Returns a new instance of ExampleEnv.



92
93
94
# File 'lib/environment.rb', line 92

def initialize
  @_checks = []
end

Class Attribute Details

.setup_blockObject

Returns the value of attribute setup_block.



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

def setup_block
  @setup_block
end

Instance Attribute Details

#_checksObject

Returns the value of attribute _checks.



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

def _checks
  @_checks
end

Class Method Details

.fake_stderr!Object

tests are run with a fake stderr so warnings output can be assoicated with the specific test. this is still a little hokey and hard to test properly



40
41
42
43
44
# File 'lib/environment.rb', line 40

def fake_stderr!
  fake = StringIO.new
  @real_stderr = $stderr
  $stderr = fake
end

.render_checks(checks) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/environment.rb', line 57

def render_checks(checks)
  failure = nil
  out = []
  checks.each do |check|
    failure = check if check.failure?
    break if failure
    out << OrderedHash do |o|
      o['name'] = check.name
      o['status'] = check.status.to_s
      o['result'] = render_value check.value
    end
  end
  if failure
    out << OrderedHash do |o|
      o['name'] = failure.name
      o['status'] = failure.status.to_s
      o['expected'] = failure.expectation
      o['actual'] = render_value failure.value
    end
  end
  out
end

.render_error(error) ⇒ Object



80
81
82
83
84
85
86
# File 'lib/environment.rb', line 80

def render_error(error)
  OrderedHash do |o|
    o['class'] = error.class.name
    o['message'] = error.message
    o['backtrace'] = error.backtrace
  end
end

.render_value(value) ⇒ Object

yaml doesn’t want to print a class



53
54
55
# File 'lib/environment.rb', line 53

def render_value(value)
  value.kind_of?(Class) ? value.inspect : value
end

.restore_stderr!Object



46
47
48
# File 'lib/environment.rb', line 46

def restore_stderr!
  $stderr = @real_stderr
end

.run(&code) ⇒ Object

runs the block in the example environment, returns triple:

status, result, stderr


14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/environment.rb', line 14

def run(&code)
  env = self.new
  stderr = fake_stderr!
  status, result = begin

    env.instance_eval(&self.setup_block) if self.setup_block
    value = env.instance_eval(&code)
    if env._checks.empty?
      [:info, render_value(value)]
    else # :infos or :success
      [env._status, render_checks(env._checks)]
    end

  rescue Check::Failure => failure
    [:failure, render_checks(env._checks)]
  rescue Object => error
    [:error, render_error(error)]
  ensure
    restore_stderr!
  end
  [status, result, stderr.rewind && stderr.read]
end

.setup(&blk) ⇒ Object



10
# File 'lib/environment.rb', line 10

def setup(&blk) self.setup_block = blk end

Instance Method Details

#_statusObject



105
106
107
108
# File 'lib/environment.rb', line 105

def _status
  (:success if _checks.all? { |c| c.success? }) ||
   :infos
end

#Check(value) ⇒ Object



96
97
98
99
100
101
102
103
# File 'lib/environment.rb', line 96

def Check(value)
  file, line_number = caller.first.match(/^(.+):(\d+)/).captures
  line = File.readlines(file)[line_number.to_i - 1].strip
  name = line[/Check\((.+?)\)\s*($|#|\[|\.is.+)/,1]
  check = Check.new(name, value)
  _checks << check
  check
end