Class: Rcov::DifferentialAnalyzer

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

Direct Known Subclasses

CallSiteAnalyzer, CodeCoverageAnalyzer

Constant Summary collapse

@@mutex =
Mutex.new

Instance Method Summary collapse

Constructor Details

#initialize(install_hook_meth, remove_hook_meth, reset_meth) ⇒ DifferentialAnalyzer

Returns a new instance of DifferentialAnalyzer.



6
7
8
9
10
11
12
13
14
# File 'lib/rcov/differential_analyzer.rb', line 6

def initialize(install_hook_meth, remove_hook_meth, reset_meth)
  @cache_state = :wait
  @start_raw_data = data_default
  @end_raw_data = data_default
  @aggregated_data = data_default
  @install_hook_meth = install_hook_meth
  @remove_hook_meth= remove_hook_meth
  @reset_meth= reset_meth
end

Instance Method Details

#install_hookObject

Start monitoring execution to gather information. Such data will be collected until #remove_hook is called.

Use #run_hooked instead if possible.



29
30
31
32
33
34
# File 'lib/rcov/differential_analyzer.rb', line 29

def install_hook
  @start_raw_data = raw_data_absolute
  Rcov::RCOV__.send(@install_hook_meth)
  @cache_state = :hooked
  @@mutex.synchronize{ self.class.hook_level += 1 }
end

#remove_hookObject

Stop collecting information. #remove_hook will also stop collecting info if it is run inside a #run_hooked block.



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/rcov/differential_analyzer.rb', line 39

def remove_hook
  @@mutex.synchronize do 
    self.class.hook_level -= 1
    Rcov::RCOV__.send(@remove_hook_meth) if self.class.hook_level == 0
  end
  @end_raw_data = raw_data_absolute
  @cache_state = :done
  # force computation of the stats for the traced code in this run;
  # we cannot simply let it be if self.class.hook_level == 0 because 
  # some other analyzer could install a hook, causing the raw_data_absolute
  # to change again.
  # TODO: lazy computation of raw_data_relative, only when the hook gets
  # activated again.
  raw_data_relative
end

#resetObject

Remove the data collected so far. Further collection will start from scratch.



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/rcov/differential_analyzer.rb', line 57

def reset
  @@mutex.synchronize do
    if self.class.hook_level == 0
      # Unfortunately there's no way to report this as covered with rcov:
      # if we run the tests under rcov self.class.hook_level will be >= 1 !
      # It is however executed when we run the tests normally.
      Rcov::RCOV__.send(@reset_meth)
      @start_raw_data = data_default
      @end_raw_data = data_default
    else
      @start_raw_data = @end_raw_data = raw_data_absolute
    end
    @raw_data_relative = data_default
    @aggregated_data = data_default
  end
end

#run_hookedObject

Execute the code in the given block, monitoring it in order to gather information about which code was executed.



18
19
20
21
22
23
# File 'lib/rcov/differential_analyzer.rb', line 18

def run_hooked
  install_hook
  yield
ensure
  remove_hook
end