Class: Tryouts::Drill::Response

Inherits:
Object
  • Object
show all
Defined in:
lib/tryouts/drill/response.rb

Overview

Response

A generic base class for Dream and Reality

Direct Known Subclasses

Dream, Reality

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(output = nil, format = nil) ⇒ Response

Returns a new instance of Response.



9
10
11
# File 'lib/tryouts/drill/response.rb', line 9

def initialize(output=nil, format=nil)
  @output, @format = output, format
end

Instance Attribute Details

#format(val = nil) ⇒ Object

Returns the value of attribute format.



8
9
10
# File 'lib/tryouts/drill/response.rb', line 8

def format
  @format
end

#outputObject

Returns the value of attribute output.



8
9
10
# File 'lib/tryouts/drill/response.rb', line 8

def output
  @output
end

Class Method Details

.compare(dream, reality) ⇒ Object



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
# File 'lib/tryouts/drill/response.rb', line 16

def Response.compare(dream, reality)
  return false if reality.nil?
  
  ## I don't think this check is necessary or useful
  ##return false unless reality.error.nil? && reality.trace.nil?
  return true if reality.output == true and dream.nil?
  
  # Refactor like:
  # http://github.com/why/hpricot/blob/master/lib/hpricot/elements.rb#L475
  
  begin
    case dream.format
    when :exception
      reality.etype == dream.output
    when :match
      reality.output.respond_to?(:match) &&
      !reality.output.match(dream.output).nil?
    when :proc
      dream.output.is_a?(Proc) &&
      reality.comparison_value(dream) == dream.comparison_value
    when :mean, :sdev
      reality.comparison_value(dream) <= dream.comparison_value
    when :gt
      reality.output > dream.output
    when :gte
      reality.output >= dream.output
    when :lt
      reality.output < dream.output
    when :lte
      reality.output <= dream.output
    when :ne
      reality.output != dream.output
    when :respond_to?, :kind_of?, :is_a?
      reality.output.send(dream.format, dream.output)
    when :grep
      !reality.output.grep(dream.output).empty?
    else 
    
      if dream.format.nil?
        reality.output == dream.output
      elsif reality.output.respond_to?(dream.format)
        reality.comparison_value(dream)  == dream.output
      else 
        false
      end
    
    end
  rescue => ex
    puts ex.message, ex.backtrace if Tryouts.debug? 
    reality.error, reality.trace, reality.etype = ex.message, ex.backtrace, ex.class
    return false
  end
end

.compare_string(dream, reality) ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/tryouts/drill/response.rb', line 70

def Response.compare_string(dream, reality)
  return "[noreality]" if reality.nil?
  
  if reality.output == true and dream.nil?
    return "#{reality.output.inspect} == true" 
  end
  
  begin
    case dream.format
    when :proc
      test = dream.output
      test.arity > 0 ? "Proc.call(reality) == true" : "Proc.call == true"
    when :exception
      "#{reality.etype} == #{dream.output}"
    when :mean, :sdev
      "#{reality.comparison_value(dream)} <= #{dream.output}"
    when :match
      "#{reality.output.inspect}.match(#{dream.output.inspect})"
    when :gt, :gte, :lt, :lte, :ne
      op = {:gt=>'>',:gte=>'>=', :lt=>'<', :lte => '<=', :ne => '!='}.find { |i| i[0] == dream.format }
      "#{reality.output.inspect} #{op[1]} #{dream.output.inspect}"
    when :respond_to?, :kind_of?, :is_a?
      "#{reality.output.class}.#{dream.format} #{dream.output.inspect}"
    when :grep
      "!#{reality.output}.grep(#{dream.output.inspect}).empty?"
    else 
    
      if dream.format.nil?
        "#{reality.output.inspect} == #{dream.output.inspect}"
      elsif reality.output.respond_to?(dream.format)
        "#{reality.output.inspect}.#{dream.format} == #{dream.output.inspect}"
      else
        "Unknown method #{dream.format.inspect} for #{reality.output.class} "
      end
    
    end
  rescue => ex
    puts ex.message, ex.backtrace if Tryouts.debug? 
    reality.error, reality.trace, reality.etype = ex.message, ex.backtrace, ex.class
    return ""
  end

end