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, :string, :integer, :double]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ TestMethod

Returns a new instance of TestMethod.



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
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/origen_testers/smartest_based_tester/base/test_method.rb', line 19

def initialize(options)
  @type = options[:type]
  @library = options[:library]
  @class_name = options[:methods].delete(:class_name)
  @parameters = {}
  # Add limits by default
  define_singleton_method('limits') do
    @limits
  end
  @limits = TestMethods::Limits.new(self)
  # 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)
        define_singleton_method method_name do |*args|
          instance_variable_get(var_name).call(self, *args)
        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]
      aliases << clean_attr.underscore if clean_attr.underscore != clean_attr
      aliases.each do |alias_|
        define_singleton_method("#{alias_}=") 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
        define_singleton_method(alias_) do
          instance_variable_get("@#{clean_attr}")
        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)
      define_singleton_method("#{alias_}=") do |v|
        send("#{clean_attr}=", v)
      end
      define_singleton_method(alias_) do
        send(clean_attr)
      end
    end
  end
  # Finally set any initial values that have been supplied
  options[:attrs].each do |k, v|
    send("#{k}=", v) if respond_to?("#{k}=")
  end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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



129
130
131
132
133
134
135
# File 'lib/origen_testers/smartest_based_tester/base/test_method.rb', line 129

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.



17
18
19
# File 'lib/origen_testers/smartest_based_tester/base/test_method.rb', line 17

def abs_class_name
  @abs_class_name
end

#class_nameObject

Returns the value of attribute class_name.



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

def class_name
  @class_name
end

#idObject Also known as: name

Returns the value of attribute id.



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

def id
  @id
end

#libraryObject (readonly)

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



9
10
11
# File 'lib/origen_testers/smartest_based_tester/base/test_method.rb', line 9

def library
  @library
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.



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

def parameters
  @parameters
end

#typeObject (readonly)

Returns the value of attribute type.



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

def type
  @type
end

Instance Method Details

#finalizeObject



125
126
127
# File 'lib/origen_testers/smartest_based_tester/base/test_method.rb', line 125

def finalize
  @finalize
end

#format(attr) ⇒ Object



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
# File 'lib/origen_testers/smartest_based_tester/base/test_method.rb', line 88

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$/, '')))
    else
      fail "Unknown attribute type: #{parameters[attr]}"
    end
  end
  case type
  when :current, 'CURR'
    "#{val}[A]"
  when :voltage, 'VOLT'
    "#{val}[V]"
  when :time
    "#{val}[s]"
  when :frequency
    "#{val}[Hz]"
  when :string, :integer, :double
    val.to_s
  else
    fail "Unknown type for attribute #{attr}: #{type}"
  end
end

#klassObject



120
121
122
123
# File 'lib/origen_testers/smartest_based_tester/base/test_method.rb', line 120

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

#respond_to?(method) ⇒ Boolean

Returns:

  • (Boolean)


137
138
139
# File 'lib/origen_testers/smartest_based_tester/base/test_method.rb', line 137

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

#sorted_parametersObject



141
142
143
144
145
146
147
148
149
150
151
152
153
# File 'lib/origen_testers/smartest_based_tester/base/test_method.rb', line 141

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