| // 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 ( |
| "bytes" |
| "testing" |
| |
| "io" |
| |
| "android.googlesource.com/platform/tools/gpu/framework/log" |
| ) |
| |
| func TestParse(t *testing.T) { |
| tests := []struct { |
| line string |
| severity log.Severity |
| msg string |
| }{ |
| {"Emergency:Hello", log.EmergencyLevel, "Hello"}, |
| {"Alert:Hello", log.AlertLevel, "Hello"}, |
| {"Critical:Hello", log.CriticalLevel, "Hello"}, |
| {"Error:Hello", log.ErrorLevel, "Hello"}, |
| {"Warning:Hello", log.WarningLevel, "Hello"}, |
| {"Notice:Hello", log.NoticeLevel, "Hello"}, |
| {"Info:Hello", log.InfoLevel, "Hello"}, |
| {"Debug:Hello", log.DebugLevel, "Hello"}, |
| } |
| for _, test := range tests { |
| r := log.Parse(log.Background(), test.line) |
| if r.Severity != test.severity { |
| t.Errorf("Got severity %v expected %v from %q", r.Severity, test.severity, test.line) |
| } |
| if r.Message != test.msg { |
| t.Errorf("Got message %q expected %q from %q", r.Message, test.msg, test.line) |
| } |
| } |
| } |
| |
| func TestActive(t *testing.T) { |
| ctx := log.Background().PreFilter(log.Limit(log.NoticeLevel)).Filter(log.Pass) |
| for severity := log.EmergencyLevel; severity <= log.DebugLevel; severity++ { |
| active := ctx.At(severity).Active() |
| expect := severity <= log.NoticeLevel |
| if active != expect { |
| t.Errorf("At %v got active %v expected %v", severity, active, expect) |
| } |
| } |
| } |
| |
| func TestLoggerChain(t *testing.T) { |
| expect := "Error:Test:Because:A=a<<Error:Test:An error:B=b>>\n" |
| buf := &bytes.Buffer{} |
| ctx := log.Background().PreFilter(log.NoLimit).Filter(log.Pass).Handler(log.Writer(log.Normal, buf)).Enter("Test") |
| a := ctx.V("A", "a") |
| b := ctx.V("B", "b") |
| err := b.AsError("An error") |
| a.Fail(err, "Because") |
| got := buf.String() |
| if expect != got { |
| t.Errorf("Got %q expected %q", got, expect) |
| } |
| } |
| |
| func TestLoggerWriter(t *testing.T) { |
| expect := `Info:Line 1 |
| Info:Line 2 |
| Info:Line 3 |
| Warning:Inside torn line 1 |
| Warning:Inside torn line 2 |
| Info:Torn Apart Line |
| Warning:After terminated line |
| Info:Unterminated Line |
| ` |
| buf := &bytes.Buffer{} |
| ctx := log.Background().PreFilter(log.NoLimit).Filter(log.Pass).Handler(log.Writer(log.Normal, buf)) |
| info := ctx.Info().Writer() |
| io.WriteString(info, "Line 1\nLine 2\n") |
| io.WriteString(info, "Line 3\nTorn ") |
| ctx.Warning().Log("Inside torn line 1") |
| io.WriteString(info, "Apart ") |
| ctx.Warning().Log("Inside torn line 2") |
| io.WriteString(info, "Line\nUnterminated Line") |
| ctx.Warning().Log("After terminated line") |
| info.Close() |
| got := buf.String() |
| if expect != got { |
| t.Errorf("Got %q expected %q", got, expect) |
| } |
| } |