| 1 |
/* |
| 2 |
Copyright 2021 The logr Authors. |
| 3 |
|
| 4 |
Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 |
you may not use this file except in compliance with the License. |
| 6 |
You may obtain a copy of the License at |
| 7 |
|
| 8 |
http://www.apache.org/licenses/LICENSE-2.0 |
| 9 |
|
| 10 |
Unless required by applicable law or agreed to in writing, software |
| 11 |
distributed under the License is distributed on an "AS IS" BASIS, |
| 12 |
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 |
See the License for the specific language governing permissions and |
| 14 |
limitations under the License. |
| 15 |
*/ |
| 16 |
|
| 17 |
package logr |
| 18 |
|
| 19 |
import ( |
| 20 |
"context" |
| 21 |
"testing" |
| 22 |
) |
| 23 |
|
| 24 |
func TestContext(t *testing.T) { |
| 25 |
ctx := context.Background() |
| 26 |
|
| 27 |
if out, err := FromContext(ctx); err == nil { |
| 28 |
t.Errorf("expected error, got %#v", out) |
| 29 |
} else if _, ok := err.(notFoundError); !ok { |
| 30 |
t.Errorf("expected a notFoundError, got %#v", err) |
| 31 |
} |
| 32 |
|
| 33 |
out := FromContextOrDiscard(ctx) |
| 34 |
if out.sink != nil { |
| 35 |
t.Errorf("expected a nil sink, got %#v", out) |
| 36 |
} |
| 37 |
|
| 38 |
sink := &testLogSink{} |
| 39 |
logger := New(sink) |
| 40 |
lctx := NewContext(ctx, logger) |
| 41 |
if out, err := FromContext(lctx); err != nil { |
| 42 |
t.Errorf("unexpected error: %v", err) |
| 43 |
} else if p, _ := out.sink.(*testLogSink); p != sink { |
| 44 |
t.Errorf("expected output to be the same as input, got in=%p, out=%p", sink, p) |
| 45 |
} |
| 46 |
out = FromContextOrDiscard(lctx) |
| 47 |
if p, _ := out.sink.(*testLogSink); p != sink { |
| 48 |
t.Errorf("expected output to be the same as input, got in=%p, out=%p", sink, p) |
| 49 |
} |
| 50 |
} |