Class: Spec::Matchers::Cross

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

Constant Summary collapse

@@called =
{}
@@backtrace =
{}
@@raiseException =
{}

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(target, function, raiseException = false) ⇒ Cross

Returns a new instance of Cross.



15
16
17
18
19
20
# File 'lib/appswarm/cross_spectool.rb', line 15

def initialize(target,function,raiseException=false)
  @target=target
  @function=function
  @expected="#{target}.#{function}(.)"
  @@raiseException[@expected]=raiseException
end

Class Method Details

.symCall(name) ⇒ Object



62
63
64
65
66
67
# File 'lib/appswarm/cross_spectool.rb', line 62

def Cross.symCall(name)
  @@called[name]+=1
  @@backtrace[name]=caller
  raise "Should never cross this" if @@raiseException[name]
  nil
end

Instance Method Details

#failure_messageObject



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

def failure_message
  "expected #{@proc.inspect} to call #{@expected}"
end

#matches?(proc) ⇒ Boolean

Returns:

  • (Boolean)


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
# File 'lib/appswarm/cross_spectool.rb', line 21

def matches?(proc)      
  @proc = proc
  callName=@expected
  @@called[callName]=0
  
  if @target.is_a?(Class) and @target.singleton_methods.member?(@function.to_s)
    mclass=@target
    
    function=@function
    oldFunction=@target.method(@function)
    mclass.define_cmethod(@function) do 
      Cross.symCall(callName)
    end
    
    @proc.call
    mclass.define_cmethod(@function,oldFunction)
    
    return @@called[callName]>0

  end
  @callName=callName
  mclass=@target
  mclass=@target.class unless @target.is_a?(Class)

  oldFunction=mclass.instance_method(@function)
  mclass.send(:define_method,@function) do 
    Cross.symCall(callName) 
  end
  @proc.call
  mclass.send(:define_method,@function,oldFunction)
  @@called[callName]>0
end

#negative_failure_messageObject



56
57
58
59
60
61
# File 'lib/appswarm/cross_spectool.rb', line 56

def negative_failure_message
  bt=""
  bt=@@backtrace[@callName].join("\n") if @@backtrace[@callName] 
  #printStacktrace
  "expected #{@proc.inspect} not to call #{@expected} BT:#{bt}"
end