blob: bcfaff612eda8bc8bb972551129a211375320865 [file] [log] [blame]
// Copyright (C) 2016 The Android Open Source Project
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package log_test
import (
"testing"
"time"
"android.googlesource.com/platform/tools/gpu/framework/log"
)
func TestError(t *testing.T) {
const message = "A message of hope"
const expect = "Error:" + message
var err error = log.Background().AsError(message)
s := err.Error()
if s != expect {
t.Errorf("Got %q expected %q", s, expect)
}
}
func TestLogError(t *testing.T) {
list := log.List{}
ctx := log.Background().Handler(list.CollectAll()).PreFilter(log.Limit(log.NoticeLevel)).Filter(log.Pass)
for _, test := range tests {
expect := len(list)
if test.severity <= log.NoticeLevel {
expect += 1
}
err := ctx.Severity(test.severity).LogError(test.message)
if len(list) < expect {
t.Errorf("Unexpected log entry")
} else if len(list) > expect {
t.Errorf("Missing log entry")
}
s := err.Error() + "\n"
if s != test.expect {
t.Errorf("Got %q expected %q", s, test.expect)
}
}
}
func TestCollect(t *testing.T) {
list := log.List{}
handler := list.CollectAll()
for i, test := range tests {
handler(log.Background().Severity(test.severity).Record(test.message))
if len(list) != i+1 {
t.Errorf("List length is %d expected %d", len(list), i+1)
}
}
for i, test := range tests {
if list[i].Severity != test.severity {
t.Errorf("At %d got %s expected %s", i, list[i].Severity, test.severity)
}
if list[i].Message != test.message {
t.Errorf("At %d got %s expected %s", i, list[i].Message, test.message)
}
}
}
func testCollectPanic(handler log.Handler) (panicked interface{}) {
defer func() { panicked = recover() }()
for _, test := range tests {
handler(log.Background().Severity(test.severity).Record(test.message))
}
return nil
}
func TestCollectLimit(t *testing.T) {
for i := 1; i < len(tests); i++ {
list := log.List{}
handler := list.Collect(i)
panicked := testCollectPanic(handler)
switch panicked {
case nil:
t.Errorf("Expected panic at %d", i)
case log.ErrorLimit:
// expected panic
default:
t.Errorf("Expected ErrorLimit got %T:%v", panicked, panicked)
}
if len(list) != i {
t.Errorf("Expected panic at %d got %d", i, len(list))
}
}
}
func TestTimeStamp(t *testing.T) {
before := time.Now()
r := log.Background().Record("")
after := time.Now()
if r.TimeStamp.Before(before) || r.TimeStamp.After(after) {
t.Errorf("Timestamp %v not in range %v to %v", r.TimeStamp, before, after)
}
}