mirror of
				https://github.com/fluffle/goirc
				synced 2025-11-03 19:48:04 +00:00 
			
		
		
		
	More tests for the logging package.
This commit is contained in:
		
							parent
							
								
									c769723596
								
							
						
					
					
						commit
						2467e5cc93
					
				
					 2 changed files with 47 additions and 20 deletions
				
			
		| 
						 | 
					@ -181,16 +181,15 @@ func (l *logger) write(lv LogLevel, fm string, v ...interface{}) {
 | 
				
			||||||
		return
 | 
							return
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	fm = fmt.Sprintf(LogString(lv)+" "+fm, v...)
 | 
						fm = fmt.Sprintf(LogString(lv)+" "+fm, v...)
 | 
				
			||||||
	if _, ok := logString[lv]; !ok {
 | 
						if lv > Debug || lv < Fatal {
 | 
				
			||||||
		// This is an unrecognised log level, so log it to Debug
 | 
							// This is an unrecognised log level, so log it to Debug
 | 
				
			||||||
		lv = Debug
 | 
							lv = Debug
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	l.Lock()
 | 
						l.Lock()
 | 
				
			||||||
	defer l.Unlock()
 | 
						defer l.Unlock()
 | 
				
			||||||
	_log := l.log[lv]
 | 
					 | 
				
			||||||
	// Writing the log is deceptively simple
 | 
						// Writing the log is deceptively simple
 | 
				
			||||||
	_log.Output(3, fm)
 | 
						l.log[lv].Output(3, fm)
 | 
				
			||||||
	if lv == Fatal {
 | 
						if lv == Fatal {
 | 
				
			||||||
		// Always fatal to stderr too. Use panic so (a) we get a backtrace,
 | 
							// Always fatal to stderr too. Use panic so (a) we get a backtrace,
 | 
				
			||||||
		// and (b) it's trappable for testing (and maybe other times too).
 | 
							// and (b) it's trappable for testing (and maybe other times too).
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -9,7 +9,7 @@ import (
 | 
				
			||||||
// Note: the below is deliberately PLACED AT THE TOP OF THIS FILE because
 | 
					// Note: the below is deliberately PLACED AT THE TOP OF THIS FILE because
 | 
				
			||||||
// it is fragile. It ensures the right file:line is logged. Sorry!
 | 
					// it is fragile. It ensures the right file:line is logged. Sorry!
 | 
				
			||||||
func TestLogCorrectLineNumbers(t *testing.T) {
 | 
					func TestLogCorrectLineNumbers(t *testing.T) {
 | 
				
			||||||
	l, m := setUp()
 | 
						l, m := SetUp()
 | 
				
			||||||
	l.Log(Error, "Error!")
 | 
						l.Log(Error, "Error!")
 | 
				
			||||||
	if s := string(m[Error].written); s[20:] != "log_test.go:13: ERROR Error!\n" {
 | 
						if s := string(m[Error].written); s[20:] != "log_test.go:13: ERROR Error!\n" {
 | 
				
			||||||
		t.Errorf("Error incorrectly logged (check line numbers!)")
 | 
							t.Errorf("Error incorrectly logged (check line numbers!)")
 | 
				
			||||||
| 
						 | 
					@ -31,7 +31,8 @@ func (w *mockWriter) reset() {
 | 
				
			||||||
	w.written = w.written[:0]
 | 
						w.written = w.written[:0]
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func setUp() (*logger, writerMap) {
 | 
					// Make these accessible for other packages doing logging testing
 | 
				
			||||||
 | 
					func SetUp() (*logger, writerMap) {
 | 
				
			||||||
	wMap := writerMap{
 | 
						wMap := writerMap{
 | 
				
			||||||
		Debug: &mockWriter{make([]byte, 0)},
 | 
							Debug: &mockWriter{make([]byte, 0)},
 | 
				
			||||||
		Info:  &mockWriter{make([]byte, 0)},
 | 
							Info:  &mockWriter{make([]byte, 0)},
 | 
				
			||||||
| 
						 | 
					@ -46,7 +47,7 @@ func setUp() (*logger, writerMap) {
 | 
				
			||||||
	return New(logMap, Error, false), wMap
 | 
						return New(logMap, Error, false), wMap
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func (m writerMap) checkNothingWritten(t *testing.T) {
 | 
					func (m writerMap) CheckNothingWritten(t *testing.T) {
 | 
				
			||||||
	for lv, w := range m {
 | 
						for lv, w := range m {
 | 
				
			||||||
		if len(w.written) > 0 {
 | 
							if len(w.written) > 0 {
 | 
				
			||||||
			t.Errorf("%d bytes logged at level %s, expected none:",
 | 
								t.Errorf("%d bytes logged at level %s, expected none:",
 | 
				
			||||||
| 
						 | 
					@ -57,7 +58,7 @@ func (m writerMap) checkNothingWritten(t *testing.T) {
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func (m writerMap) checkWrittenAtLevel(t *testing.T, lv LogLevel, exp string) {
 | 
					func (m writerMap) CheckWrittenAtLevel(t *testing.T, lv LogLevel, exp string) {
 | 
				
			||||||
	var w *mockWriter
 | 
						var w *mockWriter
 | 
				
			||||||
	if _, ok := m[lv]; !ok {
 | 
						if _, ok := m[lv]; !ok {
 | 
				
			||||||
		w = m[Debug]
 | 
							w = m[Debug]
 | 
				
			||||||
| 
						 | 
					@ -69,37 +70,64 @@ func (m writerMap) checkWrittenAtLevel(t *testing.T, lv LogLevel, exp string) {
 | 
				
			||||||
		t.Errorf("\t%s", exp)
 | 
							t.Errorf("\t%s", exp)
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	// 32 bytes covers the date, time and filename up to the colon in
 | 
						// 32 bytes covers the date, time and filename up to the colon in
 | 
				
			||||||
	// 2011/10/22 10:22:57 log_test.go:<line no>: <log message>
 | 
						// 2011/10/22 10:22:57 log_test.go:<line no>: <level> <log message>
 | 
				
			||||||
	s := string(w.written[32:])
 | 
						s := string(w.written[32:])
 | 
				
			||||||
	// 3 covers the : itself and the two extra spaces
 | 
						// 2 covers the : itself and the extra space
 | 
				
			||||||
	idx := strings.Index(s, ":") + len(LogString(lv)) + 3
 | 
						idx := strings.Index(s, ":") + 2
 | 
				
			||||||
	// s will end in "\n", so -1 to chop that off too
 | 
						// s will end in "\n", so -1 to chop that off too
 | 
				
			||||||
	s = s[idx:len(s)-1]
 | 
						s = s[idx:len(s)-1]
 | 
				
			||||||
 | 
						// expected won't contain the log level prefix, so prepend that
 | 
				
			||||||
 | 
						exp = LogString(lv) + " " + exp
 | 
				
			||||||
	if s != exp {
 | 
						if s != exp {
 | 
				
			||||||
		t.Errorf("Log message at level %s differed.", LogString(lv))
 | 
							t.Errorf("Log message at level %s differed.", LogString(lv))
 | 
				
			||||||
		t.Errorf("\texp: %s\n\tgot: %s", exp, s)
 | 
							t.Errorf("\texp: %s\n\tgot: %s", exp, s)
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	w.reset()
 | 
						w.reset()
 | 
				
			||||||
 | 
						// Calling checkNothingWritten here both tests that w.reset() works
 | 
				
			||||||
 | 
						// and verifies that nothing was written at any other levels.
 | 
				
			||||||
 | 
						m.CheckNothingWritten(t)
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func TestLogging(t *testing.T) {
 | 
					func TestStandardLogging(t *testing.T) {
 | 
				
			||||||
	l, m := setUp()
 | 
						l, m := SetUp()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	l.Log(4, "Nothing should be logged yet")
 | 
						l.Log(4, "Nothing should be logged yet")
 | 
				
			||||||
	m.checkNothingWritten(t)
 | 
						m.CheckNothingWritten(t)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	l.Log(Debug, "or yet...")
 | 
						l.Log(Debug, "or yet...")
 | 
				
			||||||
	m.checkNothingWritten(t)
 | 
						m.CheckNothingWritten(t)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	l.Log(Info, "or yet...")
 | 
						l.Log(Info, "or yet...")
 | 
				
			||||||
	m.checkNothingWritten(t)
 | 
						m.CheckNothingWritten(t)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	l.Log(Warn, "or yet!")
 | 
						l.Log(Warn, "or yet!")
 | 
				
			||||||
	m.checkNothingWritten(t)
 | 
						m.CheckNothingWritten(t)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	l.Log(Error, "Error!")
 | 
						l.Log(Error, "Error!")
 | 
				
			||||||
	m.checkWrittenAtLevel(t, Error, "Error!")
 | 
						m.CheckWrittenAtLevel(t, Error, "Error!")
 | 
				
			||||||
	// Calling checkNothingWritten here both tests that w.reset() works
 | 
					}
 | 
				
			||||||
	// and verifies that nothing was written at any other levels than Error.
 | 
					
 | 
				
			||||||
	m.checkNothingWritten(t)
 | 
					func TestAllLoggingLevels(t *testing.T) {
 | 
				
			||||||
 | 
						l, m := SetUp()
 | 
				
			||||||
 | 
						l.SetLogLevel(5)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						l.Log(4, "Log to level 4.")
 | 
				
			||||||
 | 
						m.CheckWrittenAtLevel(t, 4, "Log to level 4.")
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						l.Debug("Log to debug.")
 | 
				
			||||||
 | 
						m.CheckWrittenAtLevel(t, Debug, "Log to debug.")
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						l.Info("Log to info.")
 | 
				
			||||||
 | 
						m.CheckWrittenAtLevel(t, Info, "Log to info.")
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						l.Warn("Log to warning.")
 | 
				
			||||||
 | 
						m.CheckWrittenAtLevel(t, Warn, "Log to warning.")
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						l.Error("Log to error.")
 | 
				
			||||||
 | 
						m.CheckWrittenAtLevel(t, Error, "Log to error.")
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						// recover to track the panic caused by Fatal.
 | 
				
			||||||
 | 
						defer func() { recover() }()
 | 
				
			||||||
 | 
						l.Fatal("Log to fatal.")
 | 
				
			||||||
 | 
						m.CheckWrittenAtLevel(t, Fatal, "Log to fatal.")
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue