logger_test.go 1.63 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
package federation
//
// GangGo Diaspora Federation Library
// Copyright (C) 2017 Lukas Matt <lukas@zauberstuhl.de>
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program.  If not, see <http://www.gnu.org/licenses/>.
//

import (
  "testing"
  "log"
  "os"
Lukas Matt's avatar
Lukas Matt committed
24 25
  "strings"
  "bytes"
26 27
)

Lukas Matt's avatar
Lukas Matt committed
28
func TestLoggerOutput(t *testing.T) {
29
  var buf bytes.Buffer
Lukas Matt's avatar
Lukas Matt committed
30 31 32 33
  var msg = "Hello World"

  SetLogger(log.New(&buf, "", log.Lshortfile))
  logger.Info(msg)
34

Lukas Matt's avatar
Lukas Matt committed
35 36 37 38
  expected := LOG_C_TUR + msg + LOG_C_RESET
  if strings.Contains(expected, buf.String()) {
    t.Errorf("Expected to contain '%s', got '%s'", expected, buf.String())
  }
39

Lukas Matt's avatar
Lukas Matt committed
40 41 42 43 44
  buf.Reset()
  logger.Error(msg)
  expected = LOG_C_RED + msg + LOG_C_RESET
  if strings.Contains(expected, buf.String()) {
    t.Errorf("Expected to contain '%s', got '%s'", expected, buf.String())
45 46
  }

Lukas Matt's avatar
Lukas Matt committed
47 48 49 50 51
  buf.Reset()
  logger.Warn(expected)
  expected = LOG_C_YELLOW + msg + LOG_C_RESET
  if strings.Contains(expected, buf.String()) {
    t.Errorf("Expected to contain '%s', got '%s'", expected, buf.String())
52 53 54 55 56
  }

  // reset otherwise it will break test output
  SetLogger(log.New(os.Stdout, defaultPrefix, log.Lshortfile))
}