Class: InlineTests

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

Class Method Summary collapse

Class Method Details

.run!Object



7
8
9
10
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
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
# File 'lib/inline_tests.rb', line 7

def self.run!
  test_passes = 0
  test_fails  = 0

  # If we're a gem in a library like Rails that loads models on demand,
  # we need to make sure all the models are loaded before running any tests,
  # because it's the loading of the models that registers each method's tests.
  begin
    Rails.application.eager_load!
  rescue
    # TODO: handle/display any Railsy errors here
  end

  puts "Starting inline test suite:"
  all_tests_start_time = Time.now
  METHODS_WITH_INLINE_TESTS.each do |method|
    # Kernel.class_variable_set(:@@method_being_tested, method)
    
    method_signature = if method.receiver.class.name === Object.name
      # If the receiver is an Object, it's probably #main, in which case we can just print it directly
      "#{method.receiver}::#{method.name}"

    elsif method.receiver.class.name == Class.name
      # If the receiver is the base Class, then we're dealing with a class method so we have a class ref already
      "#{method.receiver.name}::self.#{method.name}"

    else
      # If the receiver is some child class class, we want to use the class name in printable output
      "#{method.receiver.class.name}::#{method.name}"
    end

    start_time  = Time.now
    begin
      # TODO: it'd actually be great if we could bind #run_inline_tests to some context where all our
      #       asserts/testing stuff is defined so we don't have to clutter up the global Kernel namespace
      method.run_inline_tests # Throws an exception on fail
      test_time    = Time.now - start_time
      test_passes += 1

      puts "  #{method_signature} - PASSED (#{format_tiny_time test_time} seconds)"

    rescue InlineTestFailure => failure_information
      test_time   = Time.now - start_time
      errors      = failure_information
      test_fails += 1

      puts "  #{method_signature} - FAILED (#{format_tiny_time test_time} seconds)"
      puts errors
    end
  end
  all_tests_end_time = Time.now

  # Print overall test results & stats
  # print_results(method_passing, method_errors)
  puts "#{METHODS_WITH_INLINE_TESTS.count} inline tests ran in #{format_tiny_time all_tests_end_time - all_tests_start_time} seconds."
  puts "  #{test_passes} PASSED"
  puts "  #{test_fails} FAILS"
  puts
  puts "#{METHODS_THAT_NEED_TESTS.count} methods still need tests:"
  METHODS_THAT_NEED_TESTS.each do |method|
    puts "  #{method.receiver.name}##{method.original_name}"
    puts "    #{method.source_location.join(':')}"
  end

  # Intentionally return nil so we don't re-output what we just printed as a return value
  nil
end