forked from andelf/go-curl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
logging_test.go
64 lines (54 loc) · 1.64 KB
/
logging_test.go
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
package curl
import (
"testing"
"bytes"
"log"
"os"
"fmt"
"regexp"
)
func TestDefaultLogLevel(t *testing.T) {
if log_level != _DEFAULT_LOG_LEVEL {t.Error("Test failed, expected DEFAULT_LOG_LEVEL level.")}
}
func TestSetLogLevel(t *testing.T) {
SetLogLevel("DEBUG")
defer SetLogLevel("DEFAULT_LOG_LEVEL")
if log_level != _DEBUG {t.Error("Test failed, expected DEBUG level.")}
SetLogLevel("INFO")
if log_level != _INFO {t.Error("Test failed, expected INFO level.")}
SetLogLevel("WARN")
if log_level != _WARN {t.Error("Test failed, expected WARN level.")}
SetLogLevel("ERROR")
if log_level != _ERROR {t.Error("Test failed, expected ERROR level.")}
}
var (
testFormat = "test format %s"
testArgument = "test string 1"
expectedRegexp = regexp.MustCompile(".*" + fmt.Sprintf(testFormat, testArgument) + "\n$")
)
func TestLogf(t *testing.T) {
buf := new(bytes.Buffer)
log.SetOutput(buf)
defer log.SetOutput(os.Stderr)
SetLogLevel("DEBUG")
defer SetLogLevel("DEFAULT_LOG_LEVEL")
logf(_DEBUG, testFormat, testArgument)
line := buf.String()
matched := expectedRegexp.MatchString(line)
if !matched {
t.Errorf("log output should match %q and is %q.", expectedRegexp, line)
}
}
func TestLogfUsesLogLevel(t *testing.T) {
buf := new(bytes.Buffer)
log.SetOutput(buf)
defer log.SetOutput(os.Stderr)
SetLogLevel("WARN")
defer SetLogLevel("DEFAULT_LOG_LEVEL")
logf(_DEBUG, testFormat, testArgument)
line := buf.String()
expectedLine := ""
if line != expectedLine {
t.Errorf("log output should match %q and is %q.", expectedLine, line)
}
}