Class: RSpec::ApiGen::Given

Inherits:
Object
  • Object
show all
Defined in:
lib/rspec-apigen/given.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(context, method, list_of_args, given_caller, &block) ⇒ Given

list_of_args - the list of arguments current method accept When the given block is evaluated in this method the Given#args will return a hash of name or argument and its value.



13
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
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
67
68
69
70
71
72
73
# File 'lib/rspec-apigen/given.rb', line 13

def initialize(context, method, list_of_args, given_caller, &block)
  this = self # so we can access it as closure
  @arg = Object.new
  @args = {}
  @fixtures = {}
  block_arg = nil

  list_of_args.find_all { |a| a.kind_of?(Argument) }.each do |a|
    MetaHelper.create_singleton_method(@arg, "#{a.name}=") do |val|
      this.args[a.name] = val
    end
    if (a.accept_block?)
      MetaHelper.create_singleton_method(@arg, "#{a.name}") do | &bl |
        block_arg = bl
      end
    else
      MetaHelper.create_singleton_method(@arg, "#{a.name}") do
        this.args[a.name]
      end
    end
  end

  context.it "no arguments" do
    # create the arguments
    MetaHelper.create_singleton_method(self, :arg) { this.arg }

    # accessor for setting fixture
    MetaHelper.create_singleton_method(self, :fixtures) { this.fixtures }

    begin
      self.instance_eval &block
    rescue Exception => e
      this.set_backtrace(example, e, given_caller)
    end if block

    list_of_args.delete_if { |a| a.kind_of?(Argument) && a.accept_block? }

    # now, the args hash should have been populated
    # for each param we replace the args with the real value
    list_of_args.collect! { |a| a.kind_of?(Argument) ? this.args[a.name] : a }

    example.[:description] = "arguments #{Argument.describe(list_of_args)}" unless list_of_args.empty?

    # get the subject which we will use to test the method on, and store it so we can check it
    this.subject = subject

    begin
      example.execution_result[:exception_encountered] = given_caller

      if (block_arg)
        # call the method on this instance which will will test
        this.return = this.subject.send(method, *list_of_args, &block_arg)
      else
        this.return = this.subject.send(method, *list_of_args)
      end
    rescue Exception => e
      this.set_backtrace(example, e, given_caller)
    end

  end
end

Instance Attribute Details

#argObject (readonly)

the DSL object used to set the args



4
5
6
# File 'lib/rspec-apigen/given.rb', line 4

def arg
  @arg
end

#argsObject (readonly)

contains name of argument and its value



3
4
5
# File 'lib/rspec-apigen/given.rb', line 3

def args
  @args
end

#fixturesObject (readonly)

Returns the value of attribute fixtures.



5
6
7
# File 'lib/rspec-apigen/given.rb', line 5

def fixtures
  @fixtures
end

#returnObject

Returns the value of attribute return.



7
8
9
# File 'lib/rspec-apigen/given.rb', line 7

def return
  @return
end

#subjectObject

Returns the value of attribute subject.



6
7
8
# File 'lib/rspec-apigen/given.rb', line 6

def subject
  @subject
end

Instance Method Details

#set_backtrace(example, e, given_caller) ⇒ Object



75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/rspec-apigen/given.rb', line 75

def set_backtrace(example, e, given_caller)
  trace = e.backtrace

  # TODO very ugly - don't know how to filter rspec own backtrace
  # remove all lines containing ../lib/rspec
  trace.delete_if {|line| line =~ /\/lib\/rspec\//}
  trace.delete_if {|line| line =~ /\/rubygems\/custom_require/}
  trace.delete_if {|line| line =~ /\/bin\/rspec/}

  bt = trace + given_caller
  e.set_backtrace(bt)
  example.set_exception(e)
  example.execution_result[:exception_encountered] = bt
end