Class: Test::Unit::TestCase

Inherits:
Object
  • Object
show all
Defined in:
lib/ruby-prof/profile_test_case.rb

Instance Method Summary collapse

Instance Method Details

#create_output_directoryObject



61
62
63
64
65
# File 'lib/ruby-prof/profile_test_case.rb', line 61

def create_output_directory
  if not File.exist?(output_directory)
    Dir.mkdir(output_directory)
  end
end

#file_extensionObject



67
68
69
70
71
72
73
# File 'lib/ruby-prof/profile_test_case.rb', line 67

def file_extension
  if printer == RubyProf::FlatPrinter
    '.html'
  else
    '.txt'
  end
end

#min_percentObject

Add some additional methods



52
53
54
# File 'lib/ruby-prof/profile_test_case.rb', line 52

def min_percent
  1
end

#output_directoryObject



56
57
58
59
# File 'lib/ruby-prof/profile_test_case.rb', line 56

def output_directory
  # Put results in subdirectory called profile
  File.join(Dir.getwd, 'profile')
end

#printerObject



75
76
77
# File 'lib/ruby-prof/profile_test_case.rb', line 75

def printer
  RubyProf::GraphHtmlPrinter
end

#run(result, &block) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/ruby-prof/profile_test_case.rb', line 11

def run(result, &block)
  test_name = @method_name.to_sym
  alias_test_name = (@method_name + '__profile__').to_sym
  
  self.class.class_eval("alias :#{alias_test_name} :#{test_name}")
          
  self.class.send(:define_method, test_name) do 
    # Run the profiler        
    RubyProf.start
    __send__(alias_test_name)
    result = RubyProf.stop

    create_output_directory
      
    # Get the result file name
    file_name = name.gsub(/\(/, '_').gsub(/\)/, '')
    file_name = self.underscore(file_name)
    file_path = File.join(output_directory, file_name)
    file_path += file_extension

    # Create a printer
    printer = self.printer.new(result)
    
    # Write the results
    File.open(file_path, 'w') do |file|
      printer.print(file, min_percent)
    end
  end
  
  self.run__profile__(result, &block)
end

#run__profile__Object



9
# File 'lib/ruby-prof/profile_test_case.rb', line 9

alias :run__profile__ :run

#underscore(camel_cased_word) ⇒ Object

Taken from rails



44
45
46
47
48
49
# File 'lib/ruby-prof/profile_test_case.rb', line 44

def underscore(camel_cased_word)
  camel_cased_word.to_s.gsub(/::/, '/').
    gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2').
    gsub(/([a-z\d])([A-Z])/,'\1_\2').
    tr("-", "_").downcase
end