root / lib / test / unit / notification.rb @ 16a276fdc77847a92cba21d060b9880eb1fe75bb
View | Annotate | Download (3 KB)
| 1 | require 'test/unit/util/backtracefilter'
|
|---|---|
| 2 | |
| 3 | module Test |
| 4 | module Unit |
| 5 | class Notification |
| 6 | include Util::BacktraceFilter |
| 7 | attr_reader :test_name, :location, :message |
| 8 | |
| 9 | SINGLE_CHARACTER = 'N' |
| 10 | LABEL = "Notification" |
| 11 | |
| 12 | # Creates a new Notification with the given location and
|
| 13 | # message.
|
| 14 | def initialize(test_name, location, message) |
| 15 | @test_name = test_name
|
| 16 | @location = location
|
| 17 | @message = message
|
| 18 | end
|
| 19 | |
| 20 | # Returns a single character representation of a notification.
|
| 21 | def single_character_display |
| 22 | SINGLE_CHARACTER
|
| 23 | end
|
| 24 | |
| 25 | def label |
| 26 | LABEL
|
| 27 | end
|
| 28 | |
| 29 | # Returns a brief version of the error description.
|
| 30 | def short_display |
| 31 | "#{@test_name}: #{@message.split("\n")[0]}"
|
| 32 | end
|
| 33 | |
| 34 | # Returns a verbose version of the error description.
|
| 35 | def long_display |
| 36 | backtrace = filter_backtrace(location).join("\n")
|
| 37 | "#{label}: #{@message}\n#{@test_name}\n#{backtrace}"
|
| 38 | end
|
| 39 | |
| 40 | # Overridden to return long_display.
|
| 41 | def to_s |
| 42 | long_display |
| 43 | end
|
| 44 | end
|
| 45 | |
| 46 | class NotifiedError < StandardError |
| 47 | end
|
| 48 | |
| 49 | |
| 50 | module TestCaseNotificationSupport |
| 51 | class << self |
| 52 | def included(base) |
| 53 | base.class_eval do
|
| 54 | include NotificationHandler
|
| 55 | end
|
| 56 | end
|
| 57 | end
|
| 58 | |
| 59 | # Notify some information.
|
| 60 | #
|
| 61 | # Example:
|
| 62 | # def test_notification
|
| 63 | # notify("I'm here!")
|
| 64 | # # Reached here
|
| 65 | # notify("Special!") if special_case?
|
| 66 | # # Reached here too
|
| 67 | # end
|
| 68 | def notify(message, &block) |
| 69 | notification = Notification.new(name, filter_backtrace(caller), message)
|
| 70 | add_notification(notification) |
| 71 | end
|
| 72 | |
| 73 | private |
| 74 | def add_notification(notification) |
| 75 | current_result.add_notification(notification) |
| 76 | end
|
| 77 | end
|
| 78 | |
| 79 | module NotificationHandler |
| 80 | class << self |
| 81 | def included(base) |
| 82 | base.exception_handler(:handle_Notified_error)
|
| 83 | end
|
| 84 | end
|
| 85 | |
| 86 | private |
| 87 | def handle_Notified_error(exception) |
| 88 | return false unless exception.is_a?(NotifiedError) |
| 89 | notification = Notification.new(name,
|
| 90 | filter_backtrace(exception.backtrace), |
| 91 | exception.message) |
| 92 | add_notification(notification) |
| 93 | true
|
| 94 | end
|
| 95 | end
|
| 96 | |
| 97 | module TestResultNotificationSupport |
| 98 | attr_reader :notifications
|
| 99 | |
| 100 | # Records a Test::Unit::Notification.
|
| 101 | def add_notification(notification) |
| 102 | @notifications << notification
|
| 103 | notify_fault(notification) |
| 104 | notify_changed |
| 105 | end
|
| 106 | |
| 107 | # Returns the number of notifications this TestResult has
|
| 108 | # recorded.
|
| 109 | def notification_count |
| 110 | @notifications.size
|
| 111 | end
|
| 112 | |
| 113 | private |
| 114 | def initialize_containers |
| 115 | super
|
| 116 | @notifications = []
|
| 117 | @summary_generators << :notification_summary |
| 118 | end
|
| 119 | |
| 120 | def notification_summary |
| 121 | "#{notification_count} notifications"
|
| 122 | end
|
| 123 | end
|
| 124 | end
|
| 125 | end
|