Module: Typeguard::Metrics

Defined in:
lib/typeguard/metrics.rb

Defined Under Namespace

Classes: Log

Class Method Summary collapse

Class Method Details

.config(validation) ⇒ Object



11
12
13
14
# File 'lib/typeguard/metrics.rb', line 11

def self.config(validation)
  @raise_on_unexpected_argument = validation.raise_on_unexpected_argument
  @raise_on_unexpected_return = validation.raise_on_unexpected_return
end

.flushObject



24
25
26
27
28
29
30
# File 'lib/typeguard/metrics.rb', line 24

def self.flush
  new_line = "\n" unless @logs.empty?
  puts "\ntypeguard errors [start]: #{@logs.length} #{new_line}\n"
  @logs.each { |log| puts format_log(log) }
  puts "\ntypeguard errors [end]: #{@logs.length} #{new_line}"
  @logs.clear
end

.format_log(log) ⇒ Object



16
17
18
19
20
21
22
# File 'lib/typeguard/metrics.rb', line 16

def self.format_log(log)
  "    - \#{log.error.upcase} - Expected \#{log.expected} for \#{log.type} but received incompatible \\\n    \#{log.actual} in '\#{log.module}#\#{log.definition}' defined in \#{log.source} \\\n    and called from \#{log.caller}\n  MESSAGE\nend\n"

.report(mod, definition, error, expected, actual) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/typeguard/metrics.rb', line 32

def self.report(mod, definition, error, expected, actual)
  caller = caller_locations(1, 1).first
  caller_string = caller.label.split('::').last
  module_name = mod.name.to_sym
  type = definition.class.name.split('::').last.gsub('Definition', '').to_sym
  source = definition.source
  log = Log.new(module: module_name, definition: definition.name, type: type, error: error,
                expected: expected, actual: actual, source: source,
                caller: caller_string)
  @logs << log
  log
end

.report_unexpected_argument(sig, expected, actual, mod_name, parameter) ⇒ Object

Raises:

  • (TypeError)


58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/typeguard/metrics.rb', line 58

def self.report_unexpected_argument(sig, expected, actual, mod_name, parameter)
  caller = caller_locations(4, 1).first
  caller_string = "#{caller.path}:#{caller.lineno}"
  method_name = sig.name
  parameter_name = parameter.name
  source = parameter.source
  log = Log.new(module: mod_name, definition: method_name, type: parameter_name,
                error: :unexpected_argument, source: source, caller: caller_string,
                expected: expected, actual: actual.class.to_s)
  @logs << log
  raise TypeError, format_log(log) if @raise_on_unexpected_argument

  log
end

.report_unexpected_return(sig, return_object, result, mod_name) ⇒ Object

Raises:

  • (TypeError)


45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/typeguard/metrics.rb', line 45

def self.report_unexpected_return(sig, return_object, result, mod_name)
  caller = caller_locations(2, 1).first
  caller_string = "#{caller.path}:#{caller.lineno}"
  source = sig.returns.source
  log = Log.new(module: mod_name, definition: sig.name, type: :Return,
                error: :unexpected_return, source: source, caller: caller_string,
                expected: return_object, actual: result.class.to_s)
  @logs << log
  raise TypeError, format_log(log) if @raise_on_unexpected_return

  log
end