Class: OptparseLite::Test::Capture::Capturer

Inherits:
Object
  • Object
show all
Defined in:
lib/optparse-lite/test/setup.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(mod) ⇒ Capturer

Returns a new instance of Capturer.



22
23
24
25
26
27
28
29
30
31
# File 'lib/optparse-lite/test/setup.rb', line 22

def initialize mod
  # make a regexp that can deduce the local module name from the
  # name of the class that minitest generates, e.g.:
  # OptparseLite::Test => /^OptparseliteTest(.+)Spec/
  @mod = mod
  @regexp = Regexp.new('^'+
   mod.to_s.split('::').map{|x| x.downcase.capitalize}.join +
   '(.+)Spec'
  )
end

Instance Attribute Details

#last_return_valueObject

Returns the value of attribute last_return_value.



87
88
89
# File 'lib/optparse-lite/test/setup.rb', line 87

def last_return_value
  @last_return_value
end

#regexpObject

Returns the value of attribute regexp.



88
89
90
# File 'lib/optparse-lite/test/setup.rb', line 88

def regexp
  @regexp
end

Instance Method Details

#capture(app = nil, &b) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/optparse-lite/test/setup.rb', line 32

def capture app=nil, &b
  app ||= @application_module
  do_record = @spec && @spec.respond_to?(:nandoc)
  ui = app.send(:ui)
  ui.push
  if do_record
    app.set_argv_hook do |argv|
      @spec.nandoc.recordings.add(:app, app)
      @spec.nandoc.recordings.add(:argv, argv)
    end
  end
  @last_return_value = app.instance_eval(&b)
  str = ui.pop.to_str # tacitly assumes stderr is empty, breaks else
  if do_record
    @spec.nandoc.recordings.add(:out, str)
  end
  $stdout.puts "\n\n#{str}\n" if Test.verbose
  str
end

#capture2(app = nil, &b) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/optparse-lite/test/setup.rb', line 51

def capture2 app=nil, &b
  do_record = @spec && @spec.respond_to?(:nandoc)
  app ||= @application_module
  ui = app.send(:ui)
  ui.push
  @last_return_value = app.instance_eval(&b)
  out, err = ui.pop(true)
  out, err = out.to_s, err.to_s
  if Test.verbose
    $stdout.puts "\n\nout:\n#{out}\n."
    $stdout.puts "\n\nerr:\n#{out}\n."
  end
  if do_record
    if ""!=out && ""!=err
      fail("don't be a dick")
    end
    @spec.nandoc.recordings.add(:out, out + err)
  end
  [out, err]
end

#fork(thing) ⇒ Object



82
83
84
85
86
# File 'lib/optparse-lite/test/setup.rb', line 82

def fork thing
  ret = dup
  ret.init_fork(thing)
  ret
end

#get_app_from_spec(spec) ⇒ Object



71
72
73
74
75
76
77
# File 'lib/optparse-lite/test/setup.rb', line 71

def get_app_from_spec spec
  name = @regexp.match(spec.class.to_s)[1].downcase
  @cache ||= Hash[
    @mod.constants.map{|x|[x.downcase, @mod.const_get(x)]}
  ]
  @cache[name]
end

#init_fork(spec) ⇒ Object



78
79
80
81
# File 'lib/optparse-lite/test/setup.rb', line 78

def init_fork spec
  @application_module = get_app_from_spec(spec)
  @spec = spec # this has nandoc
end