github.com/thiagoyeds/go-cloud@v0.26.0/internal/gcerr/gcerr_test.go (about)

     1  // Copyright 2019 The Go Cloud Development Kit Authors
     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  //     https://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  
    15  package gcerr
    16  
    17  import (
    18  	"errors"
    19  	"fmt"
    20  	"regexp"
    21  	"strconv"
    22  	"strings"
    23  	"testing"
    24  )
    25  
    26  func TestNewf(t *testing.T) {
    27  	e := Newf(Internal, nil, "a %d b", 3)
    28  	got := e.Error()
    29  	want := "a 3 b (code=Internal)"
    30  	if got != want {
    31  		t.Errorf("got %q, want %q", got, want)
    32  	}
    33  }
    34  
    35  func TestFormatting(t *testing.T) {
    36  	for i, test := range []struct {
    37  		err  *Error
    38  		verb string
    39  		want []string // regexps, one per line
    40  	}{
    41  		{
    42  			New(NotFound, nil, 1, "message"),
    43  			"%v",
    44  			[]string{`^message \(code=NotFound\)$`},
    45  		},
    46  		{
    47  			New(NotFound, nil, 1, "message"),
    48  			"%+v",
    49  			[]string{
    50  				`^message \(code=NotFound\):$`,
    51  				`\s+gocloud.dev/internal/gcerr.TestFormatting$`,
    52  				`\s+.*/internal/gcerr/gcerr_test.go:\d+$`,
    53  			},
    54  		},
    55  		{
    56  			New(AlreadyExists, errors.New("wrapped"), 1, "message"),
    57  			"%v",
    58  			[]string{`^message \(code=AlreadyExists\): wrapped$`},
    59  		},
    60  		{
    61  			New(AlreadyExists, errors.New("wrapped"), 1, "message"),
    62  			"%+v",
    63  			[]string{
    64  				`^message \(code=AlreadyExists\):`,
    65  				`^\s+gocloud.dev/internal/gcerr.TestFormatting$`,
    66  				`^\s+.*/internal/gcerr/gcerr_test.go:\d+$`,
    67  				`^\s+- wrapped$`,
    68  			},
    69  		},
    70  		{
    71  			New(AlreadyExists, errors.New("wrapped"), 1, ""),
    72  			"%v",
    73  			[]string{`^code=AlreadyExists: wrapped`},
    74  		},
    75  		{
    76  			New(AlreadyExists, errors.New("wrapped"), 1, ""),
    77  			"%+v",
    78  			[]string{
    79  				`^code=AlreadyExists:`,
    80  				`^\s+gocloud.dev/internal/gcerr.TestFormatting$`,
    81  				`^\s+.*/internal/gcerr/gcerr_test.go:\d+$`,
    82  				`^\s+- wrapped$`,
    83  			},
    84  		},
    85  	} {
    86  		t.Run(strconv.Itoa(i), func(t *testing.T) {
    87  			gotString := fmt.Sprintf(test.verb, test.err)
    88  			gotLines := strings.Split(gotString, "\n")
    89  			if got, want := len(gotLines), len(test.want); got != want {
    90  				t.Fatalf("got %d lines, want %d. got:\n%s", got, want, gotString)
    91  			}
    92  			for j, gl := range gotLines {
    93  				matched, err := regexp.MatchString(test.want[j], gl)
    94  				if err != nil {
    95  					t.Fatal(err)
    96  				}
    97  				if !matched {
    98  					t.Fatalf("line #%d: got %q, which doesn't match %q", j, gl, test.want[j])
    99  				}
   100  			}
   101  		})
   102  	}
   103  }
   104  
   105  func TestError(t *testing.T) {
   106  	// Check that err.Error() == fmt.Sprintf("%s", err)
   107  	for _, err := range []*Error{
   108  		New(NotFound, nil, 1, "message"),
   109  		New(AlreadyExists, errors.New("wrapped"), 1, "message"),
   110  		New(AlreadyExists, errors.New("wrapped"), 1, ""),
   111  	} {
   112  		got := err.Error()
   113  		want := fmt.Sprint(err)
   114  		if got != want {
   115  			t.Errorf("%v: got %q, want %q", err, got, want)
   116  		}
   117  	}
   118  }