Class: Reflection

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

Constant Summary collapse

TIME =

Keys.

"t"
INPUT =
"i"
OUTPUT =
"o"
TYPE =
"T"
COUNT =
"C"
VALUE =
"V"
STATUS =
"s"
MESSAGE =
"m"
PASS =

Values.

"p"
FAIL =
"f"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(execution, method, is_control) ⇒ Reflection

Returns a new instance of Reflection.



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/Reflection.rb', line 18

def initialize(execution, method, is_control)

  @execution = execution
  @method = method
  @is_control = is_control

  # Clone the execution's object.
  @clone = execution.object.clone
  @clone_id = nil

  # Result.
  @status = nil
  @time = Time.now.to_i
  @input = []
  @output = nil

end

Instance Attribute Details

#cloneObject

Returns the value of attribute clone.



16
17
18
# File 'lib/Reflection.rb', line 16

def clone
  @clone
end

Instance Method Details

#normalize_input(args) ⇒ Object

Normalize inputs.

Parameters:

  • args
    • The actual inputs.

Returns:

    • A generic inputs representation.



94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/Reflection.rb', line 94

def normalize_input(args)
  inputs = []
  args.each do |arg|
    input = {
      TYPE => arg.class.to_s,
      VALUE => normalize_value(arg)
    }
    if (arg.class == Array)
      input[COUNT] = arg.count
    end
    inputs << input
  end
  inputs
end

#normalize_output(input) ⇒ Object

Normalize output.

Parameters:

  • input
    • The actual output.

Returns:

    • A generic output representation.



115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/Reflection.rb', line 115

def normalize_output(input)

  output = {
    TYPE => input.class.to_s,
    VALUE => normalize_value(input)
  }

  if (input.class == Array || input.class == Hash)
    output[COUNT] = input.count
  elsif (input.class == TrueClass || input.class == FalseClass)
    output[TYPE] = :Boolean
  end

  return output

end

#normalize_value(value) ⇒ Object



132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/Reflection.rb', line 132

def normalize_value(value)

  unless value.nil?
    value = value.to_s.gsub(/\r?\n/, " ").to_s
    if value.length >= 30
      value = value[0, value.rindex(/\s/,30)].rstrip() + '...'
    end
  end

  return value

end

#reflect(*args) ⇒ Object

Reflect on a method.

Creates a shadow execution stack.

Parameters:

  • method
    • The name of the method.

  • *args
    • The method arguments.

Returns:

    • A reflection hash.



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
74
75
# File 'lib/Reflection.rb', line 46

def reflect(*args)

  # Reflect on real world arguments.
  if @is_control
    @input = *args
  # Reflect on deviated arguments.
  else
    args.each do |arg|
      case arg
      when Integer
        @input << rand(9999)
      else
        @input << arg
      end
    end
  end

  # Action method with new arguments.
  begin
    @output = @clone.send(@method, *@input)
  # When fail.
  rescue StandardError => message
    @status = MESSAGE
    @message = message
  # When pass.
  else
    @status = PASS
  end

end

#resultObject



77
78
79
80
81
82
83
84
85
86
# File 'lib/Reflection.rb', line 77

def result()
  # Build reflection.
  reflection = {
    TIME => @time,
    STATUS => @status,
    INPUT => normalize_input(@input),
    OUTPUT => normalize_output(@output),
    MESSAGE => @message
  }
end