knative.dev/pkg@v0.0.0-20260602142205-ac97e43f6622/apis/duck/v1/addressable_types_test.go (about)

     1  /*
     2  Copyright 2019 The Knative 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 v1
    18  
    19  import (
    20  	"context"
    21  	"testing"
    22  
    23  	"github.com/google/go-cmp/cmp"
    24  	"knative.dev/pkg/apis"
    25  )
    26  
    27  func TestConversion(t *testing.T) {
    28  	tests := []struct {
    29  		name        string
    30  		addr        *Addressable
    31  		conv        apis.Convertible
    32  		want        string
    33  		wantErrUp   bool
    34  		wantErrDown bool
    35  	}{{
    36  		name: "v1",
    37  		addr: &Addressable{
    38  			URL: &apis.URL{
    39  				Scheme: "https",
    40  				Host:   "bar.com",
    41  			},
    42  		},
    43  		conv:        &Addressable{},
    44  		wantErrUp:   true,
    45  		wantErrDown: true,
    46  	}}
    47  
    48  	for _, test := range tests {
    49  		t.Run(test.name, func(t *testing.T) {
    50  			conv := test.conv
    51  			if err := test.addr.ConvertTo(context.Background(), conv); err != nil {
    52  				if !test.wantErrUp {
    53  					t.Error("ConvertTo() =", err)
    54  				}
    55  			} else if test.wantErrUp {
    56  				t.Errorf("ConvertTo() = %#v, wanted error", conv)
    57  			}
    58  			got := &Addressable{}
    59  			if err := got.ConvertFrom(context.Background(), conv); err != nil {
    60  				if !test.wantErrDown {
    61  					t.Error("ConvertFrom() =", err)
    62  				}
    63  				return
    64  			} else if test.wantErrDown {
    65  				t.Errorf("ConvertFrom() = %#v, wanted error", conv)
    66  				return
    67  			}
    68  
    69  			if diff := cmp.Diff(test.addr, got); diff != "" {
    70  				t.Error("roundtrip (-want, +got) =", diff)
    71  			}
    72  		})
    73  	}
    74  }