Class: Exemplor::Environment

Inherits:
Object
  • Object
show all
Includes:
Rack::Test::Methods
Defined in:
lib/environment.rb,
lib/exemplor/rack.rb

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeEnvironment

Returns a new instance of Environment.



79
80
81
# File 'lib/environment.rb', line 79

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.



77
78
79
# File 'lib/environment.rb', line 77

def _checks
  @_checks
end

Class Method Details

.app(app) ⇒ Object

Public: Sets the target app for rack/test

Examples

# Sinatra
eg.app(Sinatra::Application)
# Rails
eg.app(ActionController::Dispatcher.new)


15
16
17
# File 'lib/exemplor/rack.rb', line 15

def self.app(app)
  @@app = app
end

.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
# File 'lib/environment.rb', line 57

def render_checks(checks)
  checks.map do |check|
    OrderedHash do |o|
      o['name'] = check.name
      o['status'] = check.status.to_s
      o['result'] = render_value check.value if check.info?
    end
  end
end

.render_error(error) ⇒ Object



67
68
69
70
71
72
73
# File 'lib/environment.rb', line 67

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 Assert::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



114
115
116
117
# File 'lib/environment.rb', line 114

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

#appObject



19
20
21
# File 'lib/exemplor/rack.rb', line 19

def app
  @@app
end

#Assert(value) ⇒ Object



90
91
92
93
94
95
96
# File 'lib/environment.rb', line 90

def Assert(value)
  name = extract_argstring_from :Assert, caller
  check = Assert.new(name, value)
  _checks << check
  check.run
  check
end

#Check(value) ⇒ Object



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

def Check(value)
  warn "Check is depreciated, use Show"
  name = extract_argstring_from :Check, caller
  check = Show.new(name, value)
  _checks << check
  check
end

#extract_argstring_from(name, call_stack) ⇒ Object



106
107
108
109
110
111
112
# File 'lib/environment.rb', line 106

def extract_argstring_from name, call_stack
  file, line_number = call_stack.first.match(/^(.+):(\d+)/).captures
  line = File.readlines(file)[line_number.to_i - 1].strip
  argstring = line[/#{name}\((.+?)\)\s*($|#|\[|\})/,1]
  raise "unable to extract name for #{name} from #{file} line #{line_number}:\n  #{line}" unless argstring
  argstring
end

#Show(value) ⇒ Object



83
84
85
86
87
88
# File 'lib/environment.rb', line 83

def Show(value)
  name = extract_argstring_from :Show, caller
  check = Show.new(name, value)
  _checks << check
  check
end