Class: OrigenTesters::SmartestBasedTester::Base::TestMethod

Inherits:
Object
  • Object
show all
Defined in:
lib/origen_testers/smartest_based_tester/base/test_method.rb

Direct Known Subclasses

V93K::TestMethod

Constant Summary collapse

FORMAT_TYPES =
[:current, :voltage, :time, :frequency, :string, :integer, :double, :boolean, :class, :list_strings, :list_classes]

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ TestMethod

Returns a new instance of TestMethod.



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
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/origen_testers/smartest_based_tester/base/test_method.rb', line 41

def initialize(options)
  @type = options[:type]
  @library = options[:library]
  @class_name = options[:methods].delete(:class_name)
  @parameters = {}
  @limits_id = options[:methods].delete(:limits_id) || options[:methods].delete(:limit_id)
  @limits = TestMethods::Limits.new(self)
  @limits.render = false if options[:methods].delete(:render_limits_in_tf) == false
  # Add any methods
  if options[:methods][:methods]
    methods = options[:methods][:methods]
    @finalize = methods[:finalize]
    methods.each do |method_name, function|
      unless method_name == :finalize
        var_name = "@#{method_name}".gsub(/=|\?/, '_')
        instance_variable_set(var_name, function)
        unless self.class.installed_dynamic_methods[method_name]
          self.class.send(:define_method, method_name) do |*args|
            instance_variable_get(var_name).call(self, *args)
          end
          self.class.installed_dynamic_methods[method_name] = true
        end
      end
    end
  end
  # Create attributes corresponding to the test method type represented
  # by this method instance
  options[:methods].each do |attr, type_default|
    unless attr == :limits_type || attr == :aliases || attr == :methods
      clean_attr = clean_attr_name(attr)
      type = type_default[0]
      default = type_default[1]
      allowed = type_default[2]
      @parameters[attr] = type
      aliases = [clean_attr]
      underscored_attr = self.class.underscore_parameter_name(clean_attr)
      aliases << underscored_attr if underscored_attr != clean_attr
      aliases.each do |alias_|
        setter = :"#{alias_}="
        unless self.class.installed_dynamic_methods[setter]
          self.class.send(:define_method, setter) do |v|
            v = v.to_s if v.is_a?(Symbol)
            if allowed
              unless allowed.include?(v)
                fail "Cannot set #{alias_} to #{v}, valid values are: #{allowed.join(', ')}"
              end
            end
            instance_variable_set("@#{clean_attr}", v)
          end
          self.class.installed_dynamic_methods[setter] = true
        end
        alias_name = alias_.to_sym
        unless self.class.installed_dynamic_methods[alias_name]
          self.class.send(:define_method, alias_name) do
            instance_variable_get("@#{clean_attr}")
          end
          self.class.installed_dynamic_methods[alias_name] = true
        end
      end
      send("#{clean_attr}=", default)
    end
  end
  if options[:methods][:aliases]
    options[:methods][:aliases].each do |alias_, attr|
      clean_attr = clean_attr_name(attr)
      setter = :"#{alias_}="
      unless self.class.installed_dynamic_methods[setter]
        self.class.send(:define_method, setter) do |v|
          send("#{clean_attr}=", v)
        end
        self.class.installed_dynamic_methods[setter] = true
      end
      alias_name = alias_.to_sym
      unless self.class.installed_dynamic_methods[alias_name]
        self.class.send(:define_method, alias_name) do
          send(clean_attr)
        end
        self.class.installed_dynamic_methods[alias_name] = true
      end
    end
  end
  # Finally set any initial values that have been supplied
  options[:attrs].each do |k, v|
    accessor = "#{k}="
    if respond_to?(accessor)
      send(accessor, v)
    else
      accessor = "#{self.class.underscore_parameter_name(k.to_s)}="
      send(accessor, v) if respond_to?(accessor)
    end
  end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Object



217
218
219
220
221
222
223
# File 'lib/origen_testers/smartest_based_tester/base/test_method.rb', line 217

def method_missing(method, *args, &block)
  if limits && limits.respond_to?(method)
    limits.send(method, *args, &block)
  else
    super
  end
end

Instance Attribute Details

#abs_class_nameObject

Returns the value of attribute abs_class_name.



33
34
35
# File 'lib/origen_testers/smartest_based_tester/base/test_method.rb', line 33

def abs_class_name
  @abs_class_name
end

#class_nameObject

Returns the value of attribute class_name.



32
33
34
# File 'lib/origen_testers/smartest_based_tester/base/test_method.rb', line 32

def class_name
  @class_name
end

#idObject Also known as: name

Returns the value of attribute id.



27
28
29
# File 'lib/origen_testers/smartest_based_tester/base/test_method.rb', line 27

def id
  @id
end

#libraryObject (readonly)

Returns the object representing the test method library that the given test method is defined in



25
26
27
# File 'lib/origen_testers/smartest_based_tester/base/test_method.rb', line 25

def library
  @library
end

#limitsObject (readonly)

Returns the value of attribute limits.



34
35
36
# File 'lib/origen_testers/smartest_based_tester/base/test_method.rb', line 34

def limits
  @limits
end

#limits_idObject Also known as: limit_id

Returns the value of attribute limits_id.



35
36
37
# File 'lib/origen_testers/smartest_based_tester/base/test_method.rb', line 35

