summaryrefslogtreecommitdiff
path: root/src/lib/log_test.go
blob: f0cfb13bcee2524dc66175a83acc9d3ed7e6935e (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
// Copyright 2009 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package log

// These tests are too simple.

import (
	"bufio";
	"log";
	"os";
	"testing";
)

func test(t *testing.T, flag int, expect string) {
	fd0, fd1, err1 := os.Pipe();
	if err1 != nil {
		t.Error("pipe", err1);
	}
	buf, err2 := bufio.NewBufRead(fd0);
	if err2 != nil {
		t.Error("bufio.NewBufRead", err2);
	}
	l := NewLogger(fd1, nil, flag);
	l.Log("hello", 23, "world");	/// the line number of this line needs to be placed in the expect strings
	line, err3 := buf.ReadLineString('\n', false);
	if line[len(line)-len(expect):len(line)] != expect {
		t.Error("log output should be ...", expect, "; is " , line);
	}
	t.Log(line);
	fd0.Close();
	fd1.Close();
}

func TestRegularLog(t *testing.T) {
	test(t, Lok, "/go/src/lib/log_test.go:25: hello 23 world");
}

func TestShortNameLog(t *testing.T) {
	test(t, Lok|Lshortname, " log_test.go:25: hello 23 world")
}

func testFormatted(t *testing.T, flag int, expect string) {
	fd0, fd1, err1 := os.Pipe();
	if err1 != nil {
		t.Error("pipe", err1);
	}
	buf, err2 := bufio.NewBufRead(fd0);
	if err2 != nil {
		t.Error("bufio.NewBufRead", err2);
	}
	l := NewLogger(fd1, nil, flag);
	l.Logf("hello %d world", 23);	/// the line number of this line needs to be placed in the expect strings
	line, err3 := buf.ReadLineString('\n', false);
	if line[len(line)-len(expect):len(line)] != expect {
		t.Error("log output should be ...", expect, "; is " , line);
	}
	t.Log(line);
	fd0.Close();
	fd1.Close();
}

func TestRegularLogFormatted(t *testing.T) {
	testFormatted(t, Lok, "/go/src/lib/log_test.go:53: hello 23 world");
}

func TestShortNameLogFormatted(t *testing.T) {
	testFormatted(t, Lok|Lshortname, " log_test.go:53: hello 23 world")
}