summaryrefslogtreecommitdiff
path: root/spec/ruby/library/logger/logger/add_spec.rb
blob: 3f709e18ba24b6e25a239565b992a5b12b09f269 (plain)
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
require_relative '../../../spec_helper'
require_relative '../fixtures/common'

describe "Logger#add" do
  before :each do
    @path = tmp("test_log.log")
    @log_file = File.open(@path, "w+")
    @logger = Logger.new(@path)
  end

  after :each do
    @logger.close
    @log_file.close unless @log_file.closed?
    rm_r @path
  end

  it "writes a new message to the logger" do
    @logger.add(Logger::WARN, "Test")
    @log_file.rewind
    message = @log_file.readlines.last
    LoggerSpecs.strip_date(message).should == "WARN -- : Test\n"
  end

  it "receives a severity" do
    @logger.log(Logger::INFO,  "Info message")
    @logger.log(Logger::DEBUG, "Debug message")
    @logger.log(Logger::WARN,  "Warn message")
    @logger.log(Logger::ERROR, "Error message")
    @logger.log(Logger::FATAL, "Fatal message")

    @log_file.rewind

    info, debug, warn, error, fatal = @log_file.readlines

    LoggerSpecs.strip_date(info).should == "INFO -- : Info message\n"
    LoggerSpecs.strip_date(debug).should == "DEBUG -- : Debug message\n"
    LoggerSpecs.strip_date(warn).should == "WARN -- : Warn message\n"
    LoggerSpecs.strip_date(error).should == "ERROR -- : Error message\n"
    LoggerSpecs.strip_date(fatal).should == "FATAL -- : Fatal message\n"
  end

  it "receives a message" do
    @logger.log(nil, "test")
    @log_file.rewind
    LoggerSpecs.strip_date(@log_file.readline).should == "ANY -- : test\n"
  end

  it "receives a program name" do
    @logger.log(nil, "test", "TestApp")
    @log_file.rewind
    LoggerSpecs.strip_date(@log_file.readline).should == "ANY -- TestApp: test\n"
  end

  it "receives a block" do
    -> {
      @logger.log(nil, "test", "TestApp") do
        1+1
      end
    }.should_not raise_error
  end

  it "calls the block if message is nil" do
    temp = 0
    -> {
      @logger.log(nil, nil, "TestApp") do
        temp = 1+1
      end
    }.should_not raise_error
    temp.should == 2
  end

  it "ignores the block if the message is not nil" do
    temp = 0
    -> {
      @logger.log(nil, "not nil", "TestApp") do
        temp = 1+1
      end
    }.should_not raise_error
    temp.should == 0
  end
end