-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f1d3dcf
commit 20e3a6d
Showing
2 changed files
with
332 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,330 @@ | ||
package golog | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"testing" | ||
) | ||
|
||
func Test_CreateLogger_ReturnsLogger(t *testing.T) { | ||
|
||
// ARRANGE | ||
logger := LoggingConfiguration(). | ||
Configure(Verbose, "[%l %t] %m"). | ||
WriteTo(&testsink{}). | ||
CreateLogger() | ||
|
||
// ACT | ||
|
||
// ASSERT | ||
if logger == nil { | ||
t.Errorf("LOGGER IS NIL") | ||
} | ||
} | ||
|
||
func Test_CreateLogger_ReturnsLogger_WithVerbosityVerbose(t *testing.T) { | ||
|
||
// ARRANGE | ||
logger := LoggingConfiguration(). | ||
Configure(Verbose, "[%l %t] %m"). | ||
WriteTo(&testsink{}). | ||
CreateLogger() | ||
|
||
// ACT | ||
|
||
// ASSERT | ||
if logger.CurrentLevel() != Verbose { | ||
t.Errorf("GOT %q, EXPECTED %q", logger.CurrentLevel(), Verbose) | ||
} | ||
} | ||
|
||
func Test_CreateLogger_ReturnsLogger_WithVerbosityDebug(t *testing.T) { | ||
|
||
// ARRANGE | ||
logger := LoggingConfiguration(). | ||
Configure(Debug, "[%l %t] %m"). | ||
WriteTo(&testsink{}). | ||
CreateLogger() | ||
|
||
// ACT | ||
|
||
// ASSERT | ||
if logger.CurrentLevel() != Debug { | ||
t.Errorf("GOT %q, EXPECTED %q", logger.CurrentLevel(), Debug) | ||
} | ||
} | ||
|
||
func Test_CreateLogger_ReturnsLogger_WithVerbosityInformation(t *testing.T) { | ||
|
||
// ARRANGE | ||
logger := LoggingConfiguration(). | ||
Configure(Information, "[%l %t] %m"). | ||
WriteTo(&testsink{}). | ||
CreateLogger() | ||
|
||
// ACT | ||
|
||
// ASSERT | ||
if logger.CurrentLevel() != Information { | ||
t.Errorf("GOT %q, EXPECTED %q", logger.CurrentLevel(), Information) | ||
} | ||
} | ||
|
||
func Test_CreateLogger_ReturnsLogger_WithVerbosityWarn(t *testing.T) { | ||
|
||
// ARRANGE | ||
logger := LoggingConfiguration(). | ||
Configure(Warn, "[%l %t] %m"). | ||
WriteTo(&testsink{}). | ||
CreateLogger() | ||
|
||
// ACT | ||
|
||
// ASSERT | ||
if logger.CurrentLevel() != Warn { | ||
t.Errorf("GOT %q, EXPECTED %q", logger.CurrentLevel(), Warn) | ||
} | ||
} | ||
|
||
func Test_CreateLogger_ReturnsLogger_WithVerbosityError(t *testing.T) { | ||
|
||
// ARRANGE | ||
logger := LoggingConfiguration(). | ||
Configure(Error, "[%l %t] %m"). | ||
WriteTo(&testsink{}). | ||
CreateLogger() | ||
|
||
// ACT | ||
|
||
// ASSERT | ||
if logger.CurrentLevel() != Error { | ||
t.Errorf("GOT %q, EXPECTED %q", logger.CurrentLevel(), Error) | ||
} | ||
} | ||
|
||
func Test_CreateLogger_ReturnsLogger_WithVerbosityFatal(t *testing.T) { | ||
|
||
// ARRANGE | ||
logger := LoggingConfiguration(). | ||
Configure(Fatal, "[%l %t] %m"). | ||
WriteTo(&testsink{}). | ||
CreateLogger() | ||
|
||
// ACT | ||
|
||
// ASSERT | ||
if logger.CurrentLevel() != Fatal { | ||
t.Errorf("GOT %q, EXPECTED %q", logger.CurrentLevel(), Fatal) | ||
} | ||
} | ||
|
||
func Test_CreateLogger_ReturnsLogger_DoesntWriteDebug(t *testing.T) { | ||
|
||
// ARRANGE | ||
logger := LoggingConfiguration(). | ||
Configure(Information, "[%l %t] %m"). | ||
WriteTo(&testsink{}). | ||
CreateLogger() | ||
|
||
// ACT | ||
|
||
logger.Debug("Writing Debug message should not write as the verbosity is set to debug") | ||
|
||
// ASSERT | ||
if wrote { | ||
t.Errorf("Debug message was writen with higher verbostity") | ||
} | ||
|
||
wrote = false | ||
} | ||
|
||
func Test_CreateLogger_ReturnsLogger_DoesntWriteInformation(t *testing.T) { | ||
|
||
// ARRANGE | ||
logger := LoggingConfiguration(). | ||
Configure(Warn, "[%l %t] %m"). | ||
WriteTo(&testsink{}). | ||
CreateLogger() | ||
|
||
// ACT | ||
|
||
logger.Information("Writing Information message should not write as the verbosity is set to debug") | ||
|
||
// ASSERT | ||
if wrote { | ||
t.Errorf("Information message was writen with higher verbostity") | ||
} | ||
|
||
wrote = false | ||
} | ||
|
||
func Test_CreateLogger_ReturnsLogger_DoesntWriteWarn(t *testing.T) { | ||
|
||
// ARRANGE | ||
logger := LoggingConfiguration(). | ||
Configure(Error, "[%l %t] %m"). | ||
WriteTo(&testsink{}). | ||
CreateLogger() | ||
|
||
// ACT | ||
|
||
logger.Warn("Writing Warn message should not write as the verbosity is set to debug") | ||
|
||
// ASSERT | ||
if wrote { | ||
t.Errorf("Warn message was writen with higher verbostity") | ||
} | ||
|
||
wrote = false | ||
} | ||
|
||
func Test_CreateLogger_ReturnsLogger_DoesntWriteError(t *testing.T) { | ||
|
||
// ARRANGE | ||
logger := LoggingConfiguration(). | ||
Configure(Fatal, "[%l %t] %m"). | ||
WriteTo(&testsink{}). | ||
CreateLogger() | ||
|
||
// ACT | ||
|
||
logger.Error("Writing Error message should not write as the verbosity is set to debug", errors.New("ERROR")) | ||
|
||
// ASSERT | ||
if wrote { | ||
t.Errorf("Error message was writen with higher verbostity") | ||
} | ||
|
||
wrote = false | ||
} | ||
|
||
func Test_CreateLogger_ReturnsLogger_ThatWritesVerbose(t *testing.T) { | ||
|
||
// ARRANGE | ||
logger := LoggingConfiguration(). | ||
Configure(Verbose, "[%l %t] %m"). | ||
WriteTo(&testsink{}). | ||
CreateLogger() | ||
|
||
// ACT | ||
|
||
logger.Verbose("Writing Debug message should not write as the verbosity is set to debug") | ||
|
||
// ASSERT | ||
if !wrote { | ||
t.Errorf("Verbose message was not writen with = || > verbostity") | ||
} | ||
|
||
wrote = false | ||
} | ||
|
||
func Test_CreateLogger_ReturnsLogger_ThatWritesDebug(t *testing.T) { | ||
|
||
// ARRANGE | ||
logger := LoggingConfiguration(). | ||
Configure(Debug, "[%l %t] %m"). | ||
WriteTo(&testsink{}). | ||
CreateLogger() | ||
|
||
// ACT | ||
|
||
logger.Debug("Writing Debug message should not write as the verbosity is set to debug") | ||
|
||
// ASSERT | ||
if !wrote { | ||
t.Errorf("Debug message was not writen with = || > verbostity") | ||
} | ||
|
||
wrote = false | ||
} | ||
|
||
func Test_CreateLogger_ReturnsLogger_ThatWritesInformation(t *testing.T) { | ||
|
||
// ARRANGE | ||
logger := LoggingConfiguration(). | ||
Configure(Information, "[%l %t] %m"). | ||
WriteTo(&testsink{}). | ||
CreateLogger() | ||
|
||
// ACT | ||
|
||
logger.Information("Writing Debug message should not write as the verbosity is set to debug") | ||
|
||
// ASSERT | ||
if !wrote { | ||
t.Errorf("Information message was not writen with = || > verbostity") | ||
} | ||
|
||
wrote = false | ||
} | ||
|
||
func Test_CreateLogger_ReturnsLogger_ThatWritesWarn(t *testing.T) { | ||
|
||
// ARRANGE | ||
logger := LoggingConfiguration(). | ||
Configure(Warn, "[%l %t] %m"). | ||
WriteTo(&testsink{}). | ||
CreateLogger() | ||
|
||
// ACT | ||
|
||
logger.Warn("Writing Debug message should not write as the verbosity is set to debug") | ||
|
||
// ASSERT | ||
if !wrote { | ||
t.Errorf("Warn message was not writen with = || > verbostity") | ||
} | ||
|
||
wrote = false | ||
} | ||
|
||
func Test_CreateLogger_ReturnsLogger_ThatWritesError(t *testing.T) { | ||
|
||
// ARRANGE | ||
logger := LoggingConfiguration(). | ||
Configure(Error, "[%l %t] %m"). | ||
WriteTo(&testsink{}). | ||
CreateLogger() | ||
|
||
// ACT | ||
|
||
logger.Error("Writing Error message should not write as the verbosity is set to debug", errors.New("ERROR")) | ||
|
||
// ASSERT | ||
if !wrote { | ||
t.Errorf("Error message was not writen with = || > verbostity") | ||
} | ||
|
||
wrote = false | ||
} | ||
|
||
func Test_CreateLogger_ReturnsLogger_ThatWritesFatal(t *testing.T) { | ||
|
||
// ARRANGE | ||
logger := LoggingConfiguration(). | ||
Configure(Fatal, "[%l %t] %m"). | ||
WriteTo(&testsink{}). | ||
CreateLogger() | ||
|
||
// ACT | ||
|
||
logger.Fatal("Writing Debug message should not write as the verbosity is set to debug", errors.New("ERROR")) | ||
|
||
// ASSERT | ||
if !wrote { | ||
t.Errorf("Fatal message was not writen with = || > verbostity") | ||
} | ||
|
||
wrote = false | ||
} | ||
|
||
var wrote = false | ||
|
||
type testsink struct { | ||
} | ||
|
||
func (s *testsink) WriteTo(message LogEvent) error { | ||
wrote = true | ||
fmt.Printf("%s", message.Message) | ||
return fmt.Errorf("%d MESSAGE WAS WRITEN", message.Level) | ||
} |