Exemple #1
0
func Test_Logger_Output(t *testing.T) {
	// mock os.Stdout
	stdout := os.Stdout
	r, w, _ := os.Pipe()
	os.Stdout = w

	s := "output testing"
	expected := s + "\n"
	testCases := map[Level]string{
		Ldebug: "[DEBUG]",
		Linfo:  "[INFO]",
		Lwarn:  "[WARN]",
		Lerror: "[ERROR]",
		Lfatal: "[FATAL]",
		Lpanic: "[PANIC]",
		Ltrace: "[Stack]",
	}

	logger, _ := New("stdout")
	logger.SetSkip(1)

	for level, tag := range testCases {
		logger.Output(level, s)

		buf := make([]byte, 1024)
		n, err := r.Read(buf)
		assert.Nil(t, err)
		assert.Contains(t, string(buf[:n]), tag)
		assert.Contains(t, string(buf[:n]), expected)
	}

	os.Stdout = stdout
}
Exemple #2
0
func Test_Logger_AddTags(t *testing.T) {
	// mock os.Stdout
	stdout := os.Stdout
	r, w, _ := os.Pipe()
	os.Stdout = w

	tags := []string{"testing", "logger"}
	newTags := []string{"new_testing", "logger"}
	s := "output testing"
	expected := "[DEBUG, testing, logger, new_testing]"

	logger, _ := New("stdout")
	logger.SetSkip(1)
	logger.SetTags(tags...)
	logger.AddTags(newTags...)

	logger.Debug(s)

	buf := make([]byte, 1024)
	n, err := r.Read(buf)
	assert.Nil(t, err)
	assert.Contains(t, string(buf[:n]), expected)

	os.Stdout = stdout
}
Exemple #3
0
// AssertContains tests that the response contains the given string.
func (test *Client) AssertContains(s string) {
	assert.Contains(test.t, string(test.ResponseBody), s, "Expected response body contains "+s+".")
}