File: logify_spec.rb

package info (click to toggle)
ruby-logify 0.2.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 128 kB
  • sloc: ruby: 408; makefile: 5
file content (68 lines) | stat: -rw-r--r-- 1,836 bytes parent folder | download | duplicates (3)
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
require 'spec_helper'

module Logify
  describe do
    before { Logify.reset! }

    describe '.level & .level=' do
      it 'defaults to WARN' do
        expect(Logify.level).to eq(Logger::WARN)
      end

      it 'uses the level set in the main thread' do
        Logify.level = :fatal
        Thread.new { Logify.level = :warn }.join
        expect(Logify.level).to eq(Logger::FATAL)
      end

      it 'uses the level set in the current thread' do
        Logify.level = :fatal
        Thread.new do
          Logify.level = :debug
          expect(Logify.level).to eq(Logger::DEBUG)
        end.join

        # Make sure the parent thread isn't modified
        expect(Logify.level).to eq(Logger::FATAL)
      end

      it 'uses the level set in the main thread' do
        Logify.level = :fatal

        # Set the log level in another thread
        Thread.new { Logify.level }.join

        # Since Logify.level = :fatal was set in the main thread,
        # it should be the default moving forward
        Thread.new { expect(Logify.level).to eq(Logger::FATAL) }.join
      end
    end

    describe '.logger_for' do
      it 'uses a cached logger' do
        logger = double
        Logify.send(:loggers)['default'] = logger
        expect(Logify.logger_for('default')).to be(logger)
      end

      it 'creates a new logger when one does not exist' do
        Logger.stub(:new)
        expect(Logger).to receive(:new).with('default').once
        Logify.logger_for('default')
      end
    end

    describe '.included' do
      let(:klass)    { Class.new { include Logify } }
      let(:instance) { klass.new }

      it 'includes the instances methods' do
        expect(klass).to respond_to(:log)
      end

      it 'includes the class methods' do
        expect(klass).to respond_to(:log)
      end
    end
  end
end