aboutsummaryrefslogtreecommitdiffstats
path: root/activesupport/test/broadcast_logger_test.rb
blob: e7d56c80c3f078fa2b3d00696edcf191217f8ea6 (plain) (blame)
1
2
3
4
5
6
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
require 'abstract_unit'

module ActiveSupport
  class BroadcastLoggerTest < TestCase
    attr_reader :logger, :receiving_logger
    def setup
      @logger = FakeLogger.new
      @receiving_logger = FakeLogger.new
      @logger.extend Logger.broadcast @receiving_logger
    end

    def test_debug
      logger.debug "foo"
      assert_equal 'foo', logger.adds.first[2]
      assert_equal 'foo', receiving_logger.adds.first[2]
    end

    def test_debug_without_message_broadcasts
      logger.broadcast_messages = false
      logger.debug "foo"
      assert_equal 'foo', logger.adds.first[2]
      assert_equal [], receiving_logger.adds
    end

    def test_close
      logger.close
      assert logger.closed, 'should be closed'
      assert receiving_logger.closed, 'should be closed'
    end

    def test_chevrons
      logger << "foo"
      assert_equal %w{ foo }, logger.chevrons
      assert_equal %w{ foo }, receiving_logger.chevrons
    end

    def test_chevrons_without_message_broadcasts
      logger.broadcast_messages = false
      logger << "foo"
      assert_equal %w{ foo }, logger.chevrons
      assert_equal [], receiving_logger.chevrons
    end

    def test_level
      assert_nil logger.level
      logger.level = 10
      assert_equal 10, logger.level
      assert_equal 10, receiving_logger.level
    end

    def test_progname
      assert_nil logger.progname
      logger.progname = 10
      assert_equal 10, logger.progname
      assert_equal 10, receiving_logger.progname
    end

    def test_formatter
      assert_nil logger.formatter
      logger.formatter = 10
      assert_equal 10, logger.formatter
      assert_equal 10, receiving_logger.formatter
    end

    class FakeLogger
      attr_reader :adds, :closed, :chevrons
      attr_accessor :level, :progname, :formatter, :broadcast_messages

      def initialize
        @adds      = []
        @closed    = false
        @chevrons  = []
        @level     = nil
        @progname  = nil
        @formatter = nil
        @broadcast_messages = true
      end

      def debug msg, &block
        add(:omg, nil, msg, &block)
      end

      def << x
        @chevrons << x
      end

      def add(*args)
        @adds << args
      end

      def close
        @closed = true
      end
    end
  end
end