...

Source file src/k8s.io/apimachinery/pkg/runtime/serializer/cbor/internal/modes/modes_test.go

Documentation: k8s.io/apimachinery/pkg/runtime/serializer/cbor/internal/modes

     1  /*
     2  Copyright 2024 The Kubernetes 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 modes_test
    18  
    19  import (
    20  	"errors"
    21  	"testing"
    22  
    23  	"github.com/fxamacker/cbor/v2"
    24  	"github.com/google/go-cmp/cmp"
    25  	"k8s.io/apimachinery/pkg/runtime/serializer/cbor/internal/modes"
    26  )
    27  
    28  var encModeNames = map[cbor.EncMode]string{
    29  	modes.Encode:                 "Encode",
    30  	modes.EncodeNondeterministic: "EncodeNondeterministic",
    31  }
    32  
    33  var allEncModes = []cbor.EncMode{
    34  	modes.Encode,
    35  	modes.EncodeNondeterministic,
    36  }
    37  
    38  var decModeNames = map[cbor.DecMode]string{
    39  	modes.Decode:    "Decode",
    40  	modes.DecodeLax: "DecodeLax",
    41  }
    42  
    43  var allDecModes = []cbor.DecMode{
    44  	modes.Decode,
    45  	modes.DecodeLax,
    46  }
    47  
    48  func assertNilError(t *testing.T, e error) {
    49  	if e != nil {
    50  		t.Errorf("expected nil error, got: %v", e)
    51  	}
    52  }
    53  
    54  func assertOnConcreteError[E error](fn func(*testing.T, E)) func(t *testing.T, e error) {
    55  	return func(t *testing.T, ei error) {
    56  		var ec E
    57  		if !errors.As(ei, &ec) {
    58  			t.Errorf("expected concrete error type %T, got %T: %v", ec, ei, ei)
    59  			return
    60  		}
    61  		fn(t, ec)
    62  	}
    63  }
    64  
    65  func assertIdenticalError[E error](expected E) func(*testing.T, error) {
    66  	return assertOnConcreteError(func(t *testing.T, actual E) {
    67  		if diff := cmp.Diff(expected, actual); diff != "" {
    68  			t.Errorf("diff between actual error and expected error:\n%s", diff)
    69  		}
    70  	})
    71  }
    72  

View as plain text