def limits_id
  @limits_id
end

#parametersObject (readonly)

Returns an hash corresponding to the parameters that the given test method has. The keys are the parameter names and the values are the parameter type.



31
32
33
# File 'lib/origen_testers/smartest_based_tester/base/test_method.rb', line 31

def parameters
  @parameters
end

#sub_test_nameObject

Used to store the name of the primary test logged in SMT8



39
40
41
# File 'lib/origen_testers/smartest_based_tester/base/test_method.rb', line 39

def sub_test_name
  @sub_test_name
end

#typeObject (readonly)

Returns the value of attribute type.



26
27
28
# File 'lib/origen_testers/smartest_based_tester/base/test_method.rb', line 26

def type
  @type
end

Class Method Details

.installed_dynamic_methodsObject



11
12
13
# File 'lib/origen_testers/smartest_based_tester/base/test_method.rb', line 11

def self.installed_dynamic_methods
  @installed_dynamic_methods ||= {}
end

.parameter_sort_keysObject



7
8
9
# File 'lib/origen_testers/smartest_based_tester/base/test_method.rb', line 7

def self.parameter_sort_keys
  @@parameter_sort_keys ||= {}
end

.underscore_parameter_name(name) ⇒ Object



19
20
21
# File 'lib/origen_testers/smartest_based_tester/base/test_method.rb', line 19

def self.underscore_parameter_name(name)
  underscored_parameter_names[name] ||= name.underscore
end

.underscored_parameter_namesObject



15
16
17
# File 'lib/origen_testers/smartest_based_tester/base/test_method.rb', line 15

def self.underscored_parameter_names
  @@underscored_parameter_names ||= {}
end

Instance Method Details

#finalizeObject



213
214
215
# File 'lib/origen_testers/smartest_based_tester/base/test_method.rb', line 213

def finalize
  @finalize
end

#format(attr) ⇒ Object



134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
# File 'lib/origen_testers/smartest_based_tester/base/test_method.rb', line 134

def format(attr)
  clean_attr = clean_attr_name(attr)
  val = send(clean_attr)
  if FORMAT_TYPES.include?(parameters[attr])
    type = parameters[attr]
  else
    # The type is based on the value of another attribute
    name = clean_attr_name(parameters[attr])
    if respond_to?(name)
      type = send(name)
    elsif respond_to?(name.sub(/b$/, ''))
      type = inverse_of(send(name.sub(/b$/, '')))
    elsif parameters[attr].is_a?(Hash) || parameters[attr.to_sym].is_a?(Hash)
      type = :hash
    else
      fail "Unknown attribute type: #{parameters[attr]}"
    end
  end
  if val.nil? && !tester.print_all_params
    nil
  else
    handle_val_type(val, type, attr)
  end
end

#handle_val_type(val, type, attr) ⇒ Object



159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
# File 'lib/origen_testers/smartest_based_tester/base/test_method.rb', line 159

def handle_val_type(val, type, attr)
  return val if val == '' && !tester.smt8?

  case type
  when :current, 'CURR'
    "#{val}[A]"
  when :voltage, 'VOLT'
    "#{val}[V]"
  when :time
    "#{val}[s]"
  when :frequency
    "#{val}[Hz]"
  when :string
    val.to_s
  when :integer
    val.to_i
  when :double
    val.to_f
  when :boolean
    # Check for valid values
    if [0, 1, true, false, 'true', 'false'].include?(val)
      # Use true/false for smt8 and 0/1 for smt7
      if [1, true, 'true'].include?(val)
        tester.smt8? ? true : 1
      else
        tester.smt8? ? false : 0
      end
    else
      fail "Unknown boolean value for attribute #{attr}: #{val}"
    end
  when :hash, :class
    val
  when :list_strings
    unless val.is_a?(Array)
      fail "#{val} is not an Array. List_strings must have Array values"
    end

    "##{val}"
  when :list_classes
    unless val.is_a?(Array)
      fail "#{val} is not an Array. List_classes must have Array values"
    end

    "##{val.to_s.gsub('"', '')}"
  else
    fail "Unknown type for attribute #{attr}: #{type}"
  end
end

#klassObject



208
209
210
211
# File 'lib/origen_testers/smartest_based_tester/base/test_method.rb', line 208

def klass
  @abs_class_name ||
    "#{library.klass}.#{@class_name || type.to_s.camelize}"
end

#remove_parameter(name) ⇒ Object



241
242
243
# File 'lib/origen_testers/smartest_based_tester/base/test_method.rb', line 241

def remove_parameter(name)
  @parameters.delete(name)
end

#respond_to?(method) ⇒ Boolean

Returns:

  • (Boolean)


225
226
227
# File 'lib/origen_testers/smartest_based_tester/base/test_method.rb', line 225

def respond_to?(method)
  (limits && limits.respond_to?(method)) || super
end

#sorted_parametersObject



229
230
231
232
233
234
235
236
237
238
239
# File 'lib/origen_testers/smartest_based_tester/base/test_method.rb', line 229

def sorted_parameters
  @parameters.sort_by do |name|
    self.class.parameter_sort_keys[name] ||= if name.is_a?(String)
                                               name
                                             elsif name.to_s[0] == '_'
                                               name.to_s.camelize(:upper)
                                             else
                                               name.to_s.camelize(:lower)
                                             end
  end
end