blob: 616c017e7dcb6c702e8ad4a5a5c9c92e364ec1f7 [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"
"android.googlesource.com/platform/tools/gpu/framework/log"
)
func TestFilterContext(t *testing.T) {
ctx := log.Background().PreFilter(log.NoLimit)
var id interface{}
keyFilter := func(ctx log.Context, key string, value interface{}) bool { id = key; return true }
valueFilter := func(ctx log.Context, key string, value interface{}) bool { id = value; return true }
ctx = ctx.Filter(keyFilter)
ctx.Info().WithValue("1", 2)
if id != "1" {
t.Errorf("Context did not install filter correctly, got %v", id)
}
ctx = ctx.Filter(valueFilter)
ctx.Info().WithValue("3", 4)
if id != 4 {
t.Errorf("Context did not override filter correctly, got %v", id)
}
}
func TestPreFilterContext(t *testing.T) {
ctx := log.Background().Filter(log.Pass)
id := 0
ctx = ctx.PreFilter(func(s log.Severity) bool { id = 1; return true })
ctx.Info()
if id != 1 {
t.Errorf("Context did not install pre filter correctly, got %v", id)
}
ctx = ctx.PreFilter(func(s log.Severity) bool { id = 2; return true })
ctx.Info()
if id != 2 {
t.Errorf("Context did not override pre filter correctly, got %v", id)
}
}
type filterTest struct {
key string
value interface{}
expected bool
}
func testFilter(t *testing.T, f log.Filter, tests []filterTest) {
for _, test := range tests {
got := f(log.Background(), test.key, test.value)
if got != test.expected {
t.Errorf("Got %v expected %v testing %v %v", got, test.expected, test.key, test.value)
}
}
}
func TestPass(t *testing.T) {
testFilter(t, log.Pass, []filterTest{
{"", "", true},
})
}
type preFilterTest struct {
Severity log.Severity
Expected bool
}
func testPreFilter(t *testing.T, tag interface{}, f log.PreFilter, sev log.Severity, exp bool) {
got := f(sev)
if got != exp {
t.Errorf("%v: Got %v expected %v testing %v", tag, got, exp, sev)
}
}
func TestNull(t *testing.T) {
for severity := log.EmergencyLevel; severity <= log.DebugLevel; severity++ {
testPreFilter(t, "null", log.Null, severity, false)
}
}
func TestNoLimit(t *testing.T) {
for severity := log.EmergencyLevel; severity <= log.DebugLevel; severity++ {
testPreFilter(t, "nolimit", log.NoLimit, severity, true)
}
}
func TestLimit(t *testing.T) {
for limit := log.EmergencyLevel; limit <= log.DebugLevel; limit++ {
f := log.Limit(limit)
for severity := log.EmergencyLevel; severity <= log.DebugLevel; severity++ {
testPreFilter(t, limit, f, severity, severity <= limit)
}
}
}