• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1// Copyright 2022 The Bazel Authors. All rights reserved.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//    http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15package respipe
16
17import (
18	"errors"
19	"reflect"
20	"testing"
21
22	"context"
23)
24
25func TestPrefixErr(t *testing.T) {
26	tests := []struct {
27		ctx  context.Context
28		fmts string
29		args []interface{}
30		want error
31	}{
32		{
33			ctx:  context.Background(),
34			fmts: "Hello world",
35			want: errors.New("Hello world"),
36		},
37		{
38			ctx:  PrefixErr(context.Background(), "file: foo: "),
39			fmts: "Hello world: %d",
40			args: []interface{}{1},
41			want: errors.New("file: foo: Hello world: 1"),
42		},
43		{
44			ctx:  PrefixErr(PrefixErr(context.Background(), "file: foo: "), "tag: <resources>: "),
45			fmts: "Hello world: %d",
46			args: []interface{}{1},
47			want: errors.New("file: foo: tag: <resources>: Hello world: 1"),
48		},
49	}
50	for _, tc := range tests {
51		got := Errorf(tc.ctx, tc.fmts, tc.args...)
52		if !reflect.DeepEqual(got, tc.want) {
53			t.Errorf("Errorf(%v, %v, %v): %v wanted %v", tc.ctx, tc.fmts, tc.args, got, tc.want)
54		}
55	}
56}
57