-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4 from harrisonhjones/feature-allow-customization…
…-of-logger Allow customization of the logger with NewWith
- Loading branch information
Showing
7 changed files
with
123 additions
and
15 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 |
---|---|---|
|
@@ -14,4 +14,5 @@ | |
# Dependency directories (remove the comment below to include it) | ||
# vendor/ | ||
|
||
# IDE artifacts | ||
.idea/ |
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
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,70 @@ | ||
package emf | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"testing" | ||
"time" | ||
) | ||
|
||
func TestNew(t *testing.T) { | ||
tcs := []struct { | ||
name string | ||
opts []LoggerOption | ||
expected *Logger | ||
}{ | ||
{ | ||
name: "default", | ||
expected: &Logger{ | ||
out: os.Stdout, | ||
timestamp: time.Now().UnixNano() / int64(time.Millisecond), | ||
}, | ||
}, | ||
{ | ||
name: "with options", | ||
opts: []LoggerOption{ | ||
WithWriter(os.Stderr), | ||
WithTimestamp(time.Now().Add(time.Hour)), | ||
}, | ||
expected: &Logger{ | ||
out: os.Stderr, | ||
timestamp: time.Now().Add(time.Hour).UnixNano() / int64(time.Millisecond), | ||
}, | ||
}, | ||
} | ||
|
||
for _, tc := range tcs { | ||
t.Run(tc.name, func(t *testing.T) { | ||
actual := New(tc.opts...) | ||
if err := loggersEqual(actual, tc.expected); err != nil { | ||
t.Errorf("logger does not match: %v", err) | ||
} | ||
}) | ||
} | ||
|
||
} | ||
|
||
// loggersEqual returns a non-nil error if the loggers do not match. | ||
// Currently it only checks that the loggers' output writer and timestamp match. | ||
func loggersEqual(actual, expected *Logger) error { | ||
if actual.out != expected.out { | ||
return fmt.Errorf("output does not match") | ||
} | ||
|
||
if err := approxInt64(actual.timestamp, expected.timestamp, 100 /* ms */); err != nil { | ||
return fmt.Errorf("timestamp %v", err) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func approxInt64(actual, expected, tolerance int64) error { | ||
diff := expected - actual | ||
if diff < 0 { | ||
diff = -diff | ||
} | ||
if diff > tolerance { | ||
return fmt.Errorf("value %v is out of tolerance %v±%v", actual, expected, tolerance) | ||
} | ||
return nil | ||
} |
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