mirror of
https://github.com/OffchainLabs/prysm.git
synced 2026-01-09 13:28:01 -05:00
Add a log handler helper (#188)
sharding: add a log-handler helper for tests Former-commit-id: fb083848d8615f36786f5c6b96a2182a8be21b9d [formerly 1c62e207943d9265a59f0ff6577479c6a2eaf76b] Former-commit-id: 0677762c40db862185c7b951e419473949b761c9
This commit is contained in:
committed by
Raul Jordan
parent
50c69a6571
commit
ab2954a693
44
sharding/utils/log_helper_test.go
Normal file
44
sharding/utils/log_helper_test.go
Normal file
@@ -0,0 +1,44 @@
|
||||
package utils
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/ethereum/go-ethereum/log"
|
||||
)
|
||||
|
||||
var _ = log.Handler(&LogHandler{})
|
||||
|
||||
// LogHandler provides methods for testing ethereum logs.
|
||||
type LogHandler struct {
|
||||
records []*log.Record
|
||||
t *testing.T
|
||||
}
|
||||
|
||||
// Log adds records to the record slice.
|
||||
func (t *LogHandler) Log(r *log.Record) error {
|
||||
t.records = append(t.records, r)
|
||||
return nil
|
||||
}
|
||||
|
||||
// Pop the record at index 0.
|
||||
func (t *LogHandler) Pop() *log.Record {
|
||||
var r *log.Record
|
||||
r, t.records = t.records[0], t.records[1:]
|
||||
return r
|
||||
}
|
||||
|
||||
// Length of the records slice.
|
||||
func (t *LogHandler) Len() int {
|
||||
return len(t.records)
|
||||
}
|
||||
|
||||
// VerifyLogMsg verfies that the log at index 0 matches the string exactly.
|
||||
// This method removes the verified message from the slice.
|
||||
func (h *LogHandler) VerifyLogMsg(str string) {
|
||||
if h.Len() == 0 {
|
||||
h.t.Error("Expected a log, but there were none!")
|
||||
}
|
||||
if l := h.Pop(); l.Msg != str {
|
||||
h.t.Errorf("Unexpected log: %v. Wanted: %s", l, str)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user